Class: SeedFu::Seeder

Inherits:
Object
  • Object
show all
Defined in:
lib/seed-fu-ndo/seeder.rb

Overview

Enhanced class from Seed Fu to allow for recording and destroying seed data.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#model_classObject (readonly)

Returns the value of attribute model_class.



6
7
8
# File 'lib/seed-fu-ndo/seeder.rb', line 6

def model_class
  @model_class
end

Instance Method Details

#existing_recordsObject

List of the seeded records that exist in the database.



29
30
31
# File 'lib/seed-fu-ndo/seeder.rb', line 29

def existing_records
  @data.map { |record_data| find_record(record_data.symbolize_keys) }.compact
end

#seed_with_undoObject

Record instead of inserting the data if in recording mode.



9
10
11
12
13
14
15
16
17
18
# File 'lib/seed-fu-ndo/seeder.rb', line 9

def seed_with_undo
  if r = SeedFuNdo.recorder
    r.record self
    
    # return existing records in case they are processed by the caller
    @data.map { |record_data| find_record(record_data) }
  else
    seed_without_undo
  end
end

#unseedObject

Destroy the seed data.



22
23
24
25
26
# File 'lib/seed-fu-ndo/seeder.rb', line 22

def unseed
  @model_class.transaction do
    existing_records.reverse.each { |record| unseed_record(record) }
  end
end