forked from pinballmap/pbm
/
location.rb
107 lines (85 loc) · 4.81 KB
/
location.rb
1
2
3
4
5
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
32
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
class Location < ActiveRecord::Base
include Rakismet::Model
rakismet_attrs content: :description
validates_presence_of :name, :street, :city, :state, :zip
validates :phone, format: { with: /\d{3}-\d{3}-\d{4}/, message: 'format invalid, please use ###-###-####' }, if: :phone?
validates :website, format: { with: %r{^http:\/\/}, message: 'must begin with http://', multiline: true }, if: :website?
validates :name, :street, :city, :state, format: { with: /^\S.*/, message: "Can't start with a blank", multiline: true }
validates :lat, :lon, presence: { message: 'Latitude/Longitude failed to generate. Please double check address and try again, or manually enter the lat/lon' }
belongs_to :location_type
belongs_to :zone
belongs_to :region
belongs_to :operator
has_many :events
has_many :machines, through: :location_machine_xrefs
has_many :location_machine_xrefs
has_many :location_picture_xrefs
geocoded_by :full_street_address, latitude: :lat, longitude: :lon
before_validation :geocode, unless: ENV['SKIP_GEOCODE'] || (:lat && :lon)
scope :region, lambda {|name|
r = Region.find_by_name(name.downcase) || Region.where(name: 'portland').first
where(region_id: r.id)
}
scope :by_type_id, ->(id) { where('location_type_id in (?)', id.split('_').map(&:to_i)) }
scope :by_operator_id, ->(id) { where(operator_id: id) }
scope :by_location_id, ->(id) { where('id in (?)', id.split('_').map(&:to_i)) }
scope :by_zone_id, ->(id) { where('zone_id in (?)', id.split('_').map(&:to_i)) }
scope :by_city_id, ->(city) { where(city: city) }
scope :by_location_name, ->(name) { where(name: name) }
scope :by_ipdb_id, lambda { |id|
machines = Machine.where('ipdb_id in (?)', id.split('_').map(&:to_i)).map { |m| m.all_machines_in_machine_group }.flatten
joins(:location_machine_xrefs).where('locations.id = location_machine_xrefs.location_id and location_machine_xrefs.machine_id in (?)', machines.map { |m| m.id })
}
scope :by_machine_id, lambda { |id|
machines = Machine.where('id in (?)', id.split('_').map(&:to_i)).map { |m| m.all_machines_in_machine_group }.flatten
joins(:location_machine_xrefs).where('locations.id = location_machine_xrefs.location_id and location_machine_xrefs.machine_id in (?)', machines.map { |m| m.id })
}
scope :by_machine_group_id, lambda { |id|
machines = Machine.where('machine_group_id in (?)', id).flatten
joins(:location_machine_xrefs).where('locations.id = location_machine_xrefs.location_id and location_machine_xrefs.machine_id in (?)', machines.map { |m| m.id })
}
scope :by_machine_name, lambda { |name|
machine = Machine.find_by_name(name)
return nil if machine.nil?
machines = machine.machine_group_id ? Machine.where('machine_group_id = ?', machine.machine_group_id).map { |m| m.all_machines_in_machine_group }.flatten : [machine]
joins(:location_machine_xrefs).where('locations.id = location_machine_xrefs.location_id and location_machine_xrefs.machine_id in (?)', machines.map { |m| m.id })
}
scope :by_at_least_n_machines_city, lambda { |n|
where(Location.by_at_least_n_machines_sql(n))
}
scope :by_at_least_n_machines_zone, lambda { |n|
where(Location.by_at_least_n_machines_sql(n))
}
scope :by_at_least_n_machines_type, lambda { |n|
where(Location.by_at_least_n_machines_sql(n))
}
attr_accessible :name, :street, :city, :state, :zip, :phone, :lat, :lon, :website, :zone_id, :region_id, :location_type_id, :description, :operator_id, :date_last_updated
before_destroy do |record|
Event.destroy_all "location_id = #{record.id}"
LocationPictureXref.destroy_all "location_id = #{record.id}"
MachineScoreXref.destroy_all "location_machine_xref_id in (select id from location_machine_xrefs where location_id = #{record.id})"
LocationMachineXref.destroy_all "location_id = #{record.id}"
end
def self.by_at_least_n_machines_sql(n)
"id in (select location_id from (select location_id, count(*) as count from location_machine_xrefs group by location_id) x where x.count >= #{n})"
end
def machine_names
machines.sort_by(&:massaged_name).map { |m| m.name_and_year }
end
def content_for_infowindow
content = "'<div class=\"infowindow\" id=\"infowindow_#{id}\">"
content += "<div class=\"gm_location_name\">#{name.gsub("'", "\\\\'")}</div>"
content += "<div class=\"gm_address\">#{[street.gsub("'", "\\\\'"), [city.gsub("'", "\\\\'"), state, zip].join(', '), phone].join('<br />')}</div>"
content += '<hr />'
machines = machine_names.map { |m| m.gsub("'", "\\\\'") + '<br />' }
content += "<div class=\"gm_machines\" id=\"gm_machines_#{id}\">#{machines.join}</div>"
content += "</div>'"
content.html_safe
end
def full_street_address
[street, city, state, zip].join(', ')
end
def newest_machine_xref
location_machine_xrefs.sort_by(&:created_at).last
end
end