simple_nested_set 0.0.12 → 0.0.13
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/simple_nested_set.rb +2 -0
- data/lib/simple_nested_set/act_macro.rb +4 -1
- data/lib/simple_nested_set/inspect.rb +30 -0
- data/lib/simple_nested_set/instance_methods.rb +10 -2
- data/lib/simple_nested_set/move/by_attributes.rb +3 -3
- data/lib/simple_nested_set/patches/arel_table_initialization.rb +68 -0
- data/lib/simple_nested_set/version.rb +1 -1
- metadata +33 -14
data/lib/simple_nested_set.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
require 'active_record'
|
2
|
+
require 'simple_nested_set/patches/arel_table_initialization'
|
2
3
|
|
3
4
|
module SimpleNestedSet
|
4
5
|
ATTRIBUTES = [:parent, :parent_id, :left_id, :right_id, :lft, :rgt, :level, :path]
|
5
6
|
|
6
7
|
autoload :ActMacro, 'simple_nested_set/act_macro'
|
7
8
|
autoload :ClassMethods, 'simple_nested_set/class_methods'
|
9
|
+
autoload :Inspect, 'simple_nested_set/inspect'
|
8
10
|
autoload :InstanceMethods, 'simple_nested_set/instance_methods'
|
9
11
|
autoload :NestedSet, 'simple_nested_set/nested_set'
|
10
12
|
autoload :SqlAbstraction, 'simple_nested_set/sql_abstraction.rb'
|
@@ -3,8 +3,11 @@ module SimpleNestedSet
|
|
3
3
|
def acts_as_nested_set(options = {})
|
4
4
|
return if acts_as_nested_set?
|
5
5
|
|
6
|
-
include SimpleNestedSet::InstanceMethods
|
7
6
|
extend SimpleNestedSet::ClassMethods
|
7
|
+
extend SimpleNestedSet::Inspect
|
8
|
+
|
9
|
+
include SimpleNestedSet::InstanceMethods
|
10
|
+
include SimpleNestedSet::Inspect
|
8
11
|
|
9
12
|
define_callbacks :move, :terminator => "result == false"
|
10
13
|
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module SimpleNestedSet
|
2
|
+
module Inspect
|
3
|
+
def inspect_tree(attributes = [:id, :lft, :rgt, :parent_id, :slug, :path, :level])
|
4
|
+
if is_a?(Class)
|
5
|
+
nodes = all
|
6
|
+
".\n" << with_exclusive_scope { Inspect.tree(nodes) }
|
7
|
+
else
|
8
|
+
".\n" << Inspect.tree([self])
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class << self
|
13
|
+
def tree(nodes, indent = '')
|
14
|
+
nodes.inject('') do |out, node|
|
15
|
+
last = node == nodes.last
|
16
|
+
out << line(node, indent, last)
|
17
|
+
out << tree(node.children, next_indent(indent, last))
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def line(node, indent, last)
|
22
|
+
"#{indent}#{last ? '└' : '├'}── #{node.class.name} id: #{node.id}\n"
|
23
|
+
end
|
24
|
+
|
25
|
+
def next_indent(indent, last)
|
26
|
+
"#{indent}#{last ? ' ' : '| '}"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -153,12 +153,20 @@ module SimpleNestedSet
|
|
153
153
|
|
154
154
|
# Move the node to the left of another node
|
155
155
|
def move_to_left_of(node)
|
156
|
-
|
156
|
+
if node
|
157
|
+
nested_set.move_to(node, :left)
|
158
|
+
elsif left_most = siblings.first
|
159
|
+
move_to_right_of(left_most)
|
160
|
+
end
|
157
161
|
end
|
158
162
|
|
159
163
|
# Move the node to the left of another node
|
160
164
|
def move_to_right_of(node)
|
161
|
-
|
165
|
+
if node
|
166
|
+
nested_set.move_to(node, :right)
|
167
|
+
elsif right_most = siblings.last
|
168
|
+
move_to_left_of(right_most)
|
169
|
+
end
|
162
170
|
end
|
163
171
|
|
164
172
|
# Makes this node to the given path
|
@@ -16,11 +16,11 @@ module SimpleNestedSet
|
|
16
16
|
def perform
|
17
17
|
if path && node.path_changed?
|
18
18
|
node.move_to_path(path)
|
19
|
-
elsif left_id && left_id != node.id
|
19
|
+
elsif attributes.key?(:left_id) && left_id != node.id
|
20
20
|
node.move_to_right_of(left_id)
|
21
|
-
elsif right_id && right_id != node.id
|
21
|
+
elsif attributes.key?(:right_id) && right_id != node.id
|
22
22
|
node.move_to_left_of(right_id)
|
23
|
-
elsif parent_id && parent_id != node.parent_id
|
23
|
+
elsif attributes.key?(:parent_id) && parent_id != node.parent_id
|
24
24
|
node.move_to_child_of(parent_id)
|
25
25
|
end
|
26
26
|
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'gem_patching'
|
2
|
+
|
3
|
+
# Arel 1.0.0.rc1 Arel::Table#initialize and #table_exists? does not support
|
4
|
+
# instantiating an Arel::Table before the database table has been created.
|
5
|
+
#
|
6
|
+
# This happens in adva-cms2 during the setup of the cucumber test application
|
7
|
+
# where the environment has to be loaded (and thus models will be loaded) and
|
8
|
+
# migrations will only be run afterwards.
|
9
|
+
#
|
10
|
+
# see http://github.com/rails/arel/commit/19c5a95f1093653d2628dfb2f53637b0425dbba4#commitcomment-133903
|
11
|
+
#
|
12
|
+
# Also, in Arel 1.0.0.rc1 Arel::Table#initialize @options won't be initialized
|
13
|
+
# if the second argument is an engine, so #as will crash subsequently.
|
14
|
+
#
|
15
|
+
# These issues have been fixed in:
|
16
|
+
#
|
17
|
+
# http://github.com/svenfuchs/arel/commit/4b476404cbbecfedc255039c66c6eececb667d7f
|
18
|
+
# http://github.com/svenfuchs/arel/commit/3b1b24551106bc116cba404c992b513c5fbd137b
|
19
|
+
|
20
|
+
Gem.patching('arel', '1.0.1') do
|
21
|
+
Arel::Table.class_eval do
|
22
|
+
def initialize(name, options = {})
|
23
|
+
@name = name.to_s
|
24
|
+
@table_exists = nil
|
25
|
+
@table_alias = nil
|
26
|
+
@christener = Arel::Sql::Christener.new
|
27
|
+
@attributes = nil
|
28
|
+
@matching_attributes = nil
|
29
|
+
|
30
|
+
if options.is_a?(Hash)
|
31
|
+
@options = options
|
32
|
+
@engine = options[:engine] || Arel::Table.engine
|
33
|
+
|
34
|
+
if options[:as]
|
35
|
+
as = options[:as].to_s
|
36
|
+
@table_alias = as unless as == @name
|
37
|
+
end
|
38
|
+
else
|
39
|
+
@engine = options # Table.new('foo', engine)
|
40
|
+
@options = {}
|
41
|
+
end
|
42
|
+
|
43
|
+
if @engine.connection
|
44
|
+
begin
|
45
|
+
require "arel/engines/sql/compilers/#{@engine.adapter_name.downcase}_compiler"
|
46
|
+
rescue LoadError
|
47
|
+
begin
|
48
|
+
# try to load an externally defined compiler, in case this adapter has defined the compiler on its own.
|
49
|
+
require "#{@engine.adapter_name.downcase}/arel_compiler"
|
50
|
+
rescue LoadError
|
51
|
+
raise "#{@engine.adapter_name} is not supported by Arel."
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
@@tables ||= engine.connection.tables
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def as(table_alias)
|
60
|
+
@options ||= {}
|
61
|
+
Arel::Table.new(name, options.merge(:as => table_alias))
|
62
|
+
end
|
63
|
+
|
64
|
+
def table_exists?
|
65
|
+
@table_exists ||= @@tables.include?(name) || engine.connection.table_exists?(name)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_nested_set
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 5
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 13
|
10
|
+
version: 0.0.13
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Sven Fuchs
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-09-
|
18
|
+
date: 2010-09-29 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -24,15 +24,14 @@ dependencies:
|
|
24
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
25
|
none: false
|
26
26
|
requirements:
|
27
|
-
- -
|
27
|
+
- - ~>
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
hash:
|
29
|
+
hash: 7
|
30
30
|
segments:
|
31
31
|
- 3
|
32
32
|
- 0
|
33
33
|
- 0
|
34
|
-
|
35
|
-
version: 3.0.0.beta4
|
34
|
+
version: 3.0.0
|
36
35
|
type: :runtime
|
37
36
|
version_requirements: *id001
|
38
37
|
- !ruby/object:Gem::Dependency
|
@@ -50,7 +49,7 @@ dependencies:
|
|
50
49
|
type: :runtime
|
51
50
|
version_requirements: *id002
|
52
51
|
- !ruby/object:Gem::Dependency
|
53
|
-
name:
|
52
|
+
name: gem_patching
|
54
53
|
prerelease: false
|
55
54
|
requirement: &id003 !ruby/object:Gem::Requirement
|
56
55
|
none: false
|
@@ -61,10 +60,10 @@ dependencies:
|
|
61
60
|
segments:
|
62
61
|
- 0
|
63
62
|
version: "0"
|
64
|
-
type: :
|
63
|
+
type: :runtime
|
65
64
|
version_requirements: *id003
|
66
65
|
- !ruby/object:Gem::Dependency
|
67
|
-
name:
|
66
|
+
name: database_cleaner
|
68
67
|
prerelease: false
|
69
68
|
requirement: &id004 !ruby/object:Gem::Requirement
|
70
69
|
none: false
|
@@ -78,7 +77,7 @@ dependencies:
|
|
78
77
|
type: :development
|
79
78
|
version_requirements: *id004
|
80
79
|
- !ruby/object:Gem::Dependency
|
81
|
-
name:
|
80
|
+
name: test_declarative
|
82
81
|
prerelease: false
|
83
82
|
requirement: &id005 !ruby/object:Gem::Requirement
|
84
83
|
none: false
|
@@ -91,7 +90,25 @@ dependencies:
|
|
91
90
|
version: "0"
|
92
91
|
type: :development
|
93
92
|
version_requirements: *id005
|
94
|
-
|
93
|
+
- !ruby/object:Gem::Dependency
|
94
|
+
name: pathname_local
|
95
|
+
prerelease: false
|
96
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
hash: 3
|
102
|
+
segments:
|
103
|
+
- 0
|
104
|
+
version: "0"
|
105
|
+
type: :development
|
106
|
+
version_requirements: *id006
|
107
|
+
description: |
|
108
|
+
SimpleNestedSet is a gem which gives the ability to handle nested sets in ActiveRecord-Objects
|
109
|
+
|
110
|
+
It is built upon ActiveRecord3 and therefore Rails3-ready
|
111
|
+
|
95
112
|
email: svenfuchs@artweb-design.de
|
96
113
|
executables: []
|
97
114
|
|
@@ -103,11 +120,13 @@ files:
|
|
103
120
|
- lib/simple_nested_set.rb
|
104
121
|
- lib/simple_nested_set/act_macro.rb
|
105
122
|
- lib/simple_nested_set/class_methods.rb
|
123
|
+
- lib/simple_nested_set/inspect.rb
|
106
124
|
- lib/simple_nested_set/instance_methods.rb
|
107
125
|
- lib/simple_nested_set/move/by_attributes.rb
|
108
126
|
- lib/simple_nested_set/move/protection.rb
|
109
127
|
- lib/simple_nested_set/move/to_target.rb
|
110
128
|
- lib/simple_nested_set/nested_set.rb
|
129
|
+
- lib/simple_nested_set/patches/arel_table_initialization.rb
|
111
130
|
- lib/simple_nested_set/rebuild/from_paths.rb
|
112
131
|
- lib/simple_nested_set/sql_abstraction.rb
|
113
132
|
- lib/simple_nested_set/version.rb
|
@@ -144,6 +163,6 @@ rubyforge_project: "[none]"
|
|
144
163
|
rubygems_version: 1.3.7
|
145
164
|
signing_key:
|
146
165
|
specification_version: 3
|
147
|
-
summary:
|
166
|
+
summary: a simple to use nested set solution for ActiveRecord 3
|
148
167
|
test_files: []
|
149
168
|
|