From f35b5be65652c7fe19243cbba92dc7afb546bc6b Mon Sep 17 00:00:00 2001 From: Ian White Date: Tue, 9 Mar 2010 13:14:59 +0000 Subject: [PATCH] Implement referenceable models for find with table --- .../pickle/create_from_active_record.feature | 1 - lib/pickle/session.rb | 14 +++++++++++--- .../pickle/templates/pickle_steps.rb | 3 +-- spec/lib/pickle_session_spec.rb | 18 +++++++++++++++--- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/features/pickle/create_from_active_record.feature b/features/pickle/create_from_active_record.feature index 17de2009..f5972f30 100644 --- a/features/pickle/create_from_active_record.feature +++ b/features/pickle/create_from_active_record.feature @@ -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" - diff --git a/lib/pickle/session.rb b/lib/pickle/session.rb index c36457b0..641ce3f4 100644 --- a/lib/pickle/session.rb +++ b/lib/pickle/session.rb @@ -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 @@ -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) diff --git a/rails_generators/pickle/templates/pickle_steps.rb b/rails_generators/pickle/templates/pickle_steps.rb index ae4564fe..9f612171 100755 --- a/rails_generators/pickle/templates/pickle_steps.rb +++ b/rails_generators/pickle/templates/pickle_steps.rb @@ -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 diff --git a/spec/lib/pickle_session_spec.rb b/spec/lib/pickle_session_spec.rb index d27f689c..aad6a643 100644 --- a/spec/lib/pickle_session_spec.rb +++ b/spec/lib/pickle_session_spec.rb @@ -207,17 +207,23 @@ 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(, ) 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(,
) 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 @@ -225,11 +231,17 @@ def do_create_model @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(,
) 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(,
) 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