acts_as_many_trees 0.1.1 → 0.2.0

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
  SHA1:
3
- metadata.gz: 36fc7e3900e483ed1185826c883f7a224388f085
4
- data.tar.gz: d4a96aeb0141b6835ab8a1c7b821c9ce8e6c2715
3
+ metadata.gz: 4cb9522ff927a74b55edbe763d0d20f035e862d9
4
+ data.tar.gz: 86bb4f983cc3de97fde2809852b1d0f98c293b32
5
5
  SHA512:
6
- metadata.gz: 91837d6eac4e8e4a5f8500582699503b6563a7b7ba1bd8397e8b8ccc5e40a0ff86bdffc2b326ccd915c51f3b9ca4f5d9a1306beed5d2934e3baa234e25d728eb
7
- data.tar.gz: cf1d3b22bd6b3d8024b080909b5b8b6e7d75e7ea96785ed0dd9a1a52122741b9a6fc27047c66e0b0591b0f5c62445ba4d7ff62ac40228b6c5fd1566b696dbecf
6
+ metadata.gz: c1861df1b3c04446f86761ea74a26325993cf840b4c77417135d70bca9166257da786a94208953a4990744e0cc0bb770e3203a9ab0794fe5f19534f34f5b2a58
7
+ data.tar.gz: c47ce4acd75e28b18c49ad7ceca5e66daa775ed05b0a89e7ece1bdf8975a0b2e475ccc584fc00c91f3a9d2678f9fefd2a0d75835868ec0b878e3947357c6a09b
@@ -98,6 +98,8 @@ module ActsAsManyTrees
98
98
  wrk_item=self.create(descendant_id:item.id,ancestor_id:item.id,generation: 0,hierarchy_scope: hierarchy_scope,position: position)
99
99
  end
100
100
  temp_name = SecureRandom.hex
101
+ # BEWARE this works when added default trees and adding default trees to a new scope but it won't work when moving
102
+ # items within a named tree or when adding items from one named scope to another
101
103
  create_tree(wrk_item,wrk_parent,temp_name)
102
104
  delete_item_ancestors(wrk_item)
103
105
  delete_ancestors_of_item_children(wrk_item,hierarchy_scope)
@@ -129,9 +131,10 @@ module ActsAsManyTrees
129
131
  position
130
132
  end
131
133
 
132
- def self.create_tree(wrk_item,wrk_parent,temp_name)
134
+ def self.create_tree(wrk_item,wrk_parent,temp_name,existing_name='')
133
135
  if wrk_parent
134
- sql=<<-SQL
136
+ if existing_name != wrk_item.hierarchy_scope
137
+ sql=<<-SQL
135
138
  insert into #{table_name}(ancestor_id,descendant_id,generation,hierarchy_scope,position)
136
139
  select a.ancestor_id,b.descendant_id,a.generation+b.generation+1,'#{temp_name}',b.position
137
140
  from #{table_name} a, #{table_name} b
@@ -144,7 +147,35 @@ module ActsAsManyTrees
144
147
  from #{table_name} c
145
148
  where c.ancestor_id = #{wrk_item.descendant_id}
146
149
  and c.hierarchy_scope = '#{wrk_item.hierarchy_scope}'
147
- SQL
150
+ union
151
+ select c.ancestor_id,c.descendant_id,c.generation,'#{temp_name}',c.position
152
+ from #{table_name} c
153
+ where c.ancestor_id = #{wrk_item.descendant_id}
154
+ and c.ancestor_id <> c.descendant_id
155
+ and c.hierarchy_scope = '#{existing_name}'
156
+ union
157
+ select #{wrk_parent.descendant_id},c.descendant_id,c.generation,'#{temp_name}',c.position
158
+ from #{table_name} c
159
+ where c.ancestor_id = #{wrk_item.descendant_id}
160
+ and c.ancestor_id <> c.descendant_id
161
+ and c.hierarchy_scope = '#{existing_name}'
162
+ SQL
163
+ else
164
+ sql=<<-SQL
165
+ insert into #{table_name}(ancestor_id,descendant_id,generation,hierarchy_scope,position)
166
+ select a.ancestor_id,b.descendant_id,a.generation+b.generation+1,'#{temp_name}',b.position
167
+ from #{table_name} a, #{table_name} b
168
+ where a.descendant_id=#{wrk_parent.descendant_id}
169
+ and b.ancestor_id=#{wrk_item.ancestor_id}
170
+ and a.hierarchy_scope = b.hierarchy_scope
171
+ and a.hierarchy_scope = '#{wrk_item.hierarchy_scope}'
172
+ union
173
+ select c.ancestor_id,c.descendant_id,c.generation,'#{temp_name}',c.position
174
+ from #{table_name} c
175
+ where c.ancestor_id = #{wrk_item.descendant_id}
176
+ and c.hierarchy_scope = '#{wrk_item.hierarchy_scope}'
177
+ SQL
178
+ end
148
179
  else
149
180
  sql=<<-SQL
150
181
  insert into #{table_name}(ancestor_id,descendant_id,generation,hierarchy_scope,position)
@@ -155,6 +186,10 @@ module ActsAsManyTrees
155
186
  SQL
156
187
  end
157
188
  connection.execute(sql)
189
+ # puts '============='
190
+ # wrk_item.class.find_by_sql("select * from #{table_name} where hierarchy_scope='#{temp_name}'").each do | i |
191
+ # puts "a=#{i.ancestor_id} d=#{i.descendant_id} scope = #{i.hierarchy_scope} wrk.a = #{wrk_item.ancestor_id} wrk.b=#{wrk_item.descendant_id} parent_scope = #{wrk_parent.hierarchy_scope} parent.a =#{wrk_parent.ancestor_id} parent.d =#{wrk_parent.descendant_id}"
192
+ # end
158
193
  end
159
194
 
160
195
  def self.delete_item_ancestors(wrk_item)
@@ -208,19 +243,19 @@ module ActsAsManyTrees
208
243
  def self.reset_descendant_position(parent,before_position,hierarchy_scope='')
209
244
  after_position = parent.position
210
245
  gap = before_position - after_position
211
- # p "before position: #{before_position}, after_position: #{after_position} gap: #{gap}"
212
- # sql = <<-SQL
213
- # select ancestor_id,descendant_id,hierarchy_scope,(#{after_position} + (
214
- # (CAST ((rank() over (partition by ancestor_id order by position)-1) AS numeric))
215
- # /( CAST (count(*) over (partition by ancestor_id) AS numeric)) * #{gap})) as position
216
- # from #{table_name}
217
- # where ancestor_id=#{parent.descendant_id}
218
- # and hierarchy_scope='#{hierarchy_scope}'
219
- # SQL
220
- # res = connection.execute(sql)
221
- # res.each_row do |row|
222
- # p row
223
- # end
246
+ # p "before position: #{before_position}, after_position: #{after_position} gap: #{gap}"
247
+ # sql = <<-SQL
248
+ # select ancestor_id,descendant_id,hierarchy_scope,(#{after_position} + (
249
+ # (CAST ((rank() over (partition by ancestor_id order by position)-1) AS numeric))
250
+ # /( CAST (count(*) over (partition by ancestor_id) AS numeric)) * #{gap})) as position
251
+ # from #{table_name}
252
+ # where ancestor_id=#{parent.descendant_id}
253
+ # and hierarchy_scope='#{hierarchy_scope}'
254
+ # SQL
255
+ # res = connection.execute(sql)
256
+ # res.each_row do |row|
257
+ # p row
258
+ # end
224
259
  sql = <<-SQL
225
260
  with new_position as (select ancestor_id,descendant_id,hierarchy_scope,(#{after_position} + (
226
261
  (CAST ((rank() over (partition by ancestor_id order by position)-1) AS numeric))
@@ -237,13 +272,13 @@ module ActsAsManyTrees
237
272
  and t.hierarchy_scope = new_position.hierarchy_scope
238
273
  SQL
239
274
  connection.execute(sql)
240
- # sql=<<-SQL
241
- # select * from #{table_name} where hierarchy_scope='#{hierarchy_scope}' order by position
242
- # SQL
243
- # res = connection.execute(sql)
244
- # res.each_row do |row|
245
- # p row
246
- # end
275
+ # sql=<<-SQL
276
+ # select * from #{table_name} where hierarchy_scope='#{hierarchy_scope}' order by position
277
+ # SQL
278
+ # res = connection.execute(sql)
279
+ # res.each_row do |row|
280
+ # p row
281
+ # end
247
282
  end
248
283
  end
249
284
  end
@@ -1,3 +1,3 @@
1
1
  module ActsAsManyTrees
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_many_trees
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Small
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-04 00:00:00.000000000 Z
11
+ date: 2016-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails