where_chain 0.2.3 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 00b02560557c167de9ce11d97c1d0c9184579c753ca0e8b735d9b1734cc77317
4
- data.tar.gz: 38dd58fcefd3dc9975b984b3742c558f74a59e813fb0f72a6dea6548ddeed0c2
3
+ metadata.gz: bc7e391acb640aba967c22926907bc95c610bba037ff38f1890274d751797d50
4
+ data.tar.gz: ff165ee3f9bda32f0625fc8214f7cfba1f335b273260b008d2e33a26be5505ac
5
5
  SHA512:
6
- metadata.gz: 2e19f7b633157d5a84a43c28b084cf2e2fc29e19d1a61fae14cf6951bf7619f751287099656ebd2b8504e79072a9a405735e1bece3d221d5622ef944a701f80f
7
- data.tar.gz: 5e5b9cb231931d9949126eed5f365d69f6c1c9eccae6b87cc8e05d0a544d24373dd95d4630be5aef9a6a9ea3ef12d24f56dc3be5a7d05d30e1557486bd58a733
6
+ metadata.gz: 77ab8fd3e5f1f8f9aebaa880a420c72200a789cef67c5e5c1da603fa34b8d37b6f76e6a5e8be21381e6de4cc8a036c6a482baf9fe412e09339238de3610e7601
7
+ data.tar.gz: 7fd1435df353db94e480285f02451f75168486f7d24a3bd04c357cbe4a7de75cd79d226d1b9c8136344e9c246d43fc8e67620cddb4a4866d4aa2f7906e45b399
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [![Gem Version](https://badge.fury.io/rb/where_chain.svg)](https://badge.fury.io/rb/where_chain) [![CircleCI](https://circleci.com/gh/marcinruszkiewicz/where_chain.svg?style=shield)](https://circleci.com/gh/marcinruszkiewicz/where_chain)
1
+ [![Gem Version](https://badge.fury.io/rb/where_chain.svg)](https://badge.fury.io/rb/where_chain) [![TravisCI](https://travis-ci.com/marcinruszkiewicz/where_chain.svg?branch=master)](https://travis-ci.com/marcinruszkiewicz/where_chain)
2
2
 
3
3
  # WhereChain
4
4
 
@@ -6,11 +6,11 @@ In Rails, we usually use Active Record, which allows us to escape from writing S
6
6
 
7
7
  In the older versions, you also had to write `Post.where('name IS NOT null')` to do a negation. Rails 4.0 added a class called `WhereChain` that added some [new possibilities](https://github.com/rails/rails/commit/de75af7acc5c05c708443de40e78965925165217), one of which was a `not` method. The proper way to write became `Post.where.not(name: nil)` instead.
8
8
 
9
- Within the same comment there were also two new methods that [didn't survive to the release of Rails 4.0](https://github.com/rails/rails/commit/8d02afeaee8993bd0fde69687fdd9bf30921e805) - `.like` and `.not_like`. As you can read in this commit discussion, there has been work made to bring them back, like the [activerecord-like](https://github.com/ReneB/activerecord-like) gem or [Squeel](https://github.com/activerecord-hackery/squeel), but these has their own problems - activerecord-like only adds `.like` and `.not_like` back and the latest version is locked to Active Record 5; and Squeel provides a whole new query DSL, which not everyone will like. There was actually a pull request adding `.gt` and other [inequality methods](https://github.com/rails/rails/pull/8453), which was closed even faster than the first one.
9
+ Within the same commit there were also two new methods that [didn't survive to the release of Rails 4.0](https://github.com/rails/rails/commit/8d02afeaee8993bd0fde69687fdd9bf30921e805) - `.like` and `.not_like`. As you can read in this commit discussion, there has been work made to bring them back, like the [activerecord-like](https://github.com/ReneB/activerecord-like) gem or [Squeel](https://github.com/activerecord-hackery/squeel), but these has their own problems - activerecord-like only adds `.like` and `.not_like` back and the latest version is locked to Active Record 5; and Squeel provides a whole new query DSL, which not everyone will like. There was actually a pull request adding `.gt` and other [inequality methods](https://github.com/rails/rails/pull/8453), which was closed even faster than the first one.
10
10
 
11
11
  This gem brings these two methods back and extends WhereChain with additional methods: `.gt`, `.gte`, `.lt` and `.lte`, so that by using it you can replace the SQL strings like `Post.where('comments > 5')` with `Post.where.gt(comments: 5)`.
12
12
 
13
- WhereChain depends on the Active Record gem in a version higher than 4.2, due to problems with Ruby versions lesser than 2.4. Rails 4.2 is already the version that's being maintained, so you probably should not use an earlier one anyway. The gem is tested on the latest Ruby and all current Rails versions - 4.2, 5.0, 5.1 and 5.2.
13
+ WhereChain depends on the Active Record gem in a version higher than 4.2, due to problems with Ruby versions lesser than 2.4. Rails 4.2 is already the version that's being maintained, so you probably should not use an earlier one anyway. The gem is tested on the latest Ruby and all current Rails versions - 4.2, 5.0, 5.1, 5.2 and 6.0.
14
14
 
15
15
  ## Usage
16
16
 
@@ -84,6 +84,12 @@ $ gem install where_chain
84
84
 
85
85
  ## Testing the code
86
86
 
87
+ Before running any test, you should finish installing the appraised gems:
88
+
89
+ ```bash
90
+ $ bundle exec appraisal install
91
+ ```
92
+
87
93
  Running all the tests for all Rails version targets:
88
94
 
89
95
  ```bash
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  begin
2
4
  require 'bundler/setup'
3
5
  rescue LoadError
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module QueryMethods
3
5
  class WhereChain
@@ -34,7 +36,7 @@ module ActiveRecord
34
36
  end
35
37
  end
36
38
 
37
- @scope.references!(PredicateBuilder.references(opts)) if Hash === opts
39
+ @scope.references!(PredicateBuilder.references(opts)) if opts.is_a?(Hash)
38
40
  @scope.where_values += where_value
39
41
  @scope
40
42
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  # Ruby complains about missing the class if we try to patch it without it
3
5
  class Relation
@@ -6,6 +8,7 @@ module ActiveRecord
6
8
 
7
9
  module QueryMethods
8
10
  class WhereChain
11
+ include ActiveModel::ForbiddenAttributesProtection
9
12
  include WhereChainSharedMethods
10
13
 
11
14
  # Returns a new relation expressing WHERE + NOT condition
@@ -21,16 +24,16 @@ module ActiveRecord
21
24
  #
22
25
  # Note: This is the Active Record 5 version.
23
26
  def not(opts = :chain, *rest)
24
- if :chain == opts
27
+ if opts == :chain
25
28
  @invert = true
26
29
  return self
27
30
  end
28
31
 
29
32
  opts = sanitize_forbidden_attributes(opts)
30
33
 
31
- where_clause = @scope.send(:where_clause_factory).build(opts, rest)
34
+ where_clause = where_clause_factory_build(opts, rest)
32
35
 
33
- @scope.references!(PredicateBuilder.references(opts)) if Hash === opts
36
+ @scope.references!(PredicateBuilder.references(opts.stringify_keys)) if opts.is_a?(Hash)
34
37
  @scope.where_clause += where_clause.invert
35
38
  @scope
36
39
  end
@@ -44,16 +47,16 @@ module ActiveRecord
44
47
  def prepare_where(node_type, infix, opts, rest)
45
48
  @scope.tap do |s|
46
49
  opts.each_pair do |key, value|
47
- equal_where_clause = s.send(:where_clause_factory).build({ key => value }, rest)
50
+ equal_where_clause = where_clause_factory_build({ key => value }, rest)
48
51
  equal_where_clause_predicate = equal_where_clause.send(:predicates).first
49
52
 
50
53
  new_predicate = arel_node(node_type, infix, equal_where_clause_predicate)
51
54
  new_where_clause = build_where_clause(new_predicate, equal_where_clause)
52
- if @invert
53
- s.where_clause += new_where_clause.invert
54
- else
55
- s.where_clause += new_where_clause
56
- end
55
+ s.where_clause += if @invert
56
+ new_where_clause.invert
57
+ else
58
+ new_where_clause
59
+ end
57
60
  end
58
61
  end
59
62
  end
@@ -67,6 +70,16 @@ module ActiveRecord
67
70
  Relation::WhereClause.new([new_predicate])
68
71
  end
69
72
  end
73
+
74
+ # Active Record 6.1 removed the where_clause_factory.build method
75
+ # method was replaced by build_where_clause
76
+ def where_clause_factory_build(opts, rest = [])
77
+ if ActiveRecord.version.release >= Gem::Version.new('6.1.0')
78
+ @scope.send(:build_where_clause, opts, rest)
79
+ else
80
+ @scope.send(:where_clause_factory).build(opts, rest)
81
+ end
82
+ end
70
83
  end
71
84
  end
72
85
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module WhereChainSharedMethods
3
5
  extend ActiveSupport::Concern
@@ -5,7 +7,7 @@ module ActiveRecord
5
7
  included do
6
8
  # Initialize the chain with a scope and a toggle to invert the generated
7
9
  # where statement
8
- def initialize(scope, invert=false)
10
+ def initialize(scope, invert = false)
9
11
  @scope = scope
10
12
  @invert = invert
11
13
  end
@@ -30,7 +32,7 @@ module ActiveRecord
30
32
  def unlike(opts, *rest)
31
33
  prepare_where(Arel::Nodes::DoesNotMatch, nil, opts, rest)
32
34
  end
33
- alias not_like unlike
35
+ alias_method :not_like, :unlike
34
36
 
35
37
  # Returns a new relation expressing a greater than condition in WHERE
36
38
  # according to the conditions in an argument hash
@@ -79,7 +81,7 @@ module ActiveRecord
79
81
  def ensure_proper_attributes(opts)
80
82
  raise ArgumentError, 'This method requires a Hash as an argument.' unless opts.is_a?(Hash)
81
83
 
82
- opts.each_pair do |key, value|
84
+ opts.each_pair do |_key, value|
83
85
  if value.is_a?(Hash) || value.is_a?(Array)
84
86
  raise ArgumentError, 'The value passed to this method should be a valid type.'
85
87
  end
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_record/where_chain_shared_methods'
2
4
 
3
5
  # Active Record changed a lot in Rails 5 and 4 is still supported
4
6
  if ActiveRecord::VERSION::MAJOR == 4
5
7
  require 'active_record/where_chain_extensions_rails4'
6
- elsif ActiveRecord::VERSION::MAJOR == 5
8
+ elsif ActiveRecord::VERSION::MAJOR == 5 || ActiveRecord::VERSION::MAJOR == 6
7
9
  require 'active_record/where_chain_extensions_rails5'
8
10
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module WhereChain
2
- VERSION = '0.2.3'
4
+ VERSION = '0.4.0'
3
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: where_chain
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcin Ruszkiewicz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-05 00:00:00.000000000 Z
11
+ date: 2021-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -25,7 +25,7 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '4.2'
27
27
  - !ruby/object:Gem::Dependency
28
- name: sqlite3
28
+ name: appraisal
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: pry-rails
42
+ name: database_cleaner
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -53,7 +53,7 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: rspec-rails
56
+ name: factory_bot_rails
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
@@ -67,7 +67,7 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: factory_bot_rails
70
+ name: pry-rails
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
@@ -81,7 +81,7 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: appraisal
84
+ name: rspec-rails
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ">="
@@ -95,7 +95,7 @@ dependencies:
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
- name: database_cleaner
98
+ name: rspec_junit_formatter
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - ">="
@@ -109,7 +109,7 @@ dependencies:
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  - !ruby/object:Gem::Dependency
112
- name: rspec_junit_formatter
112
+ name: rubocop
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - ">="
@@ -122,9 +122,24 @@ dependencies:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
- description: 'This is a Rails plugin that extends Active Record with additional methods:
126
- .like, .unlike, .gt, .gte, .lt and .lte, so that you can replace the SQL strings
127
- like Post.where(''comments > 5'') with Post.where.gt(comments: 5)'
125
+ - !ruby/object:Gem::Dependency
126
+ name: sqlite3
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 1.3.0
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 1.3.0
139
+ description: |
140
+ This is a Rails plugin that extends Active Record with additional methods: .like, .unlike,
141
+ .gt, .gte, .lt and .lte, so that you can replace the SQL strings like
142
+ Post.where('comments > 5') with Post.where.gt(comments: 5)
128
143
  email:
129
144
  - marcin.ruszkiewicz@polcode.net
130
145
  executables: []
@@ -158,8 +173,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
158
173
  - !ruby/object:Gem::Version
159
174
  version: '0'
160
175
  requirements: []
161
- rubyforge_project:
162
- rubygems_version: 2.7.6
176
+ rubygems_version: 3.0.3
163
177
  signing_key:
164
178
  specification_version: 4
165
179
  summary: 'WhereChain extensions - Model.where.lt(created_at: Date.today)'