class ActiveRecord::Relation

Public Instance Methods

take_with_cache_enumeration() click to toggle source
# File lib/cached_enumeration/cached_enumeration.rb, line 213
def take_with_cache_enumeration
  if cache_enumeration? && cache_enumeration.cached?
    case
      #when just_modified?(:limit)
      #  cache_enumeration.first #tsk the first value of the default order
      when just_modified?(:where) && where_is_cached?
        get_by_where
      else
        take_without_cache_enumeration
    end
  else
    cache_enumeration.cache! if cache_enumeration?
    take_without_cache_enumeration
  end
end
to_a_with_cache_enumeration() click to toggle source
# File lib/cached_enumeration/cached_enumeration.rb, line 176
def to_a_with_cache_enumeration
  res=nil

  if cache_enumeration? && cache_enumeration.cached? && order_is_cached?
    res=case
      when just_modified?(:order)
        #all and the order is cached?
        cache_enumeration.all
      when just_modified?(:limit, :order, :where)
        case
          when limit_value == 1 && where_values.blank?
            # usually the #first case
            [cache_enumeration.first]
          when limit_value == 1 && where_values.present? && where_is_cached?
            # usually "= 1" or "= ?" .first or find or find_by
            [get_by_where]
          when limit_value.blank? && where_values.present? && where_is_cached?
            # usually the association case (where id in (1,2,56,6))
            get_by_where
          else
            to_a_without_cache_enumeration #where is to complicated for us
        end
    end

  end

  if res #got a result the return it
    res
  else
    cache_enumeration.cache! if cache_enumeration?
    to_a_without_cache_enumeration
  end

end

Private Instance Methods

get_by_where() click to toggle source
# File lib/cached_enumeration/cached_enumeration.rb, line 233
def get_by_where
  att_name=where_values[0].left.name
  identifier = where_values[0].right

  if identifier.kind_of?(Array)
    identifier.map do |id|
      cache_enumeration.get_by(att_name, id)
    end.compact
  else
    identifier=bind_values[0][1] if identifier=='?' || identifier=='$1'
    cache_enumeration.get_by(att_name, identifier)
  end
end
just_modified?(*modified) click to toggle source

*modified is an array like :limit, :where, :order, :select, :includes, :preload :readonly

# File lib/cached_enumeration/cached_enumeration.rb, line 267
def just_modified?(*modified)
  return false if @klass.locking_enabled?
  return false if limit_value.present? && !modified.include?(:limit)
  return false if where_values.present? && !modified.include?(:where)
  return false if order_values.present? && !modified.include?(:order)
  return false if select_values.present? && !modified.include?(:select)
  return false if includes_values.present? && !modified.include?(:includes)
  return false if preload_values.present? && !modified.include?(:preload)
  return false if readonly_value.present? && !modified.include?(:readonly)
  return false if joins_values.present? && !modified.include?(:joins)
  true
end
order_is_cached?() click to toggle source

just one ascending order which is the same as the cached one or none

# File lib/cached_enumeration/cached_enumeration.rb, line 248
def order_is_cached?
  order_values.empty? || (
  order_values.size == 1 &&
    ((order_values[0].respond_to?(:ascending?) && order_values[0].ascending? &&
      cache_enumeration.order == order_values[0].expr.name) ||
      order_values[0] == cache_enumeration.order #sometimes the order is just as string
    )
  )
end
where_is_cached?() click to toggle source
# File lib/cached_enumeration/cached_enumeration.rb, line 258
def where_is_cached?
  where_values.size == 1 &&
    where_values[0].kind_of?(Arel::Nodes::Node) &&
    where_values[0].operator == :== &&
    cache_enumeration.hashed_by?(where_values[0].left.name)
end