Sha256: da04cea822b62d17b01a26fffa0711a4dc45e0688f5a2347909adab9d90893a4

Contents?: true

Size: 1.54 KB

Versions: 33

Compression:

Stored size: 1.54 KB

Contents

module Piggybak 
  class Address < ActiveRecord::Base
    belongs_to :state
    belongs_to :country
    has_one :order_shipping, :foreign_key => "shipping_address_id", :class_name => "Piggybak::Order"
    has_one :order_billing, :foreign_key => "billing_address_id", :class_name => "Piggybak::Order"

    validates_presence_of :firstname
    validates_presence_of :lastname
    validates_presence_of :address1
    validates_presence_of :city
    validates_presence_of :state_id
    validates_presence_of :country_id
    validates_presence_of :zip

    after_initialize :set_default_country
    after_save :document_address_changes

    def set_default_country
      self.country ||= Country.find_by_abbr(Piggybak.config.default_country)
    end

    def admin_label
      address = "#{self.firstname} #{self.lastname}<br />"
      address += "#{self.address1}<br />"
      if self.address2 && self.address2 != ''
        address += "#{self.address2}<br />"
      end
      address += "#{self.city}, #{self.state_display} #{self.zip}<br />"
      address += "#{self.country.name}"
      address
    end
    alias :display :admin_label  

    def state_display
      self.state ? self.state.name : self.state_id
    end

    def document_address_changes
      # TODO: Fix this. It isn't working.
      if self.order_billing.present? && self.changed?
        self.order_billing.recorded_changes << self.formatted_changes
      end
      if self.order_shipping.present? && self.changed?
        self.order_shipping.recorded_changes << self.formatted_changes
      end
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
piggybak-0.5.5 app/models/piggybak/address.rb
piggybak-0.5.4 app/models/piggybak/address.rb
piggybak-0.5.3 app/models/piggybak/address.rb
piggybak-0.5.2 app/models/piggybak/address.rb
piggybak-0.5.1 app/models/piggybak/address.rb
piggybak-0.5.0 app/models/piggybak/address.rb
piggybak-0.4.39 app/models/piggybak/address.rb
piggybak-0.4.38 app/models/piggybak/address.rb
piggybak-0.4.37 app/models/piggybak/address.rb
piggybak-0.4.36 app/models/piggybak/address.rb
piggybak-0.4.35 app/models/piggybak/address.rb
piggybak-0.4.34 app/models/piggybak/address.rb
piggybak-0.4.33 app/models/piggybak/address.rb
piggybak-0.4.32 app/models/piggybak/address.rb
piggybak-0.4.31 app/models/piggybak/address.rb
piggybak-0.4.30 app/models/piggybak/address.rb
piggybak-0.4.29 app/models/piggybak/address.rb
piggybak-0.4.28 app/models/piggybak/address.rb
piggybak-0.4.27 app/models/piggybak/address.rb
piggybak-0.4.26 app/models/piggybak/address.rb