Compare commits
3 Commits
bopm/maste
...
master
Author | SHA1 | Date | |
---|---|---|---|
|
9feb8e6459 | ||
|
f7a07580a9 | ||
|
6cac392486 |
5
features/seed_once.feature
Normal file
5
features/seed_once.feature
Normal file
@ -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
|
@ -0,0 +1,6 @@
|
|||||||
|
Given(/^I have already seeded a record$/) do
|
||||||
|
record = Record.new
|
||||||
|
record[:_id] = 1
|
||||||
|
record[:data] = 'existing'
|
||||||
|
record.upsert
|
||||||
|
end
|
@ -0,0 +1,4 @@
|
|||||||
|
Then(/^I should not seed the record again$/) do
|
||||||
|
Record.count.should == 1
|
||||||
|
Record.find(1).data.should == 'existing'
|
||||||
|
end
|
@ -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
|
@ -27,12 +27,8 @@ end
|
|||||||
|
|
||||||
module Mongoid::Document
|
module Mongoid::Document
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
def seed_once(*constraints)
|
def seed_once(*constraints, &block)
|
||||||
seeder = SeedFuMongoid::DocumentSeeder.new(self, constraints, block)
|
SeedFuMongoid::DocumentSeeder.new(self, constraints, block).seed_once!
|
||||||
|
|
||||||
if seeder.new?
|
|
||||||
seeder.seed!
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def seed(*constraints_and_objects, &block)
|
def seed(*constraints_and_objects, &block)
|
||||||
|
@ -58,9 +58,9 @@ module SeedFuMongoid
|
|||||||
return @constraint_search if @constraint_search
|
return @constraint_search if @constraint_search
|
||||||
|
|
||||||
@constraint_search = {}
|
@constraint_search = {}
|
||||||
constraints.each do |constraint|
|
@constraints.each do |constraint|
|
||||||
if data[constraint]
|
if data[constraint]
|
||||||
@constraint_search[constraint] = proxy[constraint]
|
@constraint_search[constraint] = data[constraint]
|
||||||
else
|
else
|
||||||
raise ConstraintNotDefined.new(constraint)
|
raise ConstraintNotDefined.new(constraint)
|
||||||
end
|
end
|
||||||
@ -74,10 +74,14 @@ module SeedFuMongoid
|
|||||||
document.send :[]=, key, value
|
document.send :[]=, key, value
|
||||||
end
|
end
|
||||||
|
|
||||||
puts "#{@klass.name} #{document.attributes}"
|
puts "#{@klass.name} #{document.attributes}" unless SeedFuMongoid.quiet
|
||||||
|
|
||||||
document.upsert
|
document.upsert
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def new?
|
||||||
|
!document.persisted?
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_document(block_or_object)
|
def create_document(block_or_object)
|
||||||
@ -94,9 +98,20 @@ module SeedFuMongoid
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def new?
|
def seed_once!
|
||||||
!document.persisted?
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user