Class: Search

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/search.rb

Constant Summary collapse

ANY_EVENT =
-1
ANY_MEMBERSHIP_TYPE =
-1

Instance Method Summary collapse

Instance Method Details

#attach_action(action) ⇒ Object



38
39
40
# File 'app/models/search.rb', line 38

def attach_action(action)
  Delayed::Job.enqueue(ActionJob.new(action, people))
end

#descriptionObject



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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'app/models/search.rb', line 46

def description
  c = ->(s){ "<li>#{s}</li>" }
  conditions = ""

  conditions << c.call("Are tagged with #{tagging}.") if tagging.present?
  conditions << c.call("Have #{relation.indefinite_article} '#{relation.description}' relationship") if relation.present?

  if event_id.present?
    if has_purchased_for
      conditions << c.call("Purchased tickets for #{event_name}.")
    else
      conditions << c.call("Have not purchased tickets for #{event_name}.")
    end

    conditions << c.call("For show dates after #{show_date_start.strftime('%D')}") if show_date_start.present?
    conditions << c.call("For show dates through #{show_date_end.strftime('%D')}") if show_date_end.present?
  end

  if zip.present? || state.present?
    locations = []
    locations << state if state.present?
    locations << "the zipcode of #{zip}" if zip.present?
    conditions << c.call("Are located within #{locations.to_sentence}.")
  end
  if min_lifetime_value.present? && max_lifetime_value.present?
    conditions << c.call("Have a lifetime value between $#{min_lifetime_value} and $#{max_lifetime_value}.")
  elsif min_lifetime_value.present?
    conditions << c.call("Have a minimum lifetime value of $#{min_lifetime_value}.")
  elsif max_lifetime_value.present?
    conditions << c.call("Have a maximum lifetime value of $#{max_lifetime_value}.")
  end

  unless discount_code.blank?
    conditions << ((discount_code == Discount::ALL_DISCOUNTS_STRING) ? c.call("Used any discount code") : ("Used discount code #{discount_code}."))
  end

  unless [min_donations_amount, max_donations_amount, min_donations_date, max_donations_date].all?(&:blank?)
    if min_donations_amount.present? && max_donations_amount.present?
      string = "Made between $#{min_donations_amount} and $#{max_donations_amount} in donations"
    elsif min_donations_amount.present?
      string = "Made a total minimum of $#{min_donations_amount} in donations"
    elsif max_donations_amount.present?
      string = "Made no more than $#{max_donations_amount} in total donations"
    else
      string = "Made any donations"
    end

    if min_donations_date.present? && max_donations_date.present?
      string << " from #{min_donations_date.strftime('%D')} to #{max_donations_date.strftime('%D')}."
    elsif min_donations_date.present?
      string << " after #{min_donations_date.strftime('%D')}."
    elsif max_donations_date.present?
      string << " before #{max_donations_date.strftime('%D')}."
    else
      string << " overall."
    end
    conditions << c.call(string)
  end

  categories = []

  if output_companies
    if person_subtype.present?
      categories << person_subtype.downcase.pluralize
    else
      categories << "companies" if output_companies
    end
  end

  if searching_membership?
    if membership_status.present?
      state_str = (membership_status == "None") ? "not" : membership_status.downcase
      conditions << c.call("Are #{state_str} members")
    end

    if membership_type_id.present?
      if any_membership_type?
        conditions << c.call("Are members")
      else
        conditions << c.call("Are #{membership_type.name} members")
      end
    end


    # Membership Start
    if min_membership_start_date.present? && max_membership_start_date.present?
      conditions << c.call("Have memberships starting from #{min_membership_start_date.strftime('%D')} through #{max_membership_start_date.strftime('%D')}.")
    elsif min_membership_start_date.present?
      conditions << c.call("Have memberships starting on or after #{min_membership_start_date.strftime('%D')}.")
    elsif max_membership_start_date.present?
      conditions << c.call("Have memberships starting on or before #{max_membership_start_date.strftime('%D')}.")
    end
    
    # Membership End
    if min_membership_end_date.present? && max_membership_end_date.present?
      conditions << c.call("Have memberships ending from #{min_membership_end_date.strftime('%D')} through #{max_membership_end_date.strftime('%D')}.")
    elsif min_membership_end_date.present?
      conditions << c.call("Have memberships ending on or after #{min_membership_end_date.strftime('%D')}.")
    elsif max_membership_end_date.present?
      conditions << c.call("Have memberships ending on or before #{max_membership_end_date.strftime('%D')}.")
    end
  end

  if searching_passes?
    conditions << c.call("Have a current #{pass_type.passerize}.")
  end

  categories << "individuals" if output_individuals
  
  if self.organization.can? :access,:relationships
    categories << "households" if output_households
  end

  categories[-1] = "and #{categories[-1]}" if categories.length > 1

  categories << "Anything " if categories.empty?

  if conditions.blank?
    result = "All " << categories.join(", ") << "."
  else
    result = categories.join(" ") << " that: " << "<ul>" << conditions << "</ul>"
    result[0] = result[0].upcase
  end

  result

end

#event_nameObject



42
43
44
# File 'app/models/search.rb', line 42

def event_name
  event.try(:name) || Event::ANY_EVENT_TEXT
end

#lengthObject



26
27
28
# File 'app/models/search.rb', line 26

def length
  people.length
end

#offset(datetime) ⇒ Object



184
185
186
# File 'app/models/search.rb', line 184

def offset(datetime)
  @offset ||= datetime.in_time_zone(ActiveSupport::TimeZone.create(self.organization.time_zone)).formatted_offset
end

#offset_show_date_endObject



179
180
181
182
# File 'app/models/search.rb', line 179

def offset_show_date_end
  return nil if self.show_date_end.blank?
  @offset_show_date_end ||= self.show_date_end.to_datetime.end_of_day.change(:offset => offset(self.show_date_end))
end

#offset_show_date_startObject



174
175
176
177
# File 'app/models/search.rb', line 174

def offset_show_date_start
  return nil if self.show_date_start.blank?
  @offset_show_date_start ||= self.show_date_start.to_datetime.change(:offset => offset(self.show_date_start))
end

#peopleObject



30
31
32
# File 'app/models/search.rb', line 30

def people
  @people ||= find_people
end

#tag(tag) ⇒ Object



34
35
36
# File 'app/models/search.rb', line 34

def tag(tag)
  Delayed::Job.enqueue(TagJob.new(tag, people))
end