where_chain 0.2.2 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +29 -6
- data/Rakefile +2 -0
- data/lib/active_record/where_chain_extensions_rails4.rb +3 -1
- data/lib/active_record/where_chain_extensions_rails5.rb +9 -7
- data/lib/active_record/where_chain_shared_methods.rb +5 -3
- data/lib/where_chain.rb +3 -1
- data/lib/where_chain/version.rb +3 -1
- metadata +29 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f908efafd89e45ee67d4051326d7998aee4dea9d008e6e95a2dc23064754f12f
|
4
|
+
data.tar.gz: 368905be74dfd90a3eb62cf190b06a800e1764c235870364cf2c79eb243fde92
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2c71031c54d4e19143165b9ea03f12b548dd1bf3850649a8937a3c376f36168559d359f93a1a250fc61a7ba0a8e22b85a69e8af34d69eaf3d29f01fb4aa991e2
|
7
|
+
data.tar.gz: 65f15b0f747953b251b4f48c87f84d4eda3064e739d575ea4fa02de81093d05f1efa4b884340a466e020965c8fe8031fe0269a429dce46b7cff163318852729f
|
data/README.md
CHANGED
@@ -1,16 +1,16 @@
|
|
1
|
-
[![Gem Version](https://badge.fury.io/rb/where_chain.svg)](https://badge.fury.io/rb/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
|
|
5
|
-
In Rails, we usually use Active Record, which allows us to escape from writing SQL code like this `SELECT * FROM posts WHERE posts.name = 'Foo'` and allows us to write `Post.where(name: 'Foo')` instead. However, this has always been limited to matching equality, so you still have to write `Post.where('comments > ?', 5)` to get Posts that have more than 5 comments.
|
5
|
+
In Rails, we usually use Active Record, which allows us to escape from writing SQL code like this `SELECT * FROM posts WHERE posts.name = 'Foo'` and allows us to write `Post.where(name: 'Foo')` instead. However, this has always been limited to matching equality, so you still have to write `Post.where('comments > ?', 5)` to get Posts that have more than 5 comments.
|
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
|
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
|
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
|
|
@@ -51,7 +51,7 @@ Post.where.gt(comments: { bad: :thing })
|
|
51
51
|
ArgumentError: The value passed to this method should be a valid type.
|
52
52
|
|
53
53
|
Post.where.gt('comments > ?', 5)
|
54
|
-
Post.where.gt([{number: 5}, 'name > ?'], 'abc')
|
54
|
+
Post.where.gt([{ number: 5 }, 'name > ?'], 'abc')
|
55
55
|
|
56
56
|
ArgumentError: This method requires a Hash as an argument.
|
57
57
|
```
|
@@ -79,7 +79,30 @@ $ gem install where_chain
|
|
79
79
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
80
80
|
3. Commit your changes (`git commit -am 'Added some feature'`)
|
81
81
|
4. Push to the branch (`git push origin my-new-feature`)
|
82
|
-
5.
|
82
|
+
5. Make sure all the tests pass on all versions
|
83
|
+
6. Create new Pull Request
|
84
|
+
|
85
|
+
## Testing the code
|
86
|
+
|
87
|
+
Before running any test, you should finish installing the appraised gems:
|
88
|
+
|
89
|
+
```bash
|
90
|
+
$ bundle exec appraisal install
|
91
|
+
```
|
92
|
+
|
93
|
+
Running all the tests for all Rails version targets:
|
94
|
+
|
95
|
+
```bash
|
96
|
+
$ bundle exec appraisal rspec
|
97
|
+
```
|
98
|
+
|
99
|
+
Running tests for a specific version:
|
100
|
+
|
101
|
+
```bash
|
102
|
+
$ bundle exec appraisal 5.0 rspec
|
103
|
+
```
|
104
|
+
|
105
|
+
You can see all the targets to use instead of `5.0` in the `Appraisals` file.
|
83
106
|
|
84
107
|
## License
|
85
108
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
CHANGED
@@ -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
|
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
|
@@ -21,7 +23,7 @@ module ActiveRecord
|
|
21
23
|
#
|
22
24
|
# Note: This is the Active Record 5 version.
|
23
25
|
def not(opts = :chain, *rest)
|
24
|
-
if
|
26
|
+
if opts == :chain
|
25
27
|
@invert = true
|
26
28
|
return self
|
27
29
|
end
|
@@ -30,7 +32,7 @@ module ActiveRecord
|
|
30
32
|
|
31
33
|
where_clause = @scope.send(:where_clause_factory).build(opts, rest)
|
32
34
|
|
33
|
-
@scope.references!(PredicateBuilder.references(opts)) if Hash
|
35
|
+
@scope.references!(PredicateBuilder.references(opts)) if opts.is_a?(Hash)
|
34
36
|
@scope.where_clause += where_clause.invert
|
35
37
|
@scope
|
36
38
|
end
|
@@ -49,11 +51,11 @@ module ActiveRecord
|
|
49
51
|
|
50
52
|
new_predicate = arel_node(node_type, infix, equal_where_clause_predicate)
|
51
53
|
new_where_clause = build_where_clause(new_predicate, equal_where_clause)
|
52
|
-
if @invert
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
54
|
+
s.where_clause += if @invert
|
55
|
+
new_where_clause.invert
|
56
|
+
else
|
57
|
+
new_where_clause
|
58
|
+
end
|
57
59
|
end
|
58
60
|
end
|
59
61
|
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
|
-
|
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 |
|
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
|
data/lib/where_chain.rb
CHANGED
@@ -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
|
data/lib/where_chain/version.rb
CHANGED
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.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marcin Ruszkiewicz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-06-25 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:
|
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:
|
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:
|
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:
|
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:
|
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:
|
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:
|
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
|
-
|
126
|
-
|
127
|
-
|
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: []
|
@@ -139,7 +154,7 @@ files:
|
|
139
154
|
- lib/active_record/where_chain_shared_methods.rb
|
140
155
|
- lib/where_chain.rb
|
141
156
|
- lib/where_chain/version.rb
|
142
|
-
homepage: https://github.com/marcinruszkiewicz
|
157
|
+
homepage: https://github.com/marcinruszkiewicz/where_chain
|
143
158
|
licenses:
|
144
159
|
- MIT
|
145
160
|
metadata: {}
|
@@ -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
|
-
|
162
|
-
rubygems_version: 2.7.3
|
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)'
|