Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Deprecate attributes_for and build_stubbed
  • Loading branch information
joshuaclayton committed Jan 16, 2012
1 parent 69957ea commit a883315
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/factory_girl/syntax/methods.rb
Expand Up @@ -5,6 +5,10 @@ module Methods
# can be individually overridden by passing in a Hash of attribute => value
# pairs.
#
# DEPRECATED
#
# Use build or create instead, calling attributes on the result
#
# Arguments:
# * name: +Symbol+ or +String+
# The name of the factory that should be used.
Expand All @@ -18,6 +22,7 @@ module Methods
# A set of attributes that can be used to build an instance of the class
# this factory generates.
def attributes_for(name, *traits_and_overrides, &block)
$stderr.puts "DEPRECATION WARNING: FactoryGirl.attributes_for is deprecated; use FactoryGirl.build or FactoryGirl.create and call #attributes on the result instead."
run_factory_girl_proxy(name, traits_and_overrides, Proxy::AttributesFor, &block)
end

Expand Down Expand Up @@ -67,6 +72,10 @@ def create(name, *traits_and_overrides, &block)
# stubbed out. Attributes can be individually overridden by passing in a Hash
# of attribute => value pairs.
#
# DEPRECATED
#
# Use build instead
#
# Arguments:
# * name: +Symbol+ or +String+
# The name of the factory that should be used.
Expand All @@ -79,6 +88,7 @@ def create(name, *traits_and_overrides, &block)
# Returns: +Object+
# An object with generated attributes stubbed out.
def build_stubbed(name, *traits_and_overrides, &block)
$stderr.puts "DEPRECATION WARNING: FactoryGirl.build_stubbed is deprecated; use FactoryGirl.build instead."
run_factory_girl_proxy(name, traits_and_overrides, Proxy::Stub, &block)
end

Expand Down
8 changes: 8 additions & 0 deletions lib/factory_girl/syntax/vintage.rb
Expand Up @@ -110,6 +110,10 @@ def self.alias(pattern, replace)
end

# Alias for FactoryGirl.attributes_for
#
# DEPRECATED
#
# Use build or create instead, calling attributes on the result
def self.attributes_for(name, overrides = {})
FactoryGirl.attributes_for(name, overrides)
end
Expand All @@ -125,6 +129,10 @@ def self.create(name, overrides = {})
end

# Alias for FactoryGirl.build_stubbed.
#
# DEPRECATED
#
# Use build instead
def self.stub(name, overrides = {})
FactoryGirl.build_stubbed(name, overrides)
end
Expand Down

1 comment on commit a883315

@masterkain
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uhm..while @track_attributes = attributes_for(:audio_file) returned a set of attributes valid for creating a record @track_attributes = build(:audio_file).attributes will return a complete object with the undeclared attributes set to nil

The key difference here is that if someone is using config.active_record.mass_assignment_sanitizer = :strict like me we have a hard time using the method to generate a proper test without filtering out nil values:

  string = $verifier.generate(audio: @track_attributes, auth_token: @user.authentication_token, now: Time.now.utc, pass: Settings.app.node.crypto_key)
  post 'notify_file_ready', k: string, format: 'json'

 Failure/Error: post 'notify_file_ready', k: string, format: 'json'
 ActiveModel::MassAssignmentSecurity::Error:
Can't mass-assign protected attributes: ....

So yeah, it can be useful to have a method to generate a object with only valid (declared) attributes in it.

Please sign in to comment.