Module: HasLocation::ClassMethods

Defined in:
lib/has_location.rb

Instance Method Summary collapse

Instance Method Details

#has_location(options = {}) ⇒ Object



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
# File 'lib/has_location.rb', line 17

def has_location( options={} )
  class_eval do

    unless self.column_names.include?("location_id")
      puts "WARNING: '#{self.name}' does not have a 'location_id' column. has_location will not work!"
    end
    unless self.column_names.include?("location_string")
      puts "WARNING: '#{self.name}' does not have a 'location_string' column. has_location will not work!"
    end
    unless File.exists?("#{Rails.root}/app/models/location.rb") #Kernel.const_defined?("Location")
      puts "WARNING: '#{Rails.root}/app/models/location.rb' does not exist. has_location will not work!"
    end
    unless Location.respond_to?(:normalize)
      puts "WARNING: 'Location' does not respond to :normalize. has_location will not work!"
    end

    # attr_accessible :location_id, :location_string
    belongs_to :location

    before_save :detect_location

    def detect_location
      return unless self.location_string_changed?
      self.location_id = (Location.normalize( self.location_string ).id rescue nil)
    end
  end
end

#percent_with_locationObject



45
46
47
# File 'lib/has_location.rb', line 45

def percent_with_location
  ((where("location_id IS NOT NULL").count.to_f / count.to_f) * 100).round(1)
end