Module: Qor::Cache::InstanceMethods

Defined in:
lib/qor_cache/active_record.rb

Instance Method Summary collapse

Instance Method Details

#_qor_cache_expireObject



36
37
38
# File 'lib/qor_cache/active_record.rb', line 36

def _qor_cache_expire
  self.class._qor_cache_expire
end

#_qor_cache_sync_cached_fieldsObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/qor_cache/active_record.rb', line 40

def _qor_cache_sync_cached_fields
  nodes = Qor::Cache::Configuration.deep_find(:cache_field) do |node|
    node.parent.is_node?(:scope, self.class.name.demodulize.underscore)
  end

  nodes.map do |node|
    obj = node.options[:from].inject(self) { |obj, value| obj.try(value) }
    self.send("#{node.name}=", obj)
  end
end

#_qor_cache_sync_qor_cache_fieldsObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/qor_cache/active_record.rb', line 51

def _qor_cache_sync_qor_cache_fields
  nodes = Qor::Cache::Configuration.deep_find(:cache_field) do |node|
    node.options[:from][-2].to_sym == self.class.name.demodulize.underscore.to_sym
  end

  nodes.map do |node|
    node_model = node.parent.name.to_s.classify.constantize
    fk = self.class.reflections.select {|name, ref| ref.klass == node_model}.first.last.foreign_key

    node_method = node.options[:from][-1]
    updates = {node.name => try(node_method)}

    node_model.update_all(updates, ["#{fk} = ?", self.id]) if updates.present?
  end
end