where_chain 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9056c05e208067983f91e1bcb9343cd8c1e43a924351cfcf0092efc012a806fa
4
+ data.tar.gz: 6f9b610d7b560e4d457e79d5448d5afd849924d6b0c1a71a3228e55d0ad62e54
5
+ SHA512:
6
+ metadata.gz: 5338073a0179b6084b87211d915542500b4064552c0f536c5f0f78ae39ce6abb18914c60d87bc5636215da77c822877d0004eb46076d5ecc362b1f099d874224
7
+ data.tar.gz: ba3d5820c1d7384918e1eb2744113fbef2d4f75f797d6316cdf27cea8ec316c86b88e0fa811a73bb70d075b9e5e062d2865e05df5c5df12d33ec049a7b596dec
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2018 Marcin Ruszkiewicz
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,85 @@
1
+ [![CircleCI](https://circleci.com/gh/marcinruszkiewicz/where_chain.svg?style=svg)](https://circleci.com/gh/marcinruszkiewicz/where_chain)
2
+
3
+ # WhereChain
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.
6
+
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
+
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.
10
+
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
+
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 RC 1.
14
+
15
+ ## Usage
16
+
17
+ Some examples of using the gem and what can be replaced with it:
18
+
19
+ | Rails SQL string | with WhereChain |
20
+ |------------|-----------------|
21
+ |`Post.where('comments > ?', 5)` | `Post.where.gt(comments: 5)` |
22
+ |`Post.where('comments >= ?', 5)` | `Post.where.gte(comments: 5)` |
23
+ |`Post.where('comments < ?', 5)` | `Post.where.lt(comments: 5)` |
24
+ |`Post.where('comments <= ?', 5)` | `Post.where.lte(comments: 5)` |
25
+ |`Post.where('name LIKE ?', "%foo%")` | `Post.where.like(name: "%foo%")` |
26
+ |`Post.where('name NOT LIKE ?', "%foo%")` | `Post.where.unlike(name: "%foo%")` |
27
+ |`Post.where('comments > ? AND shares > ?', 5, 10)` | `Post.where.gt({ comments: 5, shares: 10 })` |
28
+ |`Post.where('comments > ? OR shares > ?', 5, 10)` | `Post.where.gt(comments: 5).or(Post.where.gt(shares: 10))` |
29
+
30
+ You can now also chain the methods in your Active Record queries:
31
+
32
+ ```ruby
33
+ Post.where.not.gt(comments: 5).where.like(name: '%foo%')
34
+ ```
35
+
36
+ This, however, will **NOT** work at all:
37
+
38
+ ```ruby
39
+ Post.gt(comments: 5).like(name: '%foo%')
40
+ Post.where.gt(comments: 5).lt(comments: 10)
41
+ ```
42
+
43
+ You need to prepend each of the new methods with either `.where` or `.where.not` for them to work.
44
+
45
+ All the methods accept a Hash attribute and can compare all proper types as values, except Arrays and Hashes. These will **not** work and will raise an ArgumentError exception:
46
+
47
+ ```ruby
48
+ Post.where.gt(comments: [1, 2, 3])
49
+ Post.where.gt(comments: { bad: :thing })
50
+
51
+ ArgumentError: The value passed to this method should be a valid type.
52
+
53
+ Post.where.gt('comments > ?', 5)
54
+ Post.where.gt([{number: 5}, 'name > ?'], 'abc')
55
+
56
+ ArgumentError: This method requires a Hash as an argument.
57
+ ```
58
+
59
+ ## Installation
60
+ Add this line to your application's Gemfile:
61
+
62
+ ```ruby
63
+ gem 'where_chain'
64
+ ```
65
+
66
+ And then execute:
67
+ ```bash
68
+ $ bundle
69
+ ```
70
+
71
+ Or install it yourself as:
72
+ ```bash
73
+ $ gem install where_chain
74
+ ```
75
+
76
+ ## Contributing
77
+
78
+ 1. Fork it
79
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
80
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
81
+ 4. Push to the branch (`git push origin my-new-feature`)
82
+ 5. Create new Pull Request
83
+
84
+ ## License
85
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'WhereChain'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
@@ -0,0 +1,46 @@
1
+ module ActiveRecord
2
+ module QueryMethods
3
+ class WhereChain
4
+ include WhereChainSharedMethods
5
+
6
+ # if passed nothing, default to chaining further
7
+ def not(opts = :chain, *rest)
8
+ where_value = @scope.send(:build_where, opts, rest).map do |rel|
9
+ case rel
10
+ when :chain
11
+ @invert = true
12
+ return self
13
+ when NilClass
14
+ raise ArgumentError, 'Invalid argument for .where.not(), got nil.'
15
+ when Arel::Nodes::In
16
+ Arel::Nodes::NotIn.new(rel.left, rel.right)
17
+ when Arel::Nodes::Equality
18
+ Arel::Nodes::NotEqual.new(rel.left, rel.right)
19
+ when String
20
+ Arel::Nodes::Not.new(Arel::Nodes::SqlLiteral.new(rel))
21
+ else
22
+ Arel::Nodes::Not.new(rel)
23
+ end
24
+ end
25
+
26
+ @scope.references!(PredicateBuilder.references(opts)) if Hash === opts
27
+ @scope.where_values += where_value
28
+ @scope
29
+ end
30
+
31
+ private
32
+
33
+ def prepare_where(node_type, infix, opts, *rest)
34
+ where_value = @scope.send(:build_where, opts, rest).map do |rel|
35
+ if @invert
36
+ Arel::Nodes::Not.new arel_node(node_type, infix, rel)
37
+ else
38
+ arel_node(node_type, infix, rel)
39
+ end
40
+ end
41
+ @scope.where_values += where_value
42
+ @scope
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,53 @@
1
+ module ActiveRecord
2
+ class Relation
3
+ class QueryMethods; end
4
+ end
5
+
6
+ module QueryMethods
7
+ class WhereChain
8
+ include WhereChainSharedMethods
9
+
10
+ def not(opts = :chain, *rest)
11
+ if :chain == opts
12
+ @invert = true
13
+ return self
14
+ end
15
+
16
+ opts = sanitize_forbidden_attributes(opts)
17
+
18
+ where_clause = @scope.send(:where_clause_factory).build(opts, rest)
19
+
20
+ @scope.references!(PredicateBuilder.references(opts)) if Hash === opts
21
+ @scope.where_clause += where_clause.invert
22
+ @scope
23
+ end
24
+
25
+ private
26
+
27
+ def prepare_where(node_type, infix, opts, rest)
28
+ @scope.tap do |s|
29
+ opts.each_pair do |key, value|
30
+ equal_where_clause = s.send(:where_clause_factory).build({ key => value }, rest)
31
+ equal_where_clause_predicate = equal_where_clause.send(:predicates).first
32
+
33
+ new_predicate = arel_node(node_type, infix, equal_where_clause_predicate)
34
+ new_where_clause = build_where_clause(new_predicate, equal_where_clause)
35
+ if @invert
36
+ s.where_clause += new_where_clause.invert
37
+ else
38
+ s.where_clause += new_where_clause
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ def build_where_clause(new_predicate, old_where_clause)
45
+ if old_where_clause.respond_to?(:binds)
46
+ Relation::WhereClause.new([new_predicate], old_where_clause.binds)
47
+ else
48
+ Relation::WhereClause.new([new_predicate])
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,71 @@
1
+ module ActiveRecord
2
+ module WhereChainSharedMethods
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ def initialize(scope, invert=false)
7
+ @scope = scope
8
+ @invert = invert
9
+ end
10
+
11
+ # Returns a new relation expressing WHERE + LIKE condition
12
+ # according to the conditions provided as a hash in the arguments.
13
+ #
14
+ # Book.where.like(title: "Rails%")
15
+ # # SELECT * FROM books WHERE title LIKE 'Rails%'
16
+ def like(opts, *rest)
17
+ prepare_where(Arel::Nodes::Matches, nil, opts, rest)
18
+ end
19
+
20
+ # Returns a new relation expressing WHERE + NOT LIKE condition
21
+ # according to the conditions provided as a hash in the arguments.
22
+ #
23
+ # Conference.where.not_like(name: "%Kaigi")
24
+ # # SELECT * FROM conferences WHERE name NOT LIKE '%Kaigi'
25
+ def unlike(opts, *rest)
26
+ prepare_where(Arel::Nodes::DoesNotMatch, nil, opts, rest)
27
+ end
28
+ alias not_like unlike # maintain compatibility with activerecord-like gem
29
+
30
+ def gt(opts, *rest)
31
+ ensure_proper_attributes(opts)
32
+ prepare_where(Arel::Nodes::InfixOperation, '>', opts, rest)
33
+ end
34
+
35
+ def gte(opts, *rest)
36
+ ensure_proper_attributes(opts)
37
+ prepare_where(Arel::Nodes::InfixOperation, '>=', opts, rest)
38
+ end
39
+
40
+ def lt(opts, *rest)
41
+ ensure_proper_attributes(opts)
42
+ prepare_where(Arel::Nodes::InfixOperation, '<', opts, rest)
43
+ end
44
+
45
+ def lte(opts, *rest)
46
+ ensure_proper_attributes(opts)
47
+ prepare_where(Arel::Nodes::InfixOperation, '<=', opts, rest)
48
+ end
49
+
50
+ private
51
+
52
+ def ensure_proper_attributes(opts)
53
+ raise ArgumentError, 'This method requires a Hash as an argument.' unless opts.is_a?(Hash)
54
+
55
+ opts.each_pair do |key, value|
56
+ if value.is_a?(Hash) || value.is_a?(Array)
57
+ raise ArgumentError, 'The value passed to this method should be a valid type.'
58
+ end
59
+ end
60
+ end
61
+
62
+ def arel_node(node_type, infix, rel)
63
+ if infix.present?
64
+ node_type.new(infix, rel.left, rel.right)
65
+ else
66
+ node_type.new(rel.left, rel.right)
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,7 @@
1
+ require 'active_record/where_chain_shared_methods'
2
+
3
+ if ActiveRecord::VERSION::MAJOR == 4
4
+ require 'active_record/where_chain_extensions_rails4'
5
+ elsif ActiveRecord::VERSION::MAJOR == 5
6
+ require 'active_record/where_chain_extensions_rails5'
7
+ end
@@ -0,0 +1,3 @@
1
+ module WhereChain
2
+ VERSION = '0.2.0'
3
+ end
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: where_chain
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Marcin Ruszkiewicz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-03-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: factory_bot_rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: appraisal
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: database_cleaner
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec_junit_formatter
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: 'WhereChain is a Rails plugin that provides extensions for ActiveRecord.
126
+ Since Rails 4 you can do Model.where.not(name: ''Bad'') and this module adds '
127
+ email:
128
+ - marcin.ruszkiewicz@polcode.net
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - MIT-LICENSE
134
+ - README.md
135
+ - Rakefile
136
+ - lib/active_record/where_chain_extensions_rails4.rb
137
+ - lib/active_record/where_chain_extensions_rails5.rb
138
+ - lib/active_record/where_chain_shared_methods.rb
139
+ - lib/where_chain.rb
140
+ - lib/where_chain/version.rb
141
+ homepage: https://github.com/marcinruszkiewicz
142
+ licenses:
143
+ - MIT
144
+ metadata: {}
145
+ post_install_message:
146
+ rdoc_options: []
147
+ require_paths:
148
+ - lib
149
+ required_ruby_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ required_rubygems_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ requirements: []
160
+ rubyforge_project:
161
+ rubygems_version: 2.7.3
162
+ signing_key:
163
+ specification_version: 4
164
+ summary: 'WhereChain extensions - Model.where.lt(created_at: Date.today)'
165
+ test_files: []