active_record_extended 0.5.0.beta2 → 0.5.0.beta3

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
  SHA256:
3
- metadata.gz: df4c51db1e23fe823d4f551b4b44eb607133b431f0970286b0e3b3a576c6ad42
4
- data.tar.gz: cd2558c39e0224d6fc9719dc7ceb27a45ed0e98da7141ed620287f95ce4a6062
3
+ metadata.gz: af96e1630626fddb09778e5ff69dc4bf01b06e17e2e462c7e65a2a165cac43dc
4
+ data.tar.gz: 512da6651d7413301e720414ecddfdca47700ce3bd22adce5957f444c8b34f9f
5
5
  SHA512:
6
- metadata.gz: 520880c113c258caaa12a3ef2f83b92f1f7d6e71b03beaab5acd03050267071f2025b268f8156eb70206ad84887a13195717d76e328e10832257433a6963421e
7
- data.tar.gz: 8ba91e9f95d7f77541d5f956811b4c61e9e04a2e8ecbe8cc12cff6fe79ead72ce7233699f7d89de53fcd67f1aa1a932ea2d31bbc144adc7943cd119e1f8e748b
6
+ metadata.gz: d165d1174ccb1f3fe3eb38cd08b399507564116da0a41847600b1dbc170f362b7cc763ef1191564a40c635abb1e07e0ec93bb92675f2283328cab78d032a5eda
7
+ data.tar.gz: 3b790bf77539c16b30ecb217400601b1d0e1bc7ddae2e01ca11c3886ff054acf062eefceab6f766b4c37e050659a098083197696055cbaa75ea796854a574c9f
data/CHANGELOG.md CHANGED
@@ -1,3 +1,25 @@
1
+ # 0.5.0.beta3 - May 28th 2018
2
+
3
+ Added `inet_contains_or_contained_within/1` method
4
+
5
+ # 0.5.0.beta2 - May 27th 2018
6
+
7
+ Renamed inet functions to hopefully give a clearer understanding to what these methods are used for.
8
+
9
+ Added support for Postgres Inet functions. View the readme for more details on the following:
10
+
11
+ - `#inet_contained_within/1`
12
+ - `#inet_contained_within_or_equals/1`
13
+ - `#inet_contains_or_equals/1`
14
+ - `#inet_contains/1`
15
+
16
+ ### Deprecation Warnings
17
+ The following will be dropped upon v1.0 release. In favor of their prefixed counterparts.
18
+
19
+ - `#contained_within/1`
20
+ - `#contained_within_or_equals/1`
21
+ - `#contains_or_equals/1`
22
+
1
23
  # 0.5.0.beta1 - May 26th 2018
2
24
 
3
25
  Added support for Rails 5.0.x
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- active_record_extended (0.5.0.beta2)
4
+ active_record_extended (0.5.0.beta3)
5
5
  activerecord (>= 5.0, < 6.0)
6
6
  ar_outer_joins (~> 0.2)
7
7
  pg (~> 0.18)
data/README.md CHANGED
@@ -14,6 +14,7 @@
14
14
  - [Inet Contains or Equals](#inet-contains-or-equals)
15
15
  - [Inet Contained Within](#inet-contained-within)
16
16
  - [Inet Contained Within or Equals](#inet-contained-within-or-equals)
17
+ - [Inet Contains or Contained Within](#inet-contains-or-contained-within)
17
18
  - [Conditional Methods](#conditional-methods)
18
19
  - [Any_of / None_of](#any_of--none_of)
19
20
  - [Either Join](#either-join)
@@ -140,7 +141,7 @@ Person.where.inet_contains_or_equals(ip: "127.0.0.1/32") #=> [alice, bob]
140
141
  ```
141
142
 
142
143
  ##### Inet Contained Within
143
- [Postgres << (contained within) Network Expression](https://www.postgresql.org/docs/current/static/functions-net.html)
144
+ [Postgres << (contained by) Network Expression](https://www.postgresql.org/docs/current/static/functions-net.html)
144
145
 
145
146
  For the `inet_contained_within` method, we try to find IP's that fall within a submasking range we provide.
146
147
 
@@ -154,7 +155,7 @@ Person.where.inet_contained_within(ip: "127.0.0.1/16") #=> [alice, bob, randy]
154
155
  ```
155
156
 
156
157
  ##### Inet Contained Within or Equals
157
- [Postgres <<= (contained within or equals) Network Expression](https://www.postgresql.org/docs/current/static/functions-net.html)
158
+ [Postgres <<= (contained by or equals) Network Expression](https://www.postgresql.org/docs/current/static/functions-net.html)
158
159
 
159
160
  The `inet_contained_within_or_equals` method works much like the [Inet Contained Within](#inet-contained-within) method, but will also accept a submask range.
160
161
 
@@ -168,6 +169,21 @@ Person.where.inet_contained_within_or_equals(ip: "127.0.0.1/16") #=> [bob, randy
168
169
  Person.where.inet_contained_within_or_equals(ip: "127.0.0.44/8") #=> [alice, bob, randy]
169
170
  ```
170
171
 
172
+ ##### Inet Contains or Contained Within
173
+ [Postgres && (contains or is contained by) Network Expression](https://www.postgresql.org/docs/current/static/functions-net.html)
174
+
175
+ The `inet_contains_or_contained_within` method is a combination of [Inet Contains](#inet-contains) and [Inet Contained Within](#inet-contained-within).
176
+ It essentially (the database) tries to use both methods to find as many records as possible that match either condition on both sides.
177
+
178
+ ```ruby
179
+ alice = Person.create!(ip: "127.0.0.1/24")
180
+ bob = Person.create!(ip: "127.0.22.44/8")
181
+ randy = Person.create!(ip: "127.0.99.1")
182
+
183
+ Person.where.inet_contains_or_is_contained_within(ip: "127.0.255.80") #=> [bob]
184
+ Person.where.inet_contains_or_is_contained_within(ip: "127.0.0.80") #=> [alice, bob]
185
+ Person.where.inet_contains_or_is_contained_within(ip: "127.0.0.80/8") #=> [alice, bob, randy]
186
+ ```
171
187
 
172
188
  ### Conditional Methods
173
189
  #### Any_of / None_of
@@ -52,6 +52,12 @@ module Arel
52
52
  :"<<="
53
53
  end
54
54
  end
55
+
56
+ class ContainsOrContainedWithin < Arel::Nodes::Binary
57
+ def operator
58
+ :"&&"
59
+ end
60
+ end
55
61
  end
56
62
 
57
63
  class Node
@@ -27,6 +27,10 @@ module Arel
27
27
  Nodes::ContainedInArray.new self, Nodes.build_quoted(other, self)
28
28
  end
29
29
 
30
+ def inet_contains_or_is_contained_within(other)
31
+ Nodes::Inet::ContainsOrContainedWithin.new self, Nodes.build_quoted(other, self)
32
+ end
33
+
30
34
  def inet_contained_within(other)
31
35
  Nodes::Inet::ContainedWithin.new self, Nodes.build_quoted(other, self)
32
36
  end
@@ -55,6 +55,10 @@ module ActiveRecordExtended
55
55
  infix_value object, collector, " >>= "
56
56
  end
57
57
 
58
+ def visit_Arel_Nodes_Inet_ContainsOrContainedWithin(object, collector)
59
+ infix_value object, collector, " && "
60
+ end
61
+
58
62
  def matchable_column?(col, object)
59
63
  col.name == object.left.name.to_s || col.name == object.left.relation.name.to_s
60
64
  end
@@ -77,6 +77,24 @@ module ActiveRecordExtended
77
77
  def inet_contains(opts, *rest)
78
78
  substitute_comparisons(opts, rest, Arel::Nodes::Contains, "inet_contains")
79
79
  end
80
+
81
+ # This method is a combination of `inet_contains` and `inet_contained_within`
82
+ #
83
+ # Finds records that are contained within a given submask. And will also find records where their submask is also
84
+ # contains a given IP or IP submask.
85
+ #
86
+ # Column(inet) && "127.0.0.1/28"
87
+ #
88
+ # User.where.inet_contains_or_is_contained_by(ip: "127.0.255.255/28")
89
+ # #=> "SELECT \"users\".* FROM \"users\" WHERE \"users\".\"ip\" && '127.0.255.255/28'"
90
+ #
91
+ # User.where.inet_contains_or_is_contained_by(ip: IPAddr.new("127.0.0.255"))
92
+ # #=> "SELECT \"users\".* FROM \"users\" WHERE \"users\".\"ip\" && '127.0.0.255/32'"
93
+ #
94
+ def inet_contains_or_is_contained_within(opts, *rest)
95
+ substitute_comparisons(opts, rest, Arel::Nodes::Inet::ContainsOrContainedWithin,
96
+ "inet_contains_or_is_contained_within")
97
+ end
80
98
  end
81
99
  end
82
100
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecordExtended
4
- VERSION = "0.5.0.beta2"
4
+ VERSION = "0.5.0.beta3"
5
5
  end
@@ -33,7 +33,7 @@ RSpec.describe "Active Record Inet Query Methods" do
33
33
  end
34
34
  end
35
35
 
36
- describe "contained_within_or_equals" do
36
+ describe "inet_contained_within_or_equals" do
37
37
  let!(:local_1) { Person.create!(ip: "127.0.0.1/10") }
38
38
  let!(:local_44) { Person.create!(ip: "127.0.0.44/32") }
39
39
  let!(:local_99_1) { Person.create!(ip: "127.0.99.1") }
@@ -94,4 +94,28 @@ RSpec.describe "Active Record Inet Query Methods" do
94
94
  expect(query).to be_empty
95
95
  end
96
96
  end
97
+
98
+ describe "#inet_contains_or_is_contained_within" do
99
+ let!(:local_1) { Person.create!(ip: "127.0.0.1/24") }
100
+ let!(:local_44) { Person.create!(ip: "127.0.22.44/8") }
101
+ let!(:local_99_1) { Person.create!(ip: "127.0.99.1") }
102
+
103
+ it "should find records where the records contain the given IP" do
104
+ query = Person.where.inet_contains_or_is_contained_within(ip: "127.0.255.80")
105
+ expect(query).to include(local_44)
106
+ expect(query).to_not include(local_1, local_99_1)
107
+
108
+ query = Person.where.inet_contains_or_is_contained_within(ip: "127.0.0.80")
109
+ expect(query).to include(local_1, local_44)
110
+ expect(query).to_not include(local_99_1)
111
+ end
112
+
113
+ it "Should find records that the where query contains a valid range" do
114
+ query = Person.where.inet_contains_or_is_contained_within(ip: "127.0.0.80/8")
115
+ expect(query).to include(local_1, local_44, local_99_1)
116
+
117
+ query = Person.where.inet_contains_or_is_contained_within(ip: "127.0.0.80/16")
118
+ expect(query).to include(local_1, local_44, local_99_1)
119
+ end
120
+ end
97
121
  end
@@ -48,4 +48,19 @@ RSpec.describe "Inet Column Predicates" do
48
48
  expect(Person.where(arel_table[:ip].inet_contains("127.0.0.1")).count).to eq(0)
49
49
  end
50
50
  end
51
+
52
+ describe "#inet_contains_or_is_contained_within" do
53
+ it "converts Arel inet contained within statement" do
54
+ query = arel_table.where(arel_table[:ip].inet_contains_or_is_contained_within("127.0.0.1")).to_sql
55
+ expect(query).to match_regex(/&& '127\.0\.0\.1'/)
56
+
57
+ query = arel_table.where(arel_table[:ip].inet_contains_or_is_contained_within(IPAddr.new("127.0.0.1"))).to_sql
58
+ expect(query).to match_regex(%r{&& '127\.0\.0\.1/32'})
59
+ end
60
+
61
+ it "works with count" do
62
+ expect(Person.where(arel_table[:ip].inet_contains_or_is_contained_within("127.0.0.1")).count).to eq(0)
63
+ expect(Person.where(arel_table[:ip].inet_contains_or_is_contained_within(IPAddr.new("127.0.0.1"))).count).to eq(0)
64
+ end
65
+ end
51
66
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_extended
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0.beta2
4
+ version: 0.5.0.beta3
5
5
  platform: ruby
6
6
  authors:
7
7
  - George Protacio-Karaszi