app/components/kadmin/form.rb in kadmin-0.7.4 vs app/components/kadmin/form.rb in kadmin-0.7.5
- old
+ new
@@ -33,10 +33,14 @@
# Provides translation scope and helpers (useful for error messages)
# Also includes ActiveModel::Naming at the same time
extend ActiveModel::Translation
+ # Provides callback functionality similar to ActiveRecord
+ extend ActiveModel::Callbacks
+ define_model_callbacks :save
+
# @return [ActiveModel::Model] underlying model to populate
attr_reader :model
delegate :id, :persisted?, :to_key, :to_query, :to_param, :type_for_attribute, to: :model
@@ -168,27 +172,33 @@
# @!group Persistence
def save
saved = false
+ # TODO: Check if the top level transaction is necessary here
+ # run_callbacks is already a transaction?
@model.class.transaction do
- saved = @model.save
- @associated_forms.values.flatten do |form|
- saved &&= form.save
- end
+ run_callbacks :save do
+ saved = @model.save
+ @associated_forms.values.flatten do |form|
+ saved &&= form.save
+ end
- raise ActiveRecord::Rollback unless saved
+ raise ActiveRecord::Rollback unless saved
+ end
end
return saved
end
def save!
saved = false
@model.class.transaction do
- saved = @model.save!
- @associated_forms.values.flatten.each do |form|
- saved &&= form.save! # no need to raise anything, save! will do so
+ run_callbacks :save do
+ saved = @model.save!
+ @associated_forms.values.flatten.each do |form|
+ saved &&= form.save! # no need to raise anything, save! will do so
+ end
end
end
return saved
end