Class: ActiveRecord::Base

Inherits:
Object show all
Defined in:
lib/active_scaffold/extensions/unsaved_associated.rb,
lib/active_scaffold/extensions/to_label.rb

Overview

the ever-useful to_label method

Direct Known Subclasses

ActiveScaffold::Tableless

Instance Method Summary collapse

Instance Method Details

#associated_valid?(path = ::Set.new) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
8
9
10
11
12
# File 'lib/active_scaffold/extensions/unsaved_associated.rb', line 5

def associated_valid?(path = ::Set.new)
  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? }.all?
end

#no_errors_in_associated?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/active_scaffold/extensions/unsaved_associated.rb', line 22

def no_errors_in_associated?
  with_unsaved_associated { |a| a.errors.none? && a.no_errors_in_associated? }.all?
end

#save_associatedObject

rubocop:disable Naming/PredicateMethod



14
15
16
# File 'lib/active_scaffold/extensions/unsaved_associated.rb', line 14

def save_associated # rubocop:disable Naming/PredicateMethod
  with_unsaved_associated { |a| a.save && a.save_associated }.all?
end

#save_associated!Object



18
19
20
# File 'lib/active_scaffold/extensions/unsaved_associated.rb', line 18

def save_associated!
  save_associated || raise(ActiveRecord::RecordNotSaved, "Fail saving associations for #{inspect}")
end

#to_labelObject



5
6
7
8
9
10
# File 'lib/active_scaffold/extensions/to_label.rb', line 5

def to_label
  to_label_method = ActiveScaffold::Registry.cache :to_label, self.class.name do
    i[name label title to_s].find { |attribute| respond_to?(attribute) }
  end
  send(to_label_method).to_s if to_label_method
end