2011-04-11 12:04:31 -04:00
|
|
|
require 'ransack/visitor'
|
2016-04-05 05:09:21 -04:00
|
|
|
Ransack::Adapters.object_mapper.require_context
|
2011-04-11 12:04:31 -04:00
|
|
|
|
2011-03-30 20:31:39 -04:00
|
|
|
module Ransack
|
|
|
|
class Context
|
2015-01-10 10:33:30 -05:00
|
|
|
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
|
|
|
|
|
2014-08-01 02:36:12 -04:00
|
|
|
def for_class(klass, options = {})
|
|
|
|
raise "not implemented"
|
|
|
|
end
|
|
|
|
|
|
|
|
def for_object(object, options = {})
|
|
|
|
raise "not implemented"
|
|
|
|
end
|
|
|
|
|
2011-09-03 15:37:02 -04:00
|
|
|
def for(object, options = {})
|
2015-08-29 16:52:57 -04:00
|
|
|
context =
|
|
|
|
if Class === object
|
|
|
|
for_class(object, options)
|
|
|
|
else
|
|
|
|
for_object(object, options)
|
|
|
|
end
|
2013-12-10 13:06:07 -05:00
|
|
|
context or raise ArgumentError,
|
|
|
|
"Don't know what context to use for #{object}"
|
2011-03-30 20:31:39 -04:00
|
|
|
end
|
|
|
|
|
2014-08-01 02:36:12 -04:00
|
|
|
end # << self
|
2011-03-30 20:31:39 -04:00
|
|
|
|
2011-09-03 15:37:02 -04:00
|
|
|
def initialize(object, options = {})
|
2014-08-01 02:36:12 -04:00
|
|
|
raise "not implemented"
|
2011-03-30 20:31:39 -04:00
|
|
|
end
|
|
|
|
|
2013-05-23 07:33:00 -04:00
|
|
|
def klassify(obj)
|
2014-08-01 02:36:12 -04:00
|
|
|
raise "not implemented"
|
2013-05-23 07:33:00 -04:00
|
|
|
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)
|
2017-08-02 16:32:11 -04:00
|
|
|
parent, attr_name = bind_pair_for(str)
|
2011-04-09 20:55:28 -04:00
|
|
|
table_for(parent)[attr_name]
|
|
|
|
end
|
|
|
|
|
2014-06-22 05:15:10 -04:00
|
|
|
def chain_scope(scope, args)
|
|
|
|
return unless @klass.method(scope) && args != false
|
|
|
|
@object = if scope_arity(scope) < 1 && args == true
|
|
|
|
@object.public_send(scope)
|
|
|
|
else
|
|
|
|
@object.public_send(scope, *args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def scope_arity(scope)
|
|
|
|
@klass.method(scope).arity
|
|
|
|
end
|
|
|
|
|
2011-04-09 20:55:28 -04:00
|
|
|
def bind(object, str)
|
2016-01-02 13:27:52 -05:00
|
|
|
return nil unless str
|
2017-08-02 16:32:11 -04:00
|
|
|
object.parent, object.attr_name = bind_pair_for(str)
|
2011-03-30 20:31:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def traverse(str, base = @base)
|
2015-08-29 16:52:57 -04:00
|
|
|
str ||= ''.freeze
|
2018-11-12 06:20:30 -05:00
|
|
|
segments = str.split(Constants::UNDERSCORE)
|
|
|
|
unless segments.empty?
|
2011-10-14 11:33:19 -04:00
|
|
|
remainder = []
|
2011-03-30 20:31:39 -04:00
|
|
|
found_assoc = nil
|
2018-11-12 06:20:30 -05:00
|
|
|
until found_assoc || segments.empty?
|
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
|
2014-11-21 14:03:20 -05:00
|
|
|
assoc, klass = unpolymorphize_association(
|
2017-08-02 16:32:11 -04:00
|
|
|
segments.join(Constants::UNDERSCORE)
|
2018-11-12 06:20:30 -05:00
|
|
|
)
|
2011-04-08 11:33:10 -04:00
|
|
|
if found_assoc = get_association(assoc, base)
|
2014-10-06 17:27:55 -04:00
|
|
|
base = traverse(
|
2017-08-02 16:32:11 -04:00
|
|
|
remainder.join(Constants::UNDERSCORE), klass || found_assoc.klass
|
2018-11-12 06:20:30 -05:00
|
|
|
)
|
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
|
2018-11-12 06:20:30 -05:00
|
|
|
unless found_assoc
|
|
|
|
raise(UntraversableAssociationError,
|
|
|
|
"No association matches #{str}")
|
|
|
|
end
|
2011-03-30 20:31:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
klassify(base)
|
|
|
|
end
|
|
|
|
|
|
|
|
def association_path(str, base = @base)
|
|
|
|
base = klassify(base)
|
2015-08-29 16:52:57 -04:00
|
|
|
str ||= ''.freeze
|
2011-03-30 20:31:39 -04:00
|
|
|
path = []
|
2017-08-02 16:32:11 -04:00
|
|
|
segments = str.split(Constants::UNDERSCORE)
|
2011-03-30 20:31:39 -04:00
|
|
|
association_parts = []
|
2018-11-12 06:20:30 -05:00
|
|
|
unless segments.empty?
|
|
|
|
while !segments.empty? &&
|
|
|
|
!base.columns_hash[segments.join(Constants::UNDERSCORE)] &&
|
|
|
|
association_parts << segments.shift
|
2014-10-06 17:27:55 -04:00
|
|
|
assoc, klass = unpolymorphize_association(
|
2014-11-21 14:03:20 -05:00
|
|
|
association_parts.join(Constants::UNDERSCORE)
|
2018-11-12 06:20:30 -05:00
|
|
|
)
|
|
|
|
next unless found_assoc = get_association(assoc, base)
|
|
|
|
path += association_parts
|
|
|
|
association_parts = []
|
|
|
|
base = klassify(klass || found_assoc)
|
2011-03-30 20:31:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-21 14:03:20 -05:00
|
|
|
path.join(Constants::UNDERSCORE)
|
2011-03-30 20:31:39 -04:00
|
|
|
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
|
|
|
|
|
2015-12-08 23:03:07 -05:00
|
|
|
def ransackable_alias(str)
|
|
|
|
klass._ransack_aliases.fetch(str, str)
|
|
|
|
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) ||
|
2018-11-12 06:20:30 -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
|
|
|
|
|
2014-06-22 05:15:10 -04:00
|
|
|
def ransackable_scope?(str, klass)
|
2017-08-02 16:32:11 -04:00
|
|
|
klass.ransackable_scopes(auth_object).any? { |s| s.to_sym == str.to_sym }
|
2014-06-22 05:15:10 -04:00
|
|
|
end
|
|
|
|
|
2018-07-17 12:18:28 -04:00
|
|
|
def ransackable_scope_skip_sanitize_args?(str, klass)
|
|
|
|
klass.ransackable_scopes_skip_sanitize_args.any? { |s| s.to_sym == str.to_sym }
|
|
|
|
end
|
|
|
|
|
2015-08-29 16:52:57 -04:00
|
|
|
def searchable_attributes(str = ''.freeze)
|
2011-04-11 20:07:23 -04:00
|
|
|
traverse(str).ransackable_attributes(auth_object)
|
2011-03-30 20:31:39 -04:00
|
|
|
end
|
|
|
|
|
2015-08-29 16:52:57 -04:00
|
|
|
def sortable_attributes(str = ''.freeze)
|
2013-12-10 13:06:07 -05:00
|
|
|
traverse(str).ransortable_attributes(auth_object)
|
2011-05-30 13:00:59 -04:00
|
|
|
end
|
2013-02-19 22:45:02 -05:00
|
|
|
|
2015-08-29 16:52:57 -04:00
|
|
|
def searchable_associations(str = ''.freeze)
|
2011-05-30 13:00:59 -04:00
|
|
|
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
|