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
- case Arel::VERSION
4
- when '1.0.1'
5
- Gem.patching('arel', '1.0.1') do
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
- Arel::Table.class_eval do
23
- def initialize(name, options = {})
24
- @name = name.to_s
25
- @table_exists = nil
26
- @table_alias = nil
27
- @christener = Arel::Sql::Christener.new
28
- @attributes = nil
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
- if options[:as]
36
- as = options[:as].to_s
37
- @table_alias = as unless as == @name
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
- if @engine.connection
45
- begin
46
- require "arel/engines/sql/compilers/#{@engine.adapter_name.downcase}_compiler"
47
- rescue LoadError
48
- begin
49
- # try to load an externally defined compiler, in case this adapter has defined the compiler on its own.
50
- require "#{@engine.adapter_name.downcase}/arel_compiler"
51
- rescue LoadError
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
- @@tables ||= engine.connection.tables
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
- def as(table_alias)
61
- @options ||= {}
62
- Arel::Table.new(name, options.merge(:as => table_alias))
63
- end
55
+ @@tables ||= engine.connection.tables
56
+ end
57
+ end
64
58
 
65
- def table_exists?
66
- @table_exists ||= @@tables.include?(name) || engine.connection.table_exists?(name)
67
- end
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
- def initialize name, engine = Arel::Table.engine
76
- @name = name.to_s
77
- @engine = engine
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
- if Hash === engine
84
- @options = engine # this line has been added
85
- @engine = engine[:engine] || Arel::Table.engine
86
- @columns = attributes_for engine[:columns]
87
+ initialize_without_saving_options *args
88
+ end
87
89
 
88
- # Sometime AR sends an :as parameter to table, to let the table know
89
- # that it is an Alias. We may want to override new, and return a
90
- # TableAlias node?
91
- @table_alias = engine[:as] unless engine[:as].to_s == @name
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
- # this whole method is new
96
- def as(table_alias)
97
- @options ||= {}
98
- Arel::Table.new(name, options.merge(:as => table_alias))
99
- end
100
- end
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
@@ -1,3 +1,3 @@
1
1
  module SimpleNestedSet
2
- VERSION = "0.1.7"
2
+ VERSION = "0.1.8"
3
3
  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: 21
4
+ hash: 11
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 7
10
- version: 0.1.7
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-05-20 00:00:00 Z
19
+ date: 2011-07-12 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  name: activerecord