Skip to content

Commit

Permalink
Added assign configuration option.
Browse files Browse the repository at this point in the history
  • Loading branch information
panthomakos committed Jun 13, 2012
1 parent c9cfcb1 commit e8cebb7
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,27 @@ Alternative sham configurations can be invoked by passing their name into the
Item.sham!(:small, :quantity => 100)
Item.sham!(:large, :build, :quantity => 0)

## Assign Shams

If you have configured mass-assignment protected attributes in Rails, or you
would prefer your object to go through regular instance setters rather than the
initializer, you can use the `assign` configuration.

# sham/user.rb
Sham.config(User) do |c|
c.assign do
{ :name => 'John Doe' }
end
end

When executing `User.sham!` all attributes will be assigned using the instance
setters instead of the initializer. A `save` will also be called unless the
`:build` parameters is used.

User.any_instance.should_receive(:name=).with('Jane Doe')
User.any_instance.should_receive(:save)
User.sham!(:name => 'Jane Doe')

## Empty Shams

Sometimes you simply want to be able to sham an object without passing any
Expand Down
40 changes: 40 additions & 0 deletions lib/sham/config/assign.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'sham/config/base'
require 'sham/util'

module Sham
class Config
class Assign < Base
def initialize(config)
@config = config
end

def sham(build = false)
object = super(true)

@options.each do |key, value|
object.public_send("#{key}=", value)
end

if !build && object.respond_to?(:save)
object.save
end

object
end

def options(*args)
@options = ::Sham::Util.extract_options!(args)

@config.call.each do |key, value|
@options[key] = parse!(value) unless @options.has_key?(key)
end

self
end

def args
[]
end
end
end
end
58 changes: 58 additions & 0 deletions spec/lib/sham/config/assign_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require 'sham/config/assign'

describe Sham::Config::Assign do
before(:all) do
Object.send(:remove_const, :AssignTester) if defined?(AssignTester)
class AssignTester; end
end

let(:id){ stub }
let(:options){ { :id => id } }
let(:subject){ described_class.new(lambda{ options }) }
let(:config){ subject.object(AssignTester) }
let(:instance){ stub.as_null_object }

before{ AssignTester.stub(:new){ instance } }

it 'sets default options' do
instance.should_receive(:public_send).with('id=', id)
config.options.sham
end

it 'prioritizes passed options' do
instance.should_receive(:id=).with(2)
config.options(:id => 2).sham
end

it 'merges passed options' do
instance.should_receive(:name=).with('A')
config.options(:name => 'A').sham
end

it 'parses default options' do
subject.should_receive(:parse!).with(id)
config.options.sham
end

it 'does not parse passed options' do
subject.should_receive(:parse!).with(2).never
config.options(:id => 2).sham
end

it 'calls save if the instance responds to save' do
instance.stub(:respond_to?).with(:save){ true }
instance.should_receive(:save)
config.options.sham
end

it 'does not call save on build' do
instance.should_recieve(:save).never
config.options.sham(true)
end

it 'does not call save if it does not respond to save' do
instance.stub(:respond_to?).with(:save){ false }
instance.should_receive(:save).never
config.options.sham
end
end

0 comments on commit e8cebb7

Please sign in to comment.