treeify 0.03 → 0.04

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 +4 -4
  2. data/lib/treeify.rb +51 -24
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 422fe3f0d572c4b67db08dd518122b2d0a04262f
4
- data.tar.gz: bb8044d5cc0267c3f80ad7709f337bd04b826218
3
+ metadata.gz: 6e46207f75973096a118eccdbae623ae2ec680ae
4
+ data.tar.gz: 1fa74c1913e7981730e487b72dc57761286e9937
5
5
  SHA512:
6
- metadata.gz: 7a88024c53a98ffd0ca89ac1d7ce643425ded66d3fda586b3655f94ab10367f82ea47c32bd8f6aac2726b46e60c461aa4ed120cacdf491cee3f83471077333be
7
- data.tar.gz: bd3020aa3d4f182547acd64ba07cd91e54f587ad1fae9655b4dea19f938b85fd16d8ed76c0a70172b4abc884c6a2a78d49152f79c9cd699353186ca9442dcfb0
6
+ metadata.gz: 87e4847de85128b0bf240f3147a389900b1e4e4049bed54bbe7ed61f6f44a1be2cfabb0bcc8e334012f9a71f5c69a507f3bc3737b55ece9f2830226378d4417f
7
+ data.tar.gz: dc8c693c76af128d049cbcfb1b11838b39884e2969afa61d2737c7b885222d82d60e8f179b4c1627397bc532820d333dfbc8e2956d2fa7b90b036399929fc5ae
@@ -1,10 +1,15 @@
1
1
  require 'active_record'
2
+ require 'pp'
2
3
  require "active_support/concern"
3
4
  require "active_support/core_ext/module/attribute_accessors"
4
5
  require "active_support/core_ext/class/attribute"
5
6
 
6
7
  module Treeify
7
- extend ActiveSupport::Concern
8
+ extend ActiveSupport::Concern
9
+
10
+ cattr_writer :cols do
11
+ []
12
+ end
8
13
 
9
14
  included do
10
15
  has_many :children,
@@ -13,30 +18,61 @@ module Treeify
13
18
  belongs_to :parent,
14
19
  class_name: self,
15
20
  foreign_key: "parent_id"
21
+
16
22
  class_attribute :cols
17
23
  scope :roots, -> { where(parent_id: nil) }
18
- scope :tree_for, ->(instance) { where("#{table_name}.id IN (#{tree_sql_for(instance)})").order("#{table_name}.id") }
19
- scope :tree_for_ancestors, ->(instance) { where("#{table_name}.id IN (#{tree_sql_for_ancestors(instance)})").order("#{table_name}.id") }
24
+ scope :tree_for, ->(instance) { self.find_by_sql self.tree_sql_for(instance) }
25
+ scope :tree_for_ancestors, ->(instance) { self.find_by_sql self.tree_sql_for_ancestors(instance) }
20
26
  end
21
27
 
22
28
  module ClassMethods
29
+
30
+ def tree_config(hash = {})
31
+ self.cols = !hash[:cols].nil? == true ? hash[:cols] : []
32
+ end
33
+
34
+ def columns_joined(char=",")
35
+ self.cols ||= []
36
+ self.cols.join(char)
37
+ end
23
38
 
24
- def config(hash = {})
25
- # apparently columns is a reserved word in rails
26
- self.cols = hash[:cols]
39
+ def columns_with_table_name
40
+ self.cols ||= []
41
+ self.cols.map{|c|
42
+ c = "#{table_name}.#{c}"
43
+ }.join(",")
44
+ end
45
+
46
+ def has_config_defined_cols?
47
+ #return true if self.respond_to?("cols") && !self.cols.nil?
48
+ if self.respond_to?("cols")
49
+ return !self.cols.empty? if !self.cols.nil?
50
+ end
51
+ false
52
+ end
53
+
54
+ # sort of hacky, but check to see if we have any columns defined in the config
55
+ # if we do, return the string of columns, formatted appropriately
56
+ # otherwise, just return an empty string
57
+ def appropriate_column_listing(columns = columns_joined)
58
+ has_config_defined_cols? == true ? ", #{columns}" : ""
27
59
  end
28
60
 
29
61
  def tree_sql(instance)
30
- "WITH RECURSIVE cte (id, path) AS (
62
+ cte_params = has_config_defined_cols? ? "id, parent_id, path, #{columns_joined}" : "id, parent_id, path"
63
+
64
+ "WITH RECURSIVE cte (#{cte_params}) AS (
31
65
  SELECT id,
32
- array[id] AS path
66
+ parent_id,
67
+ array[id] AS path#{appropriate_column_listing}
33
68
  FROM #{table_name}
34
69
  WHERE id = #{instance.id}
35
70
 
36
71
  UNION ALL
37
72
 
38
73
  SELECT #{table_name}.id,
39
- cte.path || #{table_name}.id
74
+ #{table_name}.parent_id,
75
+ cte.path || #{table_name}.id#{appropriate_column_listing(columns_with_table_name)}
40
76
  FROM #{table_name}
41
77
  JOIN cte ON #{table_name}.parent_id = cte.id
42
78
  )"
@@ -44,25 +80,14 @@ module Treeify
44
80
 
45
81
  def tree_sql_for(instance)
46
82
  "#{tree_sql(instance)}
47
- SELECT id FROM cte
83
+ SELECT * FROM cte
48
84
  ORDER BY path"
49
85
  end
50
86
 
51
87
  def tree_sql_for_ancestors(instance)
52
- "WITH RECURSIVE cte (id, path) AS (
53
- SELECT id,
54
- array[id] AS path
55
- FROM #{table_name}
56
- WHERE id = #{instance.id}
57
-
58
- UNION ALL
59
-
60
- SELECT #{table_name}.id,
61
- cte.path || #{table_name}.id
62
- FROM #{table_name}
63
- JOIN cte ON #{table_name}.parent_id = cte.id
64
- )
65
- SELECT cte.id FROM cte WHERE cte.id != #{instance.id}"
88
+ "#{tree_sql(instance)}
89
+ SELECT * FROM cte
90
+ WHERE cte.id != #{instance.id}"
66
91
  end
67
92
  end
68
93
 
@@ -111,4 +136,6 @@ module Treeify
111
136
  nested_hash.has_key? item['parent_id']
112
137
  }.values
113
138
  end
139
+
140
+
114
141
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: treeify
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.03'
4
+ version: '0.04'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Devin Austin