ransack 1.6.6 → 2.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +3 -0
- data/.travis.yml +35 -53
- data/CHANGELOG.md +530 -9
- data/CONTRIBUTING.md +63 -26
- data/Gemfile +24 -24
- data/README.md +371 -96
- data/Rakefile +6 -29
- data/lib/polyamorous/activerecord_5.2_ruby_2/join_association.rb +24 -0
- data/lib/polyamorous/activerecord_5.2_ruby_2/join_dependency.rb +79 -0
- data/lib/polyamorous/activerecord_5.2_ruby_2/reflection.rb +11 -0
- data/lib/polyamorous/activerecord_6.0_ruby_2/join_association.rb +1 -0
- data/lib/polyamorous/activerecord_6.0_ruby_2/join_dependency.rb +80 -0
- data/lib/polyamorous/activerecord_6.0_ruby_2/reflection.rb +1 -0
- data/lib/polyamorous/activerecord_6.1_ruby_2/join_association.rb +74 -0
- data/lib/polyamorous/activerecord_6.1_ruby_2/join_dependency.rb +93 -0
- data/lib/polyamorous/activerecord_6.1_ruby_2/reflection.rb +1 -0
- data/lib/polyamorous/join.rb +70 -0
- data/lib/polyamorous/polyamorous.rb +24 -0
- data/lib/polyamorous/swapping_reflection_class.rb +11 -0
- data/lib/polyamorous/tree_node.rb +7 -0
- data/lib/ransack/adapters/active_record/base.rb +23 -2
- data/lib/ransack/adapters/active_record/context.rb +206 -139
- data/lib/ransack/adapters/active_record/ransack/constants.rb +70 -55
- data/lib/ransack/adapters/active_record/ransack/context.rb +10 -18
- data/lib/ransack/adapters/active_record/ransack/nodes/condition.rb +43 -36
- data/lib/ransack/adapters/active_record/ransack/translate.rb +1 -5
- data/lib/ransack/adapters/active_record/ransack/visitor.rb +23 -0
- data/lib/ransack/adapters/active_record.rb +11 -10
- data/lib/ransack/adapters.rb +64 -0
- data/lib/ransack/configuration.rb +91 -4
- data/lib/ransack/constants.rb +13 -26
- data/lib/ransack/context.rb +45 -40
- data/lib/ransack/helpers/form_builder.rb +21 -12
- data/lib/ransack/helpers/form_helper.rb +99 -69
- data/lib/ransack/locale/ar.yml +70 -0
- data/lib/ransack/locale/az.yml +70 -0
- data/lib/ransack/locale/bg.yml +70 -0
- data/lib/ransack/locale/ca.yml +70 -0
- data/lib/ransack/locale/da.yml +70 -0
- data/lib/ransack/locale/de.yml +70 -0
- data/lib/ransack/locale/el.yml +70 -0
- data/lib/ransack/locale/es.yml +22 -22
- data/lib/ransack/locale/fa.yml +70 -0
- data/lib/ransack/locale/fi.yml +71 -0
- data/lib/ransack/locale/id.yml +70 -0
- data/lib/ransack/locale/it.yml +70 -0
- data/lib/ransack/locale/ja.yml +70 -0
- data/lib/ransack/locale/nl.yml +4 -4
- data/lib/ransack/locale/pt-BR.yml +70 -0
- data/lib/ransack/locale/ru.yml +70 -0
- data/lib/ransack/locale/sk.yml +70 -0
- data/lib/ransack/locale/tr.yml +70 -0
- data/lib/ransack/locale/{zh.yml → zh-CN.yml} +13 -13
- data/lib/ransack/locale/zh-TW.yml +70 -0
- data/lib/ransack/nodes/attribute.rb +6 -3
- data/lib/ransack/nodes/bindable.rb +18 -6
- data/lib/ransack/nodes/condition.rb +85 -24
- data/lib/ransack/nodes/grouping.rb +17 -11
- data/lib/ransack/nodes/sort.rb +9 -5
- data/lib/ransack/nodes/value.rb +74 -68
- data/lib/ransack/nodes.rb +2 -3
- data/lib/ransack/predicate.rb +17 -20
- data/lib/ransack/search.rb +15 -15
- data/lib/ransack/translate.rb +115 -115
- data/lib/ransack/version.rb +1 -1
- data/lib/ransack/visitor.rb +1 -12
- data/lib/ransack.rb +11 -17
- data/logo/ransack-h.png +0 -0
- data/logo/ransack-h.svg +34 -0
- data/logo/ransack-v.png +0 -0
- data/logo/ransack-v.svg +34 -0
- data/logo/ransack.png +0 -0
- data/logo/ransack.svg +21 -0
- data/ransack.gemspec +9 -27
- data/spec/console.rb +4 -0
- data/spec/helpers/polyamorous_helper.rb +19 -0
- data/spec/polyamorous/join_association_spec.rb +35 -0
- data/spec/polyamorous/join_dependency_spec.rb +97 -0
- data/spec/polyamorous/join_spec.rb +19 -0
- data/spec/ransack/adapters/active_record/base_spec.rb +380 -91
- data/spec/ransack/adapters/active_record/context_spec.rb +72 -33
- data/spec/ransack/configuration_spec.rb +87 -14
- data/spec/ransack/helpers/form_builder_spec.rb +2 -11
- data/spec/ransack/helpers/form_helper_spec.rb +481 -113
- data/spec/ransack/nodes/condition_spec.rb +25 -1
- data/spec/ransack/nodes/grouping_spec.rb +62 -6
- data/spec/ransack/predicate_spec.rb +79 -5
- data/spec/ransack/search_spec.rb +159 -79
- data/spec/spec_helper.rb +8 -0
- data/spec/support/schema.rb +99 -37
- metadata +57 -184
- data/lib/ransack/adapters/active_record/3.0/compat.rb +0 -179
- data/lib/ransack/adapters/active_record/3.0/context.rb +0 -205
- data/lib/ransack/adapters/active_record/3.1/context.rb +0 -219
- data/lib/ransack/adapters/active_record/3.2/context.rb +0 -44
- data/lib/ransack/adapters/active_record/compat.rb +0 -14
- data/lib/ransack/adapters/mongoid/3.2/.gitkeep +0 -0
- data/lib/ransack/adapters/mongoid/attributes/attribute.rb +0 -37
- data/lib/ransack/adapters/mongoid/attributes/order_predications.rb +0 -17
- data/lib/ransack/adapters/mongoid/attributes/predications.rb +0 -141
- data/lib/ransack/adapters/mongoid/base.rb +0 -126
- data/lib/ransack/adapters/mongoid/context.rb +0 -208
- data/lib/ransack/adapters/mongoid/inquiry_hash.rb +0 -23
- data/lib/ransack/adapters/mongoid/ransack/constants.rb +0 -88
- data/lib/ransack/adapters/mongoid/ransack/context.rb +0 -60
- data/lib/ransack/adapters/mongoid/ransack/nodes/condition.rb +0 -27
- data/lib/ransack/adapters/mongoid/ransack/translate.rb +0 -13
- data/lib/ransack/adapters/mongoid/ransack/visitor.rb +0 -24
- data/lib/ransack/adapters/mongoid/table.rb +0 -35
- data/lib/ransack/adapters/mongoid.rb +0 -13
- data/spec/mongoid/adapters/mongoid/base_spec.rb +0 -276
- data/spec/mongoid/adapters/mongoid/context_spec.rb +0 -56
- data/spec/mongoid/configuration_spec.rb +0 -102
- data/spec/mongoid/dependencies_spec.rb +0 -8
- data/spec/mongoid/helpers/ransack_helper.rb +0 -11
- data/spec/mongoid/nodes/condition_spec.rb +0 -34
- data/spec/mongoid/nodes/grouping_spec.rb +0 -13
- data/spec/mongoid/predicate_spec.rb +0 -155
- data/spec/mongoid/search_spec.rb +0 -446
- data/spec/mongoid/support/mongoid.yml +0 -6
- data/spec/mongoid/support/schema.rb +0 -128
- data/spec/mongoid/translate_spec.rb +0 -14
- data/spec/mongoid_spec_helper.rb +0 -59
- data/spec/ransack/dependencies_spec.rb +0 -10
@@ -1,24 +0,0 @@
|
|
1
|
-
module Ransack
|
2
|
-
class Visitor
|
3
|
-
def visit_and(object)
|
4
|
-
nodes = object.values.map { |o| accept(o) }.compact
|
5
|
-
return nil unless nodes.size > 0
|
6
|
-
|
7
|
-
if nodes.size > 1
|
8
|
-
nodes.inject(&:and)
|
9
|
-
else
|
10
|
-
nodes.first
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
def quoted?(object)
|
15
|
-
case object
|
16
|
-
when Arel::Nodes::SqlLiteral, Bignum, Fixnum
|
17
|
-
false
|
18
|
-
else
|
19
|
-
true
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
end
|
24
|
-
end
|
@@ -1,35 +0,0 @@
|
|
1
|
-
module Ransack
|
2
|
-
module Adapters
|
3
|
-
module Mongoid
|
4
|
-
class Table
|
5
|
-
attr_accessor :name
|
6
|
-
|
7
|
-
alias :table_name :name
|
8
|
-
|
9
|
-
def initialize(object, engine = nil)
|
10
|
-
@object = object
|
11
|
-
@name = object.collection.name
|
12
|
-
@engine = engine
|
13
|
-
@columns = nil
|
14
|
-
@aliases = []
|
15
|
-
@table_alias = nil
|
16
|
-
@primary_key = nil
|
17
|
-
|
18
|
-
if Hash === engine
|
19
|
-
# @engine = engine[:engine] || Table.engine
|
20
|
-
|
21
|
-
# Sometime AR sends an :as parameter to table, to let the table know
|
22
|
-
# that it is an Alias. We may want to override new, and return a
|
23
|
-
# TableAlias node?
|
24
|
-
# @table_alias = engine[:as] unless engine[:as].to_s == @name
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
def [](name)
|
29
|
-
Ransack::Adapters::Mongoid::Attribute.new self, name
|
30
|
-
end
|
31
|
-
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
@@ -1,13 +0,0 @@
|
|
1
|
-
require 'ransack/adapters/mongoid/base'
|
2
|
-
::Mongoid::Document.send :include, Ransack::Adapters::Mongoid::Base
|
3
|
-
|
4
|
-
require 'ransack/adapters/mongoid/attributes/attribute'
|
5
|
-
require 'ransack/adapters/mongoid/table'
|
6
|
-
require 'ransack/adapters/mongoid/inquiry_hash'
|
7
|
-
|
8
|
-
case ::Mongoid::VERSION
|
9
|
-
when /^3\.2\./
|
10
|
-
require 'ransack/adapters/mongoid/3.2/context'
|
11
|
-
else
|
12
|
-
require 'ransack/adapters/mongoid/context'
|
13
|
-
end
|
@@ -1,276 +0,0 @@
|
|
1
|
-
require 'mongoid_spec_helper'
|
2
|
-
|
3
|
-
module Ransack
|
4
|
-
module Adapters
|
5
|
-
module Mongoid
|
6
|
-
describe Base do
|
7
|
-
|
8
|
-
subject { Person }
|
9
|
-
|
10
|
-
it { should respond_to :ransack }
|
11
|
-
it { should respond_to :search }
|
12
|
-
|
13
|
-
describe '#search' do
|
14
|
-
subject { Person.search }
|
15
|
-
|
16
|
-
it { should be_a Search }
|
17
|
-
it 'has a Mongoid::Criteria as its object' do
|
18
|
-
expect(subject.object).to be_an ::Mongoid::Criteria
|
19
|
-
end
|
20
|
-
|
21
|
-
context 'with scopes' do
|
22
|
-
before do
|
23
|
-
Person.stub :ransackable_scopes => [:active, :over_age]
|
24
|
-
end
|
25
|
-
|
26
|
-
it "applies true scopes" do
|
27
|
-
search = Person.search('active' => true)
|
28
|
-
expect(search.result.selector).to eq({ 'active' => 1 })
|
29
|
-
end
|
30
|
-
|
31
|
-
it "ignores unlisted scopes" do
|
32
|
-
search = Person.search('restricted' => true)
|
33
|
-
expect(search.result.selector).to_not eq({ 'restricted' => 1})
|
34
|
-
end
|
35
|
-
|
36
|
-
it "ignores false scopes" do
|
37
|
-
search = Person.search('active' => false)
|
38
|
-
expect(search.result.selector).to_not eq({ 'active' => 1 })
|
39
|
-
end
|
40
|
-
|
41
|
-
it "passes values to scopes" do
|
42
|
-
search = Person.search('over_age' => 18)
|
43
|
-
expect(search.result.selector).to eq({ 'age' => { '$gt' => 18 } })
|
44
|
-
end
|
45
|
-
|
46
|
-
it "chains scopes" do
|
47
|
-
search = Person.search('over_age' => 18, 'active' => true)
|
48
|
-
expect(search.result.selector).to eq({ 'age' => { '$gt' => 18 }, 'active' => 1 })
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
describe '#ransacker' do
|
54
|
-
# For infix tests
|
55
|
-
def self.sane_adapter?
|
56
|
-
case ::Mongoid::Document.connection.adapter_name
|
57
|
-
when "SQLite3", "PostgreSQL"
|
58
|
-
true
|
59
|
-
else
|
60
|
-
false
|
61
|
-
end
|
62
|
-
end
|
63
|
-
# # in schema.rb, class Person:
|
64
|
-
# ransacker :reversed_name, formatter: proc { |v| v.reverse } do |parent|
|
65
|
-
# parent.table[:name]
|
66
|
-
# end
|
67
|
-
|
68
|
-
# ransacker :doubled_name do |parent|
|
69
|
-
# Arel::Nodes::InfixOperation.new(
|
70
|
-
# '||', parent.table[:name], parent.table[:name]
|
71
|
-
# )
|
72
|
-
# end
|
73
|
-
|
74
|
-
it 'creates ransack attributes' do
|
75
|
-
s = Person.search(:reversed_name_eq => 'htimS cirA')
|
76
|
-
expect(s.result.size).to eq(Person.where(name: 'Aric Smith').count)
|
77
|
-
|
78
|
-
expect(s.result.first).to eq Person.where(name: 'Aric Smith').first
|
79
|
-
end
|
80
|
-
|
81
|
-
context 'with joins' do
|
82
|
-
before { pending 'not implemented for mongoid' }
|
83
|
-
|
84
|
-
it 'can be accessed through associations' do
|
85
|
-
s = Person.search(:children_reversed_name_eq => 'htimS cirA')
|
86
|
-
expect(s.result.to_sql).to match(
|
87
|
-
/#{quote_table_name("children_people")}.#{
|
88
|
-
quote_column_name("name")} = 'Aric Smith'/
|
89
|
-
)
|
90
|
-
end
|
91
|
-
|
92
|
-
it "should keep proper key value pairs in the params hash" do
|
93
|
-
s = Person.search(:children_reversed_name_eq => 'Testing')
|
94
|
-
expect(s.result.to_sql).to match /LEFT OUTER JOIN/
|
95
|
-
end
|
96
|
-
|
97
|
-
end
|
98
|
-
|
99
|
-
it 'allows an "attribute" to be an InfixOperation' do
|
100
|
-
s = Person.search(:doubled_name_eq => 'Aric SmithAric Smith')
|
101
|
-
expect(s.result.first).to eq Person.where(name: 'Aric Smith').first
|
102
|
-
end if defined?(Arel::Nodes::InfixOperation) && sane_adapter?
|
103
|
-
|
104
|
-
it "doesn't break #count if using InfixOperations" do
|
105
|
-
s = Person.search(:doubled_name_eq => 'Aric SmithAric Smith')
|
106
|
-
expect(s.result.count).to eq 1
|
107
|
-
end if defined?(Arel::Nodes::InfixOperation) && sane_adapter?
|
108
|
-
|
109
|
-
it "should remove empty key value pairs from the params hash" do
|
110
|
-
s = Person.search(:reversed_name_eq => '')
|
111
|
-
expect(s.result.selector).to eq({})
|
112
|
-
end
|
113
|
-
|
114
|
-
it "should function correctly when nil is passed in" do
|
115
|
-
s = Person.search(nil)
|
116
|
-
end
|
117
|
-
|
118
|
-
it "should function correctly when a blank string is passed in" do
|
119
|
-
s = Person.search('')
|
120
|
-
end
|
121
|
-
|
122
|
-
it "should function correctly when using fields with dots in them" do
|
123
|
-
s = Person.search(:email_cont => "example.com")
|
124
|
-
expect(s.result.exists?).to be true
|
125
|
-
|
126
|
-
s = Person.search(:email_cont => "example.co.")
|
127
|
-
expect(s.result.exists?).not_to be true
|
128
|
-
end
|
129
|
-
|
130
|
-
it "should function correctly when using fields with % in them" do
|
131
|
-
Person.create!(:name => "110%-er")
|
132
|
-
s = Person.search(:name_cont => "10%")
|
133
|
-
expect(s.result.exists?).to be true
|
134
|
-
end
|
135
|
-
|
136
|
-
it "should function correctly when using fields with backslashes in them" do
|
137
|
-
Person.create!(:name => "\\WINNER\\")
|
138
|
-
s = Person.search(:name_cont => "\\WINNER\\")
|
139
|
-
expect(s.result.exists?).to be true
|
140
|
-
end
|
141
|
-
|
142
|
-
it 'allows sort by "only_sort" field' do
|
143
|
-
pending "it doesn't work :("
|
144
|
-
s = Person.search(
|
145
|
-
"s" => { "0" => { "dir" => "desc", "name" => "only_sort" } }
|
146
|
-
)
|
147
|
-
expect(s.result.to_sql).to match(
|
148
|
-
/ORDER BY #{quote_table_name("people")}.#{
|
149
|
-
quote_column_name("only_sort")} ASC/
|
150
|
-
)
|
151
|
-
end
|
152
|
-
|
153
|
-
it "doesn't sort by 'only_search' field" do
|
154
|
-
pending "it doesn't work :("
|
155
|
-
s = Person.search(
|
156
|
-
"s" => { "0" => { "dir" => "asc", "name" => "only_search" } }
|
157
|
-
)
|
158
|
-
expect(s.result.to_sql).not_to match(
|
159
|
-
/ORDER BY #{quote_table_name("people")}.#{
|
160
|
-
quote_column_name("only_search")} ASC/
|
161
|
-
)
|
162
|
-
end
|
163
|
-
|
164
|
-
it 'allows search by "only_search" field' do
|
165
|
-
s = Person.search(:only_search_eq => 'htimS cirA')
|
166
|
-
expect(s.result.selector).to eq({'only_search' => 'htimS cirA'})
|
167
|
-
end
|
168
|
-
|
169
|
-
it "can't be searched by 'only_sort'" do
|
170
|
-
s = Person.search(:only_sort_eq => 'htimS cirA')
|
171
|
-
expect(s.result.selector).not_to eq({'only_sort' => 'htimS cirA'})
|
172
|
-
end
|
173
|
-
|
174
|
-
it 'allows sort by "only_admin" field, if auth_object: :admin' do
|
175
|
-
s = Person.search(
|
176
|
-
{ "s" => { "0" => { "dir" => "asc", "name" => "only_admin" } } },
|
177
|
-
{ auth_object: :admin }
|
178
|
-
)
|
179
|
-
expect(s.result.options).to eq({ sort: { '_id' => -1, 'only_admin' => 1 } })
|
180
|
-
end
|
181
|
-
|
182
|
-
it "doesn't sort by 'only_admin' field, if auth_object: nil" do
|
183
|
-
s = Person.search(
|
184
|
-
"s" => { "0" => { "dir" => "asc", "name" => "only_admin" } }
|
185
|
-
)
|
186
|
-
expect(s.result.options).to eq({ sort: {'_id' => -1}})
|
187
|
-
end
|
188
|
-
|
189
|
-
it 'allows search by "only_admin" field, if auth_object: :admin' do
|
190
|
-
s = Person.search(
|
191
|
-
{ :only_admin_eq => 'htimS cirA' },
|
192
|
-
{ :auth_object => :admin }
|
193
|
-
)
|
194
|
-
expect(s.result.selector).to eq({ 'only_admin' => 'htimS cirA' })
|
195
|
-
end
|
196
|
-
|
197
|
-
it "can't be searched by 'only_admin', if auth_object: nil" do
|
198
|
-
s = Person.search(:only_admin_eq => 'htimS cirA')
|
199
|
-
expect(s.result.selector).to eq({})
|
200
|
-
end
|
201
|
-
|
202
|
-
it 'searches by id' do
|
203
|
-
ids = ['some_bson_id', 'another_bson_id']
|
204
|
-
s = Person.search(:id_in => ids)
|
205
|
-
expect(s.result.selector).to eq({ '_id' => { '$in' => ids } })
|
206
|
-
end
|
207
|
-
end
|
208
|
-
|
209
|
-
describe '#ransackable_attributes' do
|
210
|
-
context 'when auth_object is nil' do
|
211
|
-
subject { Person.ransackable_attributes }
|
212
|
-
|
213
|
-
it { should include 'name' }
|
214
|
-
it { should include 'reversed_name' }
|
215
|
-
it { should include 'doubled_name' }
|
216
|
-
it { should include 'only_search' }
|
217
|
-
it { should_not include 'only_sort' }
|
218
|
-
it { should_not include 'only_admin' }
|
219
|
-
end
|
220
|
-
|
221
|
-
context 'with auth_object :admin' do
|
222
|
-
subject { Person.ransackable_attributes(:admin) }
|
223
|
-
|
224
|
-
it { should include 'name' }
|
225
|
-
it { should include 'reversed_name' }
|
226
|
-
it { should include 'doubled_name' }
|
227
|
-
it { should include 'only_search' }
|
228
|
-
it { should_not include 'only_sort' }
|
229
|
-
it { should include 'only_admin' }
|
230
|
-
end
|
231
|
-
end
|
232
|
-
|
233
|
-
describe '#ransortable_attributes' do
|
234
|
-
context 'when auth_object is nil' do
|
235
|
-
subject { Person.ransortable_attributes }
|
236
|
-
|
237
|
-
it { should include 'name' }
|
238
|
-
it { should include 'reversed_name' }
|
239
|
-
it { should include 'doubled_name' }
|
240
|
-
it { should include 'only_sort' }
|
241
|
-
it { should_not include 'only_search' }
|
242
|
-
it { should_not include 'only_admin' }
|
243
|
-
end
|
244
|
-
|
245
|
-
context 'with auth_object :admin' do
|
246
|
-
subject { Person.ransortable_attributes(:admin) }
|
247
|
-
|
248
|
-
it { should include 'name' }
|
249
|
-
it { should include 'reversed_name' }
|
250
|
-
it { should include 'doubled_name' }
|
251
|
-
it { should include 'only_sort' }
|
252
|
-
it { should_not include 'only_search' }
|
253
|
-
it { should include 'only_admin' }
|
254
|
-
end
|
255
|
-
end
|
256
|
-
|
257
|
-
describe '#ransackable_associations' do
|
258
|
-
before { pending "not implemented for mongoid" }
|
259
|
-
|
260
|
-
subject { Person.ransackable_associations }
|
261
|
-
|
262
|
-
it { should include 'parent' }
|
263
|
-
it { should include 'children' }
|
264
|
-
it { should include 'articles' }
|
265
|
-
end
|
266
|
-
|
267
|
-
describe '#ransackable_scopes' do
|
268
|
-
subject { Person.ransackable_scopes }
|
269
|
-
|
270
|
-
it { should eq [] }
|
271
|
-
end
|
272
|
-
|
273
|
-
end
|
274
|
-
end
|
275
|
-
end
|
276
|
-
end
|
@@ -1,56 +0,0 @@
|
|
1
|
-
require 'mongoid_spec_helper'
|
2
|
-
|
3
|
-
module Ransack
|
4
|
-
module Adapters
|
5
|
-
module Mongoid
|
6
|
-
describe Context do
|
7
|
-
subject { Context.new(Person) }
|
8
|
-
|
9
|
-
describe '#relation_for' do
|
10
|
-
before { pending "not implemented for mongoid" }
|
11
|
-
it 'returns relation for given object' do
|
12
|
-
expect(subject.object).to be_an ::ActiveRecord::Relation
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
describe '#evaluate' do
|
17
|
-
it 'evaluates search objects' do
|
18
|
-
search = Search.new(Person, :name_eq => 'Joe Blow')
|
19
|
-
result = subject.evaluate(search)
|
20
|
-
|
21
|
-
expect(result).to be_an ::Mongoid::Criteria
|
22
|
-
expect(result.selector).to eq({ 'name' => 'Joe Blow' })
|
23
|
-
end
|
24
|
-
|
25
|
-
it 'SELECTs DISTINCT when distinct: true' do
|
26
|
-
pending "distinct doesn't work"
|
27
|
-
|
28
|
-
search = Search.new(Person, :name_eq => 'Joe Blow')
|
29
|
-
result = subject.evaluate(search, :distinct => true)
|
30
|
-
|
31
|
-
expect(result).to be_an ::ActiveRecord::Relation
|
32
|
-
expect(result.to_sql).to match /SELECT DISTINCT/
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
it 'contextualizes strings to attributes' do
|
37
|
-
attribute = subject.contextualize 'name'
|
38
|
-
expect(attribute).to be_a ::Ransack::Adapters::Mongoid::Attributes::Attribute
|
39
|
-
expect(attribute.name.to_s).to eq 'name'
|
40
|
-
# expect(attribute.relation.table_alias).to eq 'parents_people'
|
41
|
-
end
|
42
|
-
|
43
|
-
it 'builds new associations if not yet built' do
|
44
|
-
pending "not implemented for mongoid"
|
45
|
-
|
46
|
-
attribute = subject.contextualize 'children_articles_title'
|
47
|
-
expect(attribute).to be_a Arel::Attributes::Attribute
|
48
|
-
expect(attribute.name.to_s).to eq 'title'
|
49
|
-
expect(attribute.relation.name).to eq 'articles'
|
50
|
-
expect(attribute.relation.table_alias).to be_nil
|
51
|
-
end
|
52
|
-
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
@@ -1,102 +0,0 @@
|
|
1
|
-
require 'mongoid_spec_helper'
|
2
|
-
|
3
|
-
module Ransack
|
4
|
-
describe Configuration do
|
5
|
-
it 'yields Ransack on configure' do
|
6
|
-
Ransack.configure { |config| expect(config).to eq Ransack }
|
7
|
-
end
|
8
|
-
|
9
|
-
it 'adds predicates' do
|
10
|
-
Ransack.configure do |config|
|
11
|
-
config.add_predicate :test_predicate
|
12
|
-
end
|
13
|
-
|
14
|
-
expect(Ransack.predicates).to have_key 'test_predicate'
|
15
|
-
expect(Ransack.predicates).to have_key 'test_predicate_any'
|
16
|
-
expect(Ransack.predicates).to have_key 'test_predicate_all'
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'avoids creating compound predicates if compounds: false' do
|
20
|
-
Ransack.configure do |config|
|
21
|
-
config.add_predicate(
|
22
|
-
:test_predicate_without_compound,
|
23
|
-
:compounds => false
|
24
|
-
)
|
25
|
-
end
|
26
|
-
expect(Ransack.predicates)
|
27
|
-
.to have_key 'test_predicate_without_compound'
|
28
|
-
expect(Ransack.predicates)
|
29
|
-
.not_to have_key 'test_predicate_without_compound_any'
|
30
|
-
expect(Ransack.predicates)
|
31
|
-
.not_to have_key 'test_predicate_without_compound_all'
|
32
|
-
end
|
33
|
-
|
34
|
-
it 'should have default value for search key' do
|
35
|
-
expect(Ransack.options[:search_key]).to eq :q
|
36
|
-
end
|
37
|
-
|
38
|
-
it 'changes default search key parameter' do
|
39
|
-
# store original state so we can restore it later
|
40
|
-
before = Ransack.options.clone
|
41
|
-
|
42
|
-
Ransack.configure do |config|
|
43
|
-
config.search_key = :query
|
44
|
-
end
|
45
|
-
|
46
|
-
expect(Ransack.options[:search_key]).to eq :query
|
47
|
-
|
48
|
-
# restore original state so we don't break other tests
|
49
|
-
Ransack.options = before
|
50
|
-
end
|
51
|
-
|
52
|
-
it 'adds predicates that take arrays, overriding compounds' do
|
53
|
-
Ransack.configure do |config|
|
54
|
-
config.add_predicate(
|
55
|
-
:test_array_predicate,
|
56
|
-
:wants_array => true,
|
57
|
-
:compounds => true
|
58
|
-
)
|
59
|
-
end
|
60
|
-
|
61
|
-
expect(Ransack.predicates['test_array_predicate'].wants_array).to eq true
|
62
|
-
expect(Ransack.predicates).not_to have_key 'test_array_predicate_any'
|
63
|
-
expect(Ransack.predicates).not_to have_key 'test_array_predicate_all'
|
64
|
-
end
|
65
|
-
|
66
|
-
describe '`wants_array` option takes precedence over Arel predicate' do
|
67
|
-
it 'implicitly wants an array for in/not in predicates' do
|
68
|
-
Ransack.configure do |config|
|
69
|
-
config.add_predicate(
|
70
|
-
:test_in_predicate,
|
71
|
-
:arel_predicate => 'in'
|
72
|
-
)
|
73
|
-
config.add_predicate(
|
74
|
-
:test_not_in_predicate,
|
75
|
-
:arel_predicate => 'not_in'
|
76
|
-
)
|
77
|
-
end
|
78
|
-
|
79
|
-
expect(Ransack.predicates['test_in_predicate'].wants_array).to eq true
|
80
|
-
expect(Ransack.predicates['test_not_in_predicate'].wants_array).to eq true
|
81
|
-
end
|
82
|
-
|
83
|
-
it 'explicitly does not want array for in/not_in predicates' do
|
84
|
-
Ransack.configure do |config|
|
85
|
-
config.add_predicate(
|
86
|
-
:test_in_predicate_no_array,
|
87
|
-
:arel_predicate => 'in',
|
88
|
-
:wants_array => false
|
89
|
-
)
|
90
|
-
config.add_predicate(
|
91
|
-
:test_not_in_predicate_no_array,
|
92
|
-
:arel_predicate => 'not_in',
|
93
|
-
:wants_array => false
|
94
|
-
)
|
95
|
-
end
|
96
|
-
|
97
|
-
expect(Ransack.predicates['test_in_predicate_no_array'].wants_array).to eq false
|
98
|
-
expect(Ransack.predicates['test_not_in_predicate_no_array'].wants_array).to eq false
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
require 'mongoid_spec_helper'
|
2
|
-
|
3
|
-
module Ransack
|
4
|
-
module Nodes
|
5
|
-
describe Condition do
|
6
|
-
|
7
|
-
context 'with multiple values and an _any predicate' do
|
8
|
-
subject { Condition.extract(Context.for(Person), 'name_eq_any', Person.first(2).map(&:name)) }
|
9
|
-
|
10
|
-
specify { expect(subject.values.size).to eq(2) }
|
11
|
-
end
|
12
|
-
|
13
|
-
context 'with an invalid predicate' do
|
14
|
-
subject { Condition.extract(Context.for(Person), 'name_invalid', Person.first.name) }
|
15
|
-
|
16
|
-
context "when ignore_unknown_conditions is false" do
|
17
|
-
before do
|
18
|
-
Ransack.configure { |config| config.ignore_unknown_conditions = false }
|
19
|
-
end
|
20
|
-
|
21
|
-
specify { expect { subject }.to raise_error ArgumentError }
|
22
|
-
end
|
23
|
-
|
24
|
-
context "when ignore_unknown_conditions is true" do
|
25
|
-
before do
|
26
|
-
Ransack.configure { |config| config.ignore_unknown_conditions = true }
|
27
|
-
end
|
28
|
-
|
29
|
-
specify { subject.should be_nil }
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|