Module: Cash::Finders::ClassMethods

Defined in:
lib/cash/finders.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(active_record_class) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/cash/finders.rb', line 10

def self.extended(active_record_class)
  class << active_record_class
    alias_method_chain :find_every, :cache
    alias_method_chain :find_from_ids, :cache
    alias_method_chain :calculate, :cache
  end
end

Instance Method Details

#_default_orderObject



18
19
20
# File 'lib/cash/finders.rb', line 18

def _default_order
  @default_order ||= { :order => default_scope.first[:find][:order] || 'id ASC' }
end

#_find_scope_with_default_orderObject



22
23
24
25
# File 'lib/cash/finders.rb', line 22

def _find_scope_with_default_order
  find_scope = scope(:find) || {}
  find_scope[:order] ? find_scope : find_scope.merge(_default_order)
end

#calculate_with_cache(operation, column_name, options = {}) ⇒ Object

User.count(:all), User.count, User.sum(…)



48
49
50
# File 'lib/cash/finders.rb', line 48

def calculate_with_cache(operation, column_name, options = {})
  Query::Calculation.perform(self, operation, column_name, options, _find_scope_with_default_order)
end

#find_by_id_with_exclusive_scope(id) ⇒ Object



27
28
29
30
31
# File 'lib/cash/finders.rb', line 27

def find_by_id_with_exclusive_scope(id)
  with_exclusive_scope({ :find => _default_order }) do
    find_from_ids_without_cache([id], {})
  end
end

#find_every_with_cache(options) ⇒ Object

User.find(:first, …), User.find_by_foo(…), User.find(:all, …), User.find_all_by_foo(…)



38
39
40
# File 'lib/cash/finders.rb', line 38

def find_every_with_cache(options)
  Query::Select.perform(self, options, _find_scope_with_default_order)
end

#find_from_ids_with_cache(ids, options) ⇒ Object

User.find(1), User.find(1, 2, 3), User.find([1, 2, 3]), User.find([])



43
44
45
# File 'lib/cash/finders.rb', line 43

def find_from_ids_with_cache(ids, options)
  Query::PrimaryKey.perform(self, ids, options, _find_scope_with_default_order)
end

#without_cache(&block) ⇒ Object



33
34
35
# File 'lib/cash/finders.rb', line 33

def without_cache(&block)
  with_scope(:find => {:readonly => true}, &block)
end