ransack 1.6.3 → 1.6.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9bf018bef97fde714331b98465544735b3cdd23c
4
- data.tar.gz: 3f5e4cae6c7bcf4f09eb6aa3851b521b18150569
3
+ metadata.gz: 954c9a11f9682b9e3fe5689c40d56d1978c24bb9
4
+ data.tar.gz: 58f30b75616e213c3afc8a91f92c8a940b4ac837
5
5
  SHA512:
6
- metadata.gz: e6b753315240e2e974e82e2542d3b0dfbd90c6fdb1718d02e917b6ff348764fefb5d87fbbc14d15f400026d9303c71013dca5e9194432e7716f9b81342c54f54
7
- data.tar.gz: e17e95e45672c4feb667f9a1a940cac994b43a8844cd5606f5a53d6e25e8284fb55b5a6ddfa8119d74dacfa8b58bb9a8198ebcc540e4be3e83ab1887e4ccce24
6
+ metadata.gz: bcbd765da64c6a1919651d7e8544ee74eedf95d69c69b0e777fffc5ebc624fe6312ae6ef88f4789dcaa54a622787f5f67804bdcfd07025163cae71b6100c0fbd
7
+ data.tar.gz: 876abe7e9d6faf89b99b64e993645add3d1c64bca934c4066c5b97790c46401ed581df97b8ffdc98685a9fd7fd5d4885196374e7a2d9ac5c581a0caba10725bc
@@ -52,7 +52,6 @@ matrix:
52
52
  env: RAILS=3-1-stable DB=mysql
53
53
  - rvm: 2.2
54
54
  env: RAILS=3-1-stable DB=postgres
55
- exclude:
56
55
  - rvm: 2.2
57
56
  env: RAILS=3-0-stable DB=sqlite
58
57
  - rvm: 2.2
@@ -1,5 +1,21 @@
1
1
  # Change Log
2
2
 
3
+ ## Version 1.6.4 - 2015-03-20
4
+
5
+ * ActionView patch to maintain compatibility with Rails 4.2.1 released today.
6
+
7
+ *Jon Atack*
8
+
9
+ * Enable scoping I18n by 'ransack.models'
10
+ ([#514](https://github.com/activerecord-hackery/ransack/pull/514)).
11
+
12
+ *nagyt234*
13
+
14
+ * Add ransacker arguments
15
+ ([#513](https://github.com/activerecord-hackery/ransack/pull/513)).
16
+
17
+ *Denis Tataurov*, *Jon Atack*
18
+
3
19
  ## Version 1.6.3 - 2015-01-21
4
20
 
5
21
  * Fix a regression
@@ -11,6 +27,12 @@
11
27
 
12
28
  *Nate Berkopec*, *Jon Atack*
13
29
 
30
+ * Update travis-ci to no longer test Rails 3.1 with Ruby 2.2 and speed up the test matrix.
31
+
32
+ * Refactor Nodes::Condition.
33
+
34
+ *Jon Atack*
35
+
14
36
 
15
37
  ## Version 1.6.2 - 2015-01-14
16
38
 
data/README.md CHANGED
@@ -27,8 +27,8 @@ instead.
27
27
  If you're viewing this at
28
28
  [github.com/activerecord-hackery/ransack](https://github.com/activerecord-hackery/ransack),
29
29
  you're reading the documentation for the master branch with the latest features.
30
- [View documentation for the last release (1.6.2).]
31
- (https://github.com/activerecord-hackery/ransack/tree/v1.6.2)
30
+ [View documentation for the last release (1.6.4).]
31
+ (https://github.com/activerecord-hackery/ransack/tree/v1.6.4)
32
32
 
33
33
  ## Getting started
34
34
 
@@ -632,6 +632,8 @@ en:
632
632
  end: ends with
633
633
  gt: greater than
634
634
  lt: less than
635
+ models:
636
+ person: Passanger
635
637
  attributes:
636
638
  person:
637
639
  name: Full Name
@@ -43,6 +43,7 @@ module Ransack
43
43
  def casted_array_with_in_predicate?(predicate)
44
44
  return unless defined?(Arel::Nodes::Casted)
45
45
  predicate.class == Arel::Nodes::In &&
46
+ predicate.right[0].respond_to?(:val) &&
46
47
  predicate.right[0].val.is_a?(Array)
47
48
  end
48
49
 
@@ -1,17 +1,36 @@
1
1
  require 'action_view'
2
2
 
3
- # This patch is needed since this Rails commit:
4
- # https://github.com/rails/rails/commit/c1a118a
5
- #
6
- # TODO: Find a better way to solve this.
3
+ # Monkey-patching, avert your eyes!
4
+ # TODO: Find a better way to solve these two issues:
7
5
  #
8
6
  module ActionView::Helpers::Tags
7
+
8
+ # This patch is needed since this Rails commit:
9
+ # https://github.com/rails/rails/commit/c1a118a
10
+ #
9
11
  class Base
10
12
  private
11
13
  def value(object)
12
14
  object.send @method_name if object # use send instead of public_send
13
15
  end
14
16
  end
17
+
18
+ # This patch is needed for Rails 4.2.1 and 5.0.0 since these commits:
19
+ #
20
+ # Rails 4.2:
21
+ # https://github.com/rails/rails/commits/4-2-stable/actionview/lib/action_view/helpers/tags/translator.rb
22
+ # https://github.com/rails/rails/commits/4-2-stable/actionview/lib/action_view/helpers/tags/placeholderable.rb
23
+ #
24
+ # Rails master:
25
+ # https://github.com/rails/rails/commits/master/actionview/lib/action_view/helpers/tags/translator.rb
26
+ # https://github.com/rails/rails/commits/master/actionview/lib/action_view/helpers/tags/placeholderable.rb
27
+ #
28
+ class Translator
29
+ def i18n_default
30
+ return '' unless model
31
+ ["#{model}.#{method_and_value}".to_sym, ""]
32
+ end
33
+ end
15
34
  end
16
35
 
17
36
  RANSACK_FORM_BUILDER = 'RANSACK_FORM_BUILDER'.freeze
@@ -3,14 +3,15 @@ module Ransack
3
3
  class Attribute < Node
4
4
  include Bindable
5
5
 
6
- attr_reader :name
6
+ attr_reader :name, :ransacker_args
7
7
 
8
8
  delegate :blank?, :present?, :==, :to => :name
9
9
  delegate :engine, :to => :context
10
10
 
11
- def initialize(context, name = nil)
11
+ def initialize(context, name = nil, ransacker_args = [])
12
12
  super(context)
13
13
  self.name = name unless name.blank?
14
+ @ransacker_args = ransacker_args
14
15
  end
15
16
 
16
17
  def name=(name)
@@ -66,7 +66,7 @@ module Ransack
66
66
  end
67
67
  when Hash
68
68
  args.each do |index, attrs|
69
- attr = Attribute.new(@context, attrs[:name])
69
+ attr = Attribute.new(@context, attrs[:name], attrs[:ransacker_args])
70
70
  self.attributes << attr if attr.valid?
71
71
  end
72
72
  else
@@ -59,7 +59,8 @@ module Ransack
59
59
 
60
60
  defaults =
61
61
  if key.blank?
62
- [:"#{context.klass.i18n_scope}.models.#{i18n_key(context.klass)}"]
62
+ [:"ransack.models.#{i18n_key(context.klass)}",
63
+ :"#{context.klass.i18n_scope}.models.#{i18n_key(context.klass)}"]
63
64
  else
64
65
  [:"ransack.associations.#{i18n_key(context.klass)}.#{key}"]
65
66
  end
@@ -1,3 +1,3 @@
1
1
  module Ransack
2
- VERSION = "1.6.3"
2
+ VERSION = "1.6.4"
3
3
  end
@@ -24,49 +24,49 @@ module Ransack
24
24
  end
25
25
 
26
26
  it "applies true scopes" do
27
- search = Person.ransack('active' => true)
28
- search.result.to_sql.should include "active = 1"
27
+ s = Person.ransack('active' => true)
28
+ s.result.to_sql.should include "active = 1"
29
29
  end
30
30
 
31
31
  it "applies stringy true scopes" do
32
- search = Person.ransack('active' => 'true')
33
- search.result.to_sql.should include "active = 1"
32
+ s = Person.ransack('active' => 'true')
33
+ s.result.to_sql.should include "active = 1"
34
34
  end
35
35
 
36
36
  it "applies stringy boolean scopes with true value in an array" do
37
- search = Person.ransack('of_age' => ['true'])
38
- search.result.to_sql.should include "age >= 18"
37
+ s = Person.ransack('of_age' => ['true'])
38
+ s.result.to_sql.should include "age >= 18"
39
39
  end
40
40
 
41
41
  it "applies stringy boolean scopes with false value in an array" do
42
- search = Person.ransack('of_age' => ['false'])
43
- search.result.to_sql.should include "age < 18"
42
+ s = Person.ransack('of_age' => ['false'])
43
+ s.result.to_sql.should include "age < 18"
44
44
  end
45
45
 
46
46
  it "ignores unlisted scopes" do
47
- search = Person.ransack('restricted' => true)
48
- search.result.to_sql.should_not include "restricted"
47
+ s = Person.ransack('restricted' => true)
48
+ s.result.to_sql.should_not include "restricted"
49
49
  end
50
50
 
51
51
  it "ignores false scopes" do
52
- search = Person.ransack('active' => false)
53
- search.result.to_sql.should_not include "active"
52
+ s = Person.ransack('active' => false)
53
+ s.result.to_sql.should_not include "active"
54
54
  end
55
55
 
56
56
  it "ignores stringy false scopes" do
57
- search = Person.ransack('active' => 'false')
58
- search.result.to_sql.should_not include "active"
57
+ s = Person.ransack('active' => 'false')
58
+ s.result.to_sql.should_not include "active"
59
59
  end
60
60
 
61
61
  it "passes values to scopes" do
62
- search = Person.ransack('over_age' => 18)
63
- search.result.to_sql.should include "age > 18"
62
+ s = Person.ransack('over_age' => 18)
63
+ s.result.to_sql.should include "age > 18"
64
64
  end
65
65
 
66
66
  it "chains scopes" do
67
- search = Person.ransack('over_age' => 18, 'active' => true)
68
- search.result.to_sql.should include "age > 18"
69
- search.result.to_sql.should include "active = 1"
67
+ s = Person.ransack('over_age' => 18, 'active' => true)
68
+ s.result.to_sql.should include "age > 18"
69
+ s.result.to_sql.should include "active = 1"
70
70
  end
71
71
  end
72
72
 
@@ -80,14 +80,6 @@ module Ransack
80
80
  end
81
81
  end
82
82
 
83
- context "search on an `in` predicate with an array" do
84
- it "should function correctly when passing an array of ids" do
85
- array = Person.all.map(&:id)
86
- s = Person.ransack(id_in: array)
87
- expect(s.result.count).to eq array.size
88
- end
89
- end
90
-
91
83
  describe '#ransacker' do
92
84
  # For infix tests
93
85
  def self.sane_adapter?
@@ -181,17 +173,32 @@ module Ransack
181
173
  expect(s.result.to_a).to eq [p]
182
174
  end
183
175
 
184
- context "search on an `in` predicate with an array to a ransacker" do
176
+ context "searching on an `in` predicate with a ransacker" do
185
177
  it "should function correctly when passing an array of ids" do
186
178
  s = Person.ransack(array_users_in: true)
187
179
  expect(s.result.count).to be > 0
188
180
  end
189
181
 
190
182
  it "should function correctly when passing an array of strings" do
191
- p = Person.create!(name: Person.first.id.to_s)
183
+ Person.create!(name: Person.first.id.to_s)
192
184
  s = Person.ransack(array_names_in: true)
193
185
  expect(s.result.count).to be > 0
194
186
  end
187
+
188
+ it 'should function correctly with an Arel SqlLiteral' do
189
+ s = Person.ransack(sql_literal_id_in: 1)
190
+ expect(s.result.count).to be 1
191
+ s = Person.ransack(sql_literal_id_in: ['2', 4, '5', 8])
192
+ expect(s.result.count).to be 4
193
+ end
194
+ end
195
+
196
+ context "search on an `in` predicate with an array" do
197
+ it "should function correctly when passing an array of ids" do
198
+ array = Person.all.map(&:id)
199
+ s = Person.ransack(id_in: array)
200
+ expect(s.result.count).to eq array.size
201
+ end
195
202
  end
196
203
 
197
204
  it "should function correctly when an attribute name ends with '_start'" do
@@ -306,6 +313,26 @@ module Ransack
306
313
  quote_column_name("only_admin")} = 'htimS cirA'/
307
314
  )
308
315
  end
316
+
317
+ it 'should allow passing ransacker arguments to a ransacker' do
318
+ s = Person.ransack(
319
+ c: [{
320
+ a: {
321
+ '0' => {
322
+ name: 'with_passed_arguments', ransacker_args: [10, 100]
323
+ }
324
+ },
325
+ p: 'cont',
326
+ v: ['Rails has been released']
327
+ }]
328
+ )
329
+ expect(s.result.to_sql).to match(
330
+ /CHAR_LENGTH\(articles.body\) BETWEEN 10 AND 100/
331
+ )
332
+ expect(s.result.to_sql).to match(
333
+ /LIKE \'\%Rails has been released\%\'/
334
+ )
335
+ end
309
336
  end
310
337
 
311
338
  describe '#ransackable_attributes' do
@@ -2,7 +2,6 @@ require 'spec_helper'
2
2
 
3
3
  module Ransack
4
4
  module Nodes
5
-
6
5
  describe Grouping do
7
6
 
8
7
  before do
@@ -1,6 +1,9 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  module Ransack
4
+ TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE'].to_set
5
+ FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE'].to_set
6
+
4
7
  describe Predicate do
5
8
 
6
9
  before do
@@ -20,18 +23,12 @@ module Ransack
20
23
  end
21
24
 
22
25
  describe 'eq' do
23
- it 'generates an equality condition for boolean true' do
24
- @s.awesome_eq = true
25
- field = "#{quote_table_name("people")}.#{quote_column_name("awesome")}"
26
- expect(@s.result.to_sql).to match /#{field} = #{
27
- ActiveRecord::Base.connection.quoted_true}/
26
+ it 'generates an equality condition for boolean true values' do
27
+ test_boolean_equality_for(true)
28
28
  end
29
29
 
30
- it 'generates an equality condition for boolean false' do
31
- @s.awesome_eq = false
32
- field = "#{quote_table_name("people")}.#{quote_column_name("awesome")}"
33
- expect(@s.result.to_sql).to match /#{field} = #{
34
- ActiveRecord::Base.connection.quoted_false}/
30
+ it 'generates an equality condition for boolean false values' do
31
+ test_boolean_equality_for(false)
35
32
  end
36
33
 
37
34
  it 'does not generate a condition for nil' do
@@ -361,5 +358,31 @@ module Ransack
361
358
  expect(@s.result.to_sql).to match /#{field} IS NOT NULL AND #{field} != ''/
362
359
  end
363
360
  end
364
- end
361
+
362
+ private
363
+
364
+ def test_boolean_equality_for(boolean_value)
365
+ query = expected_query(boolean_value)
366
+ test_values_for(boolean_value).each do |value|
367
+ s = Search.new(Person, awesome_eq: value)
368
+ expect(s.result.to_sql).to match query
369
+ end
370
+ end
371
+
372
+ def test_values_for(boolean_value)
373
+ case boolean_value
374
+ when true
375
+ TRUE_VALUES
376
+ when false
377
+ FALSE_VALUES
378
+ end
379
+ end
380
+
381
+ def expected_query(value, attribute = 'awesome', operator = '=')
382
+ field = "#{quote_table_name("people")}.#{quote_column_name(attribute)}"
383
+ quoted_value = ActiveRecord::Base.connection.quote(value)
384
+ /#{field} #{operator} #{quoted_value}/
385
+ end
386
+ end
387
+
365
388
  end
@@ -63,6 +63,23 @@ class Person < ActiveRecord::Base
63
63
  )
64
64
  end
65
65
 
66
+ ransacker :sql_literal_id do
67
+ Arel.sql('people.id')
68
+ end
69
+
70
+ ransacker :with_passed_arguments, args: [:parent, :ransacker_args] do |parent, args|
71
+ min_body, max_body = args
72
+ sql = <<-SQL
73
+ (SELECT MAX(articles.title)
74
+ FROM articles
75
+ WHERE articles.person_id = people.id
76
+ AND CHAR_LENGTH(articles.body) BETWEEN #{min_body} AND #{max_body}
77
+ GROUP BY articles.person_id
78
+ )
79
+ SQL
80
+ Arel.sql(sql.squish)
81
+ end
82
+
66
83
  def self.ransackable_attributes(auth_object = nil)
67
84
  if auth_object == :admin
68
85
  column_names + _ransackers.keys - ['only_sort']
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.6.3
4
+ version: 1.6.4
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: 2015-01-21 00:00:00.000000000 Z
13
+ date: 2015-03-20 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: actionpack
@@ -310,7 +310,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
310
310
  version: '0'
311
311
  requirements: []
312
312
  rubyforge_project: ransack
313
- rubygems_version: 2.4.5
313
+ rubygems_version: 2.4.6
314
314
  signing_key:
315
315
  specification_version: 4
316
316
  summary: Object-based searching for ActiveRecord (currently).