Como definir fabricantes de modelos com associações polimórficas

Digamos que você tenha os seguintes modelos:

class Admin < ActiveRecord::Base
has_one
:login, as: :loginable
end

class User < ActiveRecord::Base
has_one
:login, as: :loginable
end

class Login < ActiveRecord::Base
belongs_to
:login, polymorphic: true

validate
:email # your validations
validate
:password # your
end

Aqui está como definir os fabricantes.

Em login_fabricator.rb:

Fabricator(:login) do
email
{ FFaker::Internet.email }
pass
= FFaker::Internet.password
password pass

password_confirmation pass

end

Fabricator(:admin_login, from: :login) do
loginable
(fabricator: :admin)
end

Fabricator(:user_login, from: :login) do
loginable
(fabricator: :user)
end

Em admin_fabricator.rb:

Fabricator(:admin) do
...
end

Fabricator(:admin_with_login, from: :admin) do
login
{ Fabricate.build :admin_login, loginable: nil }
end

Em user_fabricator.rb:

Fabricator(:user) do
...
end

Fabricator(:user_with_login, from: :user) do
login
{ Fabricate.build :user_login, loginable: nil }
end

Isso produzirá:

Admin

admin = Fabricate :admin
admin
.login # => nil
admin_with_login = Fabricate :admin_with_login
admin_with_login
.login # => <Login:0x007f70c6b75550>

Do utilizador

user = Fabricate :user
user
.login # => nil
user_with_login = Fabricate :user_with_login
user_with_login
.login # => <Login:0x007f70c6b75550>

Conecte-se

login = Fabricate :login
login
.loginable # => nil
admin_login = Fabricate :admin_login
admin_login
.loginable # => <Account:0x007f70c518a8e8>
user_login = Fabricate :user_login
user_login
.loginable # => <User:0x007f70c9150bd0>