Module: ActiveRecordCache::RelationExtension

Extended by:
ActiveSupport::Concern
Defined in:
lib/active_record_cache/relation_extension.rb

Constant Summary collapse

SIMPLE_ORDER_BY =
/^([a-z][a-z0-9_]*)( ?((asc|desc)(ending)?)?)$/i

Instance Method Summary collapse

Instance Method Details

#from_cacheObject

Calling from_database on a Relation will force the query to bypass the cache.



21
22
23
24
25
# File 'lib/active_record_cache/relation_extension.rb', line 21

def from_cache
  relation = clone
  relation.query_from_cache_value = true
  relation
end

#from_databaseObject

Calling from_database on a Relation will force the query to bypass the cache.



14
15
16
17
18
# File 'lib/active_record_cache/relation_extension.rb', line 14

def from_database
  relation = clone
  relation.query_from_cache_value = false
  relation
end

#merge_with_record_cache(relation) ⇒ Object

Override the merge function so that the query_from_cache_value gets merged.



46
47
48
49
50
# File 'lib/active_record_cache/relation_extension.rb', line 46

def merge_with_record_cache(relation) #:nodoc:
  merged_relation = merge_without_record_cache(relation)
  merged_relation.query_from_cache_value = relation.query_from_cache_value
  merged_relation
end

#to_a_with_record_cacheObject

Override the to_a method to look in the cache.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/active_record_cache/relation_extension.rb', line 28

def to_a_with_record_cache #:nodoc:
  if !loaded? && cacheable_query?
    ids = get_where_ids
    unless ids.blank?
      records = klass.record_cache.read(ids)
      records = records[0, limit_value.to_i] if limit_value
      records = records.sort_by_field(order_values.first) unless order_values.empty?
      if logger && logger.debug?
        logger.debug("  #{klass.name} LOAD FROM RECORD CACHE #{to_sql}")
      end
      @records = records
      @loaded = true
    end
  end
  to_a_without_record_cache
end