Class: FriendlyAttributes::Base
- Inherits:
-
Object
- Object
- FriendlyAttributes::Base
- Defined in:
- lib/friendly_attributes/base.rb
Class Method Summary collapse
-
.find_or_build_by_active_record_id(active_record_id, options = {}) ⇒ FriendlyAttributes::Base
Finds an existing Friendly model, associated with an ActiveRecord model, or builds a new Friendly model, using the specified options.
Instance Method Summary collapse
-
#attributes ⇒ Object
Alias for Friendly::Document#to_hash.
-
#read_active_record_id ⇒ Integer
Get the ID of the associated ActiveRecord model.
-
#update_if_changed_with_model(active_record_id) ⇒ true, false
Save the FriendlyAttribute model if it has been changed.
-
#write_active_record_id(active_record_id) ⇒ String, Integer
Set the ID of the associated ActiveRecord model.
Class Method Details
.find_or_build_by_active_record_id(active_record_id, options = {}) ⇒ FriendlyAttributes::Base
Finds an existing Friendly model, associated with an ActiveRecord model, or builds a new Friendly model, using the specified options.
10 11 12 |
# File 'lib/friendly_attributes/base.rb', line 10 def find_or_build_by_active_record_id(active_record_id, ={}) active_record_id && first(active_record_key => active_record_id) || new(.merge(active_record_key => active_record_id)) end |
Instance Method Details
#attributes ⇒ Object
Alias for Friendly::Document#to_hash
45 46 47 |
# File 'lib/friendly_attributes/base.rb', line 45 def attributes to_hash end |
#read_active_record_id ⇒ Integer
Get the ID of the associated ActiveRecord model.
Uses the :active_record_key property of the model.
30 31 32 |
# 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) ⇒ true, false
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.
39 40 41 42 |
# 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) ⇒ String, Integer
Set the ID of the associated ActiveRecord model.
Uses the :active_record_key property of the model.
21 22 23 |
# File 'lib/friendly_attributes/base.rb', line 21 def write_active_record_id(active_record_id) send(:"#{active_record_key}=", active_record_id) end |