app/models/address.rb in artfully_ose-1.2.0 vs app/models/address.rb in artfully_ose-1.3.0.pre1

- old
+ new

@@ -1,19 +1,52 @@ class Address < ActiveRecord::Base - attr_accessible :address1, :address2, :city, :state, :zip, :country, :person_id, :household_id - + attr_accessor :member_update + attr_accessible :address1, :address2, :city, :state, :zip, :country, :person_id, + :household_id, :kind, :is_primary, :member_update belongs_to :person belongs_to :household + after_save { person.solr_index! if person } + after_destroy { person.solr_index! if person } + + searchable do + text :address1, :address2, :city, :state, :zip, :country + integer :person_id, stored: true + string :organization_id do + person.organization_id unless person.nil? + end + end + include Ext::DelayedIndexing + + # after_save do |record| + # Delayed::Job.enqueue(SuggestHouseholdsByAddressJob.new(record.id), :queue => :suggested_households) + # end + + after_save :create_update_note, :if => :member_update + after_destroy :create_delete_note, :if => :member_update + # # Needed because widget checkouts don't yet have a person_id # def valid_for_widget_checkout? !(address1.blank? || city.blank? || state.blank? || zip.blank?) end + def city_state + str = city + + if city.present? && state.present? + str += ", " + end + + if state.present? + str += state + end + str + end + def address "#{address1} #{address2}" end def to_s @@ -24,58 +57,42 @@ def blank? address1.blank? && address2.blank? && city.blank? && state.blank? && zip.blank? end def is_same_as(addr) - return address1.eql?(addr.address1) && + return !addr.nil? && + address1.eql?(addr.address1) && address2.eql?(addr.address2) && city.eql?(addr.city) && state.eql?(addr.state) && zip.eql?(addr.zip) && - country.eql?(addr.country) + country.eql?(addr.country) && + kind.eql?(addr.kind) && + is_primary.eql?(addr.is_primary) end def self.from_payment(payment) payment.try(:customer).try(:address) end def self.unhash(address) - (address.is_a? Hash) ? Address.new(address.except(:id, :created_at, :updated_at, :old_mongo_id)) : address + (address.is_a? Hash) ? Address.new(address.except(:id, :created_at, :updated_at, :_destroy)) : address end def self.find_or_create(pers_id) #refactor to first_or_initialize when Rails 3.2 where(:person_id => pers_id).first || Address.create(:person_id => pers_id) end - def update_with_note(person, user, address, time_zone, updated_by) - old_addr = to_s() - - unless is_same_as(address) - ["address1", "address2", "city", "state", "zip", "country"].each do |field| - self.send("#{field}=", address.send(field)) - end - - if save - extra = updated_by.nil? ? "" : " from #{updated_by}" - text = "address updated#{extra}" - text = text + ", old address was: (#{old_addr})" unless old_addr.blank? - note = person.notes.create({ - :occurred_at => DateTime.now.in_time_zone(time_zone), - :text => text - }) - note.user = user - else - return false - end - end - - true - end - def ==(other) - address1 == other.address1 && address2 == other.address2 && city == other.city && state == other.state && zip == other.zip && country == other.country + other.is_a?(Address) && + address1 == other.address1 && + address2 == other.address2 && + city == other.city && + state == other.state && + zip == other.zip && + country == other.country end def values_hash { :address1 => address1, @@ -85,7 +102,35 @@ :zip => zip, :country => country } end + def self.kinds + [ "Home", "Work", "Other" ] + end + private + + def create_update_note + text = "Address #{old_addr.blank? ? 'added' : 'updated'} from member dashboard" + text = text + ", old address was: (#{old_addr})" unless old_addr.blank? + create_note(text) + end + + def create_delete_note + text = "Address deleted from member dashboard" + text = text + ", it was: (#{old_addr})" unless old_addr.blank? + create_note(text) + end + + def create_note(text) + self.person.notes.create({ + :organization_id => self.person.organization.id, + :occurred_at => DateTime.now.in_time_zone(person.organization.time_zone), + :text => text + }) + end + + def old_addr + "#{address1_was} #{address2_was} #{city_was} #{state_was} #{zip_was} #{country_was}" + end end