pg_ltree 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f6149a7caa97eb49d20b3f8b9c144d98ee6af04e
4
- data.tar.gz: ab7a5c2f1d37d3f55c2c49bda92eb8f0fe43e4c2
3
+ metadata.gz: 1ea3d57bbe24a87605eb3cc1bd653cb7d0ed7170
4
+ data.tar.gz: 27e68347ac9e861a985bdba1b0b7821189d04015
5
5
  SHA512:
6
- metadata.gz: 2226c78595c239d7db0e29dc69d91ff93a1a2c4a47903fe531c88efe7d7022a24eef7b873afd5efb1610d60b6d32f3d9f22fe115f1983a6908d701e2cfe33d4e
7
- data.tar.gz: 1a2df813b90353f92db7ab1b4f5c0dc2f65af997a6c337a8df45ab906cce7ae676c6f3d53873ce3164905b4241704cece1a8744adaa54a71a3ea7dbc48394d75
6
+ metadata.gz: e55426faace49c393326faca66f9344c62b20490171415291dcb1037f4f6f1ba331b07ca51c816bf2524a4ee4ac112ff20d6646931ab0e14acbce132ea1f60fb
7
+ data.tar.gz: f7f5510c4ae6c7ea369690ee5be01700d66499b10e8fcef05c4d200439a327537d28b88e91bcd09c72a8ca1e1dcfd1a182dd1dad7fd8fa6971174a4815120061
@@ -10,14 +10,15 @@ module PgLtree
10
10
  # Initialzie ltree for active model
11
11
  #
12
12
  # @param column [String] ltree column name
13
- def ltree(column = :path)
13
+ def ltree(column = :path, options: { cascade: true })
14
14
  cattr_accessor :ltree_path_column
15
15
 
16
16
  self.ltree_path_column = column
17
17
 
18
- has_and_belongs_to_many column.to_s.tableize.to_sym,
19
-       class_name: self.class.name,
20
-       association_foreign_key: 'path'
18
+ if options[:cascade]
19
+ self.after_update :cascade_update
20
+ self.after_destroy :cascade_destroy
21
+ end
21
22
 
22
23
  extend ClassMethods
23
24
  include InstanceMethods
@@ -185,6 +186,21 @@ module PgLtree
185
186
  ltree_scope.where "? @> #{ltree_path_column} AND nlevel(#{ltree_path_column}) = NLEVEL(?) + 1",
186
187
  ltree_path, ltree_path
187
188
  end
189
+
190
+ # Update all childen for current path
191
+ #
192
+ # @return [ActiveRecord::Relation]
193
+ def cascade_update
194
+ ltree_scope.where(["#{ltree_path_column} <@ ?", ltree_path_was]).
195
+ update_all ["#{ltree_path_column} = ? || subpath(#{ltree_path_column}, nlevel(?))", ltree_path, ltree_path_was]
196
+ end
197
+
198
+ # Delete all children for current path
199
+ #
200
+ # @return [ActiveRecord::Relation]
201
+ def cascade_destroy
202
+ ltree_scope.where("#{ltree_path_column} <@ ?", ltree_path_was).delete_all
203
+ end
188
204
  end
189
205
  end
190
206
  end
@@ -19,6 +19,7 @@ module PgLtree
19
19
 
20
20
  # Define class methods
21
21
  module ClassMethods
22
+
22
23
  # Get all leaves
23
24
  #
24
25
  # @return [ActiveRecord::Relation] relations of node's leaves
@@ -4,5 +4,5 @@
4
4
  module PgLtree
5
5
 
6
6
  # Gem Version
7
- VERSION = "1.0.0"
7
+ VERSION = "1.1.0"
8
8
  end
@@ -0,0 +1,14 @@
1
+ default: &default
2
+ adapter: postgresql
3
+ host: localhost
4
+ username: postgres
5
+ min_messages: warning
6
+
7
+ development:
8
+ <<: *default
9
+ database: pg_ltree_development
10
+ password: postgres
11
+
12
+ test:
13
+ <<: *default
14
+ database: pg_ltree_test