Class: Seedie::Model::Creator

Inherits:
Object
  • Object
show all
Includes:
Reporters::Reportable
Defined in:
lib/seedie/model/creator.rb

Instance Method Summary collapse

Methods included from Reporters::Reportable

#add_observers, #report

Constructor Details

#initialize(model, reporters = []) ⇒ Creator

Returns a new instance of Creator.



8
9
10
11
12
13
# File 'lib/seedie/model/creator.rb', line 8

def initialize(model, reporters = [])
  @model = model
  @reporters = reporters

  add_observers(@reporters)
end

Instance Method Details

#create(field_values_set) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/seedie/model/creator.rb', line 22

def create(field_values_set)
  begin
    create!(field_values_set)
  rescue ActiveRecord::RecordInvalid => e
    report(:record_invalid, record: e.record)
    nil
  end
end

#create!(field_values_set) ⇒ Object



15
16
17
18
19
20
# File 'lib/seedie/model/creator.rb', line 15

def create!(field_values_set)
  record = @model.create!(field_values_set)
  report(:record_created, name: record.class.to_s, id: record.id.to_s)

  record
end