diegodfsd
2/22/2012 - 4:34 AM

address.rb

class User < ActiveRecord::Base
  has_many :credentials
  composed_of :address, :mapping => [ %w(address_postal_code postal_code),
                                      %w(address_street street),
                                      %w(address_number number),
                                      %w(address_state state),
                                      %w(address_city city),
                                      %w(address_complement complement) ],
                        :converter => Proc.new{ |value| Address.new(value[:postal_code], value[:street], value[:number], value[:state], value[:city], value[:complement]) }

  devise :database_authenticatable, :registerable, :recoverable, :trackable,
         :confirmable, :omniauthable, :validatable

  validates_uniqueness_of :email, :username, :allow_blank => false
  validates_presence_of :name, :username, :date_of_birth
  validates_associated :address

  # Setup accessible (or protected) attributes for your model
  attr_accessible :name, 
                  :email,
                  :username,
                  :date_of_birth,
                  :password, 
                  :password_confirmation, 
                  :remember_me,
                  :address


  def self.new_with_session(params, session)
      super.tap do |user|
        if data = session["devise.omniauth_data"]
          user.name = data["info"]["name"]
          user.email = data["info"]["email"]
          user.credentials.build(:provider => data[:provider], :uid => data[:uid])
        end
      end
    end

end
class Address
  attr_reader :postal_code, :street, :number, :state, :city, :complement

  attr_accessible   :postal_code,
                    :street,
                    :number,
                    :state,
                    :city,
                    :neighborhood,
                    :complement


  def initialize(postal_code, street, number, state, city, complement)
    @postal_code = postal_code
    @street = street
    @number = number
    @state = state
    @city = city
    @complement = complement
  end

  def ==(other_address)
    postal_code == other_address.postal_code &&
    street == other_address.street &&
    state == other_address.state &&
    city == other_address.city
  end
end