activerecord-hackery--ransack/lib/ransack/search.rb

145 lines
3.8 KiB
Ruby
Raw Normal View History

2011-03-31 00:31:39 +00:00
require 'ransack/nodes'
require 'ransack/context'
require 'ransack/naming'
module Ransack
class Search
include Naming
attr_reader :base, :context
delegate :object, :klass, :to => :context
delegate :new_grouping, :new_condition,
:build_grouping, :build_condition,
:translate, :to => :base
2011-03-31 00:31:39 +00:00
def initialize(object, params = {}, options = {})
params = {} unless params.is_a?(Hash)
2014-05-05 08:43:49 +00:00
(params ||= {})
.delete_if { |k, v| [*v].all? { |i| i.blank? && i != false } }
@context = options[:context] || Context.for(object, options)
@context.auth_object = options[:auth_object]
@base = Nodes::Grouping.new(@context, options[:grouping] || 'and')
@scope_args = {}
2011-03-31 00:31:39 +00:00
build(params.with_indifferent_access)
end
def result(opts = {})
@context.evaluate(self, opts)
2011-03-31 00:31:39 +00:00
end
def build(params)
collapse_multiparameter_attributes!(params).each do |key, value|
if ['s', 'sorts'].include?(key)
2011-03-31 00:31:39 +00:00
send("#{key}=", value)
elsif base.attribute_method?(key)
base.send("#{key}=", value)
elsif @context.ransackable_scope?(key, @context.object)
add_scope(key, value)
elsif !Ransack.options[:ignore_unknown_conditions]
raise ArgumentError, "Invalid search term #{key}"
2011-03-31 00:31:39 +00:00
end
end
self
end
def sorts=(args)
case args
when Array
args.each do |sort|
2014-01-06 17:59:20 +00:00
if sort.kind_of? Hash
sort = Nodes::Sort.new(@context).build(sort)
else
sort = Nodes::Sort.extract(@context, sort)
end
2011-03-31 00:31:39 +00:00
self.sorts << sort
end
when Hash
args.each do |index, attrs|
sort = Nodes::Sort.new(@context).build(attrs)
self.sorts << sort
end
when String
2011-03-31 00:31:39 +00:00
self.sorts = [args]
else
2014-05-05 08:43:49 +00:00
raise ArgumentError,
"Invalid argument (#{args.class}) supplied to sorts="
2011-03-31 00:31:39 +00:00
end
end
alias :s= :sorts=
def sorts
@sorts ||= []
end
alias :s :sorts
def build_sort(opts = {})
new_sort(opts).tap do |sort|
self.sorts << sort
end
end
def new_sort(opts = {})
Nodes::Sort.new(@context).build(opts)
end
def method_missing(method_id, *args)
method_name = method_id.to_s
getter_name = method_name.sub(/=$/, '')
if base.attribute_method?(getter_name)
2011-03-31 00:31:39 +00:00
base.send(method_id, *args)
elsif @context.ransackable_scope?(getter_name, @context.object)
if method_name =~ /=$/
add_scope getter_name, args
else
@scope_args[method_name]
end
2011-03-31 00:31:39 +00:00
else
super
end
end
def inspect
details = [
[:class, klass.name],
([:scope, @scope_args] if @scope_args.present?),
[:base, base.inspect]
].compact.map { |d| d.join(': ') }.join(', ')
"Ransack::Search<#{details}>"
end
2011-03-31 00:31:39 +00:00
private
def add_scope(key, args)
if @context.scope_arity(key) == 1
@scope_args[key] = args.is_a?(Array) ? args[0] : args
else
@scope_args[key] = args
end
@context.chain_scope(key, args)
end
2011-03-31 00:31:39 +00:00
def collapse_multiparameter_attributes!(attrs)
attrs.keys.each do |k|
if k.include?("(")
real_attribute, position = k.split(/\(|\)/)
cast = %w(a s i).include?(position.last) ? position.last : nil
position = position.to_i - 1
value = attrs.delete(k)
attrs[real_attribute] ||= []
attrs[real_attribute][position] = if cast
(value.blank? && cast == 'i') ? nil : value.send("to_#{cast}")
else
value
end
elsif Hash === attrs[k]
collapse_multiparameter_attributes!(attrs[k])
end
end
attrs
end
end
end