Class: FriendlyAttributes::DetailsDelegator
- Inherits:
-
Object
- Object
- FriendlyAttributes::DetailsDelegator
- Defined in:
- lib/friendly_attributes/details_delegator.rb
Instance Attribute Summary collapse
- #active_record_model ⇒ Object readonly
- #attributes ⇒ Object readonly
- #delegated_attributes ⇒ Object readonly
- #friendly_model ⇒ Object readonly
Class Method Summary collapse
-
.friendly_model_ivar(name_or_klass) ⇒ Symbol
Instance variable name for the FriendlyAttributes::Base or ‘friendly_model_name` passed to it.
-
.friendly_model_name(klass) ⇒ Symbol
Method name for the FriendlyAttributes::Base class passed to it.
-
.friendly_model_reader(name_or_klass) ⇒ Symbol
Reader method name for a certain FriendlyAttributes::Base instance associated with the model.
Instance Method Summary collapse
- #delegated_attribute(name, klass) ⇒ Object
- #delegated_method(name) ⇒ Object
-
#initialize(friendly_model, ar_model, attributes, options = {}) ⇒ DetailsDelegator
constructor
Initialize new DetailsDelegator instance.
- #setup_delegated_attributes ⇒ Object
Constructor Details
#initialize(friendly_model, ar_model, attributes, options = {}) ⇒ DetailsDelegator
Initialize new DetailsDelegator instance.
45 46 47 48 49 50 51 52 53 |
# File 'lib/friendly_attributes/details_delegator.rb', line 45 def initialize(friendly_model, ar_model, attributes, ={}) @active_record_model = ar_model @friendly_model = friendly_model @attributes = attributes @delegated_attributes = {} setup_friendly_model(.delete(:active_record_key) || :active_record_id) setup_active_record_model end |
Instance Attribute Details
#active_record_model ⇒ Object (readonly)
6 7 8 |
# File 'lib/friendly_attributes/details_delegator.rb', line 6 def active_record_model @active_record_model end |
#attributes ⇒ Object (readonly)
6 7 8 |
# File 'lib/friendly_attributes/details_delegator.rb', line 6 def attributes @attributes end |
#delegated_attributes ⇒ Object (readonly)
6 7 8 |
# File 'lib/friendly_attributes/details_delegator.rb', line 6 def delegated_attributes @delegated_attributes end |
#friendly_model ⇒ Object (readonly)
6 7 8 |
# File 'lib/friendly_attributes/details_delegator.rb', line 6 def friendly_model @friendly_model end |
Class Method Details
.friendly_model_ivar(name_or_klass) ⇒ Symbol
Instance variable name for the FriendlyAttributes::Base or ‘friendly_model_name` passed to it.
23 24 25 26 |
# File 'lib/friendly_attributes/details_delegator.rb', line 23 def friendly_model_ivar(name_or_klass) name_or_klass = friendly_model_name(name_or_klass) if name_or_klass.is_a?(Class) :"@#{name_or_klass}_ivar" end |
.friendly_model_name(klass) ⇒ Symbol
Method name for the FriendlyAttributes::Base class passed to it.
15 16 17 |
# File 'lib/friendly_attributes/details_delegator.rb', line 15 def friendly_model_name(klass) klass.name.underscore.to_sym end |
.friendly_model_reader(name_or_klass) ⇒ Symbol
Reader method name for a certain FriendlyAttributes::Base instance associated with the model.
32 33 34 35 |
# File 'lib/friendly_attributes/details_delegator.rb', line 32 def friendly_model_reader(name_or_klass) name_or_klass = friendly_model_name(name_or_klass) if name_or_klass.is_a?(Class) :"load_#{name_or_klass}" end |
Instance Method Details
#delegated_attribute(name, klass) ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/friendly_attributes/details_delegator.rb', line 55 def delegated_attribute(name, klass) delegated_attributes[name] = klass attribute name, klass delegated_method(:"#{name}") delegated_method(:"#{name}=") active_record_model.friendly_attributes_configuration.add_attribute(name, friendly_model) end |
#delegated_method(name) ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/friendly_attributes/details_delegator.rb', line 65 def delegated_method(name) friendly_model = self.friendly_model active_record_model.class_eval do delegate :"#{name}", :to => FriendlyAttributes::DetailsDelegator.friendly_model_reader(friendly_model) end end |
#setup_delegated_attributes ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/friendly_attributes/details_delegator.rb', line 73 def setup_delegated_attributes attributes.each do |key, value| if Array === value value.each { |v| delegated_attribute v, key } else delegated_attribute value, key end end end |