Skip to content

Commit

Permalink
Implement referenceable models for find with table
Browse files Browse the repository at this point in the history
  • Loading branch information
ianwhite committed Mar 9, 2010
1 parent 80f62f1 commit f35b5be
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
1 change: 0 additions & 1 deletion features/pickle/create_from_active_record.feature
Expand Up @@ -65,4 +65,3 @@ Feature: I can easily create models from my blueprints
And the 2nd user should be the user: "Pete"
And the user: "lonely" should be the user: "Jack"
And the user: "rotting" should be the user: "Pete"

14 changes: 11 additions & 3 deletions lib/pickle/session.rb
Expand Up @@ -32,8 +32,7 @@ def create_model(pickle_ref, fields = nil)
def create_models_from_table(plural_factory, table)
factory = plural_factory.singularize
table.hashes.each do |hash|
pickle_ref = factory
pickle_ref += ' "' + hash.delete(factory) + '"' if hash[factory]
pickle_ref = factory + (hash[factory] ? " \"#{hash.delete(factory)}\"" : "")
create_model(pickle_ref, hash)
end
end
Expand All @@ -58,7 +57,16 @@ def find_models(factory, fields = nil)
records = model_class.find(:all, :conditions => convert_models_to_attributes(model_class, parse_fields(fields)))
records.each {|record| store_model(factory, nil, record)}
end


# if a column exists in the table which matches the singular factory name, this is used as the pickle ref
def find_models_from_table(plural_factory, table)
factory = plural_factory.singularize
table.hashes.each do |hash|
pickle_ref = factory + (hash[factory] ? " \"#{hash.delete(factory)}\"" : "")
find_model(pickle_ref, hash)
end
end

# return the original model stored by create_model or find_model
def created_model(name)
factory, name_or_index = *parse_model(name)
Expand Down
3 changes: 1 addition & 2 deletions rails_generators/pickle/templates/pickle_steps.rb
Expand Up @@ -27,8 +27,7 @@

# find models with a table
Then(/^the following #{capture_plural_factory} should exists?:?$/) do |plural_factory, table|
name = plural_factory.singularize
table.hashes.each { |hash| find_model!(name, hash)}
find_models_from_table(plural_factory, table)
end

# find exactly n models
Expand Down
18 changes: 15 additions & 3 deletions spec/lib/pickle_session_spec.rb
Expand Up @@ -207,29 +207,41 @@ def do_create_model
end
end

describe "#create_models_from_table(plural_factory, table)" do
describe "create and find using plural_factory and table" do
context "when given a table without a matching pickle ref column" do
before do
@table = mock(:hashes => [{'name' => 'Fred'}, {'name' => 'Betty'}])
end

it "should call create_model for each of the table hashes with plain factory name" do
it "#create_models_from_table(<plural factory>, <table>) should call create_model for each of the table hashes with plain factory name" do
should_receive(:create_model).with("user", 'name' => "Fred").once.ordered
should_receive(:create_model).with("user", 'name' => "Betty").once.ordered
create_models_from_table("users", @table)
end

it "#find_models_from_table(<plural factory>, <table>) should call find_model for each of the table hashes with plain factory name" do
should_receive(:find_model).with("user", 'name' => "Fred").once.ordered
should_receive(:find_model).with("user", 'name' => "Betty").once.ordered
find_models_from_table("users", @table)
end
end

context "when given a table with a matching pickle ref column" do
before do
@table = mock(:hashes => [{'user' => "fred", 'name' => 'Fred'}, {'user' => "betty", 'name' => 'Betty'}])
end

it "should call create_model for each of the table hashes with labelled pickle ref" do
it "#create_models_from_table(<plural factory>, <table>) should call create_model for each of the table hashes with labelled pickle ref" do
should_receive(:create_model).with("user \"fred\"", 'name' => "Fred").once.ordered
should_receive(:create_model).with("user \"betty\"", 'name' => "Betty").once.ordered
create_models_from_table("users", @table)
end

it "#find_models_from_table(<plural factory>, <table>) should call find_model for each of the table hashes with labelled pickle ref" do
should_receive(:find_model).with("user \"fred\"", 'name' => "Fred").once.ordered
should_receive(:find_model).with("user \"betty\"", 'name' => "Betty").once.ordered
find_models_from_table("users", @table)
end
end
end

Expand Down

0 comments on commit f35b5be

Please sign in to comment.