Module: FriendlyAttributes::InstanceMethods
- Defined in:
- lib/friendly_attributes/instance_methods.rb
Instance Method Summary collapse
-
#all_friendly_instances ⇒ Array<FriendlyAttributes::Base>
List of all the FriendlyAttributes::Base instances associated with the model.
-
#changed? ⇒ true, false
Returns if the record has been changed and should be saved, taking into account any FriendlyAttributes.
-
#destroy_friendly_models ⇒ true, false
Destroys all FriendlyAttributes associated with the model.
-
#find_or_build_and_memoize_details(friendly_model) ⇒ Object
Finds or builds the Friendly instance associated through friendly_model.
-
#friendly_details_build_options(friendly_model = nil) ⇒ Hash
Hook provided in order to customize the defaults for building Friendly model instances associated with a certain model.
-
#friendly_instance_for_attribute(attr) ⇒ Class
Returns the Friendly instance corresponding to the specified attribute.
-
#friendly_instance_presence(friendly_model) ⇒ FriendlyAttributes::Base?
Returns the associated FriendlyAttributes instance, if it has been loaded.
-
#friendly_instance_present?(friendly_model) ⇒ true, false
Returns true if the FriendlyAttributes specified instance is loaded.
-
#present_friendly_instances ⇒ Array<FriendlyAttributes::Base>
List of FriendlyAttributes::Base instances that have been loaded.
-
#read_friendly_attribute(attr) ⇒ Object
Read the value of a Friendly attribute.
-
#update_friendly_models ⇒ Object
Update all associated Friendly instances, if they have been changed.
-
#write_friendly_attribute(attr, value) ⇒ Object
Write the value of a Friendly attribute.
Instance Method Details
#all_friendly_instances ⇒ Array<FriendlyAttributes::Base>
List of all the FriendlyAttributes::Base instances associated with the model. Forces loading if the details have not been loaded yet.
110 111 112 113 114 |
# File 'lib/friendly_attributes/instance_methods.rb', line 110 def all_friendly_instances friendly_attributes_configuration.friendly_models.map do |friendly_model| send(DetailsDelegator.friendly_model_reader(friendly_model)) end end |
#changed? ⇒ true, false
Returns if the record has been changed and should be saved, taking into account any FriendlyAttributes.
Overloads ActiveRecord::Base#changed?
131 132 133 |
# File 'lib/friendly_attributes/instance_methods.rb', line 131 def changed? super || present_friendly_instances.any?(&:changed?) end |
#destroy_friendly_models ⇒ true, false
Destroys all FriendlyAttributes associated with the model. Forces loading and sends :destroy to all associated Friendly models.
39 40 41 |
# File 'lib/friendly_attributes/instance_methods.rb', line 39 def destroy_friendly_models all_friendly_instances.map(&:destroy).all? end |
#find_or_build_and_memoize_details(friendly_model) ⇒ Object
Finds or builds the Friendly instance associated through friendly_model. Result is memoized in an instance variable.
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/friendly_attributes/instance_methods.rb', line 75 def find_or_build_and_memoize_details(friendly_model) friendly_model_ivar = DetailsDelegator.friendly_model_ivar(friendly_model) val = instance_variable_get(friendly_model_ivar) return val if val.present? instance_variable_set(friendly_model_ivar, friendly_model. find_or_build_by_active_record_id(id, (friendly_model))) end |
#friendly_details_build_options(friendly_model = nil) ⇒ Hash
Hook provided in order to customize the defaults for building Friendly model instances associated with a certain model. Redefine the method on the FriendlyAttributes::Base subclass to customize.
Defaults to an empty Hash.
66 67 68 |
# File 'lib/friendly_attributes/instance_methods.rb', line 66 def (friendly_model = nil) {} end |
#friendly_instance_for_attribute(attr) ⇒ Class
Returns the Friendly instance corresponding to the specified attribute
23 24 25 26 |
# File 'lib/friendly_attributes/instance_methods.rb', line 23 def friendly_instance_for_attribute(attr) klass = friendly_attributes_configuration.model_for_attribute(attr) send DetailsDelegator.friendly_model_reader(klass) end |
#friendly_instance_presence(friendly_model) ⇒ FriendlyAttributes::Base?
Returns the associated FriendlyAttributes instance, if it has been loaded. If not loaded, returns nil.
100 101 102 103 104 |
# File 'lib/friendly_attributes/instance_methods.rb', line 100 def friendly_instance_presence(friendly_model) friendly_instance_present?(friendly_model) ? send(DetailsDelegator.friendly_model_reader(friendly_model)) : nil end |
#friendly_instance_present?(friendly_model) ⇒ true, false
Returns true if the FriendlyAttributes specified instance is loaded.
90 91 92 93 94 |
# File 'lib/friendly_attributes/instance_methods.rb', line 90 def friendly_instance_present?(friendly_model) friendly_model_ivar = DetailsDelegator.friendly_model_ivar(friendly_model) val = instance_variable_get(friendly_model_ivar) val.present? end |
#present_friendly_instances ⇒ Array<FriendlyAttributes::Base>
List of FriendlyAttributes::Base instances that have been loaded. Does not force loading of details not loaded yet.
120 121 122 123 124 |
# File 'lib/friendly_attributes/instance_methods.rb', line 120 def present_friendly_instances friendly_attributes_configuration.friendly_models.map do |friendly_model| friendly_instance_presence(friendly_model) end.compact end |
#read_friendly_attribute(attr) ⇒ Object
Read the value of a Friendly attribute
7 8 9 |
# File 'lib/friendly_attributes/instance_methods.rb', line 7 def read_friendly_attribute(attr) friendly_instance_for_attribute(attr).send(attr) end |
#update_friendly_models ⇒ Object
Update all associated Friendly instances, if they have been changed. If assigning attributes resulted in new instances being built, they will be created.
30 31 32 33 34 |
# File 'lib/friendly_attributes/instance_methods.rb', line 30 def update_friendly_models present_friendly_instances.each do |details| details.update_if_changed_with_model(id) end end |
#write_friendly_attribute(attr, value) ⇒ Object
Write the value of a Friendly attribute
15 16 17 |
# File 'lib/friendly_attributes/instance_methods.rb', line 15 def write_friendly_attribute(attr, value) friendly_instance_for_attribute(attr).send(:"#{attr}=", value) end |