2011-04-11 12:04:31 -04:00
|
|
|
require 'ransack/visitor'
|
|
|
|
|
2011-03-30 20:31:39 -04:00
|
|
|
module Ransack
|
|
|
|
class Context
|
|
|
|
attr_reader :search, :object, :klass, :base, :engine, :arel_visitor
|
2012-04-11 11:58:27 -04:00
|
|
|
attr_accessor :auth_object, :search_key
|
2011-03-30 20:31:39 -04:00
|
|
|
|
|
|
|
class << self
|
|
|
|
|
2011-09-03 15:37:02 -04:00
|
|
|
def for(object, options = {})
|
2013-12-10 13:06:07 -05:00
|
|
|
context = Class === object ?
|
|
|
|
for_class(object, options) :
|
|
|
|
for_object(object, options)
|
|
|
|
context or raise ArgumentError,
|
|
|
|
"Don't know what context to use for #{object}"
|
2011-03-30 20:31:39 -04:00
|
|
|
end
|
|
|
|
|
2011-09-03 15:37:02 -04:00
|
|
|
def for_class(klass, options = {})
|
2011-03-30 20:31:39 -04:00
|
|
|
if klass < ActiveRecord::Base
|
2011-09-03 15:37:02 -04:00
|
|
|
Adapters::ActiveRecord::Context.new(klass, options)
|
2011-03-30 20:31:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-03 15:37:02 -04:00
|
|
|
def for_object(object, options = {})
|
2011-03-30 20:31:39 -04:00
|
|
|
case object
|
|
|
|
when ActiveRecord::Relation
|
2011-09-03 15:37:02 -04:00
|
|
|
Adapters::ActiveRecord::Context.new(object.klass, options)
|
2011-03-30 20:31:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2011-09-03 15:37:02 -04:00
|
|
|
def initialize(object, options = {})
|
2013-05-23 05:59:32 -04:00
|
|
|
@object = relation_for(object)
|
2011-03-30 20:31:39 -04:00
|
|
|
@klass = @object.klass
|
|
|
|
@join_dependency = join_dependency(@object)
|
2011-09-03 15:37:02 -04:00
|
|
|
@join_type = options[:join_type] || Arel::OuterJoin
|
2012-03-07 14:31:13 -05:00
|
|
|
@search_key = options[:search_key] || Ransack.options[:search_key]
|
2014-04-09 13:25:54 -04:00
|
|
|
|
|
|
|
if ::ActiveRecord::VERSION::STRING >= "4.1"
|
|
|
|
@base = @join_dependency.join_root
|
|
|
|
@engine = @base.base_klass.arel_engine
|
|
|
|
else
|
|
|
|
@base = @join_dependency.join_base
|
|
|
|
@engine = @base.arel_engine
|
|
|
|
end
|
|
|
|
|
2013-12-10 13:06:07 -05:00
|
|
|
@default_table = Arel::Table.new(
|
2014-05-01 09:55:39 -04:00
|
|
|
@base.table_name, :as => @base.aliased_table_name, :engine => @engine
|
2013-12-10 13:06:07 -05:00
|
|
|
)
|
2011-04-09 20:55:28 -04:00
|
|
|
@bind_pairs = Hash.new do |hash, key|
|
|
|
|
parent, attr_name = get_parent_and_attribute_name(key.to_s)
|
|
|
|
if parent && attr_name
|
|
|
|
hash[key] = [parent, attr_name]
|
2011-03-30 20:31:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-23 07:33:00 -04:00
|
|
|
def klassify(obj)
|
|
|
|
if Class === obj && ::ActiveRecord::Base > obj
|
|
|
|
obj
|
|
|
|
elsif obj.respond_to? :klass
|
|
|
|
obj.klass
|
2013-08-06 13:00:52 -04:00
|
|
|
elsif obj.respond_to? :active_record # Rails 3
|
|
|
|
obj.active_record
|
|
|
|
elsif obj.respond_to? :base_klass # Rails 4
|
2013-05-23 07:33:00 -04:00
|
|
|
obj.base_klass
|
|
|
|
else
|
2013-12-26 17:52:34 -05:00
|
|
|
raise ArgumentError, "Don't know how to klassify #{obj.inspect}"
|
2013-05-23 07:33:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-03-30 20:31:39 -04:00
|
|
|
# Convert a string representing a chain of associations and an attribute
|
|
|
|
# into the attribute itself
|
|
|
|
def contextualize(str)
|
2011-04-09 20:55:28 -04:00
|
|
|
parent, attr_name = @bind_pairs[str]
|
|
|
|
table_for(parent)[attr_name]
|
|
|
|
end
|
|
|
|
|
|
|
|
def bind(object, str)
|
|
|
|
object.parent, object.attr_name = @bind_pairs[str]
|
2011-03-30 20:31:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def traverse(str, base = @base)
|
|
|
|
str ||= ''
|
|
|
|
|
|
|
|
if (segments = str.split(/_/)).size > 0
|
2011-10-14 11:33:19 -04:00
|
|
|
remainder = []
|
2011-03-30 20:31:39 -04:00
|
|
|
found_assoc = nil
|
2011-10-14 11:33:19 -04:00
|
|
|
while !found_assoc && segments.size > 0 do
|
2011-05-30 13:00:59 -04:00
|
|
|
# Strip the _of_Model_type text from the association name, but hold
|
|
|
|
# onto it in klass, for use as the next base
|
2011-10-14 11:33:19 -04:00
|
|
|
assoc, klass = unpolymorphize_association(segments.join('_'))
|
2011-04-08 11:33:10 -04:00
|
|
|
if found_assoc = get_association(assoc, base)
|
2011-10-14 11:33:19 -04:00
|
|
|
base = traverse(remainder.join('_'), klass || found_assoc.klass)
|
2011-03-30 20:31:39 -04:00
|
|
|
end
|
2011-10-14 11:33:19 -04:00
|
|
|
|
|
|
|
remainder.unshift segments.pop
|
2011-03-30 20:31:39 -04:00
|
|
|
end
|
2013-12-10 13:06:07 -05:00
|
|
|
raise UntraversableAssociationError,
|
|
|
|
"No association matches #{str}" unless found_assoc
|
2011-03-30 20:31:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
klassify(base)
|
|
|
|
end
|
|
|
|
|
|
|
|
def association_path(str, base = @base)
|
|
|
|
base = klassify(base)
|
|
|
|
str ||= ''
|
|
|
|
path = []
|
|
|
|
segments = str.split(/_/)
|
|
|
|
association_parts = []
|
|
|
|
if (segments = str.split(/_/)).size > 0
|
2013-12-10 13:06:07 -05:00
|
|
|
while segments.size > 0 && !base.columns_hash[segments.join('_')] &&
|
|
|
|
association_parts << segments.shift do
|
|
|
|
assoc, klass = unpolymorphize_association(association_parts
|
|
|
|
.join('_'))
|
2011-04-08 11:33:10 -04:00
|
|
|
if found_assoc = get_association(assoc, base)
|
2011-03-30 20:31:39 -04:00
|
|
|
path += association_parts
|
|
|
|
association_parts = []
|
2011-04-08 11:33:10 -04:00
|
|
|
base = klassify(klass || found_assoc)
|
2011-03-30 20:31:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
path.join('_')
|
|
|
|
end
|
|
|
|
|
2011-04-08 11:33:10 -04:00
|
|
|
def unpolymorphize_association(str)
|
2011-08-04 17:10:33 -04:00
|
|
|
if (match = str.match(/_of_([^_]+?)_type$/))
|
2011-04-08 11:33:10 -04:00
|
|
|
[match.pre_match, Kernel.const_get(match.captures.first)]
|
|
|
|
else
|
|
|
|
[str, nil]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-30 13:00:59 -04:00
|
|
|
def ransackable_attribute?(str, klass)
|
2013-12-06 19:51:55 -05:00
|
|
|
klass.ransackable_attributes(auth_object).include?(str) ||
|
2013-12-10 13:06:07 -05:00
|
|
|
klass.ransortable_attributes(auth_object).include?(str)
|
2011-05-30 13:00:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def ransackable_association?(str, klass)
|
|
|
|
klass.ransackable_associations(auth_object).include? str
|
|
|
|
end
|
|
|
|
|
2011-04-11 20:07:23 -04:00
|
|
|
def searchable_attributes(str = '')
|
|
|
|
traverse(str).ransackable_attributes(auth_object)
|
2011-03-30 20:31:39 -04:00
|
|
|
end
|
|
|
|
|
2013-12-10 13:06:07 -05:00
|
|
|
def sortable_attributes(str = '')
|
|
|
|
traverse(str).ransortable_attributes(auth_object)
|
2011-05-30 13:00:59 -04:00
|
|
|
end
|
2013-02-19 22:45:02 -05:00
|
|
|
|
2011-05-30 13:00:59 -04:00
|
|
|
def searchable_associations(str = '')
|
|
|
|
traverse(str).ransackable_associations(auth_object)
|
|
|
|
end
|
2011-03-30 20:31:39 -04:00
|
|
|
end
|
2013-05-23 05:59:32 -04:00
|
|
|
end
|