213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
# File 'lib/record_cache.rb', line 213
def record_cache(*args)
extend RecordCache::ClassMethods
include RecordCache::InstanceMethods
opts = args.pop
opts[:fields] = args
opts[:class] = self
field_lookup = opts.delete(:field_lookup) || []
index = RecordCache::Index.new(opts)
add_cached_index(index)
first_index = (cached_indexes.size == 1)
(class << self; self; end).module_eval do
if index.includes_id?
[:first, :all, :set, :raw, :ids].each do |type|
next if type == :ids and index.name == 'by_id'
define_method( index.find_method_name(type) ) do |keys|
if self.send(:scope,:find) and self.send(:scope,:find).any?
self.method_missing(index.find_method_name(type), keys)
else
index.find_by_field(keys, self, type)
end
end
end
end
if not index.auto_name? and not index.full_record?
field = index.fields.first if index.fields.size == 1
define_method( "all_#{index.name.pluralize}_by_#{index.index_field}" ) do |keys|
index.field_lookup(keys, self, field, :all)
end
define_method( "#{index.name.pluralize}_by_#{index.index_field}" ) do |keys|
index.field_lookup(keys, self, field)
end
define_method( "#{index.name.singularize}_by_#{index.index_field}" ) do |keys|
index.field_lookup(keys, self, field, :first)
end
end
if index.auto_name?
(field_lookup + index.fields).each do |field|
next if field == index.index_field
plural_field = field.pluralize
prefix = index.prefix
prefix = "#{prefix}_" if prefix
define_method( "all_#{prefix}#{plural_field}_by_#{index.index_field}" ) do |keys|
index.field_lookup(keys, self, field, :all)
end
define_method( "#{prefix}#{plural_field}_by_#{index.index_field}" ) do |keys|
index.field_lookup(keys, self, field)
end
define_method( "#{prefix}#{field}_by_#{index.index_field}" ) do |keys|
index.field_lookup(keys, self, field, :first)
end
end
end
if first_index
alias_method_chain :find, :caching
alias_method_chain :update_all, :invalidate
alias_method_chain :delete_all, :invalidate
end
end
if first_index
after_save :invalidate_record_cache_deferred
after_destroy :invalidate_record_cache_deferred
after_commit :complete_deferred_record_cache_invalidations
after_rollback :complete_deferred_record_cache_invalidations
end
end
|