Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support :traits option on implicit association #1552

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions GETTING_STARTED.md
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,8 @@ factory :post do
association :author, :admin, factory: :user, name: 'John Doe'
# or
association :author, factory: [:user, :admin], name: 'John Doe'
# or
author factory: :user, traits: [:admin], name: 'John Doe'
end

# creates an admin user with name "John Doe"
Expand Down
2 changes: 1 addition & 1 deletion lib/factory_bot/declaration/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize(name, *options)
@options = options.dup
@overrides = options.extract_options!
@factory_name = @overrides.delete(:factory) || name
@traits = options
@traits = options + (@overrides.delete(:traits) || [])
end

def ==(other)
Expand Down
8 changes: 7 additions & 1 deletion lib/factory_bot/definition_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ class DefinitionProxy
method
].freeze

VALID_OPTION_KEYS = %i[
factory
traits
].freeze

(instance_methods + private_instance_methods).each do |method|
undef_method(method) unless UNPROXIED_METHODS.include?(method.to_s)
end
Expand Down Expand Up @@ -250,7 +255,8 @@ def __declare_attribute__(name, block)
end

def __valid_association_options?(options)
options.respond_to?(:has_key?) && options.has_key?(:factory)
options.respond_to?(:has_key?) &&
VALID_OPTION_KEYS.any? { |key| options.has_key?(key) }
end
end
end
28 changes: 28 additions & 0 deletions spec/acceptance/associations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,32 @@
)
end
end

context "when building implicit association with :traits option" do
it "builds the association according to the given strategy" do
define_model("Article", user_id: :integer) do
belongs_to :user
end

define_model("User") do
has_many :articles
attr_accessor :active
end

FactoryBot.define do
factory :article do
user traits: [:active]
end

factory :user do
trait :active do
active { true }
end
end
end

article = FactoryBot.create(:article)
expect(article.user.active).to be true
end
end
end
9 changes: 9 additions & 0 deletions spec/factory_bot/definition_proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@
.with_options(factory: :user)
end

it 'declares an association when called with a ":traits" key' do
definition = FactoryBot::Definition.new(:name)
proxy = FactoryBot::DefinitionProxy.new(definition)
proxy.author traits: [:admin]

expect(definition).to have_association_declaration(:author)
.with_options(traits: [:admin])
end

it "declares a dynamic attribute when called with a block" do
definition = FactoryBot::Definition.new(:name)
proxy = FactoryBot::DefinitionProxy.new(definition)
Expand Down