1
0
Fork 0
mirror of https://github.com/activerecord-hackery/ransack.git synced 2022-11-09 13:47:45 -05:00
activerecord-hackery--ransack/lib/ransack/context.rb

159 lines
4.4 KiB
Ruby
Raw Normal View History

2011-04-11 12:04:31 -04:00
require 'ransack/visitor'
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
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 = {})
context =
if Class === object
for_class(object, options)
else
for_object(object, options)
end
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
def klassify(obj)
2014-08-01 02:36:12 -04:00
raise "not implemented"
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)
table_for(parent)[attr_name]
end
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
def bind(object, str)
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)
str ||= ''.freeze
segments = str.split(Constants::UNDERSCORE)
unless segments.empty?
remainder = []
2011-03-30 20:31:39 -04:00
found_assoc = nil
until found_assoc || segments.empty?
# 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)
)
if found_assoc = get_association(assoc, base)
base = traverse(
2017-08-02 16:32:11 -04:00
remainder.join(Constants::UNDERSCORE), klass || found_assoc.klass
)
2011-03-30 20:31:39 -04:00
end
remainder.unshift segments.pop
2011-03-30 20:31:39 -04:00
end
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)
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 = []
unless segments.empty?
while !segments.empty? &&
!base.columns_hash[segments.join(Constants::UNDERSCORE)] &&
association_parts << segments.shift
assoc, klass = unpolymorphize_association(
2014-11-21 14:03:20 -05:00
association_parts.join(Constants::UNDERSCORE)
)
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
def unpolymorphize_association(str)
if (match = str.match(/_of_([^_]+?)_type$/))
[match.pre_match, Kernel.const_get(match.captures.first)]
else
[str, nil]
end
end
def ransackable_alias(str)
klass._ransack_aliases.fetch(str, str)
end
def ransackable_attribute?(str, klass)
2013-12-06 19:51:55 -05:00
klass.ransackable_attributes(auth_object).include?(str) ||
klass.ransortable_attributes(auth_object).include?(str)
end
def ransackable_association?(str, klass)
klass.ransackable_associations(auth_object).include? str
end
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 }
end
def ransackable_scope_skip_sanitize_args?(str, klass)
klass.ransackable_scopes_skip_sanitize_args.any? { |s| s.to_sym == str.to_sym }
end
def searchable_attributes(str = ''.freeze)
traverse(str).ransackable_attributes(auth_object)
2011-03-30 20:31:39 -04:00
end
def sortable_attributes(str = ''.freeze)
traverse(str).ransortable_attributes(auth_object)
end
def searchable_associations(str = ''.freeze)
traverse(str).ransackable_associations(auth_object)
end
2011-03-30 20:31:39 -04:00
end
end