From 6cac392486ad7d003486c6080f9e225c617a1c01 Mon Sep 17 00:00:00 2001 From: Patrick Blesi Date: Sun, 14 Dec 2014 12:23:13 -0600 Subject: [PATCH 1/2] fix seed_once! functionality --- features/seed_once.feature | 5 ++++ .../given/i_have_already_seeded_a_record.rb | 6 +++++ .../i_should_not_seed_the_record_again.rb | 4 +++ .../when/i_seed_the_record_using_seed_once.rb | 6 +++++ lib/seed-fu-mongoid.rb | 8 ++---- lib/seed-fu-mongoid/document_seeder.rb | 25 +++++++++++++++---- 6 files changed, 43 insertions(+), 11 deletions(-) create mode 100644 features/seed_once.feature create mode 100644 features/step_definitions/given/i_have_already_seeded_a_record.rb create mode 100644 features/step_definitions/then/i_should_not_seed_the_record_again.rb create mode 100644 features/step_definitions/when/i_seed_the_record_using_seed_once.rb diff --git a/features/seed_once.feature b/features/seed_once.feature new file mode 100644 index 0000000..42a00c9 --- /dev/null +++ b/features/seed_once.feature @@ -0,0 +1,5 @@ +Feature: Seed Once + Scenario: Seed a record once + Given I have already seeded a record + When I seed the record using seed_once + Then I should not seed the record again diff --git a/features/step_definitions/given/i_have_already_seeded_a_record.rb b/features/step_definitions/given/i_have_already_seeded_a_record.rb new file mode 100644 index 0000000..3626d56 --- /dev/null +++ b/features/step_definitions/given/i_have_already_seeded_a_record.rb @@ -0,0 +1,6 @@ +Given(/^I have already seeded a record$/) do + record = Record.new + record[:_id] = 1 + record[:data] = 'existing' + record.upsert +end diff --git a/features/step_definitions/then/i_should_not_seed_the_record_again.rb b/features/step_definitions/then/i_should_not_seed_the_record_again.rb new file mode 100644 index 0000000..153e8a3 --- /dev/null +++ b/features/step_definitions/then/i_should_not_seed_the_record_again.rb @@ -0,0 +1,4 @@ +Then(/^I should not seed the record again$/) do + Record.count.should == 1 + Record.find(1).data.should == 'existing' +end diff --git a/features/step_definitions/when/i_seed_the_record_using_seed_once.rb b/features/step_definitions/when/i_seed_the_record_using_seed_once.rb new file mode 100644 index 0000000..880550d --- /dev/null +++ b/features/step_definitions/when/i_seed_the_record_using_seed_once.rb @@ -0,0 +1,6 @@ +When(/^I seed the record using seed_once$/) do + Record.seed_once :id do |s| + s.id = 1 + s.data = 'data' + end +end diff --git a/lib/seed-fu-mongoid.rb b/lib/seed-fu-mongoid.rb index ae751eb..4e86b4d 100644 --- a/lib/seed-fu-mongoid.rb +++ b/lib/seed-fu-mongoid.rb @@ -27,12 +27,8 @@ end module Mongoid::Document module ClassMethods - def seed_once(*constraints) - seeder = SeedFuMongoid::DocumentSeeder.new(self, constraints, block) - - if seeder.new? - seeder.seed! - end + def seed_once(*constraints, &block) + SeedFuMongoid::DocumentSeeder.new(self, constraints, block).seed_once! end def seed(*constraints_and_objects, &block) diff --git a/lib/seed-fu-mongoid/document_seeder.rb b/lib/seed-fu-mongoid/document_seeder.rb index 941cd8c..5b9b663 100644 --- a/lib/seed-fu-mongoid/document_seeder.rb +++ b/lib/seed-fu-mongoid/document_seeder.rb @@ -58,9 +58,9 @@ module SeedFuMongoid return @constraint_search if @constraint_search @constraint_search = {} - constraints.each do |constraint| + @constraints.each do |constraint| if data[constraint] - @constraint_search[constraint] = proxy[constraint] + @constraint_search[constraint] = data[constraint] else raise ConstraintNotDefined.new(constraint) end @@ -78,6 +78,10 @@ module SeedFuMongoid document.upsert end + + def new? + !document.persisted? + end end def create_document(block_or_object) @@ -94,9 +98,20 @@ module SeedFuMongoid end end - def new? - !document.persisted? + def seed_once! + if @objects.empty? + document = create_document(@block) + if document.new? + document.seed! + end + else + @objects.each do |object| + document = create_document(object) + if document.new? + document.seed! + end + end + end end end end - -- 2.45.2 From f7a07580a936d8c8c43ce64a4a8114e15b924b6d Mon Sep 17 00:00:00 2001 From: Patrick Blesi Date: Sun, 14 Dec 2014 23:17:10 -0600 Subject: [PATCH 2/2] add quiet flag to seed output --- lib/seed-fu-mongoid/document_seeder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/seed-fu-mongoid/document_seeder.rb b/lib/seed-fu-mongoid/document_seeder.rb index 5b9b663..13b193d 100644 --- a/lib/seed-fu-mongoid/document_seeder.rb +++ b/lib/seed-fu-mongoid/document_seeder.rb @@ -74,7 +74,7 @@ module SeedFuMongoid document.send :[]=, key, value end - puts "#{@klass.name} #{document.attributes}" + puts "#{@klass.name} #{document.attributes}" unless SeedFuMongoid.quiet document.upsert end -- 2.45.2