acts_as_sane_tree 2.0.2 → 2.0.3
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.
data/CHANGELOG.rdoc
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
=== v2.0.3
|
2
|
+
* Use ordered hashes when building nested descendants (.nodes_and_descendants method)
|
3
|
+
* Only apply dynamic depth ordering to descendant scoping
|
4
|
+
* Remove deprecated #to_a references
|
5
|
+
* Remove depth ordering from tree building query
|
6
|
+
|
1
7
|
=== v2.0.2
|
2
8
|
* Fix spelling errors (thanks {Jesse}[https://github.com/jmgarrison])
|
3
9
|
|
data/lib/acts_as_sane_tree.rb
CHANGED
@@ -47,13 +47,25 @@ module ActsAsSaneTree
|
|
47
47
|
# * <tt>order</tt> - makes it possible to sort the children according to this SQL snippet.
|
48
48
|
# * <tt>counter_cache</tt> - keeps a count in a +children_count+ column if set to +true+ (default: +false+).
|
49
49
|
def acts_as_sane_tree(options = {})
|
50
|
-
@configuration = {
|
50
|
+
@configuration = {:foreign_key => :parent_id, :order => nil, :max_depth => 100000, :class => self, :dependent => :destroy, :parent_override => false}
|
51
51
|
@configuration.update(options) if options.is_a?(Hash)
|
52
52
|
|
53
|
-
class_eval do
|
53
|
+
self.class_eval do
|
54
54
|
cattr_accessor :configuration
|
55
|
-
|
56
|
-
has_many :children,
|
55
|
+
|
56
|
+
has_many :children,
|
57
|
+
:class_name => @configuration[:class].name,
|
58
|
+
:foreign_key => @configuration[:foreign_key],
|
59
|
+
:order => @configuration[:order],
|
60
|
+
:dependent => @configuration[:dependent]
|
61
|
+
belongs_to :parent,
|
62
|
+
:class_name => @configuration[:class].name,
|
63
|
+
:foreign_key => @configuration[:foreign_key]
|
64
|
+
if(@configuration[:parent_override])
|
65
|
+
def parent
|
66
|
+
self.class.where(:id => self.parent_id).first
|
67
|
+
end
|
68
|
+
end
|
57
69
|
|
58
70
|
validates_each @configuration[:foreign_key] do |record, attr, value|
|
59
71
|
record.errors.add attr, 'cannot be own parent.' if !record.id.nil? && value.to_i == record.id.to_i
|
@@ -23,7 +23,7 @@ module ActsAsSaneTree
|
|
23
23
|
# Return first root node
|
24
24
|
def root
|
25
25
|
if(rails_3?)
|
26
|
-
configuration[:class].where("#{configuration[:
|
26
|
+
configuration[:class].where("#{configuration[:foreign_key]} IS NULL").order(configuration[:order]).first
|
27
27
|
else
|
28
28
|
configuration[:class].find(
|
29
29
|
:first,
|
@@ -113,38 +113,41 @@ module ActsAsSaneTree
|
|
113
113
|
) SELECT * FROM crumbs) as #{configuration[:class].table_name}"
|
114
114
|
q = nil
|
115
115
|
if(rails_3?)
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
q = q.where(depth_clause)
|
124
|
-
end
|
125
|
-
q = q.order("#{configuration[:class].table_name}.depth ASC, #{configuration[:class].table_name}.parent_id ASC")
|
116
|
+
q = configuration[:class].from(
|
117
|
+
query
|
118
|
+
).where(
|
119
|
+
"#{configuration[:class].table_name}.depth >= 0"
|
120
|
+
)
|
121
|
+
if(depth_clause)
|
122
|
+
q = q.where(depth_clause)
|
126
123
|
end
|
124
|
+
q = q.order(Array(configuration[:order]).join(', '))
|
127
125
|
else
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
q = q.scoped(:conditions => depth_clause)
|
136
|
-
end
|
126
|
+
q = configuration[:class].scoped(
|
127
|
+
:from => query,
|
128
|
+
:conditions => "#{configuration[:class].table_name}.depth >= 0",
|
129
|
+
:order => Array(configuration[:order]).join(', ')
|
130
|
+
)
|
131
|
+
if(depth_clause)
|
132
|
+
q = q.scoped(:conditions => depth_clause)
|
137
133
|
end
|
138
134
|
end
|
135
|
+
if(defined?(ActiveRecord::Relation) && (dfs = retrieve_default_find_scope).is_a?(ActiveRecord::Relation))
|
136
|
+
q = q.scoped(:conditions => dfs.where_values_hash)
|
137
|
+
else
|
138
|
+
q = q.scoped(retrieve_default_find_scope)
|
139
|
+
end
|
139
140
|
unless(raw)
|
140
|
-
res =
|
141
|
-
cache =
|
141
|
+
res = ActiveSupport::OrderedHash.new
|
142
|
+
cache = ActiveSupport::OrderedHash.new
|
142
143
|
q.all.each do |item|
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
144
|
+
res[item] = ActiveSupport::OrderedHash.new
|
145
|
+
cache[item] = res[item]
|
146
|
+
end
|
147
|
+
cache.each_pair do |item, values|
|
148
|
+
if(cache[item.parent])
|
149
|
+
cache[item.parent][item] = values
|
150
|
+
res.delete(item)
|
148
151
|
end
|
149
152
|
end
|
150
153
|
res
|
@@ -154,5 +157,11 @@ module ActsAsSaneTree
|
|
154
157
|
end
|
155
158
|
alias_method :nodes_and_descendents, :nodes_and_descendants
|
156
159
|
|
160
|
+
private
|
161
|
+
|
162
|
+
def retrieve_default_find_scope
|
163
|
+
scope(:find).respond_to?(:call) ? scope(:find).call : scope(:find)
|
164
|
+
end
|
165
|
+
|
157
166
|
end
|
158
167
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_sane_tree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 9
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 2
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 2.0.
|
9
|
+
- 3
|
10
|
+
version: 2.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Chris Roberts
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-09-12 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|