Module: ActiveRecord::FinderMethods

Defined in:
lib/simple_cache/active_record.rb

Instance Method Summary collapse

Instance Method Details

#find(*args) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/simple_cache/active_record.rb', line 26

def find(*args)
  return to_a.find { |*block_args| yield(*block_args) } if block_given?

  options = args.extract_options!

  if options.present?
    apply_finder_options(options).find(*args)
  else
    case args.first
    when :first, :last, :all
      send(args.first)
    else
      if args.length==1
        #puts @klass.name
        cache_key = Digest::MD5.hexdigest("short_cache_by_id_#{@klass.name}_#{args[0]}")
        results = Rails.cache.read(cache_key)
        return results if results
        results = find_with_ids(*args)
        Rails.cache.write(cache_key, results, :expires_in => eval("#{@klass.name}::EXPIRE_TIME"))
        return results
      end
      find_with_ids(*args)
    end
  end
end