pg_ltree 1.1.7 → 1.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/Rakefile +2 -0
- data/lib/pg_ltree.rb +4 -2
- data/lib/pg_ltree/ltree.rb +23 -12
- data/lib/pg_ltree/version.rb +1 -1
- data/lib/pg_ltree/versions/rails_older_than_51.rb +28 -0
- data/test/pg_ltree/ltree_test.rb +10 -0
- data/test/test_helper.rb +7 -0
- metadata +8 -9
- data/lib/pg_ltree/versions/rails_5_1.rb +0 -32
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: af73780dad2653e9068d80d56ad53a1cac86f88968cbe9f697bc832e42aedcf6
|
4
|
+
data.tar.gz: a4bea89ea088fe18ce1a5d5ce9606bceecb1833a36b9ec4ce7b252bcde5e7f83
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '086bcf6439fdfd185d738ab1c120c41192567f8d3e9c1ee456c0ef82bf28d0e6c921d9c9a397ff5cacd7735dfc21605c078ade8927266e6685ef7fefbc1d0b58'
|
7
|
+
data.tar.gz: 9317b961bd9ae4f6ff781c3aa72bf2b5ee0c5136bb6b2a28482853a8c0262278f5a00daf6d8b4dfd71302d4e2b52975abc8e4f2b0629fffdb03037d95ca51ced
|
data/Rakefile
CHANGED
@@ -30,6 +30,8 @@ namespace :test do
|
|
30
30
|
activerecord_42_pg_017 activerecord_42_pg_018
|
31
31
|
activerecord_50_pg_018 activerecord_51_pg_020
|
32
32
|
activerecord_51_pg_021 activerecord_52_pg_100
|
33
|
+
activerecord_60_pg_021 activerecord_60_pg_100
|
34
|
+
activerecord_60_pg_110
|
33
35
|
).freeze
|
34
36
|
|
35
37
|
AVAILABLE_CASES.each do |version|
|
data/lib/pg_ltree.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'active_record'
|
1
2
|
require 'pg_ltree/ltree'
|
2
3
|
require 'pg_ltree/scoped_for'
|
3
4
|
require 'pg_ltree/version'
|
@@ -9,7 +10,8 @@ if defined?(ActiveRecord)
|
|
9
10
|
|
10
11
|
# The behavior of _was changes in Rails 5.1
|
11
12
|
# http://blog.toshima.ru/2017/04/06/saved-change-to-attribute.html
|
12
|
-
|
13
|
-
|
13
|
+
# This is for backward compability
|
14
|
+
if ActiveRecord::VERSION::MAJOR < 5 || (ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR < 1)
|
15
|
+
ActiveRecord::Base.extend(PgLtree::Versions::RailsOlderThan51)
|
14
16
|
end
|
15
17
|
end
|
data/lib/pg_ltree/ltree.rb
CHANGED
@@ -44,12 +44,12 @@ module PgLtree
|
|
44
44
|
#
|
45
45
|
# @return [ActiveRecord::Relation] relations of node's leaves
|
46
46
|
def leaves
|
47
|
-
subquery =
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
where subquery
|
47
|
+
subquery = unscoped.select("#{table_name}.#{ltree_path_column}")
|
48
|
+
.from("#{table_name} AS subquery")
|
49
|
+
.where("#{table_name}.#{ltree_path_column} <> subquery.#{ltree_path_column}")
|
50
|
+
.where("#{table_name}.#{ltree_path_column} @> subquery.#{ltree_path_column}")
|
51
|
+
|
52
|
+
where.not ltree_path_column => subquery
|
53
53
|
end
|
54
54
|
|
55
55
|
# Get all with nodes when path liked the lquery
|
@@ -92,11 +92,22 @@ module PgLtree
|
|
92
92
|
public_send ltree_path_column
|
93
93
|
end
|
94
94
|
|
95
|
-
# Get
|
95
|
+
# Get ltree original value before the save just occurred
|
96
|
+
# https://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/Dirty.html#method-i-attribute_before_last_save
|
96
97
|
#
|
97
98
|
# @return [String] ltree previous value
|
98
|
-
def
|
99
|
-
public_send :
|
99
|
+
def ltree_path_before_last_save
|
100
|
+
public_send :attribute_before_last_save, ltree_path_column
|
101
|
+
end
|
102
|
+
|
103
|
+
# Get lTree previous value
|
104
|
+
# originally +attribute_was+ used in before create/update, destroy won't call +save+ so this work
|
105
|
+
# https://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/Dirty.html#method-i-attribute_in_database
|
106
|
+
#
|
107
|
+
# @return [String] ltree value in database
|
108
|
+
|
109
|
+
def ltree_path_in_database
|
110
|
+
public_send :attribute_in_database, ltree_path_column
|
100
111
|
end
|
101
112
|
|
102
113
|
# Check what current node is root
|
@@ -226,15 +237,15 @@ module PgLtree
|
|
226
237
|
#
|
227
238
|
# @return [ActiveRecord::Relation]
|
228
239
|
def cascade_update
|
229
|
-
ltree_scope.where(["#{ltree_scope.table_name}.#{ltree_path_column} <@ ?",
|
230
|
-
.update_all ["#{ltree_path_column} = ? || subpath(#{ltree_path_column}, nlevel(?))", ltree_path,
|
240
|
+
ltree_scope.where(["#{ltree_scope.table_name}.#{ltree_path_column} <@ ?", ltree_path_before_last_save]).where(["#{ltree_scope.table_name}.#{ltree_path_column} != ?", ltree_path])
|
241
|
+
.update_all ["#{ltree_path_column} = ? || subpath(#{ltree_path_column}, nlevel(?))", ltree_path, ltree_path_before_last_save]
|
231
242
|
end
|
232
243
|
|
233
244
|
# Delete all children for current path
|
234
245
|
#
|
235
246
|
# @return [ActiveRecord::Relation]
|
236
247
|
def cascade_destroy
|
237
|
-
ltree_scope.where("#{ltree_scope.table_name}.#{ltree_path_column} <@ ?",
|
248
|
+
ltree_scope.where("#{ltree_scope.table_name}.#{ltree_path_column} <@ ?", ltree_path_in_database).delete_all
|
238
249
|
end
|
239
250
|
end
|
240
251
|
end
|
data/lib/pg_ltree/version.rb
CHANGED
@@ -0,0 +1,28 @@
|
|
1
|
+
module PgLtree
|
2
|
+
module Versions
|
3
|
+
module RailsOlderThan51
|
4
|
+
def ltree(column = :path, options: { cascade: true })
|
5
|
+
super
|
6
|
+
include InstanceMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
module InstanceMethods
|
10
|
+
# Get lTree previous value
|
11
|
+
#
|
12
|
+
# Related changes in > Rails 5.1.0
|
13
|
+
# https://github.com/rails/rails/pull/25337
|
14
|
+
#
|
15
|
+
# @return [String] Rails 5.1 replace attribute_was method with two methods, this is a wrapper for older rails
|
16
|
+
def ltree_path_before_last_save
|
17
|
+
public_send :attribute_was, ltree_path_column
|
18
|
+
end
|
19
|
+
|
20
|
+
#
|
21
|
+
# @return [String] Rails 5.1 replace attribute_was method with two methods, this is a wrapper for older rails
|
22
|
+
def ltree_path_in_database
|
23
|
+
public_send :attribute_was, ltree_path_column
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/test/pg_ltree/ltree_test.rb
CHANGED
@@ -53,6 +53,16 @@ class PgLtree::LtreeTest < BaseTest
|
|
53
53
|
)
|
54
54
|
end
|
55
55
|
|
56
|
+
test '#leaves with a relation' do
|
57
|
+
assert_equal TreeNode.where("path <> 'Top.Collections.Pictures.Astronomy.Stars'").leaves.pluck(:path), %w(
|
58
|
+
Top.Science.Astronomy.Astrophysics
|
59
|
+
Top.Science.Astronomy.Cosmology
|
60
|
+
Top.Hobbies.Amateurs_Astronomy
|
61
|
+
Top.Collections.Pictures.Astronomy.Galaxies
|
62
|
+
Top.Collections.Pictures.Astronomy.Astronauts
|
63
|
+
)
|
64
|
+
end
|
65
|
+
|
56
66
|
test '#where_path_liked' do
|
57
67
|
assert_equal TreeNode.where_path_liked('*{2}.Astronomy|Pictures').pluck(:path), %w(
|
58
68
|
Top.Science.Astronomy
|
data/test/test_helper.rb
CHANGED
@@ -31,6 +31,13 @@ class BaseTest < ActiveSupport::TestCase
|
|
31
31
|
exit 0
|
32
32
|
end
|
33
33
|
|
34
|
+
begin
|
35
|
+
PG.connect(host: db_connection["host"], user: db_connection["username"], password: db_connection["password"])
|
36
|
+
.exec("CREATE DATABASE #{db_connection['database']}")
|
37
|
+
rescue
|
38
|
+
# Ignore errors on DB:CEATE
|
39
|
+
end
|
40
|
+
|
34
41
|
ActiveRecord::Base.establish_connection db_connection
|
35
42
|
ActiveRecord::Schema.verbose = false
|
36
43
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pg_ltree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrei Panamarenka
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-12-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -17,9 +17,9 @@ dependencies:
|
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: 4.0.0
|
20
|
-
- - "
|
20
|
+
- - "<="
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version:
|
22
|
+
version: 7.0.0.rc1
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -27,9 +27,9 @@ dependencies:
|
|
27
27
|
- - ">="
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: 4.0.0
|
30
|
-
- - "
|
30
|
+
- - "<="
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version:
|
32
|
+
version: 7.0.0.rc1
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: pg
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -133,7 +133,7 @@ files:
|
|
133
133
|
- lib/pg_ltree/ltree.rb
|
134
134
|
- lib/pg_ltree/scoped_for.rb
|
135
135
|
- lib/pg_ltree/version.rb
|
136
|
-
- lib/pg_ltree/versions/
|
136
|
+
- lib/pg_ltree/versions/rails_older_than_51.rb
|
137
137
|
- test/database.yml
|
138
138
|
- test/database.yml.sample
|
139
139
|
- test/pg_ltree/ltree_test.rb
|
@@ -159,8 +159,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
159
159
|
- !ruby/object:Gem::Version
|
160
160
|
version: '0'
|
161
161
|
requirements: []
|
162
|
-
|
163
|
-
rubygems_version: 2.6.14
|
162
|
+
rubygems_version: 3.1.0.pre2
|
164
163
|
signing_key:
|
165
164
|
specification_version: 4
|
166
165
|
summary: Organise ActiveRecord model into a tree structure with PostgreSQL LTree
|
@@ -1,32 +0,0 @@
|
|
1
|
-
module PgLtree
|
2
|
-
module Versions
|
3
|
-
module Rails51
|
4
|
-
def ltree(column = :path, options: { cascade: true })
|
5
|
-
super
|
6
|
-
|
7
|
-
before_destroy :delete_ltree_column_value if options[:cascade]
|
8
|
-
include InstanceMethods
|
9
|
-
end
|
10
|
-
|
11
|
-
module InstanceMethods
|
12
|
-
# Get lTree previous value
|
13
|
-
#
|
14
|
-
# Related changes in > Rails 5.1.0
|
15
|
-
# https://github.com/rails/rails/pull/25337
|
16
|
-
#
|
17
|
-
# @return [String] ltree previous value
|
18
|
-
def ltree_path_was
|
19
|
-
public_send :"#{ltree_path_column}_before_last_save"
|
20
|
-
end
|
21
|
-
|
22
|
-
#
|
23
|
-
# In order for for cascade_destroy to work with the current callbacks, let's first delete the column :/.
|
24
|
-
# @author HoyaBoya [https://github.com/HoyaBoya]
|
25
|
-
#
|
26
|
-
def delete_ltree_column_value
|
27
|
-
update!(ltree_path_column => nil)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|