where_chain 0.2.0 → 0.2.1
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 +4 -4
- data/README.md +1 -1
- data/lib/active_record/where_chain_extensions_rails4.rb +17 -2
- data/lib/active_record/where_chain_extensions_rails5.rb +22 -3
- data/lib/active_record/where_chain_shared_methods.rb +36 -8
- data/lib/where_chain.rb +2 -1
- data/lib/where_chain/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 581e86507fb7660511dc6a80f900fe2d5318550cede75a424d2191f807b674ac
|
4
|
+
data.tar.gz: 33949deb16c6b373690b51100894b03dd8cc508a931f58d31f3c0e5420103d0e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a05d9262e5fbcded7b9bf93d3caf49a71400a2c3cad10f638b8171e621efcebc7f69c7c506c34f2e98199cf9346fd0005b0ba76ba50d92fdb3b720f5463f9daf
|
7
|
+
data.tar.gz: 89d930e741ce21bdd79eaac8018af0124ae94457670db8b8191a526ae64da6a83abc5b0594fc7bae3b204b97957351b43d988a384005ae7d5de774c1d2bd5dc6
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
[](https://badge.fury.io/rb/where_chain) [](https://circleci.com/gh/marcinruszkiewicz/where_chain)
|
2
2
|
|
3
3
|
# WhereChain
|
4
4
|
|
@@ -3,7 +3,18 @@ module ActiveRecord
|
|
3
3
|
class WhereChain
|
4
4
|
include WhereChainSharedMethods
|
5
5
|
|
6
|
-
#
|
6
|
+
# Returns a new relation expressing WHERE + NOT condition
|
7
|
+
# according to the conditions in the arguments.
|
8
|
+
#
|
9
|
+
# User.where.not(name: nil)
|
10
|
+
# # SELECT * FROM users WHERE users.name IS NOT NULL
|
11
|
+
#
|
12
|
+
# If there is no argument, chain further.
|
13
|
+
#
|
14
|
+
# User.where.not.gt(login_count: 3)
|
15
|
+
# # SELECT * FROM users WHERE NOT(users.login_count > 3)
|
16
|
+
#
|
17
|
+
# Note: This is the Active Record 4 version.
|
7
18
|
def not(opts = :chain, *rest)
|
8
19
|
where_value = @scope.send(:build_where, opts, rest).map do |rel|
|
9
20
|
case rel
|
@@ -26,10 +37,14 @@ module ActiveRecord
|
|
26
37
|
@scope.references!(PredicateBuilder.references(opts)) if Hash === opts
|
27
38
|
@scope.where_values += where_value
|
28
39
|
@scope
|
29
|
-
end
|
40
|
+
end
|
30
41
|
|
31
42
|
private
|
32
43
|
|
44
|
+
# Prepare the WHERE clause by inserting a proper Arel node and inverting
|
45
|
+
# it if necessary.
|
46
|
+
#
|
47
|
+
# Note: This is the Active Record 4 version.
|
33
48
|
def prepare_where(node_type, infix, opts, *rest)
|
34
49
|
where_value = @scope.send(:build_where, opts, rest).map do |rel|
|
35
50
|
if @invert
|
@@ -1,4 +1,5 @@
|
|
1
1
|
module ActiveRecord
|
2
|
+
# Ruby complains about missing the class if we try to patch it without it
|
2
3
|
class Relation
|
3
4
|
class QueryMethods; end
|
4
5
|
end
|
@@ -7,6 +8,18 @@ module ActiveRecord
|
|
7
8
|
class WhereChain
|
8
9
|
include WhereChainSharedMethods
|
9
10
|
|
11
|
+
# Returns a new relation expressing WHERE + NOT condition
|
12
|
+
# according to the conditions in the arguments.
|
13
|
+
#
|
14
|
+
# User.where.not(name: nil)
|
15
|
+
# # SELECT * FROM users WHERE users.name IS NOT NULL
|
16
|
+
#
|
17
|
+
# If there is no argument, chain further.
|
18
|
+
#
|
19
|
+
# User.where.not.gt(login_count: 3)
|
20
|
+
# # SELECT * FROM users WHERE NOT(users.login_count > 3)
|
21
|
+
#
|
22
|
+
# Note: This is the Active Record 5 version.
|
10
23
|
def not(opts = :chain, *rest)
|
11
24
|
if :chain == opts
|
12
25
|
@invert = true
|
@@ -22,8 +35,12 @@ module ActiveRecord
|
|
22
35
|
@scope
|
23
36
|
end
|
24
37
|
|
25
|
-
private
|
38
|
+
private
|
26
39
|
|
40
|
+
# Prepare the WHERE clause by inserting a proper Arel node and inverting
|
41
|
+
# it if necessary.
|
42
|
+
#
|
43
|
+
# Note: This is the Active Record 5 version.
|
27
44
|
def prepare_where(node_type, infix, opts, rest)
|
28
45
|
@scope.tap do |s|
|
29
46
|
opts.each_pair do |key, value|
|
@@ -41,13 +58,15 @@ module ActiveRecord
|
|
41
58
|
end
|
42
59
|
end
|
43
60
|
|
61
|
+
# Active Record 5.2 changed the API and removed binds method so we need to pass
|
62
|
+
# proper arguments to build a WHERE clause
|
44
63
|
def build_where_clause(new_predicate, old_where_clause)
|
45
64
|
if old_where_clause.respond_to?(:binds)
|
46
65
|
Relation::WhereClause.new([new_predicate], old_where_clause.binds)
|
47
66
|
else
|
48
67
|
Relation::WhereClause.new([new_predicate])
|
49
68
|
end
|
50
|
-
end
|
69
|
+
end
|
51
70
|
end
|
52
71
|
end
|
53
|
-
end
|
72
|
+
end
|
@@ -3,16 +3,18 @@ module ActiveRecord
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
included do
|
6
|
+
# Initialize the chain with a scope and a toggle to invert the generated
|
7
|
+
# where statement
|
6
8
|
def initialize(scope, invert=false)
|
7
9
|
@scope = scope
|
8
10
|
@invert = invert
|
9
11
|
end
|
10
|
-
|
12
|
+
|
11
13
|
# Returns a new relation expressing WHERE + LIKE condition
|
12
14
|
# according to the conditions provided as a hash in the arguments.
|
13
15
|
#
|
14
16
|
# Book.where.like(title: "Rails%")
|
15
|
-
# # SELECT * FROM books WHERE title LIKE 'Rails%'
|
17
|
+
# # SELECT * FROM books WHERE books.title LIKE 'Rails%'
|
16
18
|
def like(opts, *rest)
|
17
19
|
prepare_where(Arel::Nodes::Matches, nil, opts, rest)
|
18
20
|
end
|
@@ -21,51 +23,77 @@ module ActiveRecord
|
|
21
23
|
# according to the conditions provided as a hash in the arguments.
|
22
24
|
#
|
23
25
|
# Conference.where.not_like(name: "%Kaigi")
|
24
|
-
# # SELECT * FROM conferences WHERE name NOT LIKE '%Kaigi'
|
26
|
+
# # SELECT * FROM conferences WHERE conferences.name NOT LIKE '%Kaigi'
|
27
|
+
#
|
28
|
+
# Also aliased as .not_like to maintain compatibility with the
|
29
|
+
# activerecord-like gem
|
25
30
|
def unlike(opts, *rest)
|
26
31
|
prepare_where(Arel::Nodes::DoesNotMatch, nil, opts, rest)
|
27
32
|
end
|
28
|
-
alias not_like unlike
|
33
|
+
alias not_like unlike
|
29
34
|
|
35
|
+
# Returns a new relation expressing a greater than condition in WHERE
|
36
|
+
# according to the conditions in an argument hash
|
37
|
+
#
|
38
|
+
# Conference.where.gt(speakers: 10)
|
39
|
+
# # SELECT * FROM conferences WHERE conferences.speakers > 10
|
30
40
|
def gt(opts, *rest)
|
31
41
|
ensure_proper_attributes(opts)
|
32
42
|
prepare_where(Arel::Nodes::InfixOperation, '>', opts, rest)
|
33
43
|
end
|
34
44
|
|
45
|
+
# Returns a new relation expressing a greater than or equal condition in WHERE
|
46
|
+
# according to the conditions in an argument hash
|
47
|
+
#
|
48
|
+
# Conference.where.gte(speakers: 10)
|
49
|
+
# # SELECT * FROM conferences WHERE conferences.speakers >= 10
|
35
50
|
def gte(opts, *rest)
|
36
51
|
ensure_proper_attributes(opts)
|
37
52
|
prepare_where(Arel::Nodes::InfixOperation, '>=', opts, rest)
|
38
53
|
end
|
39
54
|
|
55
|
+
# Returns a new relation expressing a less than condition in WHERE
|
56
|
+
# according to the conditions in an argument hash
|
57
|
+
#
|
58
|
+
# Conference.where.lt(date: DateTime.new(2018, 3, 18))
|
59
|
+
# # SELECT * FROM conferences WHERE conferences.date < '2018-03-18 00:00:00'
|
40
60
|
def lt(opts, *rest)
|
41
61
|
ensure_proper_attributes(opts)
|
42
62
|
prepare_where(Arel::Nodes::InfixOperation, '<', opts, rest)
|
43
63
|
end
|
44
64
|
|
65
|
+
# Returns a new relation expressing a less than or equal condition in WHERE
|
66
|
+
# according to the conditions in an argument hash
|
67
|
+
#
|
68
|
+
# Conference.where.lt(date: DateTime.new(2018, 3, 18))
|
69
|
+
# # SELECT * FROM conferences WHERE conferences.date <= '2018-03-18 00:00:00'
|
45
70
|
def lte(opts, *rest)
|
46
71
|
ensure_proper_attributes(opts)
|
47
72
|
prepare_where(Arel::Nodes::InfixOperation, '<=', opts, rest)
|
48
73
|
end
|
49
74
|
|
50
|
-
private
|
75
|
+
private
|
51
76
|
|
77
|
+
# Ensures that the arguments passed to methods are a Hash and that the value in the argument
|
78
|
+
# hash is not another Hash or Array.
|
52
79
|
def ensure_proper_attributes(opts)
|
53
80
|
raise ArgumentError, 'This method requires a Hash as an argument.' unless opts.is_a?(Hash)
|
54
81
|
|
55
82
|
opts.each_pair do |key, value|
|
56
83
|
if value.is_a?(Hash) || value.is_a?(Array)
|
57
|
-
raise ArgumentError, 'The value passed to this method should be a valid type.'
|
84
|
+
raise ArgumentError, 'The value passed to this method should be a valid type.'
|
58
85
|
end
|
59
86
|
end
|
60
87
|
end
|
61
88
|
|
89
|
+
# Arel::Nodes::InfixOperation.new expects more arguments than other Arel nodes
|
62
90
|
def arel_node(node_type, infix, rel)
|
63
91
|
if infix.present?
|
64
92
|
node_type.new(infix, rel.left, rel.right)
|
65
93
|
else
|
66
94
|
node_type.new(rel.left, rel.right)
|
67
95
|
end
|
68
|
-
end
|
96
|
+
end
|
69
97
|
end
|
70
98
|
end
|
71
|
-
end
|
99
|
+
end
|
data/lib/where_chain.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
require 'active_record/where_chain_shared_methods'
|
2
2
|
|
3
|
+
# Active Record changed a lot in Rails 5 and 4 is still supported
|
3
4
|
if ActiveRecord::VERSION::MAJOR == 4
|
4
5
|
require 'active_record/where_chain_extensions_rails4'
|
5
6
|
elsif ActiveRecord::VERSION::MAJOR == 5
|
6
7
|
require 'active_record/where_chain_extensions_rails5'
|
7
|
-
end
|
8
|
+
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.2.
|
4
|
+
version: 0.2.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: 2018-03-
|
11
|
+
date: 2018-03-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|