Skip to content

Commit

Permalink
Change syntax for ignoring attributes to use block syntax instead of
Browse files Browse the repository at this point in the history
calling ignore on individual declarations.

Old syntax:

    factory :user do
      rockstar(true).ignore
      four { 2 + 2 }.ignore

      name { "John Doe#{" - Rockstar" if rockstar}" }
    end

New syntax:

    factory :user do
      ignore do
        rockstar true
        four     { 2 + 2 }
      end

      name { "John Doe#{" - Rockstar" if rockstar}" }
    end
  • Loading branch information
joshuaclayton committed Oct 9, 2011
1 parent 481ea09 commit dc32fd6
Show file tree
Hide file tree
Showing 26 changed files with 152 additions and 92 deletions.
6 changes: 4 additions & 2 deletions GETTING_STARTED.md
Expand Up @@ -159,8 +159,10 @@ Transient Attributes
There may be times where your code can be DRYed up by passing in transient attributes to factories.

factory :user do
rockstar(true).ignore
upcased { false }.ignore
ignore do
rockstar true
upcased { false }
end

name { "John Doe#{" - Rockstar" if rockstar}" }
email { "#{name.downcase}@example.com" }
Expand Down
8 changes: 2 additions & 6 deletions lib/factory_girl/attribute.rb
Expand Up @@ -12,16 +12,12 @@ class Attribute #:nodoc:

attr_reader :name, :ignored

def initialize(name)
def initialize(name, ignored)
@name = name.to_sym
@ignored = false
@ignored = ignored
ensure_non_attribute_writer!
end

def ignore
@ignored = true
end

def add_to(proxy)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/factory_girl/attribute/association.rb
Expand Up @@ -4,7 +4,7 @@ class Association < Attribute #:nodoc:
attr_reader :factory

def initialize(name, factory, overrides)
super(name)
super(name, false)
@factory = factory
@overrides = overrides
end
Expand Down
11 changes: 8 additions & 3 deletions lib/factory_girl/attribute/dynamic.rb
@@ -1,8 +1,8 @@
module FactoryGirl
class Attribute #:nodoc:
class Dynamic < Attribute #:nodoc:
def initialize(name, block)
super(name)
def initialize(name, ignored, block)
super(name, ignored)
@block = block
end

Expand All @@ -11,7 +11,12 @@ def add_to(proxy)
if FactoryGirl::Sequence === value
raise SequenceAbuseError
end
proxy.set(name, value, @ignored)

if @ignored
proxy.set_ignored(name, value)
else
proxy.set(name, value)
end
end
end
end
Expand Down
11 changes: 8 additions & 3 deletions lib/factory_girl/attribute/sequence.rb
Expand Up @@ -2,13 +2,18 @@ module FactoryGirl
class Attribute

class Sequence < Attribute
def initialize(name, sequence)
super(name)
def initialize(name, sequence, ignored)
super(name, ignored)
@sequence = sequence
end

def add_to(proxy)
proxy.set(name, FactoryGirl.generate(@sequence))
value = FactoryGirl.generate(@sequence)
if @ignored
proxy.set_ignored(name, value)
else
proxy.set(name, value)
end
end
end

Expand Down
10 changes: 7 additions & 3 deletions lib/factory_girl/attribute/static.rb
Expand Up @@ -5,13 +5,17 @@ class Static < Attribute #:nodoc:

attr_reader :value

def initialize(name, value)
super(name)
def initialize(name, value, ignored)
super(name, ignored)
@value = value
end

def add_to(proxy)
proxy.set(name, @value, @ignored)
if @ignored
proxy.set_ignored(name, @value)
else
proxy.set(name, @value)
end
end

def priority
Expand Down
10 changes: 5 additions & 5 deletions lib/factory_girl/declaration.rb
Expand Up @@ -2,18 +2,18 @@ module FactoryGirl
class Declaration
attr_reader :name

def initialize(name)
@name = name
def initialize(name, ignored = false)
@name = name
@ignored = ignored
end

def ignore
$stderr.puts "DEPRECATION WARNING: Use ignore block syntax instead of calling #ignore"
@ignored = true
end

def to_attributes
build.tap do |attributes|
attributes.each(&:ignore) if @ignored
end
build
end
end
end
2 changes: 1 addition & 1 deletion lib/factory_girl/declaration/association.rb
Expand Up @@ -2,7 +2,7 @@ module FactoryGirl
class Declaration
class Association < Declaration
def initialize(name, options)
super(name)
super(name, false)
@options = options
end

Expand Down
6 changes: 3 additions & 3 deletions lib/factory_girl/declaration/dynamic.rb
@@ -1,15 +1,15 @@
module FactoryGirl
class Declaration
class Dynamic < Declaration
def initialize(name, block)
super(name)
def initialize(name, ignored = false, block)
super(name, ignored)
@block = block
end

private

def build
[Attribute::Dynamic.new(name, @block)]
[Attribute::Dynamic.new(name, @ignored, @block)]
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/factory_girl/declaration/implicit.rb
@@ -1,8 +1,8 @@
module FactoryGirl
class Declaration
class Implicit < Declaration
def initialize(name, factory = nil)
super(name)
def initialize(name, factory = nil, ignored = false)
super(name, ignored)
@factory = factory
end

Expand All @@ -12,7 +12,7 @@ def build
if FactoryGirl.factories.registered?(name)
[Attribute::Association.new(name, name, {})]
elsif FactoryGirl.sequences.registered?(name)
[Attribute::Sequence.new(name, name)]
[Attribute::Sequence.new(name, name, @ignored)]
else
trait_root = @factory || FactoryGirl
trait_root.trait_by_name(name).attributes.to_a
Expand Down
6 changes: 3 additions & 3 deletions lib/factory_girl/declaration/static.rb
@@ -1,15 +1,15 @@
module FactoryGirl
class Declaration
class Static < Declaration
def initialize(name, value)
super(name)
def initialize(name, value, ignored = false)
super(name, ignored)
@value = value
end

private

def build
[Attribute::Static.new(name, @value)]
[Attribute::Static.new(name, @value, @ignored)]
end
end
end
Expand Down
16 changes: 11 additions & 5 deletions lib/factory_girl/definition_proxy.rb
Expand Up @@ -8,8 +8,9 @@ class DefinitionProxy

attr_reader :child_factories

def initialize(factory)
@factory = factory
def initialize(factory, ignore = false)
@factory = factory
@ignore = ignore
@child_factories = []
end

Expand All @@ -36,16 +37,21 @@ def add_attribute(name, value = nil, &block)
if value
raise AttributeDefinitionError, "Both value and block given"
else
declaration = Declaration::Dynamic.new(name, block)
declaration = Declaration::Dynamic.new(name, @ignore, block)
end
else
declaration = FactoryGirl::Declaration::Static.new(name, value)
declaration = FactoryGirl::Declaration::Static.new(name, value, @ignore)
end

@factory.declare_attribute(declaration)
declaration
end

def ignore(&block)
proxy = DefinitionProxy.new(@factory, true)
proxy.instance_eval(&block)
end

# Calls add_attribute using the missing method name as the name of the
# attribute, so that:
#
Expand Down Expand Up @@ -79,7 +85,7 @@ def add_attribute(name, value = nil, &block)
# are equivalent.
def method_missing(name, *args, &block)
if args.empty? && block.nil?
@factory.declare_attribute(Declaration::Implicit.new(name, @factory))
@factory.declare_attribute(Declaration::Implicit.new(name, @factory, @ignore))
elsif args.first.is_a?(Hash) && args.first.has_key?(:factory)
association(name, *args)
else
Expand Down
10 changes: 9 additions & 1 deletion lib/factory_girl/factory.rb
Expand Up @@ -60,7 +60,15 @@ def run(proxy_class, overrides) #:nodoc:
if factory_overrides.empty?
attribute.add_to(proxy)
else
factory_overrides.each { |attr, val| proxy.set(attr, val, attribute.ignored); overrides.delete(attr) }
factory_overrides.each do |attr, val|
if attribute.ignored
proxy.set_ignored(attr, val)
else
proxy.set(attr, val)
end

overrides.delete(attr)
end
end
end
overrides.each { |attr, val| proxy.set(attr, val) }
Expand Down
7 changes: 6 additions & 1 deletion lib/factory_girl/proxy.rb
Expand Up @@ -5,12 +5,17 @@ class Proxy #:nodoc:

def initialize(klass)
@callbacks = {}
@ignored_attributes = {}
end

def get(attribute)
end

def set(attribute, value, ignored = false)
def set(attribute, value)
end

def set_ignored(attribute, value)
@ignored_attributes[attribute] = value
end

def associate(name, factory, attributes)
Expand Down
8 changes: 2 additions & 6 deletions lib/factory_girl/proxy/attributes_for.rb
Expand Up @@ -11,12 +11,8 @@ def get(attribute)
@ignored_attributes[attribute] || @hash[attribute]
end

def set(attribute, value, ignored = false)
if ignored
@ignored_attributes[attribute] = value
else
@hash[attribute] = value
end
def set(attribute, value)
@hash[attribute] = value
end

def result(to_create)
Expand Down
9 changes: 2 additions & 7 deletions lib/factory_girl/proxy/build.rb
Expand Up @@ -4,7 +4,6 @@ class Build < Proxy #:nodoc:
def initialize(klass)
super(klass)
@instance = klass.new
@ignored_attributes = {}
end

def get(attribute)
Expand All @@ -15,12 +14,8 @@ def get(attribute)
end
end

def set(attribute, value, ignored = false)
if ignored
@ignored_attributes[attribute] = value
else
@instance.send(:"#{attribute}=", value)
end
def set(attribute, value)
@instance.send(:"#{attribute}=", value)
end

def associate(name, factory_name, overrides)
Expand Down
8 changes: 2 additions & 6 deletions lib/factory_girl/proxy/stub.rb
Expand Up @@ -55,12 +55,8 @@ def get(attribute)
end
end

def set(attribute, value, ignored = false)
if ignored
@ignored_attributes[attribute] = value
else
@instance.send(:"#{attribute}=", value)
end
def set(attribute, value)
@instance.send(:"#{attribute}=", value)
end

def associate(name, factory_name, overrides)
Expand Down
48 changes: 45 additions & 3 deletions spec/acceptance/transient_attributes_spec.rb
Expand Up @@ -8,9 +8,11 @@
sequence(:name) {|n| "John #{n}" }

factory :user do
four { 2 + 2 }.ignore
rockstar(true).ignore
upcased(false).ignore
ignore do
four { 2 + 2 }
rockstar true
upcased false
end

name { "#{FactoryGirl.generate(:name)}#{" - Rockstar" if rockstar}" }
email { "#{name.downcase}#{four}@example.com" }
Expand Down Expand Up @@ -66,3 +68,43 @@
end
end
end

describe "deprecated way of ignoring attributes" do
before do
define_model("User", :name => :string)

FactoryGirl.define do
factory :user do
rockstar(false).ignore

name { "John Doe#{" Rockstar" if rockstar}" }
end
end
end

it "assigns attributes correctly" do
FactoryGirl.build(:user, :rockstar => true).name.should == "John Doe Rockstar"
FactoryGirl.build(:user).name.should == "John Doe"
end
end

describe "transient sequences" do
before do
define_model("User", :name => :string)

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

name { "John Doe #{counter}" }
end
end
end

it "increments sequences correctly" do
FactoryGirl.build(:user).name.should == "John Doe 1"
FactoryGirl.build(:user).name.should == "John Doe 2"
end
end

0 comments on commit dc32fd6

Please sign in to comment.