class YAAF::Form

Parent class for form objects

Attributes

models[RW]

Public Instance Methods

save(options = {}) click to toggle source
# File lib/yaaf/form.rb, line 15
def save(options = {})
  save_form(options)
rescue ActiveRecord::RecordInvalid,
       ActiveRecord::RecordNotSaved,
       ActiveModel::ValidationError

  false
end
save!(options = {}) click to toggle source
# File lib/yaaf/form.rb, line 24
def save!(options = {})
  save_form(options)
end

Private Instance Methods

handle_transaction_rollback(exception) click to toggle source
# File lib/yaaf/form.rb, line 66
def handle_transaction_rollback(exception)
  run_callbacks :rollback
  raise exception
end
promote_errors(model) click to toggle source
# File lib/yaaf/form.rb, line 62
def promote_errors(model)
  errors.merge!(model.errors)
end
save_form(options) click to toggle source
# File lib/yaaf/form.rb, line 32
def save_form(options)
  validate! unless options[:validate] == false

  run_callbacks :commit do
    save_in_transaction(options)
  end

  true
end
save_in_transaction(options) click to toggle source
# File lib/yaaf/form.rb, line 42
def save_in_transaction(options)
  transaction do
    run_callbacks :save do
      save_models(options)
    end
  end
rescue Exception => e
  handle_transaction_rollback(e)
end
save_models(options) click to toggle source
# File lib/yaaf/form.rb, line 52
def save_models(options)
  options.merge!(validate: false)

  models.map { |model| model.save!(options) }
end
validate_models() click to toggle source
# File lib/yaaf/form.rb, line 58
def validate_models
  models.each { |model| promote_errors(model) if model.invalid? }
end