Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Renamed create_model_from_table => create_models_from_table, refactor…
…ed some of xing's code, and added specs to bring coverage to 100%
  • Loading branch information
ianwhite committed Mar 9, 2010
1 parent 5f3034a commit 5e4c25b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
31 changes: 8 additions & 23 deletions lib/pickle/session.rb
Expand Up @@ -20,20 +20,21 @@ def proxy_to_pickle_parser(world_class)
end
end

def create_model(a_model_name, fields = nil)
factory, label = *parse_model(a_model_name)
def create_model(pickle_ref, fields = nil)
factory, label = *parse_model(pickle_ref)
raise ArgumentError, "Can't create with an ordinal (e.g. 1st user)" if label.is_a?(Integer)
fields = fields.is_a?(Hash) ? parse_hash(fields) : parse_fields(fields)
record = pickle_config.factories[factory].create(fields)
store_model(factory, label, record)
end

def create_model_from_table(plural_factory, table)
model_class = model_class_for_factory(plural_factory)

# if a column exists in the table which matches the singular factory name, this is used as the pickle ref
def create_models_from_table(plural_factory, table)
factory = plural_factory.singularize
table.hashes.each do |hash|
name = construct_name_from_pickleref_and_factory(hash.delete(plural_factory.singularize), plural_factory)
create_model(name, hash)
pickle_ref = factory
pickle_ref += ' "' + hash.delete(factory) + '"' if hash[factory]
create_model(pickle_ref, hash)
end
end

Expand Down Expand Up @@ -109,22 +110,6 @@ def models(factory)
def respond_to_with_pickle_parser?(method, include_private = false)
respond_to_without_pickle_parser?(method, include_private) || pickle_parser.respond_to?(method, include_private)
end

private

def model_class_for_factory(factory_name)
name = factory_name.singularize
factory, name_or_index = *parse_model(name)
pickle_config.factories[factory].klass
end

def construct_name_from_pickleref_and_factory(pickle_ref, plural_factory)
if pickle_ref && !pickle_ref.empty?
'the ' + plural_factory.singularize + ": \"#{pickle_ref}\""
else
plural_factory.singularize
end
end

protected
def method_missing_with_pickle_parser(method, *args, &block)
Expand Down
2 changes: 1 addition & 1 deletion rails_generators/pickle/templates/pickle_steps.rb
Expand Up @@ -12,7 +12,7 @@

# create models from a table
Given(/^the following #{capture_plural_factory} exists?:?$/) do |plural_factory, table|
create_model_from_table(plural_factory, table)
create_models_from_table(plural_factory, table)
end

# find a model
Expand Down
26 changes: 26 additions & 0 deletions spec/lib/pickle_session_spec.rb
Expand Up @@ -207,6 +207,32 @@ def do_create_model
end
end

describe "#create_models_from_table(plural_factory, 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
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
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
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
end
end

describe "#find_model!" do
it "should call find_model" do
should_receive(:find_model).with('name', 'fields').and_return(mock('User'))
Expand Down

0 comments on commit 5e4c25b

Please sign in to comment.