Skip to content

Commit

Permalink
Optionally do not overwrite seed data if it already exists. In many c…
Browse files Browse the repository at this point in the history
…ases production servers will update data and be authoritative; therefore overwriting in these cases isn't what you want.

To not overwrite and just insert if not existing, use: Content.seed(:data_key, true) do |s|...end
  • Loading branch information
darcy committed Jul 7, 2008
1 parent 93e5406 commit a1c40c9
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/seeder.rb
Expand Up @@ -2,9 +2,11 @@ class Seeder
def self.plant(model_class, *constraints, &block)
constraints = [:id] if constraints.empty?
seed = Seeder.new(model_class)
insert_only = constraints.last.is_a? TrueClass
constraints.delete_at(*constraints.length-1) if (constraints.last.is_a? TrueClass or constraints.last.is_a? FalseClass)
seed.set_constraints(*constraints)
yield seed
seed.plant!
seed.plant!(insert_only)
end

def initialize(model_class)
Expand All @@ -26,8 +28,9 @@ def set_attribute(name, value)
@data[name.to_sym] = value
end

def plant!
def plant! insert_only=false
record = get
return if !record.new_record? and insert_only
@data.each do |k, v|
record.send("#{k}=", v)
end
Expand Down

0 comments on commit a1c40c9

Please sign in to comment.