Skip to content

Instantly share code, notes, and snippets.

@clyfe
Created February 23, 2011 10:39
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save clyfe/840267 to your computer and use it in GitHub Desktop.
# config/initializers/as_i92.rb
# ========================================================================
# resolution for https://github.com/vhochstein/active_scaffold/issues/92 #
# currently monkeypatching the bug, to be deleted on official fix #
# ========================================================================
module ActiveScaffold::Actions
module Update
protected
# A complex method to update a record. The complexity comes from the support for subforms, and saving associated records.
# If you want to customize this algorithm, consider using the +before_update_save+ callback
def do_update
do_edit
update_save
end
def update_save
begin
active_scaffold_config.model.transaction do
@record = update_record_from_params(@record, active_scaffold_config.update.columns, params[:record])
before_update_save(@record)
self.successful = [@record.valid?, @record.associated_valid?].all? {|v| v == true} # this syntax avoids a short-circuit
if successful?
@record.save! and @record.save_associated!
after_update_save(@record)
else
raise ActiveRecord::Rollback, "don't save associations unless record is valid"
end
end
rescue ActiveRecord::RecordInvalid
rescue ActiveRecord::StaleObjectError
@record.errors.add(:base, as_(:version_inconsistency))
self.successful=false
rescue ActiveRecord::RecordNotSaved
@record.errors.add(:base, as_(:record_not_saved)) if @record.errors.empty?
self.successful = false
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment