class ActiveRecord::Base
the ever-useful #to_label method
save and validation support for associations.
Public Instance Methods
associated_valid?(path = [])
click to toggle source
# File lib/active_scaffold/extensions/unsaved_associated.rb, line 3 def associated_valid?(path = []) return true if path.include?(self) # prevent recursion (if associated and parent are new records) path << self # using [].all? syntax to avoid a short-circuit # errors to associated record can be added by update_record_from_params when association fails to set and ActiveRecord::RecordNotSaved is raised with_unsaved_associated { |a| [a.keeping_errors { a.valid? }, a.associated_valid?(path)].all? } end
keeping_errors() { || ... }
click to toggle source
# File lib/active_scaffold/extensions/unsaved_associated.rb, line 23 def keeping_errors old_errors = errors.dup if errors.present? yield.tap do (old_errors || []).each do |attr| old_errors[attr].each { |msg| errors.add(attr, msg) unless errors.added?(attr, msg) } end
no_errors_in_associated?()
click to toggle source
# File lib/active_scaffold/extensions/unsaved_associated.rb, line 19 def no_errors_in_associated? with_unsaved_associated { |a| a.errors.count == 0 && a.no_errors_in_associated? } end
save_associated()
click to toggle source
# File lib/active_scaffold/extensions/unsaved_associated.rb, line 11 def save_associated with_unsaved_associated { |a| a.save && a.save_associated } end
save_associated!()
click to toggle source
# File lib/active_scaffold/extensions/unsaved_associated.rb, line 15 def save_associated! save_associated || raise(ActiveRecord::RecordNotSaved) end
to_label()
click to toggle source
# File lib/active_scaffold/extensions/to_label.rb, line 3 def to_label [:name, :label, :title, :to_s].each do |attribute| return send(attribute).to_s if respond_to?(attribute) end end