class FriendlyAttributes::Base

Public Class Methods

find_or_build_by_active_record_id(active_record_id, options={}) click to toggle source

Finds an existing Friendly model, associated with an ActiveRecord model, or builds a new Friendly model, using the specified options.

@param [Integer] ActiveRecord model ID for which to find the associated Friendly model @param [Hash] options @return [FriendlyAttributes::Base] found or new record

# File lib/friendly_attributes/base.rb, line 10
def find_or_build_by_active_record_id(active_record_id, options={})
  active_record_id && first(active_record_key => active_record_id) || new(options.merge(active_record_key => active_record_id))
end

Public Instance Methods

attributes() click to toggle source

Alias for Friendly::Document#to_hash

# File lib/friendly_attributes/base.rb, line 45
def attributes
  to_hash
end
read_active_record_id() click to toggle source

Get the ID of the associated ActiveRecord model.

Uses the :active_record_key property of the model.

@return [Integer] ActiveRecord ID

# File lib/friendly_attributes/base.rb, line 30
def read_active_record_id
  send(active_record_key)
end
update_if_changed_with_model(active_record_id) click to toggle source

Save the FriendlyAttribute model if it has been changed. Before saving, it sets the specified active_record_id, to handle the case when it is a new record or has been reassigned.

@param [Integer] active_record_id ID of the associated ActiveRecord model @return [true, false] result of saving the record

# File lib/friendly_attributes/base.rb, line 39
def update_if_changed_with_model(active_record_id)
  write_active_record_id(active_record_id) unless read_active_record_id == active_record_id
  save if changed?
end
write_active_record_id(active_record_id) click to toggle source

Set the ID of the associated ActiveRecord model.

Uses the :active_record_key property of the model.

@param [String, Integer] active_record_id value to set the attribute @return [String, Integer] value

# File lib/friendly_attributes/base.rb, line 21
def write_active_record_id(active_record_id)
  send(:"#{active_record_key}=", active_record_id)
end