When I include the module below, factory_girl builds my model with empty attributes. If I leave off the module, no problems. Any idea why?
User.create(attributes_for(:user)) # => this works
create(:user) # => all empty attributes
My setup:
class User < ActiveRecord::Base
include Models::Profile::UserExt
has_profile { |user| { class_name: "#{user.role.capitalize}::Profile" }}
end
module Models
module Profile
module UserExt
extend ActiveSupport::Concern
included do
after_initialize :_init_profile
after_create :create_profile
end
module ClassMethods
def has_profile(&block)
@profile_association_block = block
end
end
def _init_profile
block = self.class.instance_variable_get :@profile_association_block
self.class.has_one :profile, block.call(self)
end
end
end
end
FactoryGirl.define do
factory :user do
email { Faker::Internet.email }
role { %w(sponsor student).sample }
end
end