Module: Invoicing::CachedRecord::ClassMethods

Defined in:
lib/invoicing/cached_record.rb

Instance Method Summary collapse

Instance Method Details

#cached_record_listObject

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, options)
  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, options)
      expects_array ? [ result ] : result
    else
      cached_record_class_info.find_some(ids, options)
  end
end

#reload_cacheObject

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