Class: CabooseRets::SearchOption

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

Class Method Summary collapse

Class Method Details

.results(str, count_per_name = 10) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'app/models/caboose_rets/search_option.rb', line 77

def self.results(str, count_per_name = 10)      
  q = ["select * from (        
      select id, name, field, value, row_number() over (partition by name order by field) as rownum 
      from rets_search_options        
      where lower(value) like ?        
    ) tmp where rownum < #{count_per_name}", "%#{str}%"]      
  rows = ActiveRecord::Base.connection.select_rows(ActiveRecord::Base.send(:sanitize_sql_array, q))
  arr = rows.collect{ |row| { :id => row[0].to_i, :name => row[1], :field => row[2], :text => row[3] }}    
  return arr    
end

.update_search_optionsObject

attr_accessible :id, :name, :field, :value, :flag_for_delete



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
# File 'app/models/caboose_rets/search_option.rb', line 6

def self.update_search_options
  
  # Flag all for delete
  self.update_all(:flag_for_delete => true)
  
  names = {
    'City'          => ['city'],
    'County'        => ['county_or_parish'],
    'Zip Code'      => ['postal_code'],
    'Schools'       => ['elementary_school', 'middle_school', 'high_school'],
    'MLS Area'      => ['area'],
    'Neighborhood'  => ['subdivision'],
    'Street Name'   => ['street_name'],
    'Street Address'   => ['street_address'],
    'Property Type' => ['property_type'],      
    'MLS Number'    => ['mls_number']
  }
  names.each do |name, fields|
    fields.each do |field|
      self.update_search_options_for_field(name, field)        
    end
  end

  # Delete all flagged
  self.where(:flag_for_delete => true).delete_all        
end

.update_search_options_for_field(name, field) ⇒ Object



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
# File 'app/models/caboose_rets/search_option.rb', line 33

def self.update_search_options_for_field(name, field)
  if field == 'street_name'
    CabooseRets::Property.where(:status => "Active", :property_type => "Residential").where("street_name is not null").order(:id).all.each do |prop|
      add = "#{prop.street_name} #{prop.street_suffix}"
      val = add.blank? ? nil : add.strip
      next if val.blank?
      so = self.where(:name => name, :field => field, :value => val).first
      if so.nil? && !field.blank?
        self.create(:name => name, :field => field, :value => val)
      elsif so
        so.flag_for_delete = false
        so.save
      end
    end
  elsif field == 'street_address'
    CabooseRets::Property.where(:status => "Active", :property_type => "Residential").where("street_name is not null and street_number is not null").order(:id).all.each do |prop|
      add = "#{prop.street_number} #{prop.street_name} #{prop.street_suffix}"
      val = add.blank? ? nil : add.strip
      next if val.blank?
      so = self.where(:name => name, :field => field, :value => val).first
      if so.nil? && !field.blank?
        self.create(:name => name, :field => field, :value => val)
      elsif so
        so.flag_for_delete = false
        so.save
      end
    end
  else
    q = ["select distinct(#{field}) from rets_properties where status = ? and property_type = ?", 'Active', 'Residential']
    rows = ActiveRecord::Base.connection.select_rows(ActiveRecord::Base.send(:sanitize_sql_array, q))
    rows.each do |row|
      val = row[0].blank? ? nil : row[0].titleize.strip
      next if val.blank?
      so = self.where(:name => name, :field => field, :value => val).first
      if so.nil? && !field.blank?
        self.create(:name => name, :field => field, :value => val)
      elsif so
        so.flag_for_delete = false
        so.save
      end
    end
  end
end