Module: Bento::Models::Modules::UserAccessors

Defined in:
lib/bento/models/modules/user_accessors.rb

Constant Summary collapse

USER_ACCESSORS =
[:first_name, :last_name, :email, :password, :password_confirmation]

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/bento/models/modules/user_accessors.rb', line 7

def self.included(base)
  base.class_eval do
    include ::Bento::Models::Modules::UserAssociation
    attr_accessor *USER_ACCESSORS
    before_validation :build_user, :on => :create

    define_method("build_user") do
      user_attributes = USER_ACCESSORS.inject({}) { |h, key| h.merge(key => send(key)) }
      if user_attributes.values.any?
        @user ||= users.build(user_attributes)
        @user.tap(&:valid?).errors.each { |attribute, message| errors.add(attribute, message) }
        @user
      end
    end
  end
end