treeify 0.01

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/treeify.rb +47 -0
  3. metadata +44 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0e8cd6bfe466cc8caa03a566b068156ba7c8b2d8
4
+ data.tar.gz: 06cbbeb6e8496666055e2b5228a8591fe3b7685e
5
+ SHA512:
6
+ metadata.gz: 8fd7385eb192d70599ef492336d68570ea80a3afce23dd2825a013c9856e1cfaf2e5e77237902d3ecf620ced7ced0573530a831060cefa8b52d530b727870289
7
+ data.tar.gz: 7a9ac3a7aebbb572d4be5fc53e21e020fc212a697634e2bb53627f42193e4b480172fb0de65072b39c0a9d59f8e7c2e15554ba47226e1fd417d9166b5c2ec5ef
@@ -0,0 +1,47 @@
1
+ require 'active_record'
2
+ require "active_support/concern"
3
+ require "active_support/core_ext/module/attribute_accessors"
4
+ require "active_support/core_ext/class/attribute"
5
+
6
+ module Treeify
7
+ extend ActiveSupport::Concern
8
+
9
+ module ClassMethods
10
+ mattr_accessor :table_name
11
+ mattr_accessor :cols
12
+
13
+ def config(hash = {})
14
+ # apparently columns is a reserved word in rails
15
+ self.cols = hash[:cols]
16
+ self.table_name = hash[:table_name]
17
+ end
18
+
19
+ def query
20
+ "WITH RECURSIVE cte (id, #{self.cols.join(',')}, path, parent_id, depth) AS (
21
+ SELECT id,
22
+ #{self.cols.join(',')}
23
+ array[id] AS path,
24
+ parent_id,
25
+ 1 AS depth
26
+ FROM #{self.table_name}
27
+ WHERE parent_id IS NULL
28
+
29
+ UNION ALL
30
+
31
+ SELECT #{self.table_name}.id,
32
+ #{self.cols.map{ |c| self.table_name << '.' << c }.join(',')},
33
+ #{self.table_name}.author,
34
+ cte.path || #{self.table_name}.id,
35
+ #{self.table_name}.parent_id,
36
+ cte.depth + 1 AS depth
37
+ FROM #{self.table_name}
38
+ JOIN cte ON #{self.table_name}.parent_id = cte.id
39
+ )
40
+ SELECT id, #{self.cols.join(',')}, path, depth FROM cte
41
+ ORDER BY path;"
42
+ end
43
+ end
44
+
45
+ module InstanceMethods
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: treeify
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.01'
5
+ platform: ruby
6
+ authors:
7
+ - Devin Austin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Simple trees for ActiveRecord
14
+ email: devin.austin@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/treeify.rb
20
+ homepage: http://rubygems.org/gems/treeify
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.0.7
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Simple trees for ActiveRecord using PostgreSQL
44
+ test_files: []