Class: Ransack::Search
- Inherits:
-
Object
show all
- Includes:
- Naming
- Defined in:
- lib/ransack/search.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Naming
included, #model_name, #persisted?, #to_key, #to_model, #to_param
Constructor Details
#initialize(object, params = {}, options = {}) ⇒ Search
Returns a new instance of Search.
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/ransack/search.rb', line 22
def initialize(object, params = {}, options = {})
strip_whitespace = options.fetch(:strip_whitespace, Ransack.options[:strip_whitespace])
params = params.to_unsafe_h if params.respond_to?(:to_unsafe_h)
if params.is_a? Hash
params = params.dup
params = params.transform_values { |v| v.is_a?(String) && strip_whitespace ? v.strip : v }
params.delete_if { |k, v| [*v].all?{ |i| i.blank? && i != false } }
else
params = {}
end
@context = options[:context] || Context.for(object, options)
@context.auth_object = options[:auth_object]
@base = Nodes::Grouping.new(
@context, options[:grouping] || Constants::AND
)
@scope_args = {}
@sorts ||= []
@ignore_unknown_conditions = options[:ignore_unknown_conditions] == false ? false : true
build(params.with_indifferent_access)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_id, *args) ⇒ Object
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/ransack/search.rb', line 102
def method_missing(method_id, *args)
method_name = method_id.to_s
getter_name = method_name.sub(/=$/, ''.freeze)
if base.attribute_method?(getter_name)
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
else
super
end
end
|
Instance Attribute Details
#base ⇒ Object
Returns the value of attribute base.
15
16
17
|
# File 'lib/ransack/search.rb', line 15
def base
@base
end
|
#context ⇒ Object
Returns the value of attribute context.
15
16
17
|
# File 'lib/ransack/search.rb', line 15
def context
@context
end
|
Instance Method Details
#build(params) ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/ransack/search.rb', line 47
def build(params)
collapse_multiparameter_attributes!(params).each do |key, value|
if ['s'.freeze, 'sorts'.freeze].freeze.include?(key)
send("#{key}=", value)
elsif @context.ransackable_scope?(key, @context.object)
add_scope(key, value)
elsif base.attribute_method?(key)
base.send("#{key}=", value)
elsif !Ransack.options[:ignore_unknown_conditions] || !@ignore_unknown_conditions
raise ArgumentError, "Invalid search term #{key}"
end
end
self
end
|
#build_sort(opts = {}) ⇒ Object
92
93
94
95
96
|
# File 'lib/ransack/search.rb', line 92
def build_sort(opts = {})
new_sort(opts).tap do |sort|
self.sorts << sort
end
end
|
#inspect ⇒ Object
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/ransack/search.rb', line 118
def inspect
details = [
[:class, klass.name],
([:scope, @scope_args] if @scope_args.present?),
[:base, base.inspect]
]
.compact
.map { |d| d.join(': '.freeze) }
.join(', '.freeze)
"Ransack::Search<#{details}>"
end
|
#new_sort(opts = {}) ⇒ Object
98
99
100
|
# File 'lib/ransack/search.rb', line 98
def new_sort(opts = {})
Nodes::Sort.new(@context).build(opts)
end
|
#result(opts = {}) ⇒ Object
43
44
45
|
# File 'lib/ransack/search.rb', line 43
def result(opts = {})
@context.evaluate(self, opts)
end
|
#sorts ⇒ Object
Also known as:
s
87
88
89
|
# File 'lib/ransack/search.rb', line 87
def sorts
@sorts
end
|
#sorts=(args) ⇒ Object
Also known as:
s=
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/ransack/search.rb', line 62
def sorts=(args)
case args
when Array
args.each do |sort|
if sort.kind_of? Hash
sort = Nodes::Sort.new(@context).build(sort)
else
sort = Nodes::Sort.(@context, sort)
end
self.sorts << sort
end
when Hash
args.each do |index, attrs|
sort = Nodes::Sort.new(@context).build(attrs)
self.sorts << sort
end
when String
self.sorts = [args]
else
raise ArgumentError,
"Invalid argument (#{args.class}) supplied to sorts="
end
end
|