Skip to content

Commit

Permalink
Deprecate #ignore in favor of #transient
Browse files Browse the repository at this point in the history
Update documentation to reflect that transient is now preferred
  • Loading branch information
Ian Zabel committed May 23, 2014
1 parent 493c9ad commit 9610b38
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 25 deletions.
22 changes: 11 additions & 11 deletions GETTING_STARTED.md
Expand Up @@ -237,7 +237,7 @@ There may be times where your code can be DRYed up by passing in transient attri


```ruby ```ruby
factory :user do factory :user do
ignore do transient do
rockstar true rockstar true
upcased false upcased false
end end
Expand All @@ -254,14 +254,14 @@ create(:user, upcased: true).name
#=> "JOHN DOE - ROCKSTAR" #=> "JOHN DOE - ROCKSTAR"
``` ```


Static and dynamic attributes can be ignored. Ignored attributes will be ignored Static and dynamic attributes can be created as transient attributes. Transient
within attributes\_for and won't be set on the model, even if the attribute attributes will be ignored within attributes\_for and won't be set on the model,
exists or you attempt to override it. even if the attribute exists or you attempt to override it.


Within factory_girl's dynamic attributes, you can access ignored attributes as Within factory_girl's dynamic attributes, you can access transient attributes as
you would expect. If you need to access the evaluator in a factory_girl callback, you would expect. If you need to access the evaluator in a factory_girl callback,
you'll need to declare a second block argument (for the evaluator) and access you'll need to declare a second block argument (for the evaluator) and access
ignored attributes from there. transient attributes from there.


Associations Associations
------------ ------------
Expand Down Expand Up @@ -340,14 +340,14 @@ FactoryGirl.define do


# user_with_posts will create post data after the user has been created # user_with_posts will create post data after the user has been created
factory :user_with_posts do factory :user_with_posts do
# posts_count is declared as an ignored attribute and available in # posts_count is declared as a transient attribute and available in
# attributes on the factory, as well as the callback via the evaluator # attributes on the factory, as well as the callback via the evaluator
ignore do transient do
posts_count 5 posts_count 5
end end


# the after(:create) yields two values; the user instance itself and the # the after(:create) yields two values; the user instance itself and the
# evaluator, which stores all values from the factory, including ignored # evaluator, which stores all values from the factory, including transient
# attributes; `create_list`'s second argument is the number of records # attributes; `create_list`'s second argument is the number of records
# to create and we make sure the user is associated properly to the post # to create and we make sure the user is associated properly to the post
after(:create) do |user, evaluator| after(:create) do |user, evaluator|
Expand Down Expand Up @@ -914,7 +914,7 @@ by calling `attributes`:


```ruby ```ruby
factory :user do factory :user do
ignore do transient do
comments_count 5 comments_count 5
end end


Expand All @@ -925,7 +925,7 @@ end
``` ```


This will build a hash of all attributes to be passed to `new`. It won't This will build a hash of all attributes to be passed to `new`. It won't
include ignored attributes, but everything else defined in the factory will be include transient attributes, but everything else defined in the factory will be
passed (associations, evalued sequences, etc.) passed (associations, evalued sequences, etc.)


You can define `initialize_with` for all factories by including it in the You can define `initialize_with` for all factories by including it in the
Expand Down
6 changes: 6 additions & 0 deletions lib/factory_girl/definition_proxy.rb
Expand Up @@ -52,6 +52,12 @@ def add_attribute(name, value = nil, &block)
end end


def ignore(&block) def ignore(&block)
ActiveSupport::Deprecation.warn "`#ignore` is deprecated and will be "\
"removed in 5.0. Please use `#transient` instead."
transient &block
end

def transient(&block)
proxy = DefinitionProxy.new(@definition, true) proxy = DefinitionProxy.new(@definition, true)
proxy.instance_eval(&block) proxy.instance_eval(&block)
end end
Expand Down
4 changes: 2 additions & 2 deletions spec/acceptance/create_list_spec.rb
Expand Up @@ -55,7 +55,7 @@
end end
end end


describe "multiple creates and ignored attributes to dynamically build attribute lists" do describe "multiple creates and transient attributes to dynamically build attribute lists" do
before do before do
define_model('User', name: :string) do define_model('User', name: :string) do
has_many :posts has_many :posts
Expand All @@ -75,7 +75,7 @@
name "John Doe" name "John Doe"


factory :user_with_posts do factory :user_with_posts do
ignore do transient do
posts_count 5 posts_count 5
end end


Expand Down
14 changes: 7 additions & 7 deletions spec/acceptance/initialize_with_spec.rb
Expand Up @@ -22,7 +22,7 @@ def self.construct(name, age)
its(:age) { should eq 21 } its(:age) { should eq 21 }
end end


describe "initialize_with with FG attributes that are ignored" do describe "initialize_with with FG attributes that are transient" do
include FactoryGirl::Syntax::Methods include FactoryGirl::Syntax::Methods


before do before do
Expand All @@ -34,7 +34,7 @@ def self.construct(name)


FactoryGirl.define do FactoryGirl.define do
factory :user do factory :user do
ignore do transient do
name { "Handsome Chap" } name { "Handsome Chap" }
end end


Expand Down Expand Up @@ -64,7 +64,7 @@ def initialize(name, data)
sequence(:random_data) { 5.times.map { Kernel.rand(200) } } sequence(:random_data) { 5.times.map { Kernel.rand(200) } }


factory :report_generator do factory :report_generator do
ignore do transient do
name "My Awesome Report" name "My Awesome Report"
end end


Expand Down Expand Up @@ -94,14 +94,14 @@ def initialize(name)


FactoryGirl.define do FactoryGirl.define do
factory :awesome do factory :awesome do
ignore do transient do
name "Great" name "Great"
end end


initialize_with { Awesome.new(name) } initialize_with { Awesome.new(name) }


factory :sub_awesome do factory :sub_awesome do
ignore do transient do
name "Sub" name "Sub"
end end
end end
Expand Down Expand Up @@ -134,7 +134,7 @@ def initialize(name)


FactoryGirl.define do FactoryGirl.define do
factory :awesome do factory :awesome do
ignore do transient do
name "Great" name "Great"
end end


Expand Down Expand Up @@ -195,7 +195,7 @@ def initialize(attributes = {})
sequence(:email) { |n| "person#{n}@example.com" } sequence(:email) { |n| "person#{n}@example.com" }


factory :user do factory :user do
ignore do transient do
ignored "of course!" ignored "of course!"
end end


Expand Down
33 changes: 30 additions & 3 deletions spec/acceptance/transient_attributes_spec.rb
Expand Up @@ -8,7 +8,7 @@
sequence(:name) { |n| "John #{n}" } sequence(:name) { |n| "John #{n}" }


factory :user do factory :user do
ignore do transient do
four { 2 + 2 } four { 2 + 2 }
rockstar true rockstar true
upcased false upcased false
Expand Down Expand Up @@ -67,6 +67,33 @@
expect(rockstar.name).to eq "John 1 - Rockstar" expect(rockstar.name).to eq "John 1 - Rockstar"
end end
end end

context "using aliased 'ignore' method name" do
around do |example|
cached_silenced = ActiveSupport::Deprecation.silenced
ActiveSupport::Deprecation.silenced = true
example.run
ActiveSupport::Deprecation.silenced = cached_silenced
end

before do
FactoryGirl.define do
factory :user_using_ignore, class: User do
ignore do
honorific "Esteemed"
end

name { "#{honorific} Jane Doe" }
end
end
end

let(:esteemed) { FactoryGirl.create(:user_using_ignore) }

it "uses the default value of the attribute" do
expect(esteemed.name).to eq "Esteemed Jane Doe"
end
end
end end


describe "transient sequences" do describe "transient sequences" do
Expand All @@ -75,7 +102,7 @@


FactoryGirl.define do FactoryGirl.define do
factory :user do factory :user do
ignore do transient do
sequence(:counter) sequence(:counter)
end end


Expand Down Expand Up @@ -106,7 +133,7 @@ def initialize(id, name)


FactoryGirl.define do FactoryGirl.define do
factory :user do factory :user do
ignore do transient do
foo { Foo.new('id-of-foo', 'name-of-foo')} foo { Foo.new('id-of-foo', 'name-of-foo')}
end end


Expand Down
4 changes: 2 additions & 2 deletions spec/factory_girl/definition_proxy_spec.rb
Expand Up @@ -44,12 +44,12 @@
end end
end end


describe FactoryGirl::DefinitionProxy, "#ignore" do describe FactoryGirl::DefinitionProxy, "#transient" do
subject { FactoryGirl::Definition.new } subject { FactoryGirl::Definition.new }
let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) } let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }


it "makes all attributes added ignored" do it "makes all attributes added ignored" do
proxy.ignore do proxy.transient do
add_attribute(:attribute_name, "attribute value") add_attribute(:attribute_name, "attribute value")
end end


Expand Down

0 comments on commit 9610b38

Please sign in to comment.