chrislloyd-even_better_nested_set 0.3 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/lib/even_better_nested_set.rb +25 -30
- data/spec/directory_spec.rb +2 -2
- data/spec/employee_spec.rb +1 -1
- data/spec/spec_helper.rb +2 -2
- metadata +1 -1
data/Rakefile
CHANGED
@@ -5,7 +5,7 @@ require 'date'
|
|
5
5
|
require 'spec/rake/spectask'
|
6
6
|
|
7
7
|
GEM = "even_better_nested_set"
|
8
|
-
GEM_VERSION = "0.3"
|
8
|
+
GEM_VERSION = "0.3.1"
|
9
9
|
AUTHOR = "Jonas Nicklas"
|
10
10
|
EMAIL = "jonas.nicklas@gmail.com"
|
11
11
|
HOMEPAGE = "http://github.com/jnicklas/even_better_nested_set/tree/master"
|
@@ -16,15 +16,17 @@ module EvenBetterNestedSet
|
|
16
16
|
end
|
17
17
|
|
18
18
|
module ClassMethods
|
19
|
-
|
19
|
+
|
20
|
+
attr_accessor :nested_set_options
|
21
|
+
|
20
22
|
def find_last_root
|
21
23
|
find(:first, :order => "#{nested_set_column(:right)} DESC", :conditions => { :parent_id => nil })
|
22
24
|
end
|
23
25
|
|
24
26
|
def find_boundaries(id)
|
25
27
|
query = "SELECT #{nested_set_column(:left)}, #{nested_set_column(:right)}" +
|
26
|
-
"FROM #{
|
27
|
-
"WHERE #{
|
28
|
+
"FROM #{quote_db_property(table_name)}" +
|
29
|
+
"WHERE #{quote_db_property(primary_key)} = #{id}"
|
28
30
|
connection.select_rows(query).first
|
29
31
|
end
|
30
32
|
|
@@ -55,15 +57,8 @@ module EvenBetterNestedSet
|
|
55
57
|
return roots
|
56
58
|
end
|
57
59
|
|
58
|
-
def set_nested_set_columns(left,right)
|
59
|
-
@nested_set_columns = {
|
60
|
-
:left => quote_db_label(left||'lft'),
|
61
|
-
:right => quote_db_label(right||'rgt')
|
62
|
-
}
|
63
|
-
end
|
64
|
-
|
65
60
|
def nested_set_column(name)
|
66
|
-
|
61
|
+
quote_db_property(nested_set_options[name])
|
67
62
|
end
|
68
63
|
|
69
64
|
# Recalculates the left and right values for the entire tree
|
@@ -76,8 +71,8 @@ module EvenBetterNestedSet
|
|
76
71
|
end
|
77
72
|
end
|
78
73
|
|
79
|
-
def
|
80
|
-
"`#{
|
74
|
+
def quote_db_property(property)
|
75
|
+
"`#{property}`".gsub('.','`.`')
|
81
76
|
end
|
82
77
|
|
83
78
|
end
|
@@ -132,7 +127,7 @@ module EvenBetterNestedSet
|
|
132
127
|
|
133
128
|
transaction do
|
134
129
|
reload_boundaries
|
135
|
-
query = "SELECT id FROM #{self.class.
|
130
|
+
query = "SELECT id FROM #{self.class.quote_db_property(base_class.table_name)} " +
|
136
131
|
"WHERE #{nested_set_column(:left)} >= #{left} AND #{nested_set_column(:right)} <= #{right} " +
|
137
132
|
"ORDER BY #{nested_set_column(:left)}"
|
138
133
|
@family_ids = base_class.connection.select_values(query).map(&:to_i)
|
@@ -173,11 +168,19 @@ module EvenBetterNestedSet
|
|
173
168
|
@cached_children ||= []
|
174
169
|
@cached_children.push(*nodes)
|
175
170
|
end
|
176
|
-
|
171
|
+
|
172
|
+
def left
|
173
|
+
read_attribute(self.class.nested_set_options[:left])
|
174
|
+
end
|
175
|
+
|
177
176
|
def left=(left) #:nodoc:
|
178
177
|
raise EvenBetterNestedSet::IllegalAssignmentError, "left is an internal attribute used by EvenBetterNestedSet, do not assign it directly as is may corrupt the data in your database"
|
179
178
|
end
|
180
179
|
|
180
|
+
def right
|
181
|
+
read_attribute(self.class.nested_set_options[:right])
|
182
|
+
end
|
183
|
+
|
181
184
|
def right=(right) #:nodoc:
|
182
185
|
raise EvenBetterNestedSet::IllegalAssignmentError, "right is an internal attribute used by EvenBetterNestedSet, do not assign it directly as is may corrupt the data in your database"
|
183
186
|
end
|
@@ -195,8 +198,6 @@ module EvenBetterNestedSet
|
|
195
198
|
|
196
199
|
protected
|
197
200
|
|
198
|
-
attr_reader :nested_set_options
|
199
|
-
|
200
201
|
def illegal_nesting
|
201
202
|
if parent_id? and family_ids.include?(parent_id)
|
202
203
|
errors.add(:parent_id, 'cannot move node to its own descendant')
|
@@ -268,8 +269,8 @@ module EvenBetterNestedSet
|
|
268
269
|
end
|
269
270
|
|
270
271
|
def set_boundaries(left, right)
|
271
|
-
write_attribute(:left, left)
|
272
|
-
write_attribute(:right, right)
|
272
|
+
write_attribute(self.class.nested_set_options[:left], left)
|
273
|
+
write_attribute(self.class.nested_set_options[:right], right)
|
273
274
|
end
|
274
275
|
|
275
276
|
def reload_boundaries
|
@@ -281,10 +282,10 @@ module EvenBetterNestedSet
|
|
281
282
|
end
|
282
283
|
|
283
284
|
def validate_parent_is_within_scope
|
284
|
-
if nested_set_options[:scope] && parent_id
|
285
|
+
if self.class.nested_set_options[:scope] && parent_id
|
285
286
|
parent.reload # Make sure we are testing the record corresponding to the parent_id
|
286
|
-
if self.send(nested_set_options[:scope]) != parent.send(nested_set_options[:scope])
|
287
|
-
errors.add(:parent_id, "cannot be a record with a different #{nested_set_options[:scope]} to this record")
|
287
|
+
if self.send(self.class.nested_set_options[:scope]) != parent.send(self.class.nested_set_options[:scope])
|
288
|
+
errors.add(:parent_id, "cannot be a record with a different #{self.class.nested_set_options[:scope]} to this record")
|
288
289
|
end
|
289
290
|
end
|
290
291
|
end
|
@@ -293,12 +294,12 @@ module EvenBetterNestedSet
|
|
293
294
|
module ClassMethods
|
294
295
|
|
295
296
|
def acts_as_nested_set(options = {})
|
297
|
+
options = { :left => :left, :right => :right }.merge!(options)
|
296
298
|
options[:scope] = "#{options[:scope]}_id" if options[:scope]
|
297
299
|
|
298
|
-
|
299
300
|
include NestedSet
|
300
301
|
|
301
|
-
|
302
|
+
self.nested_set_options = options
|
302
303
|
|
303
304
|
named_scope :roots, :conditions => { :parent_id => nil }, :order => "#{nested_set_column(:left)} asc"
|
304
305
|
|
@@ -320,12 +321,6 @@ module EvenBetterNestedSet
|
|
320
321
|
validate_on_update :illegal_nesting
|
321
322
|
validate :validate_parent_is_within_scope
|
322
323
|
|
323
|
-
class_eval do
|
324
|
-
define_method :nested_set_options do
|
325
|
-
options
|
326
|
-
end
|
327
|
-
end
|
328
|
-
|
329
324
|
delegate :nested_set_column, :to => "self.class"
|
330
325
|
end
|
331
326
|
|
data/spec/directory_spec.rb
CHANGED
@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/spec_helper'
|
|
2
2
|
require File.dirname(__FILE__) + '/nested_set_behavior'
|
3
3
|
|
4
4
|
class Directory < ActiveRecord::Base
|
5
|
-
acts_as_nested_set :left =>
|
5
|
+
acts_as_nested_set :left => :lft, :right => :rgt
|
6
6
|
|
7
7
|
validates_presence_of :name
|
8
8
|
end
|
@@ -24,4 +24,4 @@ describe Directory do
|
|
24
24
|
|
25
25
|
it_should_behave_like "all nested set models"
|
26
26
|
|
27
|
-
end
|
27
|
+
end
|
data/spec/employee_spec.rb
CHANGED
@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/spec_helper'
|
|
2
2
|
require File.dirname(__FILE__) + '/nested_set_behavior'
|
3
3
|
|
4
4
|
class Employee < ActiveRecord::Base
|
5
|
-
acts_as_nested_set :scope => :company
|
5
|
+
acts_as_nested_set :scope => :company
|
6
6
|
|
7
7
|
validates_presence_of :name
|
8
8
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -49,8 +49,8 @@ end
|
|
49
49
|
class TestMigration < ActiveRecord::Migration
|
50
50
|
def self.up
|
51
51
|
create_table :directories, :force => true do |t|
|
52
|
-
t.column :
|
53
|
-
t.column :
|
52
|
+
t.column :lft, :integer
|
53
|
+
t.column :rgt, :integer
|
54
54
|
t.column :parent_id, :integer
|
55
55
|
t.column :name, :string
|
56
56
|
end
|