snapshot_tree 0.0.1 → 3.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5daf342795cf4ed35d72660dc77a8425f299961c
4
+ data.tar.gz: f03ea9a87d1984718dfc82d57d826d2ee7ab4313
5
+ SHA512:
6
+ metadata.gz: 5051a210accbca6efd5e81b65dc62cdb129302b42a5295be4114d18f70578d2b38452f6598e8765c478b235a69238d6adbb774f317cfa8c527b6db587dba7258
7
+ data.tar.gz: b900167e92be2efdb3027115165fd2d20878ffd96f636b3a173bf76ee6b9c286ff812228ce62e980cd40238ce7c82e93374dc5c8e476ac7b6afaf62ae063d138
data/README.md CHANGED
@@ -113,6 +113,14 @@ When calling the class methods, you must pass an model or model id in order to q
113
113
 
114
114
  ```Node.descendent_nodes(grandpa, :depth => 1, :as_of => '2010-01-01')```
115
115
 
116
+
117
+ ## Gem testing
118
+
119
+ 1. Copy spec/database.yml.sample to spec/database.yml and edit to appropriate values
120
+ 2. Create table 'tree_testing'
121
+ 3. Run ```load 'spec/db/schema.rb'``` in console
122
+ 4. ```bundle exec rspec spec/acts_as_tree_spec.rb```
123
+
116
124
  ## Contributing
117
125
 
118
126
  1. Fork it
@@ -59,18 +59,18 @@ module SnapshotTree
59
59
  instance_variable_set :@tree_helper, TreeHelper.new(options)
60
60
 
61
61
  has_many :parent_tree_nodes,
62
+ -> { where(options[:is_active_field].to_sym => true) if options[:is_active_field] },
62
63
  class_name: options[:join_class],
63
64
  foreign_key: options[:child_key],
64
65
  autosave: true,
65
- dependent: options[:dependent],
66
- conditions: options[:is_active_field] ? {options[:is_active_field].to_sym => true} : nil
66
+ dependent: options[:dependent]
67
67
 
68
68
  has_many :child_tree_nodes,
69
+ -> { where(options[:is_active_field].to_sym => true) if options[:is_active_field] },
69
70
  class_name: options[:join_class],
70
71
  foreign_key: options[:parent_key],
71
72
  autosave: true,
72
- dependent: options[:dependent],
73
- conditions: options[:is_active_field] ? {options[:is_active_field].to_sym => true} : nil
73
+ dependent: options[:dependent]
74
74
 
75
75
  "#{options[:node_prefix]}_depth".to_sym.tap do |field|
76
76
  define_method(field) do
@@ -140,10 +140,8 @@ module SnapshotTree
140
140
  def nodes_query(query_type)
141
141
  return @query[query_type] if @query[query_type]
142
142
 
143
- @query[query_type] = Handlebars.compile(
144
- @template["#{query_type}_query"]
145
- ).call(
146
- {
143
+ @query[query_type] = Mustache.render(
144
+ @template["#{query_type}_query"], {
147
145
  model_table: @opts[:model_table],
148
146
  join_table: @opts[:join_table],
149
147
  child_key: "#{@opts[:child_key]}",
@@ -1,36 +1,38 @@
1
1
  root_query: |
2
2
  (
3
3
  SELECT model.* FROM {{model_table}} model
4
- {{#if snapshot_field}}
4
+ {{#snapshot_field}}
5
5
  LEFT JOIN (
6
6
  SELECT {{child_key}}, {{parent_key}}, {{snapshot_field}}, id,
7
- {{#is_active_field}}{{this}}, {{/is_active_field}}
7
+ {{#is_active_field}}{{is_active_field}}, {{/is_active_field}}
8
8
  RANK() OVER (PARTITION BY {{child_key}} ORDER BY {{snapshot_field}} DESC, id DESC)
9
9
  FROM {{join_table}}
10
10
  WHERE {{snapshot_field}} <= '__snapshot_value__'
11
- {{#is_active_field}}AND {{this}}{{/is_active_field}}
11
+ {{#is_active_field}}AND {{is_active_field}}{{/is_active_field}}
12
12
  ) AS tree ON tree.{{child_key}} = model.id AND COALESCE(tree.rank, 1) = 1
13
- {{else}}
13
+ {{/snapshot_field}}
14
+ {{^snapshot_field}}
14
15
  LEFT JOIN {{join_table}} tree ON tree.{{child_key}} = model.id
15
- {{/if}}
16
+ {{/snapshot_field}}
16
17
  WHERE tree.{{parent_key}} IS NULL
17
18
  ) AS {{model_table}}
18
19
 
19
20
  leaf_query: |
20
21
  (
21
22
  SELECT model.* FROM {{model_table}} model
22
- {{#if snapshot_field}}
23
+ {{#snapshot_field}}
23
24
  LEFT JOIN (
24
25
  SELECT {{child_key}}, {{parent_key}}, {{snapshot_field}}, id,
25
- {{#is_active_field}}{{this}}, {{/is_active_field}}
26
+ {{#is_active_field}}{{is_active_field}}, {{/is_active_field}}
26
27
  RANK() OVER (PARTITION BY {{child_key}} ORDER BY {{snapshot_field}} DESC, id DESC)
27
28
  FROM {{join_table}}
28
29
  WHERE {{snapshot_field}} <= '__snapshot_value__'
29
- {{#is_active_field}}AND {{this}}{{/is_active_field}}
30
+ {{#is_active_field}}AND {{is_active_field}}{{/is_active_field}}
30
31
  ) AS tree ON tree.{{parent_key}} = model.id AND COALESCE(tree.rank, 1) = 1
31
- {{else}}
32
+ {{/snapshot_field}}
33
+ {{^snapshot_field}}
32
34
  LEFT JOIN {{join_table}} tree ON tree.{{parent_key}} = model.id
33
- {{/if}}
35
+ {{/snapshot_field}}
34
36
  WHERE tree.{{child_key}} IS NULL
35
37
  ) AS {{model_table}}
36
38
 
@@ -41,7 +43,7 @@ descendent_query: |
41
43
  alias.{{child_key}}, alias.{{parent_key}},
42
44
  ARRAY[alias.{{parent_key}}] AS {{path}}, 1 AS {{depth}}, false AS {{cycle}}
43
45
  FROM {{join_table}} alias
44
- {{#if snapshot_field}}JOIN snapshot ON snapshot.id = alias.id{{/if}}
46
+ {{#snapshot_field}}JOIN snapshot ON snapshot.id = alias.id{{/snapshot_field}}
45
47
  WHERE alias.{{parent_key}} = __model_id__
46
48
  UNION ALL
47
49
  SELECT
@@ -51,24 +53,25 @@ descendent_query: |
51
53
  alias.{{child_key}} = ANY(tree.{{path}}) AS {{cycle}}
52
54
  FROM tree
53
55
  JOIN {{join_table}} alias ON alias.{{parent_key}} = tree.{{child_key}}
54
- {{#if snapshot_field}}JOIN snapshot ON snapshot.id = alias.id{{/if}}
56
+ {{#snapshot_field}}JOIN snapshot ON snapshot.id = alias.id{{/snapshot_field}}
55
57
  WHERE NOT tree.{{cycle}}
56
58
  )
57
59
  ,snapshot AS (
58
60
  SELECT alias.id
59
61
  FROM {{model_table}} model
60
- {{#if snapshot_field}}
62
+ {{#snapshot_field}}
61
63
  LEFT JOIN (
62
64
  SELECT {{child_key}}, {{parent_key}}, {{snapshot_field}}, id,
63
- {{#is_active_field}}{{this}}, {{/is_active_field}}
65
+ {{#is_active_field}}{{is_active_field}}, {{/is_active_field}}
64
66
  RANK() OVER (PARTITION BY {{child_key}} ORDER BY {{snapshot_field}} DESC, id DESC)
65
67
  FROM {{join_table}}
66
68
  WHERE {{snapshot_field}} <= '__snapshot_value__'
67
- {{#is_active_field}}AND {{this}}{{/is_active_field}}
69
+ {{#is_active_field}}AND {{is_active_field}}{{/is_active_field}}
68
70
  ) AS alias ON alias.{{child_key}} = model.id AND COALESCE(rank, 1) = 1
69
- {{else}}
71
+ {{/snapshot_field}}
72
+ {{^snapshot_field}}
70
73
  LEFT JOIN {{join_table}} alias ON alias.{{child_key}} = model.id
71
- {{/if}}
74
+ {{/snapshot_field}}
72
75
  WHERE alias.{{parent_key}} IS NOT NULL
73
76
  )
74
77
  SELECT model.*, tree.*
@@ -84,7 +87,7 @@ ancestor_query: |
84
87
  alias.{{child_key}}, alias.{{parent_key}},
85
88
  ARRAY[alias.{{parent_key}}] AS {{path}}, 1 AS {{depth}}, false AS {{cycle}}
86
89
  FROM {{join_table}} alias
87
- {{#if snapshot_field}}JOIN snapshot ON snapshot.id = alias.id{{/if}}
90
+ {{#snapshot_field}}JOIN snapshot ON snapshot.id = alias.id{{/snapshot_field}}
88
91
  WHERE alias.{{child_key}} = __model_id__
89
92
  UNION ALL
90
93
  SELECT
@@ -94,24 +97,25 @@ ancestor_query: |
94
97
  alias.{{parent_key}} = ANY(tree.{{path}}) AS {{cycle}}
95
98
  FROM tree
96
99
  JOIN {{join_table}} alias ON alias.{{child_key}} = tree.{{parent_key}}
97
- {{#if snapshot_field}}JOIN snapshot ON snapshot.id = alias.id{{/if}}
100
+ {{#snapshot_field}}JOIN snapshot ON snapshot.id = alias.id{{/snapshot_field}}
98
101
  WHERE NOT tree.{{cycle}}
99
102
  )
100
103
  ,snapshot AS (
101
104
  SELECT alias.id
102
105
  FROM {{model_table}} model
103
- {{#if snapshot_field}}
106
+ {{#snapshot_field}}
104
107
  LEFT JOIN (
105
108
  SELECT {{child_key}}, {{parent_key}}, {{snapshot_field}}, id,
106
- {{#is_active_field}}{{this}}, {{/is_active_field}}
109
+ {{#is_active_field}}{{is_active_field}}, {{/is_active_field}}
107
110
  RANK() OVER (PARTITION BY {{child_key}} ORDER BY {{snapshot_field}} DESC, id DESC)
108
111
  FROM {{join_table}}
109
112
  WHERE {{snapshot_field}} <= '__snapshot_value__'
110
- {{#is_active_field}}AND {{this}}{{/is_active_field}}
113
+ {{#is_active_field}}AND {{is_active_field}}{{/is_active_field}}
111
114
  ) AS alias ON alias.{{child_key}} = model.id AND COALESCE(rank, 1) = 1
112
- {{else}}
115
+ {{/snapshot_field}}
116
+ {{^snapshot_field}}
113
117
  LEFT JOIN {{join_table}} alias ON alias.{{child_key}} = model.id
114
- {{/if}}
118
+ {{/snapshot_field}}
115
119
  WHERE alias.{{parent_key}} IS NOT NULL
116
120
  )
117
121
  SELECT model.*, tree.*
@@ -1,3 +1,3 @@
1
1
  module SnapshotTree
2
- VERSION = "0.0.1"
2
+ VERSION = "3.0.1"
3
3
  end
data/lib/snapshot_tree.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require "active_record"
2
- require 'handlebars'
2
+ require 'mustache'
3
3
 
4
4
  require "snapshot_tree/version"
5
5
  require "snapshot_tree/acts_as_tree"
@@ -4,7 +4,7 @@ require File.expand_path('../lib/snapshot_tree/version', __FILE__)
4
4
  Gem::Specification.new do |gem|
5
5
  gem.authors = ["Szetobo"]
6
6
  gem.email = ["szetobo@gmail.com"]
7
- gem.homepage = "https://github.com/szetobo/snapshot_tree"
7
+ gem.homepage = "https://github.com/ABAgile/snapshot_tree"
8
8
  gem.summary = "Mutliple snapshot hierarchical tree implementation of adjacency list using recursive query of Postgresql"
9
9
  gem.description = "Mutliple snapshot hierarchical tree implementation of adjacency list using recursive query of Postgresql"
10
10
 
@@ -15,13 +15,13 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = SnapshotTree::VERSION
17
17
 
18
- gem.add_dependency "activerecord", ">= 3.0.0"
19
- gem.add_dependency "activesupport", ">= 3.0.0"
20
- gem.add_dependency "pg", ">= 0.11.0"
21
- gem.add_dependency "hbs", "~> 0.1.2"
18
+ gem.add_dependency "activerecord", ">= 4.0.0"
19
+ gem.add_dependency "activesupport", ">= 4.0.0"
20
+ gem.add_dependency "pg", ">= 0.17.1"
21
+ gem.add_dependency "mustache", "~> 1.0"
22
22
 
23
23
  gem.add_development_dependency "rake"
24
- gem.add_development_dependency "rspec", "~> 2.6"
24
+ gem.add_development_dependency "rspec", "~> 2.14"
25
25
  gem.add_development_dependency "guard-rspec"
26
26
  gem.add_development_dependency "pry"
27
27
  end
metadata CHANGED
@@ -1,104 +1,127 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: snapshot_tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
5
- prerelease:
4
+ version: 3.0.1
6
5
  platform: ruby
7
6
  authors:
8
7
  - Szetobo
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-02-07 00:00:00.000000000 Z
11
+ date: 2022-01-06 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: activerecord
16
- requirement: &13337080 !ruby/object:Gem::Requirement
17
- none: false
15
+ requirement: !ruby/object:Gem::Requirement
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
- version: 3.0.0
19
+ version: 4.0.0
22
20
  type: :runtime
23
21
  prerelease: false
24
- version_requirements: *13337080
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
25
27
  - !ruby/object:Gem::Dependency
26
28
  name: activesupport
27
- requirement: &13335920 !ruby/object:Gem::Requirement
28
- none: false
29
+ requirement: !ruby/object:Gem::Requirement
29
30
  requirements:
30
- - - ! '>='
31
+ - - ">="
31
32
  - !ruby/object:Gem::Version
32
- version: 3.0.0
33
+ version: 4.0.0
33
34
  type: :runtime
34
35
  prerelease: false
35
- version_requirements: *13335920
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 4.0.0
36
41
  - !ruby/object:Gem::Dependency
37
42
  name: pg
38
- requirement: &13368200 !ruby/object:Gem::Requirement
39
- none: false
43
+ requirement: !ruby/object:Gem::Requirement
40
44
  requirements:
41
- - - ! '>='
45
+ - - ">="
42
46
  - !ruby/object:Gem::Version
43
- version: 0.11.0
47
+ version: 0.17.1
44
48
  type: :runtime
45
49
  prerelease: false
46
- version_requirements: *13368200
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 0.17.1
47
55
  - !ruby/object:Gem::Dependency
48
- name: hbs
49
- requirement: &13365500 !ruby/object:Gem::Requirement
50
- none: false
56
+ name: mustache
57
+ requirement: !ruby/object:Gem::Requirement
51
58
  requirements:
52
- - - ~>
59
+ - - "~>"
53
60
  - !ruby/object:Gem::Version
54
- version: 0.1.2
61
+ version: '1.0'
55
62
  type: :runtime
56
63
  prerelease: false
57
- version_requirements: *13365500
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
58
69
  - !ruby/object:Gem::Dependency
59
70
  name: rake
60
- requirement: &13387820 !ruby/object:Gem::Requirement
61
- none: false
71
+ requirement: !ruby/object:Gem::Requirement
62
72
  requirements:
63
- - - ! '>='
73
+ - - ">="
64
74
  - !ruby/object:Gem::Version
65
75
  version: '0'
66
76
  type: :development
67
77
  prerelease: false
68
- version_requirements: *13387820
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: rspec
71
- requirement: &13385800 !ruby/object:Gem::Requirement
72
- none: false
85
+ requirement: !ruby/object:Gem::Requirement
73
86
  requirements:
74
- - - ~>
87
+ - - "~>"
75
88
  - !ruby/object:Gem::Version
76
- version: '2.6'
89
+ version: '2.14'
77
90
  type: :development
78
91
  prerelease: false
79
- version_requirements: *13385800
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '2.14'
80
97
  - !ruby/object:Gem::Dependency
81
98
  name: guard-rspec
82
- requirement: &13382620 !ruby/object:Gem::Requirement
83
- none: false
99
+ requirement: !ruby/object:Gem::Requirement
84
100
  requirements:
85
- - - ! '>='
101
+ - - ">="
86
102
  - !ruby/object:Gem::Version
87
103
  version: '0'
88
104
  type: :development
89
105
  prerelease: false
90
- version_requirements: *13382620
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
91
111
  - !ruby/object:Gem::Dependency
92
112
  name: pry
93
- requirement: &13405940 !ruby/object:Gem::Requirement
94
- none: false
113
+ requirement: !ruby/object:Gem::Requirement
95
114
  requirements:
96
- - - ! '>='
115
+ - - ">="
97
116
  - !ruby/object:Gem::Version
98
117
  version: '0'
99
118
  type: :development
100
119
  prerelease: false
101
- version_requirements: *13405940
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
102
125
  description: Mutliple snapshot hierarchical tree implementation of adjacency list
103
126
  using recursive query of Postgresql
104
127
  email:
@@ -107,7 +130,7 @@ executables: []
107
130
  extensions: []
108
131
  extra_rdoc_files: []
109
132
  files:
110
- - .gitignore
133
+ - ".gitignore"
111
134
  - Gemfile
112
135
  - Guardfile
113
136
  - LICENSE
@@ -123,35 +146,33 @@ files:
123
146
  - spec/db/schema.rb
124
147
  - spec/setup.rb
125
148
  - spec/spec_helper.rb
126
- homepage: https://github.com/szetobo/snapshot_tree
149
+ homepage: https://github.com/ABAgile/snapshot_tree
127
150
  licenses: []
151
+ metadata: {}
128
152
  post_install_message:
129
153
  rdoc_options: []
130
154
  require_paths:
131
155
  - lib
132
156
  required_ruby_version: !ruby/object:Gem::Requirement
133
- none: false
134
157
  requirements:
135
- - - ! '>='
158
+ - - ">="
136
159
  - !ruby/object:Gem::Version
137
160
  version: '0'
138
- segments:
139
- - 0
140
- hash: 3800555106948873925
141
161
  required_rubygems_version: !ruby/object:Gem::Requirement
142
- none: false
143
162
  requirements:
144
- - - ! '>='
163
+ - - ">="
145
164
  - !ruby/object:Gem::Version
146
165
  version: '0'
147
- segments:
148
- - 0
149
- hash: 3800555106948873925
150
166
  requirements: []
151
167
  rubyforge_project:
152
- rubygems_version: 1.8.15
168
+ rubygems_version: 2.6.14.3
153
169
  signing_key:
154
- specification_version: 3
170
+ specification_version: 4
155
171
  summary: Mutliple snapshot hierarchical tree implementation of adjacency list using
156
172
  recursive query of Postgresql
157
- test_files: []
173
+ test_files:
174
+ - spec/acts_as_tree_spec.rb
175
+ - spec/db/database.yml.sample
176
+ - spec/db/schema.rb
177
+ - spec/setup.rb
178
+ - spec/spec_helper.rb