Sha256: be3dec5f9010699da991ad85866db200ca914b3f8b0cd05f5d699912572baa0b

Contents?: true

Size: 1.02 KB

Versions: 7

Compression:

Stored size: 1.02 KB

Contents

module AridCache
  class CacheProxy
    module Utilities
      extend self

      # Generate an ORDER BY clause that preserves the ordering of the ids in *ids*.
      #
      # The method we use depends on the database adapter because only MySQL
      # supports the ORDER BY FIELD() function.  For other databases we use
      # a CASE statement.
      def order_by(ids, klass=nil)
        column = if klass.respond_to?(:table_name)
          ::ActiveRecord::Base.connection.quote_table_name(klass.table_name) + '.id'
        else
          "id"
        end

        if ids.empty?
          nil
        elsif ::ActiveRecord::Base.is_mysql_adapter?
          "FIELD(#{column},#{ids.join(',')})"
        else
          order = ''
          ids.each_index { |i| order << "WHEN #{column}=#{ids[i]} THEN #{i+1} " }
          "CASE " + order + " END"
        end
      end

      # Return the object's class or the object if it is a class.
      def object_class(object)
        object.is_a?(Class) ? object : object.class
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
arid_cache-1.3.6 lib/arid_cache/cache_proxy/utilities.rb
arid_cache-1.3.5 lib/arid_cache/cache_proxy/utilities.rb
arid_cache-1.3.4 lib/arid_cache/cache_proxy/utilities.rb
arid_cache-1.3.3 lib/arid_cache/cache_proxy/utilities.rb
arid_cache-1.3.2 lib/arid_cache/cache_proxy/utilities.rb
arid_cache-1.3.1 lib/arid_cache/cache_proxy/utilities.rb
arid_cache-1.3.0 lib/arid_cache/cache_proxy/utilities.rb