Module: Invoicing::CachedRecord::ClassMethods
- Defined in:
- lib/invoicing/cached_record.rb
Instance Method Summary collapse
-
#cached_record_list ⇒ Object
Returns a list of all objects of this class.
-
#find_from_ids(ids, options) ⇒ Object
This method overrides the default
ActiveRecord::Base.find_from_ids
(which is called fromActiveRecord::Base.find
) with caching behaviour. -
#reload_cache ⇒ Object
Reloads the cached objects from the database.
Instance Method Details
#cached_record_list ⇒ Object
Returns a list of all objects of this class. Like ActiveRecord::Base.find(:all)
but coming from the cache.
66 67 68 |
# File 'lib/invoicing/cached_record.rb', line 66 def cached_record_list cached_record_class_info.list end |
#find_from_ids(ids, options) ⇒ Object
This method overrides the default ActiveRecord::Base.find_from_ids
(which is called from ActiveRecord::Base.find
) with caching behaviour. find
is also used by ActiveRecord
when evaluating associations; therefore if another model object refers to a cached record by its ID, calling the getter of that association should result in a cache hit.
FIXME: Currently options
is ignored – we should do something more useful with it to ensure CachedRecord behaviour is fully compatible with ActiveRecord
.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/invoicing/cached_record.rb', line 47 def find_from_ids(ids, ) expects_array = ids.first.kind_of?(Array) return ids.first if expects_array && ids.first.empty? ids = ids.flatten.compact.uniq case ids.size when 0 raise ::ActiveRecord::RecordNotFound, "Couldn't find #{name} without an ID" when 1 result = cached_record_class_info.find_one(ids.first, ) expects_array ? [ result ] : result else cached_record_class_info.find_some(ids, ) end end |
#reload_cache ⇒ Object
Reloads the cached objects from the database.
71 72 73 |
# File 'lib/invoicing/cached_record.rb', line 71 def reload_cache cached_record_class_info.reload_cache end |