Module: ZipSearch::HasLocations::ClassMethods

Defined in:
lib/zip_search/has_locations.rb

Instance Method Summary collapse

Instance Method Details

#has_location(title = :location, options = {}) ⇒ Object



8
9
10
11
12
# File 'lib/zip_search/has_locations.rb', line 8

def has_location(title=:location, options={})
  has_one title, -> { where zs_association_name: title.to_s },
    as: :locatable, class_name: 'ZipSearch::Location'
  initialize_location_ownership! title
end

#has_location_history(title = :location_history) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/zip_search/has_locations.rb', line 20

def has_location_history(title=:location_history)
  has_locations title
  has_location :current_location
  before_validation if: -> { current_location && current_location.changed? } do
    historical_attrs = {}
    current_location.changes.each{|k, (oldval, _)|
      historical_attrs[k] = oldval unless oldval.blank? }
    if historical_attrs.any?
      self.location_history << Location.new( Hash[
        current_location.changes.map{|k, (v, _)| [ k, v ] } ].merge(
          zs_association_name: 'location_history') )
    end
  end
end

#has_locations(title = :locations, options = {}) ⇒ Object



14
15
16
17
18
# File 'lib/zip_search/has_locations.rb', line 14

def has_locations(title=:locations, options={})
  has_many title, -> { where zs_association_name: title.to_s },
    as: :locatable, class_name: 'ZipSearch::Location'
  initialize_locations_ownership! title
end

#has_zipsearch_association?(title = nil) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/zip_search/has_locations.rb', line 43

def has_zipsearch_association?(title=nil)
  return zipsearch_associations.any? if title.nil?
  zipsearch_association_names.include?(title)
end

#initialize_location_ownership!(title = :location) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/zip_search/has_locations.rb', line 48

def initialize_location_ownership!(title=:location)
  include HasLocations::LocalInstanceMethods
  accepts_nested_attributes_for title, allow_destroy: true,
    reject_if: :"should_reject_#{title}?"
  before_validation :prune_empty_zs_locations
  after_initialize if: :"should_build_#{title}?" do
    send :"build_#{title}", zs_association_name: title end
end

#initialize_locations_ownership!(title = :locations) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/zip_search/has_locations.rb', line 56

def initialize_locations_ownership!(title=:locations)
  include HasLocations::LocalInstanceMethods
  accepts_nested_attributes_for title, allow_destroy: true,
    reject_if: :"should_reject_#{title}?"
  before_validation :prune_empty_zs_locations
  unless title == :location_history
    after_initialize if: :"should_build_#{title}?" do
      send(title).build zs_association_name: title end
  end
end

#zipsearch_association(title = nil) ⇒ Object



40
41
# File 'lib/zip_search/has_locations.rb', line 40

def zipsearch_association(title=nil)
zipsearch_associations.find{|za| title.nil? || za.name == title } end

#zipsearch_association_namesObject



38
39
# File 'lib/zip_search/has_locations.rb', line 38

def zipsearch_association_names
zipsearch_associations.map(&:name) end

#zipsearch_associationsObject



35
36
37
# File 'lib/zip_search/has_locations.rb', line 35

def zipsearch_associations
  reflect_on_all_associations.select{|a| a.foreign_key == 'locatable_id' }
end