Skip to content

Instantly share code, notes, and snippets.

@alfielapeter
Created January 13, 2011 20:08
Show Gist options
  • Save alfielapeter/778498 to your computer and use it in GitHub Desktop.
Save alfielapeter/778498 to your computer and use it in GitHub Desktop.
Search model for a multiple database real estate search.
=begin
there are probably better ways to do this now and I wrote this when I was originally learning ror,
but it is a sample of something mildly complex that I wrote.
=end
class ResSearch < ActiveRecord::Base
belongs_to :users
has_many :search_alerts, :dependent => :destroy
include ApplicationHelper
def products(current_user, page, per_page, c, d)
@res_listings ||= find_products(current_user, page, per_page, c, d)
end
private
def find_products(current_user, page, per_page, c, d)
scope = (current_user.mls + "Listing").constantize.scoped({})
scope = scope.scoped :conditions => ["#{(current_user.mls + 'Listings').underscore}.status != ?", "Inactive"]
scope = scope.scoped :conditions => ["#{(current_user.mls + 'Listings').underscore}.list_price >= ?", min_price] unless min_price.blank?
scope = scope.scoped :conditions => ["#{(current_user.mls + 'Listings').underscore}.list_price <= ?", max_price] unless max_price.blank?
scope = scope.scoped :conditions => ["#{(current_user.mls + 'Listings').underscore}.home_type IN (?)", formatted(home_type)] unless home_type.blank?
scope = scope.scoped :conditions => ["#{(current_user.mls + 'Listings').underscore}.beds >= ?", min_beds] unless min_beds.blank?
scope = scope.scoped :conditions => ["#{(current_user.mls + 'Listings').underscore}.beds <= ?", max_beds] unless max_beds.blank?
scope = scope.scoped :conditions => ["#{(current_user.mls + 'Listings').underscore}.baths >= ?", min_baths] unless min_baths.blank?
scope = scope.scoped :conditions => ["#{(current_user.mls + 'Listings').underscore}.baths <= ?", max_baths] unless max_baths.blank?
scope = scope.scoped :conditions => ["#{(current_user.mls + 'Listings').underscore}.apx_sqft >= ?", min_sqft] unless min_sqft.blank?
scope = scope.scoped :conditions => ["#{(current_user.mls + 'Listings').underscore}.apx_sqft <= ?", max_sqft] unless max_sqft.blank?
scope = scope.scoped :conditions => ["#{(current_user.mls + 'Listings').underscore}.garage >= ?", garage] unless garage.blank?
scope = scope.scoped :conditions => ["#{(current_user.mls + 'Listings').underscore}.level = ?", level] unless level.blank?
scope = scope.scoped :conditions => ["#{(current_user.mls + 'Listings').underscore}.acres >= ?", min_acres] unless min_acres.blank?
scope = scope.scoped :conditions => ["#{(current_user.mls + 'Listings').underscore}.acres <= ?", max_acres] unless max_acres.blank?
scope = scope.scoped :conditions => ["#{(current_user.mls + 'Listings').underscore}.subdivision = ?", subdivision] unless subdivision.blank?
scope = scope.scoped :conditions => ["#{(current_user.mls + 'Listings').underscore}.year_built >= ?", min_year_built] unless min_year_built.blank?
scope = scope.scoped :conditions => ["#{(current_user.mls + 'Listings').underscore}.year_built <= ?", max_year_built] unless max_year_built.blank?
scope = scope.scoped :conditions => ["#{(current_user.mls + 'Listings').underscore}.school_district = ?", school_district] unless school_district.blank?
scope = scope.scoped :conditions => ["#{(current_user.mls + 'Listings').underscore}.foreclosure = ?", foreclosure] unless foreclosure.blank?
scope = scope.scoped :conditions => ["#{(current_user.mls + 'Listings').underscore}.reo = ?", reo] unless reo.blank?
scope = scope.scoped :conditions => ["#{(current_user.mls + 'Listings').underscore}.sale_rent = ?", sale_rent] unless sale_rent.blank?
scope = scope.scoped :conditions => ["#{(current_user.mls + 'Listings').underscore}.pool IN (?)", formatted(pool)] unless pool.blank?
scope = scope.scoped :conditions => ["#{(current_user.mls + 'Listings').underscore}.city IN (?)", formatted(city)] unless city.blank?
scope = scope.scoped :conditions => ["#{(current_user.mls + 'Listings').underscore}.area IN (?)", formatted(area)] unless area.blank?
scope = scope.paginate(:page => page, :per_page => per_page, :order => sort_order('list_price', c, d), :select => "id, mls, address, unit_number, list_price, beds, baths, apx_sqft, remarks, status")
scope
end
#required for multiple area search
def formatted(a)
b = a.split(/\n/)
a_array = b[1..-1]
a = a_array.collect {|str| str.slice(2..-1)}
return a
end
def sort_order(default, c, d)
"#{(c || default.to_s).gsub(/[\s;'\"]/,'')} #{d == 'down' ? 'DESC' : 'ASC'}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment