Class: Flattery::ValueProvider::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/flattery/value_provider/processor.rb

Instance Method Summary collapse

Instance Method Details

#after_update(record) ⇒ Object

Command: pushes cache updates for related changed attributes



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/flattery/value_provider/processor.rb', line 4

def after_update(record)
  resolved_options!(record.class).each do |key,options|
    if record.changed.include?(key)
      if target_attribute = options[:as]
        method = options[:method]
        attribute = key.to_sym
        new_value = record.send(key)
        association_name = options[:to_entity]
        if options[:background_with] == :delayed_job && self.respond_to?(:delay)
          self.delay.apply_push(record,method,attribute,new_value,association_name,target_attribute)
        else
          apply_push(record,method,attribute,new_value,association_name,target_attribute)
        end
      else
        raise Flattery::CacheColumnInflectionError.new("#{record.class.name} #{key}: #{options}")
      end
    end
  end
  true
end

#apply_push(record, method, attribute, new_value, association_name, target_attribute) ⇒ Object

Command: performs an update for a specific cache setting



26
27
28
29
30
31
32
33
# File 'lib/flattery/value_provider/processor.rb', line 26

def apply_push(record,method,attribute,new_value,association_name,target_attribute)
  case method
  when :update_all
    record.send(association_name).update_all({target_attribute => new_value})
  else # it is a custom update method
    record.send(method,attribute,new_value,association_name,target_attribute)
  end
end

#resolved_options!(klass) ⇒ Object

Command: resolves value provider options for klass if required, and returns resolved options



36
37
38
# File 'lib/flattery/value_provider/processor.rb', line 36

def resolved_options!(klass)
  klass.value_provider_options.settings
end