Skip to content

Commit e5d65bb

Browse files
authored
Merge pull request #221 from aaronrussell/rails-5
Rails 5 tests passing. Fixes #220
2 parents f3a35cc + 8d54f2d commit e5d65bb

File tree

8 files changed

+72
-14
lines changed

8 files changed

+72
-14
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ tmp/
1111
.rvmrc
1212
*.lock
1313
tmp/
14-
.ruby-version
14+
.ruby-*

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ rvm:
99
gemfile:
1010
- gemfiles/activerecord_4.1.gemfile
1111
- gemfiles/activerecord_4.2.gemfile
12+
- gemfiles/activerecord_5.0.rc1.gemfile
1213
- gemfiles/activerecord_edge.gemfile
1314

1415
env:

Appraisals

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,19 @@ end
88

99
appraise 'activerecord-4.2' do
1010
gem 'activerecord', '~> 4.2.0'
11+
1112
platforms :ruby, :rbx do
1213
gem 'mysql2', '~> 0.3.20'
1314
end
1415
end
1516

17+
appraise 'activerecord-5.0.rc1' do
18+
gem 'activerecord', '~> 5.0.0.rc1'
19+
gem 'actionpack', '~> 5.0.0.rc1'
20+
gem 'railties', '~> 5.0.0.rc1'
21+
gem 'rspec-rails', '>= 3.5.0.beta4'
22+
end
23+
1624
appraise 'activerecord-edge' do
1725
gem 'activerecord', github: 'rails/rails'
1826
gem 'arel', github: 'rails/arel'
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# This file was generated by Appraisal
2+
3+
source "https://rubygems.org"
4+
5+
gem "activerecord", "~> 5.0.0.rc1"
6+
gem "actionpack", "~> 5.0.0.rc1"
7+
gem "railties", "~> 5.0.0.rc1"
8+
gem "rspec-rails", ">= 3.5.0.beta4"
9+
10+
platforms :ruby, :rbx do
11+
gem "mysql2"
12+
gem "pg"
13+
gem "sqlite3"
14+
end
15+
16+
platforms :jruby do
17+
gem "activerecord-jdbcmysql-adapter"
18+
gem "activerecord-jdbcpostgresql-adapter"
19+
gem "activerecord-jdbcsqlite3-adapter"
20+
end
21+
22+
gemspec :path => "../"

lib/closure_tree/hash_tree.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module ClassMethods
1111
# There is no default depth limit. This might be crazy-big, depending
1212
# on your tree shape. Hash huge trees at your own peril!
1313
def hash_tree(options = {})
14-
_ct.hash_tree(nil, options[:limit_depth])
14+
_ct.hash_tree(_ct.default_tree_scope(all, options[:limit_depth]))
1515
end
1616
end
1717
end

lib/closure_tree/hash_tree_support.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module ClosureTree
22
module HashTreeSupport
3-
def default_tree_scope(limit_depth = nil)
3+
def default_tree_scope(scope, limit_depth = nil)
44
# Deepest generation, within limit, for each descendant
55
# NOTE: Postgres requires HAVING clauses to always contains aggregate functions (!!)
66
having_clause = limit_depth ? "HAVING MAX(generations) <= #{limit_depth - 1}" : ''
@@ -13,15 +13,11 @@ def default_tree_scope(limit_depth = nil)
1313
) AS generation_depth
1414
ON #{quoted_table_name}.#{model_class.primary_key} = generation_depth.descendant_id
1515
SQL
16-
scope_with_order(model_class.joins(generation_depth), 'generation_depth.depth')
16+
scope_with_order(scope.joins(generation_depth), 'generation_depth.depth')
1717
end
1818

1919
def hash_tree(tree_scope, limit_depth = nil)
20-
limited_scope = if tree_scope
21-
limit_depth ? tree_scope.where("#{quoted_hierarchy_table_name}.generations <= #{limit_depth - 1}") : tree_scope
22-
else
23-
default_tree_scope(limit_depth)
24-
end
20+
limited_scope = limit_depth ? tree_scope.where("#{quoted_hierarchy_table_name}.generations <= #{limit_depth - 1}") : tree_scope
2521
build_hash_tree(limited_scope)
2622
end
2723

spec/label_spec.rb

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ def name_and_order(enum)
252252
end
253253

254254
def children_name_and_order
255-
name_and_order(@parent.children(reload = true))
255+
name_and_order(@parent.children.reload)
256256
end
257257

258258
def roots_name_and_order
@@ -454,7 +454,7 @@ def roots_name_and_order
454454
it 'should retain sort orders of descendants when moving to a new parent' do
455455
expected_order = ('a'..'z').to_a.shuffle
456456
expected_order.map { |ea| first_root.add_child(Label.new(name: ea)) }
457-
actual_order = first_root.children(reload = true).pluck(:name)
457+
actual_order = first_root.children.reload.pluck(:name)
458458
expect(actual_order).to eq(expected_order)
459459
last_root.append_child(first_root)
460460
expect(last_root.self_and_descendants.pluck(:name)).to eq(%w(10 0) + expected_order)
@@ -465,13 +465,13 @@ def roots_name_and_order
465465
z = first_root.find_or_create_by_path(path)
466466
z_children_names = (100..150).to_a.shuffle.map { |ea| ea.to_s }
467467
z_children_names.reverse.each { |ea| z.prepend_child(Label.new(name: ea)) }
468-
expect(z.children(reload = true).pluck(:name)).to eq(z_children_names)
468+
expect(z.children.reload.pluck(:name)).to eq(z_children_names)
469469
a = first_root.find_by_path(['a'])
470470
# move b up to a's level:
471471
b = a.children.first
472472
a.add_sibling(b)
473473
expect(b.parent).to eq(first_root)
474-
expect(z.children(reload = true).pluck(:name)).to eq(z_children_names)
474+
expect(z.children.reload.pluck(:name)).to eq(z_children_names)
475475
end
476476
end
477477

@@ -524,4 +524,31 @@ def roots_name_and_order
524524
expect(Label.roots_and_descendants_preordered.collect { |ea| ea.name }).to eq(expected)
525525
end
526526
end unless sqlite? # sqlite doesn't have a power function.
527+
528+
context 'hash_tree' do
529+
before do
530+
@a = EventLabel.create(name: 'a')
531+
@b = DateLabel.create(name: 'b')
532+
@c = DirectoryLabel.create(name: 'c')
533+
(1..3).each { |i| DirectoryLabel.create!(name: "c#{ i }", mother_id: @c.id) }
534+
end
535+
it 'should return tree with correct scope when called on class' do
536+
tree = DirectoryLabel.hash_tree
537+
expect(tree.keys.size).to eq(1)
538+
expect(tree.keys.first).to eq(@c)
539+
expect(tree[@c].keys.size).to eq(3)
540+
end
541+
it 'should return tree with correct scope when called on all' do
542+
tree = DirectoryLabel.all.hash_tree
543+
expect(tree.keys.size).to eq(1)
544+
expect(tree.keys.first).to eq(@c)
545+
expect(tree[@c].keys.size).to eq(3)
546+
end
547+
it 'should return tree with correct scope when called on scope chain' do
548+
tree = Label.where(name: 'b').hash_tree
549+
expect(tree.keys.size).to eq(1)
550+
expect(tree.keys.first).to eq(@b)
551+
expect(tree[@b]).to eq({})
552+
end
553+
end
527554
end

spec/spec_helper.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
44
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
5-
require 'rspec'
5+
begin
6+
require 'rspec'
7+
rescue LoadError
8+
# explciitly requiring rspec in ActiveRecord 5+ tests throws exception
9+
end
610
require 'active_record'
711
require 'database_cleaner'
812
require 'closure_tree'

0 commit comments

Comments
 (0)