Module: HasModerated::ModeratedCreate::ApplyModeration

Defined in:
lib/has_moderated/moderated_create.rb

Class Method Summary collapse

Class Method Details

.apply(klass, value, save_opts = Hash.new, preview_mode = false) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/has_moderated/moderated_create.rb', line 19

def self.apply(klass, value, save_opts = Hash.new, preview_mode = false)
  rec = nil
  if value[:create].present?
    # create the main record
    rec = klass.new
    rec.instance_variable_set(:@has_moderated_preview, true) if preview_mode
    attrs = value[:create][:attributes]
    attrs && attrs.each_pair do |key, val|
      rec.send(key.to_s+"=", val) unless key.to_s == 'id'
    end
    Moderation.without_moderation { rec.save(save_opts) }
    HasModerated::Associations::Base::ApplyModeration::apply(rec, value[:create], save_opts, preview_mode)
  end
  rec
end