simple_nested_set 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
@@ -1,102 +1,110 @@
|
|
1
1
|
require 'gem_patching'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
@matching_attributes = nil
|
30
|
-
|
31
|
-
if options.is_a?(Hash)
|
32
|
-
@options = options
|
33
|
-
@engine = options[:engine] || Arel::Table.engine
|
3
|
+
# actual patches
|
4
|
+
# patch for Arel 1.0.1
|
5
|
+
module SimpleNestedSetPatchArel1
|
6
|
+
# Arel 1.0.0.rc1 Arel::Table#initialize and #table_exists? does not support
|
7
|
+
# instantiating an Arel::Table before the database table has been created.
|
8
|
+
#
|
9
|
+
# This happens in adva-cms2 during the setup of the cucumber test application
|
10
|
+
# where the environment has to be loaded (and thus models will be loaded) and
|
11
|
+
# migrations will only be run afterwards.
|
12
|
+
#
|
13
|
+
# see http://github.com/rails/arel/commit/19c5a95f1093653d2628dfb2f53637b0425dbba4#commitcomment-133903
|
14
|
+
#
|
15
|
+
# Also, in Arel 1.0.0.rc1 Arel::Table#initialize @options won't be initialized
|
16
|
+
# if the second argument is an engine, so #as will crash subsequently.
|
17
|
+
#
|
18
|
+
# These issues have been fixed in:
|
19
|
+
#
|
20
|
+
# http://github.com/svenfuchs/arel/commit/4b476404cbbecfedc255039c66c6eececb667d7f
|
21
|
+
# http://github.com/svenfuchs/arel/commit/3b1b24551106bc116cba404c992b513c5fbd137b
|
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
|
34
29
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
end
|
39
|
-
else
|
40
|
-
@engine = options # Table.new('foo', engine)
|
41
|
-
@options = {}
|
42
|
-
end
|
30
|
+
if options.is_a?(Hash)
|
31
|
+
@options = options
|
32
|
+
@engine = options[:engine] || Arel::Table.engine
|
43
33
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
raise "#{@engine.adapter_name} is not supported by Arel."
|
53
|
-
end
|
54
|
-
end
|
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
|
55
42
|
|
56
|
-
|
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."
|
57
52
|
end
|
58
53
|
end
|
59
54
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
end
|
55
|
+
@@tables ||= engine.connection.tables
|
56
|
+
end
|
57
|
+
end
|
64
58
|
|
65
|
-
|
66
|
-
|
67
|
-
|
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
|
+
|
69
|
+
# patch for Arel 2.0.x
|
70
|
+
#
|
71
|
+
# we need to save the provided options in order to pass them to the new
|
72
|
+
# Arel-Table. This way, we should be able to maintain the interface and just
|
73
|
+
# add the "as"-funtionality
|
74
|
+
module SimpleNestedSetPatchArel2
|
75
|
+
def self.included(base)
|
76
|
+
base.class_eval do
|
77
|
+
attr_reader :provided_options
|
78
|
+
alias_method :initialize_without_saving_options, :initialize
|
79
|
+
alias_method :initialize, :initialize_with_saving_options
|
68
80
|
end
|
69
81
|
end
|
70
|
-
when /^2\.0\.[5-9]$/, /^2\.1\.[01]$/, '2.0.10'
|
71
|
-
Gem.patching('arel', Arel::VERSION) do
|
72
|
-
Arel::Table.class_eval do
|
73
|
-
attr_reader :options # this line is added
|
74
82
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
@columns = nil
|
79
|
-
@aliases = []
|
80
|
-
@table_alias = nil
|
81
|
-
@primary_key = nil
|
83
|
+
def initialize_with_saving_options(*args) # name, engine = Arel::Table.engine
|
84
|
+
engine = args[1]
|
85
|
+
@provided_options = (engine.is_a?(Hash)) ? engine : {}
|
82
86
|
|
83
|
-
|
84
|
-
|
85
|
-
@engine = engine[:engine] || Arel::Table.engine
|
86
|
-
@columns = attributes_for engine[:columns]
|
87
|
+
initialize_without_saving_options *args
|
88
|
+
end
|
87
89
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
end
|
93
|
-
end
|
90
|
+
def as(table_alias)
|
91
|
+
Arel::Table.new(name, provided_options.merge(:as => table_alias))
|
92
|
+
end
|
93
|
+
end
|
94
94
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
95
|
+
case Arel::VERSION
|
96
|
+
when '1.0.1'
|
97
|
+
Gem.patching('arel', '1.0.1') do
|
98
|
+
Arel::Table.send :include, SimpleNestedSetPatchArel1
|
99
|
+
end
|
100
|
+
when /^2\.0\.[5-9]$/, /^2\.1\.[01]$/, '2.0.10' # successfully tested, could be '~> 2.0.5'
|
101
|
+
Gem.patching('arel', Arel::VERSION) do
|
102
|
+
Arel::Table.send :include, SimpleNestedSetPatchArel2
|
103
|
+
end
|
104
|
+
else
|
105
|
+
# latest tested version to enable Gem-patching-Exceptions
|
106
|
+
# This can be removed once we cover all versions which need this patch
|
107
|
+
Gem.patching('arel', '2.0.10') do
|
108
|
+
Arel::Table.send :include, SimpleNestedSetPatchArel2
|
101
109
|
end
|
102
110
|
end
|
@@ -10,6 +10,8 @@ module SimpleNestedSet
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def run(nested_set, sort_order = :id)
|
13
|
+
nested_set.where(:parent_id => nil).update_all(:parent_id => 0)
|
14
|
+
|
13
15
|
order_columns = ([:parent_id] + Array[sort_order]).uniq.compact
|
14
16
|
|
15
17
|
db_adapter = nested_set.first.class.connection.instance_variable_get('@config')[:adapter].to_sym
|
@@ -25,7 +27,11 @@ module SimpleNestedSet
|
|
25
27
|
end.to_a
|
26
28
|
|
27
29
|
renumber(nodes.dup)
|
28
|
-
nodes.each(&:save)
|
30
|
+
result = nodes.each(&:save)
|
31
|
+
|
32
|
+
nested_set.where(:parent_id => 0).update_all(:parent_id => nil)
|
33
|
+
|
34
|
+
result
|
29
35
|
end
|
30
36
|
|
31
37
|
def renumber(nodes)
|
@@ -45,7 +51,7 @@ module SimpleNestedSet
|
|
45
51
|
end
|
46
52
|
|
47
53
|
def child?(node, child)
|
48
|
-
if child.root?
|
54
|
+
if child.root? || child.parent_id == 0
|
49
55
|
false
|
50
56
|
elsif direct_child?(node, child)
|
51
57
|
true
|
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: 11
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 8
|
10
|
+
version: 0.1.8
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Sven Fuchs
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2011-
|
19
|
+
date: 2011-07-12 00:00:00 Z
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
22
|
name: activerecord
|