Sha256: 614ea280c4268e6645b41ed8ebe751651a4c37537e47c917280c6351385aa528

Contents?: true

Size: 1.46 KB

Versions: 5

Compression:

Stored size: 1.46 KB

Contents

require 'ransack/visitor'

module Ransack
  class Context
    # attr_reader :arel_visitor

    class << self

      def for_class(klass, options = {})
        if klass.ancestors.include?(::Mongoid::Document)
          Adapters::Mongoid::Context.new(klass, options)
        end
      end

      def for_object(object, options = {})
        case object
        when ActiveRecord::Relation
          Adapters::ActiveRecord::Context.new(object.klass, options)
        end
      end

    end # << self

    def initialize(object, options = {})
      @object = relation_for(object)
      @klass = @object.klass
      # @join_dependency = join_dependency(@object)
      # @join_type = options[:join_type] || Arel::OuterJoin
      @search_key = options[:search_key] || Ransack.options[:search_key]

      @base = @object.klass
      # @engine = @base.arel_engine
    end

    def bind_pair_for(key)
      @bind_pairs ||= {}

      @bind_pairs[key] ||= begin
        parent, attr_name = get_parent_and_attribute_name(key.to_s)
        [parent, attr_name] if parent && attr_name
      end
    end

    def klassify(obj)
      if Class === obj && ::ActiveRecord::Base > obj
        obj
      elsif obj.respond_to? :klass
        obj.klass
      elsif obj.respond_to? :active_record  # Rails 3
        obj.active_record
      elsif obj.respond_to? :base_klass     # Rails 4
        obj.base_klass
      else
        raise ArgumentError, "Don't know how to klassify #{obj.inspect}"
      end
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ransack-1.8.10 lib/ransack/adapters/mongoid/ransack/context.rb
ransack-1.8.9 lib/ransack/adapters/mongoid/ransack/context.rb
ransack-1.8.8 lib/ransack/adapters/mongoid/ransack/context.rb
ransack-1.8.7 lib/ransack/adapters/mongoid/ransack/context.rb
ransack-1.8.6 lib/ransack/adapters/mongoid/ransack/context.rb