Sha256: 69992da577e4abba82d2df2c57781da501fde0050987b18f3d10ecf31f002b1e

Contents?: true

Size: 1.87 KB

Versions: 14

Compression:

Stored size: 1.87 KB

Contents

class Address < ActiveRecord::Base
  attr_accessible :address1, :address2, :city, :state, :zip, :country, :person_id
  belongs_to :person

  #
  # Note that any validations here are skipped in person.rb
  # so that unsaved people can also have unsaved addresses
  #
  validates :person_id, :presence => true

  def address
    "#{address1} #{address2}"
  end
  
  def to_s
    "#{address1} #{address2} #{city} #{state} #{zip} #{country}"
  end

  # Country intentionally omitted
  def blank?
    address1.blank? && address2.blank? && city.blank? && state.blank? && zip.blank?
  end

  def is_same_as(addr)
    return address1.eql?(addr.address1) &&
           address2.eql?(addr.address2) &&
           city.eql?(addr.city) &&
           state.eql?(addr.state) &&
           zip.eql?(addr.zip) &&
           country.eql?(addr.country)
  end

  def self.from_payment(payment)
    payment.try(:customer).try(:address)
  end
  
  def self.unhash(address)
    (address.is_a? Hash) ? Address.new(address)  : 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
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
artfully_ose-1.2.0.pre.15 app/models/address.rb
artfully_ose-1.2.0.pre.12 app/models/address.rb
artfully_ose-1.2.0.pre.11 app/models/address.rb
artfully_ose-1.2.0.pre.10 app/models/address.rb
artfully_ose-1.2.0.pre.9 app/models/address.rb
artfully_ose-1.2.0.pre.8 app/models/address.rb
artfully_ose-1.2.0.pre.7 app/models/address.rb
artfully_ose-1.2.0.pre.6 app/models/address.rb
artfully_ose-1.2.0.pre.5 app/models/address.rb
artfully_ose-1.2.0.pre.4 app/models/address.rb
artfully_ose-1.2.0.pre.3 app/models/address.rb
artfully_ose-1.2.0.pre.2 app/models/address.rb
artfully_ose-1.2.0.pre.1 app/models/address.rb
artfully_ose-1.2.0.pre app/models/address.rb