0914e5093f1af95df0ab2478818856d8

I know it looks long, but it's not that bad. I lot of repetition in there, hence the need for refactoring. If you have any question, please ask. Thanks, guys.

Brad

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  def self.find_matches(params)
    matches = self.find(:all)
    matches.map do |property| property[:match_percentage] = { } end

    unless params[:property].nil?
      params[:property].keys.each { |params_key|
        case params_key;
          when 'destination':
            selected_destinations = params[:property]['destination'].keys.map { |key| key.to_i }
            
            matches.delete_if do |property|
              (selected_destinations & property.destination.id.to_a).empty?
            end


          # Properties must have value greater or equal to selected value (drop-downs)
          when 'hotel_rating':
            selected_hotel_rating = (params[:property]['hotel_rating'].to_i .. 5).to_a
            matches.delete_if do |property|
              !selected_hotel_rating.include? property.hotel_rating
            end


          # Properties must have one of the selecte values- Percentage based on number matched versus total options selected
          when 'honeymoon_type_attribute_ids':
            selected_honeymoon_types = params[:property]['honeymoon_type_attribute_ids'].keys.map { |key| key.to_i }

            matches.delete_if do |property|
              if (selected_honeymoon_types & property.honeymoon_type_attribute_ids.to_a).empty?
                true
              else
                property[:match_percentage][:honeymoon_type] = ((selected_honeymoon_types & property.honeymoon_type_attribute_ids.to_a).length / selected_honeymoon_types.length.to_f * 100 ).round
                false
              end
            end

          when 'hotel_setting_attribute_ids':
            selected_hotel_settings = params[:property]['hotel_setting_attribute_ids'].keys.map { |key| key.to_i }

            matches.delete_if do |property|
              if (selected_hotel_settings & property.hotel_setting_attribute_ids.to_a).empty?
                true
              else
                property[:match_percentage][:hotel_setting] = ((selected_hotel_settings & property.hotel_setting_attribute_ids.to_a).length / selected_hotel_settings.length.to_f * 100 ).round
                false
              end
            end

          when 'hotel_style_attribute_ids':
            selected_hotel_styles = params[:property]['hotel_style_attribute_ids'].keys.map { |key| key.to_i }

            matches.delete_if do |property|
              if(selected_hotel_styles & property.hotel_style_attribute_ids.to_a).empty?
                true
              else
                property[:match_percentage][:hotel_style] = ((selected_hotel_styles & property.hotel_style_attribute_ids.to_a).length / selected_hotel_styles.length.to_f * 100 ).round
                false
              end
            end


          # Properties must have all selected values - Will not effect match percentage
          when 'required_amenity_tag_ids':
            params[:property]['required_amenity_tag_ids'].keys.each do |key| 
              matches.delete_if do |property|
                  !(property.amenity_tag_ids.to_a.include? key.to_i)
              end
            end
            
          when 'required_activity_tag_ids':
            params[:property]['required_activity_tag_ids'].keys.each do |key| 
              matches.delete_if do |property|
                  !(property.activity_tag_ids.to_a.include? key.to_i)
              end
            end            
 
          # These will not filter out any matchs; only effects match percentage
          when 'preferred_activity_tag_ids':
            matches.each do |property|
              number_of_activities_matched = 0
              params[:property]['preferred_activity_tag_ids'].keys.each do |key| 
                  number_of_activities_matched += 1 if(property.activity_tag_ids.to_a.include? key.to_i)
              end
              property[:match_percentage][:preferred_activities] = (number_of_activities_matched / params[:property]['preferred_activity_tag_ids'].keys.length.to_f * 100 ).round
            end       
    
          when 'preferred_amenity_tag_ids':
            matches.each do |property|
              number_of_amenities_matched = 0
              params[:property]['preferred_amenity_tag_ids'].keys.each do |key| 
                  number_of_amenities_matched += 1 if(property.amenity_tag_ids.to_a.include? key.to_i)
              end
              property[:match_percentage][:preferred_amenities] = (number_of_amenities_matched / params[:property]['preferred_amenity_tag_ids'].keys.length.to_f * 100 ).round
            end

          when 'has_good_food':
            if params[:property]['has_good_food'] == 'yes'
              matches.each do |property|
                property[:match_percentage][:has_good_food] = 100 if property.has_good_food == true
                property[:match_percentage][:has_good_food] = 0   if property.has_good_food == false
              end
            end

          when 'has_good_nightlife':
            if params[:property]['has_good_nightlife'] == 'yes'
              matches.each do |property|
                property[:match_percentage][:has_good_nightlife] = 100 if property.has_good_nightlife == true
                property[:match_percentage][:has_good_nightlife] = 0   if property.has_good_nightlife != false
              end
            end
        end
        matches = calculate_match_percentage(matches)
      }
    end 

    return matches
  end

  # no longer weighting different attributes so some of this is left in case of later implementation -Brad
  def self.calculate_match_percentage(matches)
    matches.each do |property|
      if property[:match_percentage].length.to_i != 0
        # weighted_percentages = []
        # property[:match_percentage].each_pair { | percent, weight| weighted_percentage << percent * weight }
        property[:total_match_percentage] = calculate_mean(property[:match_percentage].values)
      end
    end
    return matches
  end

Refactorings

No refactoring yet !

Your refactoring





Format Copy from initial code

or Cancel