Class: Flattery::ValueProvider::Settings
- Defined in:
- lib/flattery/value_provider/settings.rb
Instance Attribute Summary
Attributes inherited from Settings
#klass, #raw_settings, #resolved, #resolved_settings
Instance Method Summary collapse
-
#cache_attribute_for_association(klass, association_name, association_method) ⇒ Object
Returns the cache_column name given
association_nameandassociation_method. -
#resolve_settings! ⇒ Object
Command: sets resolved_settings.
-
#setting_template ⇒ Object
Returns the basic settings template.
Methods inherited from Settings
#add_setting, #initialize, #parse_option_setting, #reset!, #settings, #unresolved!
Constructor Details
This class inherits a constructor from Flattery::Settings
Instance Method Details
#cache_attribute_for_association(klass, association_name, association_method) ⇒ Object
Returns the cache_column name given association_name and association_method
70 71 72 73 74 |
# File 'lib/flattery/value_provider/settings.rb', line 70 def cache_attribute_for_association(klass,association_name,association_method) if klass.respond_to?(:value_cache_options) klass..settings.detect{|k,v| v[:from_entity] == association_name.to_sym && v[:to_entity] == association_method.to_sym }.first end end |
#resolve_settings! ⇒ Object
Command: sets resolved_settings. Returns true if resolution was success (which will set the resolution status)
Given raw settings: [{ from_entity: :name, to_entity: :notes, as: ‘cat_name’, method: :update_all }] Resolved settings: { ‘name’ => { to_entity: :notes, as: ‘cat_name’, method: :update_all } }
In the ValueProvider context:
-
from_entityis the column that provides the cache value -
to_entityis the association to which the cache value is pushed -
asis the column name onto_entityfrom which the cache value is to be stored
Validations/transformations performed:
-
to_entity is a valid association
If any of these fail, the setting is excluded from the resolved options.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/flattery/value_provider/settings.rb', line 23 def resolve_settings! self.resolved_settings = raw_settings.each_with_object({}) do |setting,memo| from_entity = setting[:from_entity] to_entity = setting[:to_entity] push_method = setting[:method] background_with = setting[:background_with] attribute_name = "#{from_entity}" assoc = klass.reflect_on_association(to_entity) = if assoc && assoc.macro == :has_many cached_attribute_name = if setting[:as].present? setting[:as].to_sym else name = nil other_assoc_name = if assoc.inverse_of assoc.inverse_of.name else end if other_assoc_name name = cache_attribute_for_association(assoc.klass,other_assoc_name,attribute_name) name ||= "#{other_assoc_name}_#{attribute_name}" end name = nil unless name && assoc.klass.column_names.include?(name) name end { to_entity: to_entity, as: cached_attribute_name, method: push_method, background_with: background_with } end if memo[attribute_name] = else memo.delete(attribute_name) end end true end |
#setting_template ⇒ Object
Returns the basic settings template
4 5 6 |
# File 'lib/flattery/value_provider/settings.rb', line 4 def setting_template {method: :update_all} end |