class Sequel::Model

the ever-useful to_label method

save and validation support for associations.

a simple (manual) unsaved? flag and method. at least it automatically reverts after a save!

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
  with_unsaved_associated { |a| [a.valid?, a.associated_valid?(path)].all? {|v| v == true} }
end
no_errors_in_associated?() click to toggle source
# File lib/active_scaffold/extensions/unsaved_associated.rb, line 18
def no_errors_in_associated?
  with_unsaved_associated {|a| a.errors.count == 0 and a.no_errors_in_associated?}
end
save_associated() click to toggle source
# File lib/active_scaffold/extensions/unsaved_associated.rb, line 10
def save_associated
  with_unsaved_associated { |a| a.save and a.save_associated }
end
save_associated!() click to toggle source
# File lib/active_scaffold/extensions/unsaved_associated.rb, line 14
def save_associated!
  save_associated or raise(Sequel::Error)
end
save_with_unsaved_flag(*args) click to toggle source

automatically unsets the unsaved flag

# File lib/active_scaffold/extensions/unsaved_record.rb, line 14
def save_with_unsaved_flag(*args)
  result = save_without_unsaved_flag(*args)
  self.unsaved = false
  return result
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
unsaved=(val) click to toggle source

acts like a dirty? flag, manually thrown during update_record_from_params.

# File lib/active_scaffold/extensions/unsaved_record.rb, line 4
def unsaved=(val)
  @unsaved = (val) ? true : false
end
unsaved?() click to toggle source

whether the unsaved? flag has been thrown

# File lib/active_scaffold/extensions/unsaved_record.rb, line 9
def unsaved?
  @unsaved
end

Protected Instance Methods

associations_for_update() click to toggle source

Provide an override to allow the model to restrict which associations are considered by ActiveScaffolds update mechanism. This allows the model to restrict things like Acts-As-Versioned versions associations being traversed.

By defining the method :scaffold_update_nofollow returning an array of associations these associations will not be traversed. By defining the method :scaffold_update_follow returning an array of associations, only those associations will be traversed.

Otherwise the default behaviour of traversing all associations will be preserved.

# File lib/active_scaffold/extensions/unsaved_associated.rb, line 34
def associations_for_update
  if self.respond_to?( :scaffold_update_nofollow )
    self.class.associations.reject {|association| self.scaffold_update_nofollow.include?(association)}
  elsif self.respond_to?( :scaffold_update_follow )
    self.class.associations.select {|association| self.scaffold_update_follow.include?(association)}
  else
    self.class.associations
  end
end

Private Instance Methods

with_unsaved_associated() { |r| ... } click to toggle source

yields every associated object that has been instantiated and is flagged as unsaved. returns false if any yield returns false. returns true otherwise, even when none of the associations have been instantiated. build wrapper methods accordingly.

# File lib/active_scaffold/extensions/unsaved_associated.rb, line 49
def with_unsaved_associated
  associations_for_update.all? do |association|
    records = send(association)
    if records
      records = [records] unless records.is_a? Array # convert singular associations into collections for ease of use
      records.select {|r| r.unsaved?}.all? {|r| yield r} # must use select instead of find_all, which Rails overrides on association proxies for db access
    else
      true
    end
  end
end