Module: Cash::Finders::ClassMethods
- Defined in:
- lib/cash/finders.rb
Class Method Summary collapse
Instance Method Summary collapse
- #_default_order ⇒ Object
- #_find_by_id_with_exclusive_scope(id) ⇒ Object
- #_find_scope_with_default_order ⇒ Object
- #_run_callbacks(result) ⇒ Object
-
#calculate_with_cache(operation, column_name, options = {}) ⇒ Object
User.count(:all), User.count, User.sum(…).
-
#find_every_with_cache(options) ⇒ Object
User.find(:first, …), User.find_by_foo(…), User.find(:all, …), User.find_all_by_foo(…).
-
#find_from_ids_with_cache(ids, options) ⇒ Object
User.find(1), User.find(1, 2, 3), User.find([1, 2, 3]), User.find([]).
- #without_cache(&block) ⇒ Object
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_order ⇒ Object
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_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_scope_with_default_order ⇒ Object
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 |
#_run_callbacks(result) ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/cash/finders.rb', line 34 def _run_callbacks(result) if result.respond_to?(:each) result.each { |r| _run_callbacks(r) } elsif result.is_a?(ActiveRecord::Base) result.send(:callback, :after_initialize) if result.respond_to?(:after_initialize) result.send(:callback, :after_find) if result.respond_to?(:after_find) end nil end |
#calculate_with_cache(operation, column_name, options = {}) ⇒ Object
User.count(:all), User.count, User.sum(…)
63 64 65 |
# File 'lib/cash/finders.rb', line 63 def calculate_with_cache(operation, column_name, = {}) Query::Calculation.perform(self, operation, column_name, , _find_scope_with_default_order) end |
#find_every_with_cache(options) ⇒ Object
User.find(:first, …), User.find_by_foo(…), User.find(:all, …), User.find_all_by_foo(…)
49 50 51 52 53 |
# File 'lib/cash/finders.rb', line 49 def find_every_with_cache() results = Query::Select.perform(self, , _find_scope_with_default_order) _run_callbacks(results) results end |
#find_from_ids_with_cache(ids, options) ⇒ Object
User.find(1), User.find(1, 2, 3), User.find([1, 2, 3]), User.find([])
56 57 58 59 60 |
# File 'lib/cash/finders.rb', line 56 def find_from_ids_with_cache(ids, ) results = Query::PrimaryKey.perform(self, ids, , _find_scope_with_default_order) _run_callbacks(results) results end |
#without_cache(&block) ⇒ Object
44 45 46 |
# File 'lib/cash/finders.rb', line 44 def without_cache(&block) with_scope(:find => {:readonly => true}, &block) end |