Class: ActiveRecord::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/api_hammer/active_record_cache_find_by.rb

Instance Method Summary collapse

Instance Method Details

#first(*args) ⇒ Object



7
8
9
# File 'lib/api_hammer/active_record_cache_find_by.rb', line 7

def first(*args)
  one_record_with_caching(args.empty?) { first_without_caching(*args) }
end

#first_without_cachingObject



6
# File 'lib/api_hammer/active_record_cache_find_by.rb', line 6

alias_method :first_without_caching, :first

#one_record_with_caching(can_cache = true) ⇒ Object

retrieves one record, hitting the cache if appropriate. the argument may bypass caching (the caller could elect to just not call this method if caching is to be avoided, but since this method already builds in opting whether or not to hit cache, the code is simpler just passing that in).

requires a block which returns the record



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
# File 'lib/api_hammer/active_record_cache_find_by.rb', line 23

def one_record_with_caching(can_cache = true)
  actual_right = proc do |where_value|
    if where_value.right.is_a?(Arel::Nodes::BindParam)
      column, value = bind_values.detect { |(column, value)| column.name.to_s == where_value.left.name.to_s }
      value
    else
      where_value.right
    end
  end
  cache_find_bys = klass.send(:cache_find_bys)
  can_cache &&= cache_find_bys &&
    !loaded? && # if it's loaded no need to hit cache 
    where_values.all? { |wv| wv.is_a?(Arel::Nodes::Equality) } && # no inequality or that sort of thing 
    cache_find_bys.include?(where_values.map { |wv| wv.left.name.to_s }.sort) && # any of the set of where-values to cache match this relation 
    where_values.map(&actual_right).all? { |r| r.is_a?(String) || r.is_a?(Numeric) } && # check all right side values are simple types, number or string 
    offset_value.nil? &&
    joins_values.blank? &&
    order_values.blank? &&
    !reverse_order_value &&
    includes_values.blank? &&
    preload_values.blank? &&
    select_values.blank? &&
    group_values.blank? &&
    from_value.nil? &&
    lock_value.nil?

  if can_cache
    cache_key = klass.send(:cache_key_for, where_values.map { |wv| [wv.left.name, actual_right.call(wv)] })
    klass.finder_cache.fetch(cache_key) do
      yield
    end
  else
    yield
  end
end

#take(*args) ⇒ Object



13
14
15
# File 'lib/api_hammer/active_record_cache_find_by.rb', line 13

def take(*args)
  one_record_with_caching(args.empty?) { take_without_caching(*args) }
end

#take_without_cachingObject



12
# File 'lib/api_hammer/active_record_cache_find_by.rb', line 12

alias_method :take_without_caching, :take