module ZipSearch::HasLocations::ClassMethods

Public Instance Methods

has_location(title=:location, options={}) click to toggle source
# 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) click to toggle source
# 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={}) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_names() click to toggle source
# File lib/zip_search/has_locations.rb, line 38
def zipsearch_association_names
  zipsearch_associations.map(&:name) end
zipsearch_associations() click to toggle source
# File lib/zip_search/has_locations.rb, line 35
def zipsearch_associations
  reflect_on_all_associations.select{|a| a.foreign_key == 'locatable_id' }
end