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") 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
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
|