class Search < ActiveRecord::Base belongs_to :organization belongs_to :event belongs_to :membership_type validates_presence_of :organization_id attr_accessible :zip, :state, :event_id, :tagging, :person_type, :person_subtype, :min_lifetime_value, :max_lifetime_value, :min_donations_amount, :max_donations_amount, :min_donations_date, :max_donations_date, :discount_code, :membership_status, :passholder, :membership_type_id, :membership_type attr_accessor :membership_status, :passholder def length people.length end def people @people ||= find_people end def tag(tag) Delayed::Job.enqueue(TagJob.new(tag, people)) end def attach_action(action) Delayed::Job.enqueue(ActionJob.new(action, people)) end def description conditions = [] conditions << "Are tagged with #{tagging}." if tagging.present? conditions << "Bought tickets for #{event.name}." if event_id.present? conditions << "Purchased #{membership_type.name}" if membership_type_id.present? if zip.present? || state.present? locations = [] locations << state if state.present? locations << "the zipcode of #{zip}" if zip.present? conditions << "Are located within #{locations.to_sentence}." end if min_lifetime_value.present? && max_lifetime_value.present? conditions << "Have a lifetime value between $#{min_lifetime_value} and $#{max_lifetime_value}." elsif min_lifetime_value.present? conditions << "Have a minimum lifetime value of $#{min_lifetime_value}." elsif max_lifetime_value.present? conditions << "Have a maximum lifetime value of $#{max_lifetime_value}." end unless discount_code.blank? conditions << ((discount_code == Discount::ALL_DISCOUNTS_STRING) ? "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 << string end String.new.tap do |s| if conditions.blank? if person_type == "Company" if person_subtype.present? return "All #{person_subtype.pluralize(2)}." else return "All companies." end else return "All individuals." end else if person_type == "Company" if person_subtype.present? s = "#{person_subtype.pluralize(2)} that: " else s = "Companies that: " end else s = "Individuals that: " end return s + "