Compare commits
2 Commits
master
...
bopm/maste
Author | SHA1 | Date | |
---|---|---|---|
|
d3f99bc66f | ||
|
8256e05d68 |
@ -1,5 +0,0 @@
|
|||||||
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
|
|
@ -1,6 +0,0 @@
|
|||||||
Given(/^I have already seeded a record$/) do
|
|
||||||
record = Record.new
|
|
||||||
record[:_id] = 1
|
|
||||||
record[:data] = 'existing'
|
|
||||||
record.upsert
|
|
||||||
end
|
|
@ -1,4 +0,0 @@
|
|||||||
Then(/^I should not seed the record again$/) do
|
|
||||||
Record.count.should == 1
|
|
||||||
Record.find(1).data.should == 'existing'
|
|
||||||
end
|
|
@ -1,6 +0,0 @@
|
|||||||
When(/^I seed the record using seed_once$/) do
|
|
||||||
Record.seed_once :id do |s|
|
|
||||||
s.id = 1
|
|
||||||
s.data = 'data'
|
|
||||||
end
|
|
||||||
end
|
|
@ -27,8 +27,12 @@ end
|
|||||||
|
|
||||||
module Mongoid::Document
|
module Mongoid::Document
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
def seed_once(*constraints, &block)
|
def seed_once(*constraints)
|
||||||
SeedFuMongoid::DocumentSeeder.new(self, constraints, block).seed_once!
|
seeder = SeedFuMongoid::DocumentSeeder.new(self, constraints, block)
|
||||||
|
|
||||||
|
if seeder.new?
|
||||||
|
seeder.seed!
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def seed(*constraints_and_objects, &block)
|
def seed(*constraints_and_objects, &block)
|
||||||
|
@ -48,7 +48,7 @@ module SeedFuMongoid
|
|||||||
|
|
||||||
def document
|
def document
|
||||||
@doc ||= begin
|
@doc ||= begin
|
||||||
@klass.find_by(constraint_search)
|
@klass.find_by(constraint_search) || @klass.new
|
||||||
rescue => e
|
rescue => e
|
||||||
@klass.new
|
@klass.new
|
||||||
end
|
end
|
||||||
@ -74,14 +74,10 @@ module SeedFuMongoid
|
|||||||
document.send :[]=, key, value
|
document.send :[]=, key, value
|
||||||
end
|
end
|
||||||
|
|
||||||
puts "#{@klass.name} #{document.attributes}" unless SeedFuMongoid.quiet
|
puts "#{@klass.name} #{document.attributes}"
|
||||||
|
|
||||||
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)
|
||||||
@ -98,20 +94,9 @@ module SeedFuMongoid
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def seed_once!
|
def new?
|
||||||
if @objects.empty?
|
!document.persisted?
|
||||||
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