acts_as_ordered_tree 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- ODI4NmU4NzcxYzdlNDY3ZTlmMTQ1ZGMyOWQ3MjhjYzc2OTVmZjZlZQ==
5
- data.tar.gz: !binary |-
6
- YzU5ZDA3YTU5NzIxOWIwYjFiZmE3ZTljYjE2OTkzYTM1NDJmNWEyNw==
2
+ SHA1:
3
+ metadata.gz: 86dc7f385344addb7fa9390045711d13e450e08f
4
+ data.tar.gz: 00ed12a00031723963094178dc4820cfde4952b2
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- NjkyMzE1NzIzNjNhNTZiNWJlZDA5OTg4MDE4NjZiZGVlOWE3OGE1M2QwYjQy
10
- ZjYwMWI4ZmRjMjhlNWYyMDRiYWUzMjU4OTU5YjE2YjEwNjhmMjRlZDFjZjlk
11
- MjhmM2NhNDNlZTAyOWNiZDA0M2FmZDc0Nzc0ZmRlYjAxOGQzYzM=
12
- data.tar.gz: !binary |-
13
- YzlmNjMxYTZkYWEzMjE0ZGZlZjQ4ODQ3ZmIxNmVlZjc0MDczYzEwMmFiNTcw
14
- ZDhhODgzZWFhNjM2MWIyYTUzOWU4YTlmN2U2MWY4OWNlMWIzYWRiMmI2MjE5
15
- ZDc3MzdkM2M4ZjBlZjE5MzUyZDMwNDNkZjg3YzliZmQxYThiY2E=
6
+ metadata.gz: 7d7dfc4a087d7cdc79f7f819b4d732cdca4371950421f7b1bad0daf2e355469675a58b5a3f07fa83311dfabbd3892b3db73fd5ffd26fd7a9bb20fbaeea2e4a99
7
+ data.tar.gz: 8e4647629a8719db47d2875306a28bcea5932d87c2c0a4c5dfd14a5c7020a1bc84b1e2ee41e6b1ef387aec64c204c15ca253a7dc935ec1ad217a8c50a9ebf713
@@ -1,5 +1,3 @@
1
- require "acts_as_ordered_tree/relation/recursive"
2
-
3
1
  module ActsAsOrderedTree
4
2
  module Adapters
5
3
  module PostgreSQLAdapter
@@ -16,8 +14,8 @@ module ActsAsOrderedTree
16
14
  INNER JOIN self_and_ancestors ON alias1.id = self_and_ancestors.#{parent_column}
17
15
  QUERY
18
16
 
19
- recursive_scope.with_recursive("self_and_ancestors", query).
20
- order("self_and_ancestors._depth DESC")
17
+ with_recursive_join(query, 'self_and_ancestors').
18
+ order('self_and_ancestors._depth DESC')
21
19
  else
22
20
  ancestors + [self]
23
21
  end
@@ -35,8 +33,7 @@ module ActsAsOrderedTree
35
33
  INNER JOIN ancestors ON alias1.id = ancestors.#{parent_column}
36
34
  QUERY
37
35
 
38
- recursive_scope.with_recursive("ancestors", query).
39
- order("ancestors._depth DESC")
36
+ with_recursive_join(query, 'ancestors').order('ancestors._depth DESC')
40
37
  end
41
38
 
42
39
  def root
@@ -54,8 +51,8 @@ module ActsAsOrderedTree
54
51
  #{self.class.quoted_table_name} alias1 ON alias1.parent_id = descendants.id
55
52
  QUERY
56
53
 
57
- recursive_scope.with_recursive("descendants", query).
58
- order("descendants._positions ASC")
54
+ with_recursive_join(query, 'descendants').
55
+ order('descendants._positions ASC')
59
56
  end
60
57
 
61
58
  def descendants
@@ -66,6 +63,35 @@ module ActsAsOrderedTree
66
63
  def recursive_scope
67
64
  ActsAsOrderedTree::Relation::Recursive.new(ordered_tree_scope)
68
65
  end
66
+
67
+ def with_recursive_join(recursive_query_sql, aliaz)
68
+ join_sql = 'INNER JOIN (' +
69
+ "WITH RECURSIVE #{aliaz} AS (" +
70
+ recursive_query_sql +
71
+ ") SELECT * FROM #{aliaz} " +
72
+ ") #{aliaz} ON #{aliaz}.id = #{self.class.quoted_table_name}.id"
73
+
74
+ ordered_tree_scope.joins(join_sql)
75
+ end
76
+
77
+ # Rails 3.0 does not support update_all with joins, so we patch it :(
78
+ if ActiveRecord::VERSION::STRING <= '3.1.0'
79
+ module Rails30UpdateAllPatch
80
+ def update_all(updates, conditions = nil, options = {})
81
+ relation = except(:joins, :where).
82
+ where(:id => select(klass.arel_table[:id]).except(:order, :limit).arel)
83
+ relation.update_all(updates, conditions, options)
84
+ end
85
+ end
86
+
87
+ def with_recursive_join_30(recursive_query_sql, aliaz)
88
+ relation = with_recursive_join_31(recursive_query_sql, aliaz)
89
+ relation.extend(Rails30UpdateAllPatch)
90
+ relation
91
+ end
92
+ alias_method :with_recursive_join_31, :with_recursive_join
93
+ alias_method :with_recursive_join, :with_recursive_join_30
94
+ end
69
95
  end
70
96
  end
71
97
  end
@@ -1,3 +1,3 @@
1
1
  module ActsAsOrderedTree
2
- VERSION = '1.2.0'
2
+ VERSION = '1.2.1'
3
3
  end
data/spec/db/config.yml CHANGED
@@ -9,5 +9,5 @@ mysql:
9
9
  password:
10
10
  encoding: utf8
11
11
  sqlite3:
12
- adapter: sqlite3
12
+ adapter: <%= "jdbc" if defined? JRUBY_VERSION %>sqlite3
13
13
  database: acts_as_ordered_tree.sqlite3.db
data/spec/spec_helper.rb CHANGED
@@ -1,37 +1,39 @@
1
+ ENV['DB'] ||= 'pg'
1
2
  test_dir = File.dirname(__FILE__)
2
3
 
3
- require "rubygems"
4
- require "bundler/setup"
4
+ require 'rubygems'
5
+ require 'bundler/setup'
5
6
 
6
- require "rspec"
7
- require "rspec-expectations"
7
+ require 'rspec'
8
+ require 'rspec-expectations'
8
9
 
9
10
  begin
10
- require "simplecov"
11
+ require 'simplecov'
11
12
  SimpleCov.start
12
13
  rescue LoadError
13
14
  #ignore
14
15
  end
15
16
 
16
- require "active_record"
17
- require "factory_girl"
17
+ require 'active_record'
18
+ require 'factory_girl'
18
19
 
19
- require "acts_as_ordered_tree"
20
- require "logger"
21
- require "yaml"
20
+ require 'acts_as_ordered_tree'
21
+ require 'logger'
22
+ require 'yaml'
23
+ require 'erb'
22
24
 
23
25
  config_file = ENV['DBCONF'] || 'config.yml'
24
26
 
25
- ActiveRecord::Base.configurations = YAML::load(IO.read(File.join(test_dir, 'db', config_file)))
26
- ActiveRecord::Base.establish_connection(ENV['DB'] || "pg")
27
+ ActiveRecord::Base.configurations = YAML::load(ERB.new(IO.read(File.join(test_dir, 'db', config_file))).result)
28
+ ActiveRecord::Base.establish_connection(ENV['DB'])
27
29
  ActiveRecord::Base.logger = Logger.new(ENV['DEBUG'] ? $stderr : '/dev/null')
28
30
  ActiveRecord::Migration.verbose = false
29
- load(File.join(test_dir, "db", "schema.rb"))
31
+ load(File.join(test_dir, 'db', 'schema.rb'))
30
32
 
31
- require "shoulda-matchers"
32
- require "support/models"
33
- require "support/factories"
34
- require "support/matchers"
33
+ require 'shoulda-matchers'
34
+ require 'support/models'
35
+ require 'support/factories'
36
+ require 'support/matchers'
35
37
 
36
38
  RSpec.configure do |config|
37
39
  config.include FactoryGirl::Syntax::Methods
@@ -1,5 +1,7 @@
1
1
  class Default < ActiveRecord::Base
2
- self.table_name = "categories"
2
+ self.table_name = 'categories'
3
+
4
+ default_scope { where('1=1') }
3
5
 
4
6
  acts_as_ordered_tree
5
7
  end
@@ -8,19 +10,25 @@ class RenamedColumns < ActiveRecord::Base
8
10
  acts_as_ordered_tree :parent_column => :mother_id,
9
11
  :position_column => :red,
10
12
  :depth_column => :pitch
13
+
14
+ default_scope { where('1=1') }
11
15
  end
12
16
 
13
17
  class DefaultWithCounterCache < ActiveRecord::Base
14
- self.table_name = "categories"
18
+ self.table_name = 'categories'
15
19
 
16
20
  acts_as_ordered_tree :counter_cache => :categories_count
21
+
22
+ default_scope { where('1=1') }
17
23
  end
18
24
 
19
25
  class DefaultWithCallbacks < ActiveRecord::Base
20
- self.table_name = "categories"
26
+ self.table_name = 'categories'
21
27
 
22
28
  acts_as_ordered_tree
23
29
 
30
+ default_scope { where('1=1') }
31
+
24
32
  after_move :after_move
25
33
  before_move :before_move
26
34
  after_reorder :after_reorder
@@ -33,7 +41,9 @@ class DefaultWithCallbacks < ActiveRecord::Base
33
41
  end
34
42
 
35
43
  class Scoped < ActiveRecord::Base
36
- self.table_name = "scoped"
44
+ self.table_name = 'scoped'
45
+
46
+ default_scope { where('1=1') }
37
47
 
38
48
  acts_as_ordered_tree :scope => :scope_type
39
49
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_ordered_tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexei Mikhailov
@@ -9,76 +9,76 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-12-03 00:00:00.000000000 Z
12
+ date: 2013-12-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - ! '>='
18
+ - - '>='
19
19
  - !ruby/object:Gem::Version
20
20
  version: 3.0.0
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - ! '>='
25
+ - - '>='
26
26
  - !ruby/object:Gem::Version
27
27
  version: 3.0.0
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: rake
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - ! '>='
32
+ - - '>='
33
33
  - !ruby/object:Gem::Version
34
34
  version: 0.9.2
35
35
  type: :development
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - ! '>='
39
+ - - '>='
40
40
  - !ruby/object:Gem::Version
41
41
  version: 0.9.2
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: bundler
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
- - - ! '>='
46
+ - - '>='
47
47
  - !ruby/object:Gem::Version
48
48
  version: '1.0'
49
49
  type: :development
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
- - - ! '>='
53
+ - - '>='
54
54
  - !ruby/object:Gem::Version
55
55
  version: '1.0'
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: rspec
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
- - - ! '>='
60
+ - - '>='
61
61
  - !ruby/object:Gem::Version
62
62
  version: '2.11'
63
63
  type: :development
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
- - - ! '>='
67
+ - - '>='
68
68
  - !ruby/object:Gem::Version
69
69
  version: '2.11'
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: shoulda-matchers
72
72
  requirement: !ruby/object:Gem::Requirement
73
73
  requirements:
74
- - - ! '>='
74
+ - - '>='
75
75
  - !ruby/object:Gem::Version
76
76
  version: 1.2.0
77
77
  type: :development
78
78
  prerelease: false
79
79
  version_requirements: !ruby/object:Gem::Requirement
80
80
  requirements:
81
- - - ! '>='
81
+ - - '>='
82
82
  - !ruby/object:Gem::Version
83
83
  version: 1.2.0
84
84
  - !ruby/object:Gem::Dependency
@@ -99,14 +99,14 @@ dependencies:
99
99
  name: appraisal
100
100
  requirement: !ruby/object:Gem::Requirement
101
101
  requirements:
102
- - - ! '>='
102
+ - - '>='
103
103
  - !ruby/object:Gem::Version
104
104
  version: 0.4.0
105
105
  type: :development
106
106
  prerelease: false
107
107
  version_requirements: !ruby/object:Gem::Requirement
108
108
  requirements:
109
- - - ! '>='
109
+ - - '>='
110
110
  - !ruby/object:Gem::Version
111
111
  version: 0.4.0
112
112
  description:
@@ -123,7 +123,6 @@ files:
123
123
  - lib/acts_as_ordered_tree/instance_methods.rb
124
124
  - lib/acts_as_ordered_tree/relation/base.rb
125
125
  - lib/acts_as_ordered_tree/relation/preloaded.rb
126
- - lib/acts_as_ordered_tree/relation/recursive.rb
127
126
  - lib/acts_as_ordered_tree/tenacious_transaction.rb
128
127
  - lib/acts_as_ordered_tree/validators.rb
129
128
  - lib/acts_as_ordered_tree/version.rb
@@ -145,17 +144,17 @@ require_paths:
145
144
  - lib
146
145
  required_ruby_version: !ruby/object:Gem::Requirement
147
146
  requirements:
148
- - - ! '>='
147
+ - - '>='
149
148
  - !ruby/object:Gem::Version
150
149
  version: '0'
151
150
  required_rubygems_version: !ruby/object:Gem::Requirement
152
151
  requirements:
153
- - - ! '>='
152
+ - - '>='
154
153
  - !ruby/object:Gem::Version
155
154
  version: '0'
156
155
  requirements: []
157
156
  rubyforge_project: acts_as_ordered_tree
158
- rubygems_version: 2.1.3
157
+ rubygems_version: 2.1.11
159
158
  signing_key:
160
159
  specification_version: 4
161
160
  summary: ActiveRecord extension for sorted adjacency lists support
@@ -1,57 +0,0 @@
1
- require "acts_as_ordered_tree/relation/base"
2
-
3
- module ActsAsOrderedTree
4
- module Relation
5
- # Recursive relation fixes Rails3.0 issue https://github.com/rails/rails/issues/522 for
6
- # relations with joins to subqueries
7
- class Recursive < Base
8
- attr_accessor :recursive_table_value, :recursive_query_value
9
-
10
- # relation.with_recursive("table_name", "SELECT * FROM table_name")
11
- def with_recursive(recursive_table_name, query)
12
- relation = clone
13
- relation.recursive_table_value = recursive_table_name
14
- relation.recursive_query_value = query
15
- relation
16
- end
17
-
18
- def build_arel
19
- if recursive_table_value && recursive_query_value
20
- join_sql = "INNER JOIN (" +
21
- recursive_query_sql +
22
- ") AS #{recursive_table_value} ON #{recursive_table_value}.id = #{table.name}.id"
23
-
24
- except(:recursive_table, :recursive_query).joins(join_sql).build_arel
25
- else
26
- super
27
- end
28
- end
29
-
30
- def update_all(updates, conditions = nil, options = {})
31
- if recursive_table_value && recursive_query_value
32
- scope = where("id IN (SELECT id FROM (#{recursive_query_sql}) AS #{recursive_table_value})").
33
- except(:recursive_table, :recursive_query, :limit, :order)
34
-
35
- scope.update_all(updates, conditions, options)
36
- else
37
- super
38
- end
39
- end
40
-
41
- def except(*skips)
42
- result = super
43
- ([:recursive_table, :recursive_query] - skips).each do |method|
44
- result.send("#{method}_value=", send(:"#{method}_value"))
45
- end
46
-
47
- result
48
- end
49
-
50
- private
51
- def recursive_query_sql
52
- "WITH RECURSIVE #{recursive_table_value} AS (#{recursive_query_value}) " +
53
- "SELECT * FROM #{recursive_table_value}"
54
- end
55
- end
56
- end
57
- end