ransack 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,11 +3,16 @@ require 'spec_helper'
3
3
  module Ransack
4
4
  module Adapters
5
5
  module ActiveRecord
6
+ version = ::ActiveRecord::VERSION
7
+ AR_version = "#{version::MAJOR}.#{version::MINOR}"
8
+
6
9
  describe Context do
7
10
  subject { Context.new(Person) }
8
11
 
9
- if ::ActiveRecord::VERSION::STRING >= "3.1"
10
- its(:alias_tracker) { should be_a ::ActiveRecord::Associations::AliasTracker }
12
+ if AR_version >= "3.1"
13
+ its(:alias_tracker) {
14
+ should be_a ::ActiveRecord::Associations::AliasTracker
15
+ }
11
16
  end
12
17
 
13
18
  describe '#relation_for' do
@@ -22,7 +27,8 @@ module Ransack
22
27
  result = subject.evaluate(search)
23
28
 
24
29
  expect(result).to be_an ::ActiveRecord::Relation
25
- expect(result.to_sql).to match /#{quote_column_name("name")} = 'Joe Blow'/
30
+ expect(result.to_sql)
31
+ .to match /#{quote_column_name("name")} = 'Joe Blow'/
26
32
  end
27
33
 
28
34
  it 'SELECTs DISTINCT when distinct: true' do
@@ -38,12 +44,15 @@ module Ransack
38
44
  let(:shared_context) { Context.for(Person) }
39
45
 
40
46
  before do
41
- Search.new(Person, {:parent_name_eq => 'A'}, context: shared_context)
42
- Search.new(Person, {:children_name_eq => 'B'}, context: shared_context)
47
+ Search.new(Person, { :parent_name_eq => 'A' },
48
+ context: shared_context)
49
+ Search.new(Person, { :children_name_eq => 'B' },
50
+ context: shared_context)
43
51
  end
44
52
 
45
- describe '#join_associations', :if => ::ActiveRecord::VERSION::STRING <= '4.0' do
46
- it 'returns dependent join associations for all searches run against the context' do
53
+ describe '#join_associations', :if => AR_version <= '4.0' do
54
+ it 'returns dependent join associations for all searches run
55
+ against the context' do
47
56
  parents, children = shared_context.join_associations
48
57
 
49
58
  expect(children.aliased_table_name).to eq "children_people"
@@ -53,22 +62,28 @@ module Ransack
53
62
  it 'can be rejoined to execute a valid query' do
54
63
  parents, children = shared_context.join_associations
55
64
 
56
- expect { Person.joins(parents).joins(children).to_a }.to_not raise_error
65
+ expect { Person.joins(parents).joins(children).to_a }
66
+ .to_not raise_error
57
67
  end
58
68
  end
59
69
 
60
- describe '#join_sources', :if => ::ActiveRecord::VERSION::STRING >= '3.1' do
61
- it 'returns dependent arel join nodes for all searches run against the context' do
70
+ describe '#join_sources' do
71
+ # FIXME: fix this test for Rails 4.2.
72
+ it 'returns dependent arel join nodes for all searches run against
73
+ the context',
74
+ :if => %w(3.1 3.2 4.0 4.1).include?(AR_version) do
62
75
  parents, children = shared_context.join_sources
63
76
 
64
77
  expect(children.left.name).to eq "children_people"
65
78
  expect(parents.left.name).to eq "parents_people"
66
79
  end
67
80
 
68
- it 'can be rejoined to execute a valid query' do
81
+ it 'can be rejoined to execute a valid query',
82
+ :if => AR_version >= '3.1' do
69
83
  parents, children = shared_context.join_sources
70
84
 
71
- expect { Person.joins(parents).joins(children).to_a }.to_not raise_error
85
+ expect { Person.joins(parents).joins(children).to_a }
86
+ .to_not raise_error
72
87
  end
73
88
  end
74
89
  end
@@ -5,13 +5,21 @@ module Ransack
5
5
  describe Condition do
6
6
 
7
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)) }
8
+ subject {
9
+ Condition.extract(
10
+ Context.for(Person), 'name_eq_any', Person.first(2).map(&:name)
11
+ )
12
+ }
9
13
 
10
14
  specify { expect(subject.values.size).to eq(2) }
11
15
  end
12
16
 
13
17
  context 'with an invalid predicate' do
14
- subject { Condition.extract(Context.for(Person), 'name_invalid', Person.first.name) }
18
+ subject {
19
+ Condition.extract(
20
+ Context.for(Person), 'name_invalid', Person.first.name
21
+ )
22
+ }
15
23
 
16
24
  context "when ignore_unknown_conditions is false" do
17
25
  before do
@@ -80,6 +80,70 @@ module Ransack
80
80
  end
81
81
  end
82
82
 
83
+ describe 'true' do
84
+ it 'generates an equality condition for boolean true' do
85
+ @s.awesome_true = true
86
+ field = "#{quote_table_name("people")}.#{quote_column_name("awesome")}"
87
+ expect(@s.result.to_sql).to match /#{field} = #{
88
+ ActiveRecord::Base.connection.quoted_true}/
89
+ end
90
+
91
+ it 'generates an inequality condition for boolean true' do
92
+ @s.awesome_true = false
93
+ field = "#{quote_table_name("people")}.#{quote_column_name("awesome")}"
94
+ expect(@s.result.to_sql).to match /#{field} != #{
95
+ ActiveRecord::Base.connection.quoted_true}/
96
+ end
97
+ end
98
+
99
+ describe 'not_true' do
100
+ it 'generates an inequality condition for boolean true' do
101
+ @s.awesome_not_true = true
102
+ field = "#{quote_table_name("people")}.#{quote_column_name("awesome")}"
103
+ expect(@s.result.to_sql).to match /#{field} != #{
104
+ ActiveRecord::Base.connection.quoted_true}/
105
+ end
106
+
107
+ it 'generates an equality condition for boolean true' do
108
+ @s.awesome_not_true = false
109
+ field = "#{quote_table_name("people")}.#{quote_column_name("awesome")}"
110
+ expect(@s.result.to_sql).to match /#{field} = #{
111
+ ActiveRecord::Base.connection.quoted_true}/
112
+ end
113
+ end
114
+
115
+ describe 'false' do
116
+ it 'generates an equality condition for boolean false' do
117
+ @s.awesome_false = true
118
+ field = "#{quote_table_name("people")}.#{quote_column_name("awesome")}"
119
+ expect(@s.result.to_sql).to match /#{field} = #{
120
+ ActiveRecord::Base.connection.quoted_false}/
121
+ end
122
+
123
+ it 'generates an inequality condition for boolean false' do
124
+ @s.awesome_false = false
125
+ field = "#{quote_table_name("people")}.#{quote_column_name("awesome")}"
126
+ expect(@s.result.to_sql).to match /#{field} != #{
127
+ ActiveRecord::Base.connection.quoted_false}/
128
+ end
129
+ end
130
+
131
+ describe 'not_false' do
132
+ it 'generates an inequality condition for boolean false' do
133
+ @s.awesome_not_false = true
134
+ field = "#{quote_table_name("people")}.#{quote_column_name("awesome")}"
135
+ expect(@s.result.to_sql).to match /#{field} != #{
136
+ ActiveRecord::Base.connection.quoted_false}/
137
+ end
138
+
139
+ it 'generates an equality condition for boolean false' do
140
+ @s.awesome_not_false = false
141
+ field = "#{quote_table_name("people")}.#{quote_column_name("awesome")}"
142
+ expect(@s.result.to_sql).to match /#{field} = #{
143
+ ActiveRecord::Base.connection.quoted_false}/
144
+ end
145
+ end
146
+
83
147
  describe 'null' do
84
148
  it 'generates a value IS NULL query' do
85
149
  @s.name_null = true
@@ -43,8 +43,12 @@ module Ransack
43
43
 
44
44
  it 'accepts a context option' do
45
45
  shared_context = Context.for(Person)
46
- search1 = Search.new(Person, {"name_eq" => "A"}, context: shared_context)
47
- search2 = Search.new(Person, {"name_eq" => "B"}, context: shared_context)
46
+ search1 = Search.new(
47
+ Person, { "name_eq" => "A" }, context: shared_context
48
+ )
49
+ search2 = Search.new(
50
+ Person, { "name_eq" => "B" }, context: shared_context
51
+ )
48
52
  expect(search1.context).to be search2.context
49
53
  end
50
54
  end
@@ -89,6 +93,11 @@ module Ransack
89
93
  expect(condition.value).to eq 'Ernie'
90
94
  end
91
95
 
96
+ it 'preserves default scope conditions for associations' do
97
+ search = Search.new(Person, :articles_title_eq => 'Test')
98
+ expect(search.result.to_sql).to include "default_scope"
99
+ end
100
+
92
101
  it 'discards empty conditions' do
93
102
  search = Search.new(Person, :children_name_eq => '')
94
103
  condition = search.base[:children_name_eq]
@@ -169,7 +178,9 @@ module Ransack
169
178
 
170
179
  context "when ignore_unknown_conditions is false" do
171
180
  before do
172
- Ransack.configure { |config| config.ignore_unknown_conditions = false }
181
+ Ransack.configure { |config|
182
+ config.ignore_unknown_conditions = false
183
+ }
173
184
  end
174
185
 
175
186
  specify { expect { subject }.to raise_error ArgumentError }
@@ -177,12 +188,20 @@ module Ransack
177
188
 
178
189
  context "when ignore_unknown_conditions is true" do
179
190
  before do
180
- Ransack.configure { |config| config.ignore_unknown_conditions = true }
191
+ Ransack.configure { |config|
192
+ config.ignore_unknown_conditions = true
193
+ }
181
194
  end
182
195
 
183
196
  specify { expect { subject }.not_to raise_error }
184
197
  end
185
198
  end
199
+
200
+ it 'does not modify the parameters' do
201
+ params = { :name_eq => '' }
202
+ expect { Search.new(Person, params) }.not_to change { params }
203
+ end
204
+
186
205
  end
187
206
 
188
207
  describe '#result' do
@@ -88,6 +88,12 @@ class Article < ActiveRecord::Base
88
88
  has_many :comments
89
89
  has_and_belongs_to_many :tags
90
90
  has_many :notes, :as => :notable
91
+
92
+ if ActiveRecord::VERSION::STRING >= '3.1'
93
+ default_scope { where("'default_scope' = 'default_scope'") }
94
+ else # Rails 3.0 does not accept a block
95
+ default_scope where("'default_scope' = 'default_scope'")
96
+ end
91
97
  end
92
98
 
93
99
  module Namespace
@@ -129,7 +135,7 @@ module Schema
129
135
  t.string :only_admin
130
136
  t.integer :salary
131
137
  t.boolean :awesome, default: false
132
- t.timestamps
138
+ t.timestamps null: false
133
139
  end
134
140
 
135
141
  create_table :articles, :force => true do |t|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ransack
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ernie Miller
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-08-23 00:00:00.000000000 Z
13
+ date: 2014-09-23 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: actionpack
@@ -192,6 +192,7 @@ extra_rdoc_files: []
192
192
  files:
193
193
  - ".gitignore"
194
194
  - ".travis.yml"
195
+ - CHANGELOG.md
195
196
  - CONTRIBUTING.md
196
197
  - Gemfile
197
198
  - LICENSE
@@ -218,6 +219,7 @@ files:
218
219
  - lib/ransack/locale/fr.yml
219
220
  - lib/ransack/locale/hu.yml
220
221
  - lib/ransack/locale/nl.yml
222
+ - lib/ransack/locale/ro.yml
221
223
  - lib/ransack/locale/zh.yml
222
224
  - lib/ransack/naming.rb
223
225
  - lib/ransack/nodes.rb