New Upstream Snapshot - ruby-awesome-nested-set
Ready changes
Summary
Merged new upstream version: 3.5.0+git20220610.1.3913eca (was: 3.5.0).
Resulting package
Built on 2023-01-20T03:47 (took 5m58s)
The resulting binary packages can be installed (if you have the apt repository enabled) by running one of:
apt install -t fresh-snapshots ruby-awesome-nested-set
Lintian Result
Diff
diff --git a/.autotest b/.autotest
new file mode 100644
index 0000000..54518a4
--- /dev/null
+++ b/.autotest
@@ -0,0 +1,13 @@
+Autotest.add_hook :initialize do |at|
+ at.clear_mappings
+
+ at.add_mapping %r%^lib/(.*)\.rb$% do |_, m|
+ at.files_matching %r%^test/#{m[1]}_test.rb$%
+ end
+
+ at.add_mapping(%r%^test/.*\.rb$%) {|filename, _| filename }
+
+ at.add_mapping %r%^test/fixtures/(.*)s.yml% do |_, _|
+ at.files_matching %r%^test/.*\.rb$%
+ end
+end
\ No newline at end of file
diff --git a/Appraisals b/Appraisals
new file mode 100644
index 0000000..bb2e63b
--- /dev/null
+++ b/Appraisals
@@ -0,0 +1,9 @@
+{ '5_2' => '5.2.0', '6_0' => '6.0.0', '6_1' => '6.1.0', '7_0' => '7.0.0' }.each do |rails, version|
+ appraise "rails-#{rails}" do
+ gem "rails", "~> #{version}"
+ end
+end
+
+appraise "rails-main" do
+ gem "rails", github: "rails/rails", branch: "main"
+end
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
new file mode 100644
index 0000000..112b942
--- /dev/null
+++ b/CONTRIBUTING.md
@@ -0,0 +1,14 @@
+# Contributing to AwesomeNestedSet
+
+If you find what you might think is a bug:
+
+1. Check the [GitHub issue tracker](https://github.com/collectiveidea/awesome_nested_set/issues/) to see if anyone else has had the same issue.
+2. If you don't see anything, create an issue with information on how to reproduce it.
+
+If you want to contribute an enhancement or a fix:
+
+1. Fork [the project on GitHub](https://github.com/collectiveidea/awesome_nested_set)
+2. Make your changes with tests.
+3. Commit the changes without making changes to the [Rakefile](Rakefile) or any other files that aren't related to your enhancement or fix.
+4. Write an entry in the [CHANGELOG](CHANGELOG)
+5. Send a pull request.
diff --git a/Gemfile b/Gemfile
new file mode 100644
index 0000000..27222c9
--- /dev/null
+++ b/Gemfile
@@ -0,0 +1,13 @@
+source 'https://rubygems.org'
+
+gemspec
+
+platforms :ruby do
+ gem 'sqlite3'
+ gem 'mysql2', "< 1.0.0"
+ gem 'pg', "< 2.0.0"
+end
+
+# Add Oracle Adapters
+# gem 'ruby-oci8'
+# gem 'activerecord-oracle_enhanced-adapter'
diff --git a/README.md b/README.md
index 6e40a1a..5e13d9a 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
# Awesome Nested Set
-[![CI](https://github.com/collectiveidea/awesome_nested_set/actions/workflows/ci.yml/badge.svg)](https://github.com/collectiveidea/awesome_nested_set/actions/workflows/ci.yml) [![Code Climate](https://codeclimate.com/github/collectiveidea/awesome_nested_set.svg)](https://codeclimate.com/github/collectiveidea/awesome_nested_set) [![Security](https://hakiri.io/github/collectiveidea/awesome_nested_set/master.svg)](https://hakiri.io/github/collectiveidea/awesome_nested_set/master)
+[![CI](https://github.com/collectiveidea/awesome_nested_set/actions/workflows/ci.yml/badge.svg)](https://github.com/collectiveidea/awesome_nested_set/actions/workflows/ci.yml) [![Code Climate](https://codeclimate.com/github/collectiveidea/awesome_nested_set.svg)](https://codeclimate.com/github/collectiveidea/awesome_nested_set)
Awesome Nested Set is an implementation of the nested set pattern for ActiveRecord models.
diff --git a/Rakefile b/Rakefile
new file mode 100755
index 0000000..72a078c
--- /dev/null
+++ b/Rakefile
@@ -0,0 +1,11 @@
+#!/usr/bin/env rake
+
+require 'bundler/gem_helper'
+require 'rspec/core/rake_task'
+require 'appraisal'
+
+Bundler::GemHelper.install_tasks(name: 'awesome_nested_set')
+
+RSpec::Core::RakeTask.new(:spec)
+
+task default: :spec
diff --git a/awesome_nested_set.gemspec b/awesome_nested_set.gemspec
index 64cc867..c0c5152 100644
--- a/awesome_nested_set.gemspec
+++ b/awesome_nested_set.gemspec
@@ -1,48 +1,34 @@
-#########################################################
-# This file has been automatically generated by gem2tgz #
-#########################################################
-# -*- encoding: utf-8 -*-
-# stub: awesome_nested_set 3.5.0 ruby lib
+# frozen_string_literal: true
+
+require File.expand_path('lib/awesome_nested_set/version', __dir__)
Gem::Specification.new do |s|
- s.name = "awesome_nested_set".freeze
- s.version = "3.5.0"
+ s.name = 'awesome_nested_set'
+ s.version = ::AwesomeNestedSet::VERSION
+ s.authors = ['Brandon Keepers', 'Daniel Morrison', 'Philip Arndt']
+ s.description = 'An awesome nested set implementation for Active Record'
+ s.email = 'info@collectiveidea.com'
+ s.extra_rdoc_files = %w[README.md]
+ s.files = Dir.glob('lib/**/*') + %w[MIT-LICENSE README.md CHANGELOG]
+ s.homepage = 'https://github.com/collectiveidea/awesome_nested_set'
+ s.rdoc_options = ['--main', 'README.md', '--inline-source', '--line-numbers']
+ s.require_paths = ['lib']
+ s.summary = 'An awesome nested set implementation for Active Record'
+ s.license = 'MIT'
- s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
- s.require_paths = ["lib".freeze]
- s.authors = ["Brandon Keepers".freeze, "Daniel Morrison".freeze, "Philip Arndt".freeze]
- s.cert_chain = ["-----BEGIN CERTIFICATE-----\nMIIEMjCCApqgAwIBAgIBATANBgkqhkiG9w0BAQsFADAjMSEwHwYDVQQDDBhnZW1z\nL0RDPXAvREM9YXJuZHQvREM9aW8wHhcNMjEwNjIzMDkyNzU2WhcNMjIwNjIzMDky\nNzU2WjAjMSEwHwYDVQQDDBhnZW1zL0RDPXAvREM9YXJuZHQvREM9aW8wggGiMA0G\nCSqGSIb3DQEBAQUAA4IBjwAwggGKAoIBgQD0CYnD37uLlJ3Urla2EnnUQ8S6s16k\nAGMpAzpmARo8YwSqtYMJVGyBzUeI7y93Fk9ncswhIFSH/hnh/Ouat/ki9flHlZ+w\nanv0M+9v3wCLyZSC5BQIWpoduFM/fuvLoDUJDWxL50RjwMS0qo2x2LvfQdHN8gn3\nJdSIV5WLJKIvlmIl9S3pw0JO5KRUGn1PcBO7C0S0SlbhVjRHtlao1ycWUULsX109\nhCh39MPGtnZpdCcxheh0TH/UA/jV0/du9/rQdoidkNHkaC24pPfBJ3nS+rAbWaHP\nWmP+0rjfk/XnGBu/HZpKvlnwQjP3QdK4UMtWl8zewqFMNcIiBRALQugnL/SfrP/4\nCSlha9LwkiE6ByeY4WGnNjNqpi5J3IzjEkZRAxG7u9gCB3FzTaBTyXZYI6jplYNw\nTcCJIBHuoPaa+m9brpjb3Uv94nfM97ZP+OmpGYCCAMq4TT7OOV+t8wJc0w8bb0FO\nROhmVNTxrBaNcl6MkZn88EMRCsGgoWklOG0CAwEAAaNxMG8wCQYDVR0TBAIwADAL\nBgNVHQ8EBAMCBLAwHQYDVR0OBBYEFGu7pbmeILyHnBmannuaNRfdN8MsMBoGA1Ud\nEQQTMBGBD2dlbXNAcC5hcm5kdC5pbzAaBgNVHRIEEzARgQ9nZW1zQHAuYXJuZHQu\naW8wDQYJKoZIhvcNAQELBQADggGBANlxc4uAnkPC3zbztG7uZfBfn4HSuvv655Pa\nUaYZ6hNETFrqg78mGs3PkFe2Ru7cVWwckbmH46aq50QoNnx4ClxT03vr03n76Jg1\n8WWHkf0+rcINFlbtIFcmcFrois5Ow3n7pH+xstDtzoWcbh41WwuZStNhrIYsnjAK\n/ovz8D5JlboxceOpVLB/0NiqNEWltK+EMQHmX25Sqf/r5o5rAL9zwEKPFp1Y5X+z\nt2jBjYt2ymr1eMWxux6e+N2uKZL4MblHawxvKlI8UHsIiV9xrc4BwlwlbitcvNIL\nZykdSlpTJd0Guy92iYjCJMC09tMRUNxiVBwD3jRGSeW9YAPIZGXIcVlm6srIRDjJ\no8wB6oOvHAkRXnntOo/4bBDH+ehmgvhh/O/mI+au6C0M430fv+ooH0w08LEXLx1k\ne17ZNASZffbQRP09MH2GZ2AOlkildTX6looWRforZEZi+qamognrozd3MI5QHi1W\nUAZUzHLrrFu7gnkFvLVpxOUf4ItOkA==\n-----END CERTIFICATE-----\n".freeze]
- s.date = "2022-02-08"
- s.description = "An awesome nested set implementation for Active Record".freeze
- s.email = "info@collectiveidea.com".freeze
- s.extra_rdoc_files = ["README.md".freeze]
- s.files = ["CHANGELOG".freeze, "MIT-LICENSE".freeze, "README.md".freeze, "lib/awesome_nested_set.rb".freeze, "lib/awesome_nested_set/awesome_nested_set.rb".freeze, "lib/awesome_nested_set/columns.rb".freeze, "lib/awesome_nested_set/helper.rb".freeze, "lib/awesome_nested_set/iterator.rb".freeze, "lib/awesome_nested_set/model.rb".freeze, "lib/awesome_nested_set/model/movable.rb".freeze, "lib/awesome_nested_set/model/prunable.rb".freeze, "lib/awesome_nested_set/model/rebuildable.rb".freeze, "lib/awesome_nested_set/model/relatable.rb".freeze, "lib/awesome_nested_set/model/transactable.rb".freeze, "lib/awesome_nested_set/model/validatable.rb".freeze, "lib/awesome_nested_set/move.rb".freeze, "lib/awesome_nested_set/set_validator.rb".freeze, "lib/awesome_nested_set/tree.rb".freeze, "lib/awesome_nested_set/version.rb".freeze]
- s.homepage = "https://github.com/collectiveidea/awesome_nested_set".freeze
- s.licenses = ["MIT".freeze]
- s.rdoc_options = ["--main".freeze, "README.md".freeze, "--inline-source".freeze, "--line-numbers".freeze]
- s.required_ruby_version = Gem::Requirement.new(">= 2.0.0".freeze)
- s.rubygems_version = "3.2.5".freeze
- s.summary = "An awesome nested set implementation for Active Record".freeze
+ s.required_ruby_version = '>= 2.0.0'
- if s.respond_to? :specification_version then
- s.specification_version = 4
- end
+ s.add_runtime_dependency 'activerecord', '>= 4.0.0', '< 7.1'
+
+ s.add_development_dependency 'appraisal'
+ s.add_development_dependency 'database_cleaner'
+ s.add_development_dependency 'pry'
+ s.add_development_dependency 'pry-nav'
+ s.add_development_dependency 'rake', '~> 12'
+ s.add_development_dependency 'rspec-rails', '~> 4.0.0'
- if s.respond_to? :add_runtime_dependency then
- s.add_runtime_dependency(%q<activerecord>.freeze, [">= 4.0.0", "< 7.1"])
- s.add_development_dependency(%q<appraisal>.freeze, [">= 0"])
- s.add_development_dependency(%q<database_cleaner>.freeze, [">= 0"])
- s.add_development_dependency(%q<pry>.freeze, [">= 0"])
- s.add_development_dependency(%q<pry-nav>.freeze, [">= 0"])
- s.add_development_dependency(%q<rake>.freeze, ["~> 12"])
- s.add_development_dependency(%q<rspec-rails>.freeze, ["~> 4.0.0"])
- else
- s.add_dependency(%q<activerecord>.freeze, [">= 4.0.0", "< 7.1"])
- s.add_dependency(%q<appraisal>.freeze, [">= 0"])
- s.add_dependency(%q<database_cleaner>.freeze, [">= 0"])
- s.add_dependency(%q<pry>.freeze, [">= 0"])
- s.add_dependency(%q<pry-nav>.freeze, [">= 0"])
- s.add_dependency(%q<rake>.freeze, ["~> 12"])
- s.add_dependency(%q<rspec-rails>.freeze, ["~> 4.0.0"])
+ s.cert_chain = [File.expand_path('certs/parndt.pem', __dir__)]
+ if $PROGRAM_NAME =~ /gem\z/ && ARGV.include?('build') && ARGV.include?(__FILE__)
+ s.signing_key = File.expand_path('~/.ssh/gem-private_key.pem')
end
end
diff --git a/certs/parndt.pem b/certs/parndt.pem
new file mode 100644
index 0000000..04fd123
--- /dev/null
+++ b/certs/parndt.pem
@@ -0,0 +1,25 @@
+-----BEGIN CERTIFICATE-----
+MIIEMjCCApqgAwIBAgIBATANBgkqhkiG9w0BAQsFADAjMSEwHwYDVQQDDBhnZW1z
+L0RDPXAvREM9YXJuZHQvREM9aW8wHhcNMjEwNjIzMDkyNzU2WhcNMjIwNjIzMDky
+NzU2WjAjMSEwHwYDVQQDDBhnZW1zL0RDPXAvREM9YXJuZHQvREM9aW8wggGiMA0G
+CSqGSIb3DQEBAQUAA4IBjwAwggGKAoIBgQD0CYnD37uLlJ3Urla2EnnUQ8S6s16k
+AGMpAzpmARo8YwSqtYMJVGyBzUeI7y93Fk9ncswhIFSH/hnh/Ouat/ki9flHlZ+w
+anv0M+9v3wCLyZSC5BQIWpoduFM/fuvLoDUJDWxL50RjwMS0qo2x2LvfQdHN8gn3
+JdSIV5WLJKIvlmIl9S3pw0JO5KRUGn1PcBO7C0S0SlbhVjRHtlao1ycWUULsX109
+hCh39MPGtnZpdCcxheh0TH/UA/jV0/du9/rQdoidkNHkaC24pPfBJ3nS+rAbWaHP
+WmP+0rjfk/XnGBu/HZpKvlnwQjP3QdK4UMtWl8zewqFMNcIiBRALQugnL/SfrP/4
+CSlha9LwkiE6ByeY4WGnNjNqpi5J3IzjEkZRAxG7u9gCB3FzTaBTyXZYI6jplYNw
+TcCJIBHuoPaa+m9brpjb3Uv94nfM97ZP+OmpGYCCAMq4TT7OOV+t8wJc0w8bb0FO
+ROhmVNTxrBaNcl6MkZn88EMRCsGgoWklOG0CAwEAAaNxMG8wCQYDVR0TBAIwADAL
+BgNVHQ8EBAMCBLAwHQYDVR0OBBYEFGu7pbmeILyHnBmannuaNRfdN8MsMBoGA1Ud
+EQQTMBGBD2dlbXNAcC5hcm5kdC5pbzAaBgNVHRIEEzARgQ9nZW1zQHAuYXJuZHQu
+aW8wDQYJKoZIhvcNAQELBQADggGBANlxc4uAnkPC3zbztG7uZfBfn4HSuvv655Pa
+UaYZ6hNETFrqg78mGs3PkFe2Ru7cVWwckbmH46aq50QoNnx4ClxT03vr03n76Jg1
+8WWHkf0+rcINFlbtIFcmcFrois5Ow3n7pH+xstDtzoWcbh41WwuZStNhrIYsnjAK
+/ovz8D5JlboxceOpVLB/0NiqNEWltK+EMQHmX25Sqf/r5o5rAL9zwEKPFp1Y5X+z
+t2jBjYt2ymr1eMWxux6e+N2uKZL4MblHawxvKlI8UHsIiV9xrc4BwlwlbitcvNIL
+ZykdSlpTJd0Guy92iYjCJMC09tMRUNxiVBwD3jRGSeW9YAPIZGXIcVlm6srIRDjJ
+o8wB6oOvHAkRXnntOo/4bBDH+ehmgvhh/O/mI+au6C0M430fv+ooH0w08LEXLx1k
+e17ZNASZffbQRP09MH2GZ2AOlkildTX6looWRforZEZi+qamognrozd3MI5QHi1W
+UAZUzHLrrFu7gnkFvLVpxOUf4ItOkA==
+-----END CERTIFICATE-----
diff --git a/checksums.yaml.gz.sig b/checksums.yaml.gz.sig
deleted file mode 100644
index 9caa908..0000000
--- a/checksums.yaml.gz.sig
+++ /dev/null
@@ -1,2 +0,0 @@
-̗
�:ǿ������1@+�b4��{$��/��[_I���cc�7ډk&�럪��#���Z�7%���$��/�V�z^{��IwIvޞ=���u\�F�C?ƾGG��>�2N��ʪG���yH�����V���\?(�� ����p�U�F��o�Xo�n��wRz���b��fM%�P�z���v���hL{���r�O�E|����td+5��g!���y�ŏy؞��^
-M�7kAlz�ឞ ��d�֛"L��6�{� `�X�f��$ay�<4���o,MjN��F�D3�2�`a�����WBg�F����E��Q�-B'�����_��s6�@%s5� ���9�dN�<��[X7lH�C�Ck7��B{�-����=
\ No newline at end of file
diff --git a/data.tar.gz.sig b/data.tar.gz.sig
deleted file mode 100644
index 7791ee2..0000000
Binary files a/data.tar.gz.sig and /dev/null differ
diff --git a/debian/changelog b/debian/changelog
index 8944988..371729c 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,9 @@
+ruby-awesome-nested-set (3.5.0+git20220610.1.3913eca-1) UNRELEASED; urgency=low
+
+ * New upstream snapshot.
+
+ -- Debian Janitor <janitor@jelmer.uk> Fri, 20 Jan 2023 03:43:59 -0000
+
ruby-awesome-nested-set (3.5.0-1) unstable; urgency=medium
* Team upload.
diff --git a/gemfiles/rails_5_2.gemfile b/gemfiles/rails_5_2.gemfile
new file mode 100644
index 0000000..952bc14
--- /dev/null
+++ b/gemfiles/rails_5_2.gemfile
@@ -0,0 +1,13 @@
+# This file was generated by Appraisal
+
+source "https://rubygems.org"
+
+gem "rails", "~> 5.2.0"
+
+platforms :ruby do
+ gem "sqlite3"
+ gem "mysql2", "< 1.0.0"
+ gem "pg", "< 2.0.0"
+end
+
+gemspec path: "../"
diff --git a/gemfiles/rails_6_0.gemfile b/gemfiles/rails_6_0.gemfile
new file mode 100644
index 0000000..da5c7c6
--- /dev/null
+++ b/gemfiles/rails_6_0.gemfile
@@ -0,0 +1,13 @@
+# This file was generated by Appraisal
+
+source "https://rubygems.org"
+
+gem "rails", "~> 6.0.0"
+
+platforms :ruby do
+ gem "sqlite3"
+ gem "mysql2", "< 1.0.0"
+ gem "pg", "< 2.0.0"
+end
+
+gemspec path: "../"
diff --git a/gemfiles/rails_6_1.gemfile b/gemfiles/rails_6_1.gemfile
new file mode 100644
index 0000000..51abb72
--- /dev/null
+++ b/gemfiles/rails_6_1.gemfile
@@ -0,0 +1,13 @@
+# This file was generated by Appraisal
+
+source "https://rubygems.org"
+
+gem "rails", "~> 6.1.0"
+
+platforms :ruby do
+ gem "sqlite3"
+ gem "mysql2", "< 1.0.0"
+ gem "pg", "< 2.0.0"
+end
+
+gemspec path: "../"
diff --git a/gemfiles/rails_7_0.gemfile b/gemfiles/rails_7_0.gemfile
new file mode 100644
index 0000000..5d1d342
--- /dev/null
+++ b/gemfiles/rails_7_0.gemfile
@@ -0,0 +1,13 @@
+# This file was generated by Appraisal
+
+source "https://rubygems.org"
+
+gem "rails", "~> 7.0.0"
+
+platforms :ruby do
+ gem "sqlite3"
+ gem "mysql2", "< 1.0.0"
+ gem "pg", "< 2.0.0"
+end
+
+gemspec path: "../"
diff --git a/gemfiles/rails_main.gemfile b/gemfiles/rails_main.gemfile
new file mode 100644
index 0000000..be0278d
--- /dev/null
+++ b/gemfiles/rails_main.gemfile
@@ -0,0 +1,13 @@
+# This file was generated by Appraisal
+
+source "https://rubygems.org"
+
+gem "rails", github: "rails/rails", branch: "main"
+
+platforms :ruby do
+ gem "sqlite3"
+ gem "mysql2", "< 1.0.0"
+ gem "pg", "< 2.0.0"
+end
+
+gemspec path: "../"
diff --git a/lib/awesome_nested_set/model/prunable.rb b/lib/awesome_nested_set/model/prunable.rb
index 9e83deb..dc84cf0 100644
--- a/lib/awesome_nested_set/model/prunable.rb
+++ b/lib/awesome_nested_set/model/prunable.rb
@@ -47,8 +47,13 @@ module CollectiveIdea #:nodoc:
elsif acts_as_nested_set_options[:dependent] == :restrict_with_error
unless leaf?
record = self.class.human_attribute_name(:children).downcase
- errors.add(:base, :"restrict_dependent_destroy.#{Rails::VERSION::MAJOR < 5 ? 'many' : 'has_many'}", record: record)
- return false
+ if Rails::VERSION::MAJOR < 5
+ errors.add(:base, :"restrict_dependent_destroy.many", record: record)
+ return false
+ else
+ errors.add(:base, :"restrict_dependent_destroy.has_many", record: record)
+ throw :abort
+ end
end
return true
elsif acts_as_nested_set_options[:dependent] == :nullify
diff --git a/metadata.gz.sig b/metadata.gz.sig
deleted file mode 100644
index 210ef15..0000000
--- a/metadata.gz.sig
+++ /dev/null
@@ -1,2 +0,0 @@
-�7��@������9�#�u���5�NK��y'5/9|���8�Uj�)$֦(�w�qZ
벐;�MF��/��dNe�@���8:y�V����TV�������+��I���e��Ǵu#N;�k݂�h�r3K�%s��Ɩ��Y:IP�֭}��r��G��* �F�ТP=\����-�[&�(�A��&��H����!^\�2 �G���'�,a @�9����e*k%�A�zO]p�u���5�Z��d��3�Uؤ�~�orŰ>�/*�/
-�����n�}�I�h�0�ދD ��|_����⯕1�$Z�4y��S�:\衋Ui��磡/��3��yF��ͣ�Ӄ,z5�-و��R�0���ɀ��=n��c
\ No newline at end of file
diff --git a/spec/awesome_nested_set/helper_spec.rb b/spec/awesome_nested_set/helper_spec.rb
new file mode 100644
index 0000000..ae0ef43
--- /dev/null
+++ b/spec/awesome_nested_set/helper_spec.rb
@@ -0,0 +1,102 @@
+require 'spec_helper'
+require 'awesome_nested_set/helper'
+
+describe "Helper" do
+ include CollectiveIdea::Acts::NestedSet::Helper
+
+ before(:all) do
+ self.class.fixtures :categories
+ end
+
+ describe "nested_set_options" do
+ it "test_nested_set_options" do
+ expected = [
+ [" Top Level", 1],
+ ["- Child 1", 2],
+ ['- Child 2', 3],
+ ['-- Child 2.1', 4],
+ ['- Child 3', 5],
+ [" Top Level 2", 6]
+ ]
+ actual = nested_set_options(Category.all) do |c|
+ "#{'-' * c.level} #{c.name}"
+ end
+ expect(actual).to eq(expected)
+ end
+
+ it "test_nested_set_options_with_mover" do
+ expected = [
+ [" Top Level", 1],
+ ["- Child 1", 2],
+ ['- Child 3', 5],
+ [" Top Level 2", 6]
+ ]
+ actual = nested_set_options(Category.all, categories(:child_2)) do |c|
+ "#{'-' * c.level} #{c.name}"
+ end
+ expect(actual).to eq(expected)
+ end
+
+ it "test_nested_set_options_with_class_as_argument" do
+ expected = [
+ [" Top Level", 1],
+ ["- Child 1", 2],
+ ['- Child 2', 3],
+ ['-- Child 2.1', 4],
+ ['- Child 3', 5],
+ [" Top Level 2", 6]
+ ]
+ actual = nested_set_options(Category) do |c|
+ "#{'-' * c.level} #{c.name}"
+ end
+ expect(actual).to eq(expected)
+ end
+
+ it "test_nested_set_options_with_class_as_argument_with_mover" do
+ expected = [
+ [" Top Level", 1],
+ ["- Child 1", 2],
+ ['- Child 3', 5],
+ [" Top Level 2", 6]
+ ]
+ actual = nested_set_options(Category, categories(:child_2)) do |c|
+ "#{'-' * c.level} #{c.name}"
+ end
+ expect(actual).to eq(expected)
+ end
+
+ it "test_nested_set_options_with_array_as_argument_without_mover" do
+ expected = [
+ [" Top Level", 1],
+ ["- Child 1", 2],
+ ['- Child 2', 3],
+ ['-- Child 2.1', 4],
+ ['- Child 3', 5],
+ [" Top Level 2", 6]
+ ]
+ actual = nested_set_options(Category.all) do |c|
+ "#{'-' * c.level} #{c.name}"
+ end
+ expect(actual.length).to eq(expected.length)
+ expected.flatten.each do |node|
+ expect(actual.flatten).to include(node)
+ end
+ end
+
+ it "test_nested_set_options_with_array_as_argument_with_mover" do
+ expected = [
+ [" Top Level", 1],
+ ["- Child 1", 2],
+ ['- Child 3', 5],
+ [" Top Level 2", 6]
+ ]
+ actual = nested_set_options(Category.all, categories(:child_2)) do |c|
+ "#{'-' * c.level} #{c.name}"
+ end
+ expect(actual.length).to eq(expected.length)
+ expected.flatten.each do |node|
+ expect(actual.flatten).to include(node)
+ end
+ end
+ end
+end
diff --git a/spec/awesome_nested_set/model/transactable_spec.rb b/spec/awesome_nested_set/model/transactable_spec.rb
new file mode 100644
index 0000000..cf57aad
--- /dev/null
+++ b/spec/awesome_nested_set/model/transactable_spec.rb
@@ -0,0 +1,19 @@
+require 'spec_helper'
+RSpec.configure do |config|
+ config.use_transactional_fixtures = false
+end
+
+describe "AwesomeNestedSet" do
+
+ describe "transactions" do
+ it "should retry with sleep on deadlocks or lock wait timeouts" do
+ i = 0
+ # eg. like a move, where a lock is found
+ # in_tenacious_transaction should retry up to 10 times. 4 retries in this example
+ DefaultScopedModel.new.send(:in_tenacious_transaction) do
+ raise ActiveRecord::StatementInvalid.new('Lock wait timeout exceeded') unless (i+=1) > 3
+ end
+ expect(i).to eq(4)
+ end
+ end
+end
\ No newline at end of file
diff --git a/spec/awesome_nested_set_spec.rb b/spec/awesome_nested_set_spec.rb
new file mode 100644
index 0000000..b4a8262
--- /dev/null
+++ b/spec/awesome_nested_set_spec.rb
@@ -0,0 +1,1369 @@
+require 'spec_helper'
+
+describe "AwesomeNestedSet" do
+ before(:all) do
+ self.class.fixtures :categories, :departments, :notes, :things, :brokens, :users, :default_scoped_models
+ end
+
+ describe "defaults" do
+ it "should have left_column_default" do
+ expect(Default.acts_as_nested_set_options[:left_column]).to eq('lft')
+ end
+
+ it "should have right_column_default" do
+ expect(Default.acts_as_nested_set_options[:right_column]).to eq('rgt')
+ end
+
+ it "should have parent_column_default" do
+ expect(Default.acts_as_nested_set_options[:parent_column]).to eq('parent_id')
+ end
+
+ it " should have a primary_column_default" do
+ expect(Default.acts_as_nested_set_options[:primary_column]).to eq('id')
+ end
+
+ it "should have scope_default" do
+ expect(Default.acts_as_nested_set_options[:scope]).to be_nil
+ end
+
+ it "should have left_column_name" do
+ expect(Default.left_column_name).to eq('lft')
+ expect(Default.new.left_column_name).to eq('lft')
+ expect(RenamedColumns.left_column_name).to eq('red')
+ expect(RenamedColumns.new.left_column_name).to eq('red')
+ end
+
+ it "should have right_column_name" do
+ expect(Default.right_column_name).to eq('rgt')
+ expect(Default.new.right_column_name).to eq('rgt')
+ expect(RenamedColumns.right_column_name).to eq('black')
+ expect(RenamedColumns.new.right_column_name).to eq('black')
+ end
+
+ it "has a depth_column_name" do
+ expect(Default.depth_column_name).to eq('depth')
+ expect(Default.new.depth_column_name).to eq('depth')
+ expect(RenamedColumns.depth_column_name).to eq('pitch')
+ expect(RenamedColumns.new.depth_column_name).to eq('pitch')
+ end
+
+ it "should have parent_column_name" do
+ expect(Default.parent_column_name).to eq('parent_id')
+ expect(Default.new.parent_column_name).to eq('parent_id')
+ expect(RenamedColumns.parent_column_name).to eq('mother_id')
+ expect(RenamedColumns.new.parent_column_name).to eq('mother_id')
+ end
+
+ it "should have primary_column_name" do
+ expect(Default.primary_column_name).to eq('id')
+ expect(Default.new.primary_column_name).to eq('id')
+ expect(User.primary_column_name).to eq('uuid')
+ expect(User.new.primary_column_name).to eq('uuid')
+ end
+ end
+
+ it "creation_with_altered_column_names" do
+ expect {
+ RenamedColumns.create!()
+ }.not_to raise_exception
+ end
+
+ it "creation when existing record has nil left column" do
+ expect {
+ Broken.create!
+ }.not_to raise_exception
+ end
+
+ describe "quoted column names" do
+ it "quoted_left_column_name" do
+ quoted = Default.connection.quote_column_name('lft')
+ expect(Default.quoted_left_column_name).to eq(quoted)
+ expect(Default.new.quoted_left_column_name).to eq(quoted)
+ end
+
+ it "quoted_right_column_name" do
+ quoted = Default.connection.quote_column_name('rgt')
+ expect(Default.quoted_right_column_name).to eq(quoted)
+ expect(Default.new.quoted_right_column_name).to eq(quoted)
+ end
+
+ it "quoted_depth_column_name" do
+ quoted = Default.connection.quote_column_name('depth')
+ expect(Default.quoted_depth_column_name).to eq(quoted)
+ expect(Default.new.quoted_depth_column_name).to eq(quoted)
+ end
+
+ it "quoted_order_column_name" do
+ quoted = Default.connection.quote_column_name('lft')
+ expect(Default.quoted_order_column_name).to eq(quoted)
+ expect(Default.new.quoted_order_column_name).to eq(quoted)
+ end
+ end
+
+ describe "protected columns" do
+ it "left_column_protected_from_assignment" do
+ expect {
+ Category.new.lft = 1
+ }.to raise_exception(ActiveRecord::ActiveRecordError)
+ end
+
+ it "right_column_protected_from_assignment" do
+ expect {
+ Category.new.rgt = 1
+ }.to raise_exception(ActiveRecord::ActiveRecordError)
+ end
+
+ it "depth_column_protected_from_assignment" do
+ expect {
+ Category.new.depth = 1
+ }.to raise_exception(ActiveRecord::ActiveRecordError)
+ end
+ end
+
+ describe "scope" do
+ it "scoped_appends_id" do
+ expect(ScopedCategory.acts_as_nested_set_options[:scope]).to eq(:organization_id)
+ end
+ end
+
+ describe "hierarchical structure" do
+ it "roots_class_method" do
+ found_by_us = Category.where(:parent_id => nil).to_a
+ found_by_roots = Category.roots.to_a
+ expect(found_by_us.length).to eq(found_by_roots.length)
+ found_by_us.each do |root|
+ expect(found_by_roots).to include(root)
+ end
+ end
+
+ it "root_class_method" do
+ expect(Category.root).to eq(categories(:top_level))
+ end
+
+ it "root_class_method with scope" do
+ categories(:top_level).update_attribute :organization_id, 999999999
+ expect(ScopedCategory.where(organization_id: 999999999).root.id).to eq(categories(:top_level).id)
+ expect(ScopedCategory.where(organization_id: 1).root).to be_nil
+ end
+
+ it "root" do
+ expect(categories(:child_3).root).to eq(categories(:top_level))
+ end
+
+ it "root when not persisted and parent_column_name value is self" do
+ new_category = Category.new
+ expect(new_category.root).to eq(new_category)
+ end
+
+ it "root when not persisted and parent_column_name value is set" do
+ last_category = Category.last
+ expect(Category.new(Default.parent_column_name => last_category.id).root).to eq(last_category.root)
+ end
+
+ it "root?" do
+ expect(categories(:top_level).root?).to be_truthy
+ expect(categories(:top_level_2).root?).to be_truthy
+ end
+
+ it "leaves_class_method" do
+ expect(Category.where("#{Category.right_column_name} - #{Category.left_column_name} = 1").to_a).to eq(Category.leaves.to_a)
+ expect(Category.leaves.count).to eq(4)
+ expect(Category.leaves).to include(categories(:child_1))
+ expect(Category.leaves).to include(categories(:child_2_1))
+ expect(Category.leaves).to include(categories(:child_3))
+ expect(Category.leaves).to include(categories(:top_level_2))
+ end
+
+ it "leaf" do
+ expect(categories(:child_1).leaf?).to be_truthy
+ expect(categories(:child_2_1).leaf?).to be_truthy
+ expect(categories(:child_3).leaf?).to be_truthy
+ expect(categories(:top_level_2).leaf?).to be_truthy
+
+ expect(categories(:top_level).leaf?).to be_falsey
+ expect(categories(:child_2).leaf?).to be_falsey
+ expect(Category.new.leaf?).to be_falsey
+ end
+
+ it "parent" do
+ expect(categories(:child_2_1).parent).to eq(categories(:child_2))
+ end
+
+ it "self_and_ancestors" do
+ child = categories(:child_2_1)
+ self_and_ancestors = [categories(:top_level), categories(:child_2), child]
+ expect(child.self_and_ancestors).to eq(self_and_ancestors)
+ end
+
+ it "ancestors" do
+ child = categories(:child_2_1)
+ ancestors = [categories(:top_level), categories(:child_2)]
+ expect(ancestors).to eq(child.ancestors)
+ end
+
+ it "self_and_siblings" do
+ child = categories(:child_2)
+ self_and_siblings = [categories(:child_1), child, categories(:child_3)]
+ expect(self_and_siblings).to eq(child.self_and_siblings)
+ expect do
+ tops = [categories(:top_level), categories(:top_level_2)]
+ assert_equal tops, categories(:top_level).self_and_siblings
+ end.not_to raise_exception
+ end
+
+ it "siblings" do
+ child = categories(:child_2)
+ siblings = [categories(:child_1), categories(:child_3)]
+ expect(siblings).to eq(child.siblings)
+ end
+
+ it "leaves" do
+ leaves = [categories(:child_1), categories(:child_2_1), categories(:child_3)]
+ expect(categories(:top_level).leaves).to eq(leaves)
+ end
+ end
+
+ describe "level" do
+ it "returns the correct level" do
+ expect(categories(:top_level).level).to eq(0)
+ expect(categories(:child_1).level).to eq(1)
+ expect(categories(:child_2_1).level).to eq(2)
+ end
+
+ context "given parent associations are loaded" do
+ it "returns the correct level" do
+ child = categories(:child_1)
+ if child.respond_to?(:association)
+ child.association(:parent).load_target
+ child.parent.association(:parent).load_target
+ expect(child.level).to eq(1)
+ else
+ skip 'associations not used where child#association is not a method'
+ end
+ end
+ end
+ end
+
+ describe "depth" do
+ context "in general" do
+ let(:lawyers) { Category.create!(:name => "lawyers") }
+ let(:us) { Category.create!(:name => "United States") }
+ let(:new_york) { Category.create!(:name => "New York") }
+ let(:patent) { Category.create!(:name => "Patent Law") }
+ let(:ch) { Category.create!(:name => "Switzerland") }
+ let(:bern) { Category.create!(:name => "Bern") }
+
+ before(:each) do
+ # lawyers > us > new_york > patent
+ # > ch > bern
+ us.move_to_child_of(lawyers)
+ new_york.move_to_child_of(us)
+ patent.move_to_child_of(new_york)
+ ch.move_to_child_of(lawyers)
+ bern.move_to_child_of(ch)
+ [lawyers, us, new_york, patent, ch, bern].each(&:reload)
+ end
+
+ it "updates depth when moved into child position" do
+ expect(lawyers.depth).to eq(0)
+ expect(us.depth).to eq(1)
+ expect(new_york.depth).to eq(2)
+ expect(patent.depth).to eq(3)
+ expect(ch.depth).to eq(1)
+ expect(bern.depth).to eq(2)
+ end
+
+ it "decreases depth of all descendants when parent is moved up" do
+ # lawyers
+ # us > new_york > patent
+ us.move_to_right_of(lawyers)
+ [lawyers, us, new_york, patent, ch, bern].each(&:reload)
+ expect(us.depth).to eq(0)
+ expect(new_york.depth).to eq(1)
+ expect(patent.depth).to eq(2)
+ expect(ch.depth).to eq(1)
+ expect(bern.depth).to eq(2)
+ end
+
+ it "keeps depth of all descendants when parent is moved right" do
+ us.move_to_right_of(ch)
+ [lawyers, us, new_york, patent, ch, bern].each(&:reload)
+ expect(us.depth).to eq(1)
+ expect(new_york.depth).to eq(2)
+ expect(patent.depth).to eq(3)
+ expect(ch.depth).to eq(1)
+ expect(bern.depth).to eq(2)
+ end
+
+ it "increases depth of all descendants when parent is moved down" do
+ us.move_to_child_of(bern)
+ [lawyers, us, new_york, patent, ch, bern].each(&:reload)
+ expect(us.depth).to eq(3)
+ expect(new_york.depth).to eq(4)
+ expect(patent.depth).to eq(5)
+ expect(ch.depth).to eq(1)
+ expect(bern.depth).to eq(2)
+ end
+ end
+
+ it "is magic and does not apply when column is missing" do
+ expect { NoDepth.create!(:name => "shallow") }.not_to raise_error
+ expect { NoDepth.first.save }.not_to raise_error
+ expect { NoDepth.rebuild! }.not_to raise_error
+
+ expect(NoDepth.method_defined?(:depth)).to be_falsey
+ expect(NoDepth.first.respond_to?(:depth)).to be_falsey
+ end
+ end
+
+ it "has_children?" do
+ expect(categories(:child_2_1).children.empty?).to be_truthy
+ expect(categories(:child_2).children.empty?).to be_falsey
+ expect(categories(:top_level).children.empty?).to be_falsey
+ end
+
+ it "self_and_descendants" do
+ parent = categories(:top_level)
+ self_and_descendants = [
+ parent,
+ categories(:child_1),
+ categories(:child_2),
+ categories(:child_2_1),
+ categories(:child_3)
+ ]
+ expect(self_and_descendants).to eq(parent.self_and_descendants)
+ expect(self_and_descendants.count).to eq(parent.self_and_descendants.count)
+ end
+
+ it "descendants" do
+ lawyers = Category.create!(:name => "lawyers")
+ us = Category.create!(:name => "United States")
+ us.move_to_child_of(lawyers)
+ patent = Category.create!(:name => "Patent Law")
+ patent.move_to_child_of(us)
+ lawyers.reload
+
+ expect(lawyers.children.size).to eq(1)
+ expect(us.children.size).to eq(1)
+ expect(lawyers.descendants.size).to eq(2)
+ end
+
+ it "self_and_descendants" do
+ parent = categories(:top_level)
+ descendants = [
+ categories(:child_1),
+ categories(:child_2),
+ categories(:child_2_1),
+ categories(:child_3)
+ ]
+ expect(descendants).to eq(parent.descendants)
+ end
+
+ it "children" do
+ category = categories(:top_level)
+ category.children.each {|c| expect(category.id).to eq(c.parent_id) }
+ end
+
+ it "order_of_children" do
+ categories(:child_2).move_left
+ expect(categories(:child_2)).to eq(categories(:top_level).children[0])
+ expect(categories(:child_1)).to eq(categories(:top_level).children[1])
+ expect(categories(:child_3)).to eq(categories(:top_level).children[2])
+ end
+
+ it "is_or_is_ancestor_of?" do
+ expect(categories(:top_level).is_or_is_ancestor_of?(categories(:child_1))).to be_truthy
+ expect(categories(:top_level).is_or_is_ancestor_of?(categories(:child_2_1))).to be_truthy
+ expect(categories(:child_2).is_or_is_ancestor_of?(categories(:child_2_1))).to be_truthy
+ expect(categories(:child_2_1).is_or_is_ancestor_of?(categories(:child_2))).to be_falsey
+ expect(categories(:child_1).is_or_is_ancestor_of?(categories(:child_2))).to be_falsey
+ expect(categories(:child_1).is_or_is_ancestor_of?(categories(:child_1))).to be_truthy
+ end
+
+ it "is_ancestor_of?" do
+ expect(categories(:top_level).is_ancestor_of?(categories(:child_1))).to be_truthy
+ expect(categories(:top_level).is_ancestor_of?(categories(:child_2_1))).to be_truthy
+ expect(categories(:child_2).is_ancestor_of?(categories(:child_2_1))).to be_truthy
+ expect(categories(:child_2_1).is_ancestor_of?(categories(:child_2))).to be_falsey
+ expect(categories(:child_1).is_ancestor_of?(categories(:child_2))).to be_falsey
+ expect(categories(:child_1).is_ancestor_of?(categories(:child_1))).to be_falsey
+ end
+
+ it "is_or_is_ancestor_of_with_scope" do
+ root = ScopedCategory.root
+ child = root.children.first
+ expect(root.is_or_is_ancestor_of?(child)).to be_truthy
+ child.update_attribute :organization_id, 999999999
+ expect(root.is_or_is_ancestor_of?(child)).to be_falsey
+ end
+
+ it "is_or_is_descendant_of?" do
+ expect(categories(:child_1).is_or_is_descendant_of?(categories(:top_level))).to be_truthy
+ expect(categories(:child_2_1).is_or_is_descendant_of?(categories(:top_level))).to be_truthy
+ expect(categories(:child_2_1).is_or_is_descendant_of?(categories(:child_2))).to be_truthy
+ expect(categories(:child_2).is_or_is_descendant_of?(categories(:child_2_1))).to be_falsey
+ expect(categories(:child_2).is_or_is_descendant_of?(categories(:child_1))).to be_falsey
+ expect(categories(:child_1).is_or_is_descendant_of?(categories(:child_1))).to be_truthy
+ end
+
+ it "is_descendant_of?" do
+ expect(categories(:child_1).is_descendant_of?(categories(:top_level))).to be_truthy
+ expect(categories(:child_2_1).is_descendant_of?(categories(:top_level))).to be_truthy
+ expect(categories(:child_2_1).is_descendant_of?(categories(:child_2))).to be_truthy
+ expect(categories(:child_2).is_descendant_of?(categories(:child_2_1))).to be_falsey
+ expect(categories(:child_2).is_descendant_of?(categories(:child_1))).to be_falsey
+ expect(categories(:child_1).is_descendant_of?(categories(:child_1))).to be_falsey
+ end
+
+ it "is_or_is_descendant_of_with_scope" do
+ root = ScopedCategory.root
+ child = root.children.first
+ expect(child.is_or_is_descendant_of?(root)).to be_truthy
+ child.update_attribute :organization_id, 999999999
+ expect(child.is_or_is_descendant_of?(root)).to be_falsey
+ end
+
+ it "same_scope?" do
+ root = ScopedCategory.root
+ child = root.children.first
+ expect(child.same_scope?(root)).to be_truthy
+ child.update_attribute :organization_id, 999999999
+ expect(child.same_scope?(root)).to be_falsey
+ end
+
+ it "left_sibling" do
+ expect(categories(:child_1)).to eq(categories(:child_2).left_sibling)
+ expect(categories(:child_2)).to eq(categories(:child_3).left_sibling)
+ end
+
+ it "left_sibling_of_root" do
+ expect(categories(:top_level).left_sibling).to be_nil
+ end
+
+ it "left_sibling_without_siblings" do
+ expect(categories(:child_2_1).left_sibling).to be_nil
+ end
+
+ it "left_sibling_of_leftmost_node" do
+ expect(categories(:child_1).left_sibling).to be_nil
+ end
+
+ it "right_sibling" do
+ expect(categories(:child_3)).to eq(categories(:child_2).right_sibling)
+ expect(categories(:child_2)).to eq(categories(:child_1).right_sibling)
+ end
+
+ it "right_sibling_of_root" do
+ expect(categories(:top_level_2)).to eq(categories(:top_level).right_sibling)
+ expect(categories(:top_level_2).right_sibling).to be_nil
+ end
+
+ it "right_sibling_without_siblings" do
+ expect(categories(:child_2_1).right_sibling).to be_nil
+ end
+
+ it "right_sibling_of_rightmost_node" do
+ expect(categories(:child_3).right_sibling).to be_nil
+ end
+
+ it "move_left" do
+ categories(:child_2).move_left
+ expect(categories(:child_2).left_sibling).to be_nil
+ expect(categories(:child_1)).to eq(categories(:child_2).right_sibling)
+ expect(Category.valid?).to be_truthy
+ end
+
+ it "move_right" do
+ categories(:child_2).move_right
+ expect(categories(:child_2).right_sibling).to be_nil
+ expect(categories(:child_3)).to eq(categories(:child_2).left_sibling)
+ expect(Category.valid?).to be_truthy
+ end
+
+ it "move_to_left_of" do
+ categories(:child_3).move_to_left_of(categories(:child_1))
+ expect(categories(:child_3).left_sibling).to be_nil
+ expect(categories(:child_1)).to eq(categories(:child_3).right_sibling)
+ expect(Category.valid?).to be_truthy
+ end
+
+ it "move_to_right_of" do
+ categories(:child_1).move_to_right_of(categories(:child_3))
+ expect(categories(:child_1).right_sibling).to be_nil
+ expect(categories(:child_3)).to eq(categories(:child_1).left_sibling)
+ expect(Category.valid?).to be_truthy
+ end
+
+ it "move_to_root" do
+ categories(:child_2).move_to_root
+ expect(categories(:child_2).parent).to be_nil
+ expect(categories(:child_2).level).to eq(0)
+ expect(categories(:child_2_1).level).to eq(1)
+ expect(categories(:child_2).left).to eq(9)
+ expect(categories(:child_2).right).to eq(12)
+ expect(Category.valid?).to be_truthy
+ end
+
+ it "move_to_child_of" do
+ categories(:child_1).move_to_child_of(categories(:child_3))
+ expect(categories(:child_3).id).to eq(categories(:child_1).parent_id)
+ expect(Category.valid?).to be_truthy
+ end
+
+ describe "#move_to_child_with_index" do
+ it "move to a node without child" do
+ categories(:child_1).move_to_child_with_index(categories(:child_3), 0)
+ expect(categories(:child_3).id).to eq(categories(:child_1).parent_id)
+ expect(categories(:child_1).left).to eq(7)
+ expect(categories(:child_1).right).to eq(8)
+ expect(categories(:child_3).left).to eq(6)
+ expect(categories(:child_3).right).to eq(9)
+ expect(Category.valid?).to be_truthy
+ end
+
+ it "move to a node to the left child" do
+ categories(:child_1).move_to_child_with_index(categories(:child_2), 0)
+ expect(categories(:child_1).parent_id).to eq(categories(:child_2).id)
+ expect(categories(:child_2_1).left).to eq(5)
+ expect(categories(:child_2_1).right).to eq(6)
+ expect(categories(:child_1).left).to eq(3)
+ expect(categories(:child_1).right).to eq(4)
+ categories(:child_2).reload
+ expect(categories(:child_2).left).to eq(2)
+ expect(categories(:child_2).right).to eq(7)
+ end
+
+ it "move to a node to the right child" do
+ categories(:child_1).move_to_child_with_index(categories(:child_2), 1)
+ expect(categories(:child_1).parent_id).to eq(categories(:child_2).id)
+ expect(categories(:child_2_1).left).to eq(3)
+ expect(categories(:child_2_1).right).to eq(4)
+ expect(categories(:child_1).left).to eq(5)
+ expect(categories(:child_1).right).to eq(6)
+ categories(:child_2).reload
+ expect(categories(:child_2).left).to eq(2)
+ expect(categories(:child_2).right).to eq(7)
+ end
+
+ it "move downward within current parent" do
+ categories(:child_1).move_to_child_with_index(categories(:top_level), 1)
+ expect(categories(:child_1).parent_id).to eq(categories(:top_level).id)
+ expect(categories(:child_1).left).to eq(6)
+ expect(categories(:child_1).right).to eq(7)
+ categories(:child_2).reload
+ expect(categories(:child_2).parent_id).to eq(categories(:top_level).id)
+ expect(categories(:child_2).left).to eq(2)
+ expect(categories(:child_2).right).to eq(5)
+ end
+
+ it "move to the same position within current parent" do
+ categories(:child_1).move_to_child_with_index(categories(:top_level), 0)
+ expect(categories(:child_1).parent_id).to eq(categories(:top_level).id)
+ expect(categories(:child_1).left).to eq(2)
+ expect(categories(:child_1).right).to eq(3)
+ end
+ end
+
+ it "move_to_child_of_appends_to_end" do
+ child = Category.create! :name => 'New Child'
+ child.move_to_child_of categories(:top_level)
+ expect(child).to eq(categories(:top_level).children.last)
+ end
+
+ it "subtree_move_to_child_of" do
+ expect(categories(:child_2).left).to eq(4)
+ expect(categories(:child_2).right).to eq(7)
+
+ expect(categories(:child_1).left).to eq(2)
+ expect(categories(:child_1).right).to eq(3)
+
+ categories(:child_2).move_to_child_of(categories(:child_1))
+ expect(Category.valid?).to be_truthy
+ expect(categories(:child_1).id).to eq(categories(:child_2).parent_id)
+
+ expect(categories(:child_2).left).to eq(3)
+ expect(categories(:child_2).right).to eq(6)
+ expect(categories(:child_1).left).to eq(2)
+ expect(categories(:child_1).right).to eq(7)
+ end
+
+ it "slightly_difficult_move_to_child_of" do
+ expect(categories(:top_level_2).left).to eq(11)
+ expect(categories(:top_level_2).right).to eq(12)
+
+ # create a new top-level node and move single-node top-level tree inside it.
+ new_top = Category.create(:name => 'New Top')
+ expect(new_top.left).to eq(13)
+ expect(new_top.right).to eq(14)
+
+ categories(:top_level_2).move_to_child_of(new_top)
+
+ expect(Category.valid?).to be_truthy
+ expect(new_top.id).to eq(categories(:top_level_2).parent_id)
+
+ expect(categories(:top_level_2).left).to eq(12)
+ expect(categories(:top_level_2).right).to eq(13)
+ expect(new_top.left).to eq(11)
+ expect(new_top.right).to eq(14)
+ end
+
+ it "difficult_move_to_child_of" do
+ expect(categories(:top_level).left).to eq(1)
+ expect(categories(:top_level).right).to eq(10)
+ expect(categories(:child_2_1).left).to eq(5)
+ expect(categories(:child_2_1).right).to eq(6)
+
+ # create a new top-level node and move an entire top-level tree inside it.
+ new_top = Category.create(:name => 'New Top')
+ categories(:top_level).move_to_child_of(new_top)
+ categories(:child_2_1).reload
+ expect(Category.valid?).to be_truthy
+ expect(new_top.id).to eq(categories(:top_level).parent_id)
+
+ expect(categories(:top_level).left).to eq(4)
+ expect(categories(:top_level).right).to eq(13)
+ expect(categories(:child_2_1).left).to eq(8)
+ expect(categories(:child_2_1).right).to eq(9)
+ end
+
+ #rebuild swaps the position of the 2 children when added using move_to_child twice onto same parent
+ it "move_to_child_more_than_once_per_parent_rebuild" do
+ root1 = Category.create(:name => 'Root1')
+ root2 = Category.create(:name => 'Root2')
+ root3 = Category.create(:name => 'Root3')
+
+ root2.move_to_child_of root1
+ root3.move_to_child_of root1
+
+ output = Category.roots.last.to_text
+ Category.update_all('lft = null, rgt = null')
+ Category.rebuild!
+
+ expect(Category.roots.last.to_text).to eq(output)
+ end
+
+ # doing move_to_child twice onto same parent from the furthest right first
+ it "move_to_child_more_than_once_per_parent_outside_in" do
+ node1 = Category.create(:name => 'Node-1')
+ node2 = Category.create(:name => 'Node-2')
+ node3 = Category.create(:name => 'Node-3')
+
+ node2.move_to_child_of node1
+ node3.move_to_child_of node1
+
+ output = Category.roots.last.to_text
+ Category.update_all('lft = null, rgt = null')
+ Category.rebuild!
+
+ expect(Category.roots.last.to_text).to eq(output)
+ end
+
+ it "should_move_to_ordered_child" do
+ node1 = Category.create(:name => 'Node-1')
+ node2 = Category.create(:name => 'Node-2')
+ node3 = Category.create(:name => 'Node-3')
+
+ node2.move_to_ordered_child_of(node1, "name")
+
+ assert_equal node1, node2.parent
+ assert_equal 1, node1.children.count
+
+ node3.move_to_ordered_child_of(node1, "name", true) # ascending
+
+ assert_equal node1, node3.parent
+ assert_equal 2, node1.children.count
+ assert_equal node2.name, node1.children[0].name
+ assert_equal node3.name, node1.children[1].name
+
+ node3.move_to_ordered_child_of(node1, "name", false) # descending
+ node1.reload
+
+ expect(node3.parent).to eq(node1)
+ expect(node1.children.count).to be(2)
+ expect(node1.children[0].name).to eq(node3.name)
+ expect(node1.children[1].name).to eq(node2.name)
+ end
+
+ it "should be able to rebuild without validating each record" do
+ root1 = Category.create(:name => 'Root1')
+ root2 = Category.create(:name => 'Root2')
+ root3 = Category.create(:name => 'Root3')
+
+ root2.move_to_child_of root1
+ root3.move_to_child_of root1
+
+ root2.name = nil
+ root2.save!(:validate => false)
+
+ output = Category.roots.last.to_text
+ Category.update_all('lft = null, rgt = null')
+ Category.rebuild!(false)
+
+ expect(Category.roots.last.to_text).to eq(output)
+ end
+
+ it "valid_with_null_lefts" do
+ expect(Category.valid?).to be_truthy
+ Category.update_all('lft = null')
+ expect(Category.valid?).to be_falsey
+ end
+
+ it "valid_with_null_rights" do
+ expect(Category.valid?).to be_truthy
+ Category.update_all('rgt = null')
+ expect(Category.valid?).to be_falsey
+ end
+
+ it "valid_with_missing_intermediate_node" do
+ # Even though child_2_1 will still exist, it is a sign of a sloppy delete, not an invalid tree.
+ expect(Category.valid?).to be_truthy
+ Category.delete(categories(:child_2).id)
+ expect(Category.valid?).to be_truthy
+ end
+
+ it "valid_with_overlapping_and_rights" do
+ expect(Category.valid?).to be_truthy
+ categories(:top_level_2)['lft'] = 0
+ categories(:top_level_2).save
+ expect(Category.valid?).to be_falsey
+ end
+
+ it "rebuild" do
+ expect(Category.valid?).to be_truthy
+ before_text = Category.root.to_text
+ Category.update_all('lft = null, rgt = null')
+ Category.rebuild!
+ expect(Category.valid?).to be_truthy
+ expect(before_text).to eq(Category.root.to_text)
+ end
+
+ describe ".rebuild!" do
+ subject { Thing.rebuild! }
+ before { Thing.update_all(children_count: 0) }
+
+ context "when items have children" do
+ it "updates their counter_cache" do
+ expect { subject }.to change {
+ things(:parent1).reload.children_count }.to(2).from(0).
+ and change { things(:child_2).reload.children_count }.to(1).from(0)
+ end
+ end
+
+ context "when items do not have children" do
+ it "doesn't change their counter_cache" do
+ subject
+ expect(things(:child_1).reload.children_count).to eq(0)
+ expect(things(:child_2_1).reload.children_count).to eq(0)
+ end
+ end
+ end
+
+ it "move_possible_for_sibling" do
+ expect(categories(:child_2).move_possible?(categories(:child_1))).to be_truthy
+ end
+
+ it "move_not_possible_to_self" do
+ expect(categories(:top_level).move_possible?(categories(:top_level))).to be_falsey
+ end
+
+ it "move_not_possible_to_parent" do
+ categories(:top_level).descendants.each do |descendant|
+ expect(categories(:top_level).move_possible?(descendant)).to be_falsey
+ expect(descendant.move_possible?(categories(:top_level))).to be_truthy
+ end
+ end
+
+ it "is_or_is_ancestor_of?" do
+ [:child_1, :child_2, :child_2_1, :child_3].each do |c|
+ expect(categories(:top_level).is_or_is_ancestor_of?(categories(c))).to be_truthy
+ end
+ expect(categories(:top_level).is_or_is_ancestor_of?(categories(:top_level_2))).to be_falsey
+ end
+
+ it "left_and_rights_valid_with_blank_left" do
+ expect(Category.left_and_rights_valid?).to be_truthy
+ categories(:child_2)[:lft] = nil
+ categories(:child_2).save(:validate => false)
+ expect(Category.left_and_rights_valid?).to be_falsey
+ end
+
+ it "left_and_rights_valid_with_blank_right" do
+ expect(Category.left_and_rights_valid?).to be_truthy
+ categories(:child_2)[:rgt] = nil
+ categories(:child_2).save(:validate => false)
+ expect(Category.left_and_rights_valid?).to be_falsey
+ end
+
+ it "left_and_rights_valid_with_equal" do
+ expect(Category.left_and_rights_valid?).to be_truthy
+ categories(:top_level_2)[:lft] = categories(:top_level_2)[:rgt]
+ categories(:top_level_2).save(:validate => false)
+ expect(Category.left_and_rights_valid?).to be_falsey
+ end
+
+ it "left_and_rights_valid_with_left_equal_to_parent" do
+ expect(Category.left_and_rights_valid?).to be_truthy
+ categories(:child_2)[:lft] = categories(:top_level)[:lft]
+ categories(:child_2).save(:validate => false)
+ expect(Category.left_and_rights_valid?).to be_falsey
+ end
+
+ it "left_and_rights_valid_with_right_equal_to_parent" do
+ expect(Category.left_and_rights_valid?).to be_truthy
+ categories(:child_2)[:rgt] = categories(:top_level)[:rgt]
+ categories(:child_2).save(:validate => false)
+ expect(Category.left_and_rights_valid?).to be_falsey
+ end
+
+ it "moving_dirty_objects_doesnt_invalidate_tree" do
+ r1 = Category.create :name => "Test 1"
+ r2 = Category.create :name => "Test 2"
+ r3 = Category.create :name => "Test 3"
+ r4 = Category.create :name => "Test 4"
+ nodes = [r1, r2, r3, r4]
+
+ r2.move_to_child_of(r1)
+ expect(Category.valid?).to be_truthy
+
+ r3.move_to_child_of(r1)
+ expect(Category.valid?).to be_truthy
+
+ r4.move_to_child_of(r2)
+ expect(Category.valid?).to be_truthy
+ end
+
+ it "multi_scoped_no_duplicates_for_columns?" do
+ expect {
+ Note.no_duplicates_for_columns?
+ }.not_to raise_exception
+ end
+
+ it "multi_scoped_all_roots_valid?" do
+ expect {
+ Note.all_roots_valid?
+ }.not_to raise_exception
+ end
+
+ it "multi_scoped" do
+ note1 = Note.create!(:body => "A", :notable_id => 2, :notable_type => 'Category')
+ note2 = Note.create!(:body => "B", :notable_id => 2, :notable_type => 'Category')
+ note3 = Note.create!(:body => "C", :notable_id => 2, :notable_type => 'Default')
+
+ expect([note1, note2]).to eq(note1.self_and_siblings)
+ expect([note3]).to eq(note3.self_and_siblings)
+ end
+
+ it "multi_scoped_rebuild" do
+ root = Note.create!(:body => "A", :notable_id => 3, :notable_type => 'Category')
+ child1 = Note.create!(:body => "B", :notable_id => 3, :notable_type => 'Category')
+ child2 = Note.create!(:body => "C", :notable_id => 3, :notable_type => 'Category')
+
+ child1.move_to_child_of root
+ child2.move_to_child_of root
+
+ Note.update_all('lft = null, rgt = null')
+ Note.rebuild!
+
+ expect(Note.roots.find_by_body('A')).to eq(root)
+ expect([child1, child2]).to eq(Note.roots.find_by_body('A').children)
+ end
+
+ it "same_scope_with_multi_scopes" do
+ expect {
+ notes(:scope1).same_scope?(notes(:child_1))
+ }.not_to raise_exception
+ expect(notes(:scope1).same_scope?(notes(:child_1))).to be_truthy
+ expect(notes(:child_1).same_scope?(notes(:scope1))).to be_truthy
+ expect(notes(:scope1).same_scope?(notes(:scope2))).to be_falsey
+ end
+
+ it "quoting_of_multi_scope_column_names" do
+ ## Proper Array Assignment for different DBs as per their quoting column behavior
+ if Note.connection.adapter_name.match(/oracle/i)
+ expected_quoted_scope_column_names = ["\"NOTABLE_ID\"", "\"NOTABLE_TYPE\""]
+ elsif Note.connection.adapter_name.match(/mysql/i)
+ expected_quoted_scope_column_names = ["`notable_id`", "`notable_type`"]
+ else
+ expected_quoted_scope_column_names = ["\"notable_id\"", "\"notable_type\""]
+ end
+ expect(Note.quoted_scope_column_names).to eq(expected_quoted_scope_column_names)
+ end
+
+ it "equal_in_same_scope" do
+ expect(notes(:scope1)).to eq(notes(:scope1))
+ expect(notes(:scope1)).not_to eq(notes(:child_1))
+ end
+
+ it "equal_in_different_scopes" do
+ expect(notes(:scope1)).not_to eq(notes(:scope2))
+ end
+
+ it "delete_does_not_invalidate" do
+ Category.acts_as_nested_set_options[:dependent] = :delete
+ categories(:child_2).destroy
+ expect(Category.valid?).to be_truthy
+ end
+
+ it "destroy_does_not_invalidate" do
+ Category.acts_as_nested_set_options[:dependent] = :destroy
+ categories(:child_2).destroy
+ expect(Category.valid?).to be_truthy
+ end
+
+ it "destroy_multiple_times_does_not_invalidate" do
+ Category.acts_as_nested_set_options[:dependent] = :destroy
+ categories(:child_2).destroy
+ categories(:child_2).destroy
+ expect(Category.valid?).to be_truthy
+ end
+
+ it "assigning_parent_id_on_create" do
+ category = Category.create!(:name => "Child", :parent_id => categories(:child_2).id)
+ expect(categories(:child_2)).to eq(category.parent)
+ expect(categories(:child_2).id).to eq(category.parent_id)
+ expect(category.left).not_to be_nil
+ expect(category.right).not_to be_nil
+ expect(Category.valid?).to be_truthy
+ end
+
+ it "assigning_parent_on_create" do
+ category = Category.create!(:name => "Child", :parent => categories(:child_2))
+ expect(categories(:child_2)).to eq(category.parent)
+ expect(categories(:child_2).id).to eq(category.parent_id)
+ expect(category.left).not_to be_nil
+ expect(category.right).not_to be_nil
+ expect(Category.valid?).to be_truthy
+ end
+
+ it "assigning_parent_id_to_nil_on_create" do
+ category = Category.create!(:name => "New Root", :parent_id => nil)
+ expect(category.parent).to be_nil
+ expect(category.parent_id).to be_nil
+ expect(category.left).not_to be_nil
+ expect(category.right).not_to be_nil
+ expect(Category.valid?).to be_truthy
+ end
+
+ it "assigning_parent_id_on_update" do
+ category = categories(:child_2_1)
+ category.parent_id = categories(:child_3).id
+ category.save
+ category.reload
+ categories(:child_3).reload
+ expect(categories(:child_3)).to eq(category.parent)
+ expect(categories(:child_3).id).to eq(category.parent_id)
+ expect(Category.valid?).to be_truthy
+ end
+
+ it "assigning_parent_on_update" do
+ category = categories(:child_2_1)
+ category.parent = categories(:child_3)
+ category.save
+ category.reload
+ categories(:child_3).reload
+ expect(categories(:child_3)).to eq(category.parent)
+ expect(categories(:child_3).id).to eq(category.parent_id)
+ expect(Category.valid?).to be_truthy
+ end
+
+ it "assigning_parent_id_to_nil_on_update" do
+ category = categories(:child_2_1)
+ category.parent_id = nil
+ category.save
+ expect(category.parent).to be_nil
+ expect(category.parent_id).to be_nil
+ expect(Category.valid?).to be_truthy
+ end
+
+ it "creating_child_from_parent" do
+ category = categories(:child_2).children.create!(:name => "Child")
+ expect(categories(:child_2)).to eq(category.parent)
+ expect(categories(:child_2).id).to eq(category.parent_id)
+ expect(category.left).not_to be_nil
+ expect(category.right).not_to be_nil
+ expect(Category.valid?).to be_truthy
+ end
+
+ def check_structure(entries, structure)
+ structure = structure.dup
+ Category.each_with_level(entries) do |category, level|
+ expected_level, expected_name = structure.shift
+ expect(expected_name).to eq(category.name)
+ expect(expected_level).to eq(level)
+ end
+ end
+
+ it "each_with_level" do
+ levels = [
+ [0, "Top Level"],
+ [1, "Child 1"],
+ [1, "Child 2"],
+ [2, "Child 2.1"],
+ [1, "Child 3" ]
+ ]
+
+ check_structure(Category.root.self_and_descendants, levels)
+
+ # test some deeper structures
+ category = Category.find_by_name("Child 1")
+ c1 = Category.new(:name => "Child 1.1")
+ c2 = Category.new(:name => "Child 1.1.1")
+ c3 = Category.new(:name => "Child 1.1.1.1")
+ c4 = Category.new(:name => "Child 1.2")
+ [c1, c2, c3, c4].each(&:save!)
+
+ c1.move_to_child_of(category)
+ c2.move_to_child_of(c1)
+ c3.move_to_child_of(c2)
+ c4.move_to_child_of(category)
+
+ levels = [
+ [0, "Top Level"],
+ [1, "Child 1"],
+ [2, "Child 1.1"],
+ [3, "Child 1.1.1"],
+ [4, "Child 1.1.1.1"],
+ [2, "Child 1.2"],
+ [1, "Child 2"],
+ [2, "Child 2.1"],
+ [1, "Child 3" ]
+ ]
+
+ check_structure(Category.root.self_and_descendants, levels)
+ end
+
+ describe "before_move_callback" do
+ it "should fire the callback" do
+ expect(categories(:child_2)).to receive(:custom_before_move)
+ categories(:child_2).move_to_root
+ end
+
+ it "should stop move when callback returns false" do
+ Category.test_allows_move = false
+ expect(categories(:child_3).move_to_root).to be_falsey
+ expect(categories(:child_3).root?).to be_falsey
+ end
+
+ it "should not halt save actions" do
+ Category.test_allows_move = false
+ categories(:child_3).parent_id = nil
+ expect(categories(:child_3).save).to be_truthy
+ end
+ end
+
+ describe "counter_cache" do
+ let(:parent1) { things(:parent1) }
+ let(:child_1) { things(:child_1) }
+ let(:child_2) { things(:child_2) }
+ let(:child_2_1) { things(:child_2_1) }
+
+ it "should allow use of a counter cache for children" do
+ expect(parent1.children_count).to eq(parent1.children.count)
+ end
+
+ it "should increment the counter cache on create" do
+ expect {
+ parent1.children.create body: "Child 3"
+ }.to change { parent1.reload.children_count }.by(1)
+ end
+
+ it "should decrement the counter cache on destroy" do
+ expect {
+ parent1.children.last.destroy
+ }.to change { parent1.reload.children_count }.by(-1)
+ end
+
+ context "when moving a grandchild to the root" do
+ subject { child_2_1.move_to_root }
+
+ it "should decrement the counter cache of its parent" do
+ expect { subject }.to change { child_2.reload.children_count }.by(-1)
+ end
+ end
+
+ context "when moving within a node" do
+ subject { child_1.move_to_right_of(child_2) }
+
+ it "should not update any values" do
+ expect { subject }.to_not change { parent1.reload.children_count }
+ end
+ end
+
+ context "when a child moves to another node" do
+ let(:old_parent) { things(:child_2) }
+ let(:child) { things(:child_2_1) }
+ let!(:new_parent) { Thing.create(body: "New Parent") }
+
+ subject { child.move_to_child_of(new_parent) }
+
+ it "should decrement the counter cache of its parent" do
+ expect { subject }.to change { old_parent.reload.children_count }.by(-1)
+ end
+
+ it "should increment the counter cache of its new parent" do
+ expect { subject }.to change { new_parent.reload.children_count }.by(1)
+ end
+ end
+
+ context "when class is an STI" do
+ let(:sti_subclass1) { Subclass1.create(name: "Subclass1") }
+ let(:sti_subclass2) { Subclass2.create(name: "Subclass2") }
+ let(:sti_subclass3) { Subclass2.create(name: "Subclass3") }
+
+ # https://github.com/collectiveidea/awesome_nested_set/issues/415
+ it 'should update counter cache of a parent' do
+ sti_subclass2.move_to_child_of sti_subclass1
+
+ sti_subclass1.reload
+ expect(sti_subclass1.children_count).to be 1
+
+ sti_subclass2.move_to_child_of sti_subclass3
+
+ sti_subclass1.reload
+ sti_subclass3.reload
+ expect(sti_subclass1.children_count).to be 0
+ expect(sti_subclass3.children_count).to be 1
+ end
+ end
+ end
+
+ describe "association callbacks on children" do
+ it "should call the appropriate callbacks on the children :has_many association " do
+ root = DefaultWithCallbacks.create
+ expect(root).not_to be_new_record
+
+ child = root.children.build
+
+ expect(root.before_add).to eq(child)
+ expect(root.after_add).to eq(child)
+
+ expect(root.before_remove).not_to eq(child)
+ expect(root.after_remove).not_to eq(child)
+
+ expect(child.save).to be_truthy
+ expect(root.children.delete(child)).to be_truthy
+
+ expect(root.before_remove).to eq(child)
+ expect(root.after_remove).to eq(child)
+ end
+ end
+
+ describe 'rebuilding tree with a default scope ordering' do
+ it "doesn't throw exception" do
+ expect { Position.rebuild! }.not_to raise_error
+ end
+ end
+
+ describe 'creating roots with a default scope ordering' do
+ it "assigns rgt and lft correctly" do
+ alpha = Order.create(:name => 'Alpha')
+ gamma = Order.create(:name => 'Gamma')
+ omega = Order.create(:name => 'Omega')
+
+ expect(alpha.lft).to eq(1)
+ expect(alpha.rgt).to eq(2)
+ expect(gamma.lft).to eq(3)
+ expect(gamma.rgt).to eq(4)
+ expect(omega.lft).to eq(5)
+ expect(omega.rgt).to eq(6)
+ end
+ end
+
+ describe 'moving node from one scoped tree to another' do
+ xit "moves single node correctly" do
+ root1 = Note.create!(:body => "A-1", :notable_id => 4, :notable_type => 'Category')
+ child1_1 = Note.create!(:body => "B-1", :notable_id => 4, :notable_type => 'Category')
+ child1_2 = Note.create!(:body => "C-1", :notable_id => 4, :notable_type => 'Category')
+ child1_1.move_to_child_of root1
+ child1_2.move_to_child_of root1
+
+ root2 = Note.create!(:body => "A-2", :notable_id => 5, :notable_type => 'Category')
+ child2_1 = Note.create!(:body => "B-2", :notable_id => 5, :notable_type => 'Category')
+ child2_2 = Note.create!(:body => "C-2", :notable_id => 5, :notable_type => 'Category')
+ child2_1.move_to_child_of root2
+ child2_2.move_to_child_of root2
+
+ child1_1.update_attributes!(:notable_id => 5)
+ child1_1.move_to_child_of root2
+
+ expect(root1.children).to eq([child1_2])
+ expect(root2.children).to eq([child2_1, child2_2, child1_1])
+
+ expect(Note.valid?).to eq(true)
+ end
+
+ xit "moves node with children correctly" do
+ root1 = Note.create!(:body => "A-1", :notable_id => 4, :notable_type => 'Category')
+ child1_1 = Note.create!(:body => "B-1", :notable_id => 4, :notable_type => 'Category')
+ child1_2 = Note.create!(:body => "C-1", :notable_id => 4, :notable_type => 'Category')
+ child1_1.move_to_child_of root1
+ child1_2.move_to_child_of child1_1
+
+ root2 = Note.create!(:body => "A-2", :notable_id => 5, :notable_type => 'Category')
+ child2_1 = Note.create!(:body => "B-2", :notable_id => 5, :notable_type => 'Category')
+ child2_2 = Note.create!(:body => "C-2", :notable_id => 5, :notable_type => 'Category')
+ child2_1.move_to_child_of root2
+ child2_2.move_to_child_of root2
+
+ child1_1.update_attributes!(:notable_id => 5)
+ child1_1.move_to_child_of root2
+
+ expect(root1.children).to eq([])
+ expect(root2.children).to eq([child2_1, child2_2, child1_1])
+ child1_1.children is_expected.to eq([child1_2])
+ expect(root2.siblings).to eq([child2_1, child2_2, child1_1, child1_2])
+
+ expect(Note.valid?).to eq(true)
+ end
+ end
+
+ describe 'specifying custom sort column' do
+ it "should sort by the default sort column" do
+ expect(Category.order_column_name).to eq('lft')
+ end
+
+ it "should sort by custom sort column" do
+ expect(OrderedCategory.acts_as_nested_set_options[:order_column]).to eq('name')
+ expect(OrderedCategory.order_column_name).to eq('name')
+ expect(OrderedCategory.first.children.explain).to include('ORDER BY name')
+ end
+
+ it "should sort by custom hash sort" do
+ expect(HashOrderedCategory.acts_as_nested_set_options[:order_column]).to eq({ :name => :desc })
+ expect(HashOrderedCategory.order_column_name).to eq({ :name => :desc })
+ expect(HashOrderedCategory.first.children.explain).to include(
+ "ORDER BY #{HashOrderedCategory.quoted_table_name}.#{HashOrderedCategory.connection.quote_column_name(:name)} DESC"
+ )
+ end
+ end
+
+ describe 'associate_parents' do
+ it 'assigns parent' do
+ root = Category.root
+ categories = root.self_and_descendants
+ categories = Category.associate_parents categories
+ expect(categories[1].parent).to be categories.first
+ end
+
+ it 'adds children on inverse of association' do
+ root = Category.root
+ categories = root.self_and_descendants
+ categories = Category.associate_parents categories
+ expect(categories[0].children.first).to be categories[1]
+ end
+ end
+
+ describe 'table inheritance' do
+ it 'allows creation of a subclass pointing to a superclass' do
+ subclass1 = Subclass1.create(name: "Subclass1")
+ Subclass2.create(name: "Subclass2", parent_id: subclass1.id)
+ end
+ end
+
+ describe 'option dependent' do
+ it 'destroy should destroy children and node' do
+ Category.acts_as_nested_set_options[:dependent] = :destroy
+ root = Category.root
+ root.destroy!
+ expect(Category.where(id: root.id)).to be_empty
+ expect(Category.where(parent_id: root.id)).to be_empty
+ end
+
+ it "properly destroy association's objects and its children and nodes" do
+ Category.acts_as_nested_set_options[:dependent] = :destroy
+ user = User.first
+ note_ids = user.note_ids
+ user.notes.destroy_all
+ expect(Note.where(id: note_ids, user_id: user.id).count).to be_zero
+ end
+
+ it 'delete should delete children and node' do
+ Category.acts_as_nested_set_options[:dependent] = :delete
+ root = Category.root
+ root.destroy!
+ expect(Category.where(id: root.id)).to be_empty
+ expect(Category.where(parent_id: root.id)).to be_empty
+ end
+
+ it 'nullify should nullify child parent IDs rather than deleting' do
+ Category.acts_as_nested_set_options[:dependent] = :nullify
+ root = Category.root
+ child_ids = root.child_ids
+ root.destroy!
+ expect(Category.where(id: child_ids)).to_not be_empty
+ expect(Category.where(parent_id: root.id)).to be_empty
+ end
+
+ describe 'restrict_with_exception' do
+ it 'raises an exception' do
+ Category.acts_as_nested_set_options[:dependent] = :restrict_with_exception
+ root = Category.root
+ expect { root.destroy! }.to raise_error ActiveRecord::DeleteRestrictionError, 'Cannot delete record because of dependent children'
+ end
+
+ it 'deletes the leaf' do
+ Category.acts_as_nested_set_options[:dependent] = :restrict_with_exception
+ leaf = Category.last
+ expect(leaf.destroy).to eq(leaf)
+ end
+ end
+
+ describe 'restrict_with_error' do
+ it 'adds the error to the parent' do
+ Category.acts_as_nested_set_options[:dependent] = :restrict_with_error
+ root = Category.root
+ root.destroy
+ expect(root.errors[:base]).to eq(["Cannot delete record because dependent children exist"])
+ end
+
+ it 'deletes the leaf' do
+ Category.acts_as_nested_set_options[:dependent] = :restrict_with_error
+ leaf = Category.last
+ expect(leaf.destroy).to eq(leaf)
+ end
+ end
+
+ describe "model with default_scope" do
+ it "should have correct #lft & #rgt" do
+ parent = DefaultScopedModel.find(6)
+
+ DefaultScopedModel.send(:default_scopes=, [])
+ DefaultScopedModel.send(:default_scope, Proc.new { parent.reload.self_and_descendants })
+
+ children = parent.children.create(name: 'Helloworld')
+
+ DefaultScopedModel.unscoped do
+ expect(children.is_descendant_of?(parent.reload)).to be true
+ end
+ end
+
+ it "is .all_roots_valid? even when default_scope has custom order" do
+ class DefaultScopedModel; default_scope -> { order(rgt: :desc) }; end
+ expect(DefaultScopedModel.all_roots_valid?).to be_truthy
+ end
+
+ it "is .all_roots_valid? even when uses multi scope" do
+ class DefaultScopedModel
+ acts_as_nested_set :scope => [:id]
+ default_scope -> { order(rgt: :desc) }
+ end
+ expect(DefaultScopedModel.all_roots_valid?).to be_truthy
+ end
+
+ it "should respect the default_scope" do
+ DefaultScopedModel.send(:default_scopes=, [])
+ DefaultScopedModel.send(:default_scope, -> { DefaultScopedModel.where(draft: false) })
+
+ no_parents = DefaultScopedModel.find(1)
+
+ expect(no_parents.self_and_ancestors.count).to eq(1)
+
+ no_parents.draft = true
+ no_parents.save
+
+ other_root = DefaultScopedModel.create!(name: 'Another root')
+ expect(other_root.self_and_ancestors.count).to eq(1)
+ end
+ end
+ end
+end
diff --git a/spec/db/database.yml b/spec/db/database.yml
new file mode 100644
index 0000000..93a82be
--- /dev/null
+++ b/spec/db/database.yml
@@ -0,0 +1,39 @@
+sqlite3:
+ adapter: <%= "jdbc" if defined? JRUBY_VERSION %>sqlite3
+ database: awesome_nested_set.sqlite3.db
+sqlite3mem:
+ adapter: <%= "jdbc" if defined? JRUBY_VERSION %>sqlite3
+ database: ":memory:"
+postgresql:
+ adapter: postgresql
+ encoding: unicode
+ database: awesome_nested_set_plugin_test
+ pool: 5
+ username: postgres
+ password: postgres
+ min_messages: warning
+ <% if ENV['CI'] %>
+ url: postgres://postgres:postgres@127.0.0.1:5432
+ <% end %>
+mysql:
+ adapter: <%= defined?(JRUBY_VERSION) ? "jdbcmysql" : "mysql2" %>
+ host: <%= ENV['CI'] ? '127.0.0.1' : 'localhost' %>
+ username: root
+ password:
+ database: awesome_nested_set_plugin_test
+ <% if ENV['CI'] %>
+ port: 3306
+ <% end %>
+mariadb:
+ adapter: <%= defined?(JRUBY_VERSION) ? "jdbcmysql" : "mysql2" %>
+ host: <%= ENV['CI'] ? '127.0.0.1' : 'localhost' %>
+ username: root
+ password:
+ database: awesome_nested_set_plugin_test
+## Add DB Configuration to run Oracle tests
+oracle:
+ adapter: oracle_enhanced
+ host: localhost
+ username: awesome_nested_set_dev
+ password:
+ database: xe
diff --git a/spec/db/schema.rb b/spec/db/schema.rb
new file mode 100644
index 0000000..d2df537
--- /dev/null
+++ b/spec/db/schema.rb
@@ -0,0 +1,103 @@
+ActiveRecord::Schema.define(:version => 0) do
+
+ create_table :default_scoped_models, :force => true do |t|
+ t.column :name, :string
+ t.column :parent_id, :integer
+ t.column :lft, :integer
+ t.column :rgt, :integer
+ t.column :depth, :integer
+ t.column :draft, :boolean, default: false
+ end
+
+ create_table :categories, :force => true do |t|
+ t.column :name, :string
+ t.column :parent_id, :integer
+ t.column :lft, :integer
+ t.column :rgt, :integer
+ t.column :depth, :integer
+ t.column :organization_id, :integer
+ end
+
+ create_table :departments, :force => true do |t|
+ t.column :name, :string
+ end
+
+ create_table :notes, :force => true do |t|
+ t.column :body, :text
+ t.column :parent_id, :integer
+ t.column :lft, :integer
+ t.column :rgt, :integer
+ t.column :depth, :integer
+ t.column :notable_id, :integer
+ t.column :notable_type, :string
+ t.column :user_id, :integer
+ end
+
+ create_table :renamed_columns, :force => true do |t|
+ t.column :name, :string
+ t.column :mother_id, :integer
+ t.column :red, :integer
+ t.column :black, :integer
+ t.column :pitch, :integer
+ end
+
+ create_table :things, :force => true do |t|
+ t.column :body, :text
+ t.column :parent_id, :integer
+ t.column :lft, :integer
+ t.column :rgt, :integer
+ t.column :depth, :integer
+ t.column :children_count, :integer, null: false, default: 0
+ end
+
+ create_table :brokens, :force => true do |t|
+ t.column :name, :string
+ t.column :parent_id, :integer
+ t.column :lft, :integer
+ t.column :rgt, :integer
+ t.column :depth, :integer
+ end
+
+ create_table :orders, :force => true do |t|
+ t.column :name, :string
+ t.column :parent_id, :integer
+ t.column :lft, :integer
+ t.column :rgt, :integer
+ t.column :depth, :integer
+ end
+
+ create_table :positions, :force => true do |t|
+ t.column :name, :string
+ t.column :parent_id, :integer
+ t.column :lft, :integer
+ t.column :rgt, :integer
+ t.column :depth, :integer
+ t.column :position, :integer
+ end
+
+ create_table :no_depths, :force => true do |t|
+ t.column :name, :string
+ t.column :parent_id, :integer
+ t.column :lft, :integer
+ t.column :rgt, :integer
+ end
+
+ create_table :single_table_inheritance, :force => true do |t|
+ t.column :type, :string
+ t.column :name, :string
+ t.column :parent_id, :integer
+ t.column :lft, :integer
+ t.column :rgt, :integer
+ t.column :children_count, :integer, null: false, default: 0
+ end
+
+ create_table :users, :force => true do |t|
+ t.column :uuid, :string
+ t.column :name, :string
+ t.column :parent_uuid, :string
+ t.column :lft, :integer
+ t.column :rgt, :integer
+ t.column :depth, :integer
+ t.column :organization_id, :integer
+ end
+end
diff --git a/spec/fixtures/brokens.yml b/spec/fixtures/brokens.yml
new file mode 100644
index 0000000..3cb6ce9
--- /dev/null
+++ b/spec/fixtures/brokens.yml
@@ -0,0 +1,3 @@
+one:
+ id: 1
+ name: One
\ No newline at end of file
diff --git a/spec/fixtures/categories.yml b/spec/fixtures/categories.yml
new file mode 100644
index 0000000..bc8e078
--- /dev/null
+++ b/spec/fixtures/categories.yml
@@ -0,0 +1,34 @@
+top_level:
+ id: 1
+ name: Top Level
+ lft: 1
+ rgt: 10
+child_1:
+ id: 2
+ name: Child 1
+ parent_id: 1
+ lft: 2
+ rgt: 3
+child_2:
+ id: 3
+ name: Child 2
+ parent_id: 1
+ lft: 4
+ rgt: 7
+child_2_1:
+ id: 4
+ name: Child 2.1
+ parent_id: 3
+ lft: 5
+ rgt: 6
+child_3:
+ id: 5
+ name: Child 3
+ parent_id: 1
+ lft: 8
+ rgt: 9
+top_level_2:
+ id: 6
+ name: Top Level 2
+ lft: 11
+ rgt: 12
diff --git a/spec/fixtures/default_scoped_models.yml b/spec/fixtures/default_scoped_models.yml
new file mode 100644
index 0000000..9c27b25
--- /dev/null
+++ b/spec/fixtures/default_scoped_models.yml
@@ -0,0 +1,31 @@
+top_level:
+ id: 1
+ name: Top Level
+ lft: 1
+ rgt: 4
+ draft: false
+child_1:
+ id: 2
+ name: Child 1
+ parent_id: 1
+ lft: 2
+ rgt: 3
+ draft: false
+top_level_2:
+ id: 6
+ name: Top Level 2
+ lft: 5
+ rgt: 6
+ draft: false
+top_level_3:
+ id: 7
+ name: Top Level 3
+ lft: 7
+ rgt: 8
+ draft: false
+old_root:
+ id: 8
+ name: Top Level
+ lft: 1
+ rgt: 4
+ draft: true
diff --git a/spec/fixtures/departments.yml b/spec/fixtures/departments.yml
new file mode 100644
index 0000000..e50a944
--- /dev/null
+++ b/spec/fixtures/departments.yml
@@ -0,0 +1,3 @@
+top:
+ id: 1
+ name: Top
\ No newline at end of file
diff --git a/spec/fixtures/notes.yml b/spec/fixtures/notes.yml
new file mode 100644
index 0000000..efce709
--- /dev/null
+++ b/spec/fixtures/notes.yml
@@ -0,0 +1,42 @@
+scope1:
+ id: 1
+ body: Top Level
+ lft: 1
+ rgt: 10
+ notable_id: 1
+ notable_type: Category
+ user_id: 1
+child_1:
+ id: 2
+ body: Child 1
+ parent_id: 1
+ lft: 2
+ rgt: 3
+ notable_id: 1
+ notable_type: Category
+ user_id: 1
+child_2:
+ id: 3
+ body: Child 2
+ parent_id: 1
+ lft: 4
+ rgt: 7
+ notable_id: 1
+ notable_type: Category
+ user_id: 1
+child_3:
+ id: 4
+ body: Child 3
+ parent_id: 1
+ lft: 8
+ rgt: 9
+ notable_id: 1
+ notable_type: Category
+ user_id: 1
+scope2:
+ id: 5
+ body: Top Level 2
+ lft: 1
+ rgt: 2
+ notable_id: 1
+ notable_type: Departments
diff --git a/spec/fixtures/things.yml b/spec/fixtures/things.yml
new file mode 100644
index 0000000..3f8e5d0
--- /dev/null
+++ b/spec/fixtures/things.yml
@@ -0,0 +1,27 @@
+parent1:
+ id: 1
+ body: Top Level
+ lft: 1
+ rgt: 10
+ children_count: 2
+child_1:
+ id: 2
+ body: Child 1
+ parent_id: 1
+ lft: 2
+ rgt: 3
+ children_count: 0
+child_2:
+ id: 3
+ body: Child 2
+ parent_id: 1
+ lft: 4
+ rgt: 7
+ children_count: 1
+child_2_1:
+ id: 4
+ body: Child 2.1
+ parent_id: 3
+ lft: 8
+ rgt: 9
+ children_count: 0
diff --git a/spec/fixtures/users.yml b/spec/fixtures/users.yml
new file mode 100644
index 0000000..71d79e5
--- /dev/null
+++ b/spec/fixtures/users.yml
@@ -0,0 +1,40 @@
+top_level:
+ id: 1
+ uuid: abcd
+ name: Top Level
+ lft: 1
+ rgt: 10
+child_1:
+ id: 2
+ uuid: efgh
+ name: Child 1
+ parent_uuid: abcd
+ lft: 2
+ rgt: 3
+child_2:
+ id: 3
+ uuid: ijkh
+ name: Child 2
+ parent_uuid: abcd
+ lft: 4
+ rgt: 7
+child_2_1:
+ id: 4
+ uuid: lmno
+ name: Child 2.1
+ parent_uuid: ijkh
+ lft: 5
+ rgt: 6
+child_3:
+ id: 5
+ uuid: pqrs
+ name: Child 3
+ parent_uuid: abcd
+ lft: 8
+ rgt: 9
+top_level_2:
+ id: 6
+ uuid: tuvw
+ name: Top Level 2
+ lft: 11
+ rgt: 12
diff --git a/spec/models/users_spec.rb b/spec/models/users_spec.rb
new file mode 100644
index 0000000..4f6761d
--- /dev/null
+++ b/spec/models/users_spec.rb
@@ -0,0 +1,861 @@
+require 'spec_helper'
+
+describe "User", :type => :model do
+ before(:all) do
+ self.class.fixtures :categories, :departments, :notes, :things, :brokens, :users
+ end
+
+ describe "hierarchical structure" do
+ it "roots_class_method" do
+ found_by_us = User.where(:parent_uuid => nil).to_a
+ found_by_roots = User.roots.to_a
+ expect(found_by_us.length).to eq(found_by_roots.length)
+ found_by_us.each do |root|
+ expect(found_by_roots).to include(root)
+ end
+ end
+
+ it "root_class_method" do
+ expect(User.root).to eq(users(:top_level))
+ end
+
+ it "root" do
+ expect(users(:child_3).root).to eq(users(:top_level))
+ end
+
+ it "root when not persisted and parent_column_name value is self" do
+ new_user = User.new
+ expect(new_user.root).to eq(new_user)
+ end
+
+ it "root?" do
+ expect(users(:top_level).root?).to be_truthy
+ expect(users(:top_level_2).root?).to be_truthy
+ end
+
+ it "leaves_class_method" do
+ expect(User.where("#{User.right_column_name} - #{User.left_column_name} = 1").to_a.sort_by(&:id)).to eq(User.leaves.to_a)
+ expect(User.leaves.count).to eq(4)
+ expect(User.leaves).to include(users(:child_1))
+ expect(User.leaves).to include(users(:child_2_1))
+ expect(User.leaves).to include(users(:child_3))
+ expect(User.leaves).to include(users(:top_level_2))
+ end
+
+ it "leaf" do
+ expect(users(:child_1).leaf?).to be_truthy
+ expect(users(:child_2_1).leaf?).to be_truthy
+ expect(users(:child_3).leaf?).to be_truthy
+ expect(users(:top_level_2).leaf?).to be_truthy
+
+ expect(users(:top_level).leaf?).to be_falsey
+ expect(users(:child_2).leaf?).to be_falsey
+ expect(User.new.leaf?).to be_falsey
+ end
+
+ it "parent" do
+ expect(users(:child_2_1).parent).to eq(users(:child_2))
+ end
+
+ it "self_and_ancestors" do
+ child = users(:child_2_1)
+ self_and_ancestors = [users(:top_level), users(:child_2), child]
+ expect(child.self_and_ancestors).to eq(self_and_ancestors)
+ end
+
+ it "ancestors" do
+ child = users(:child_2_1)
+ ancestors = [users(:top_level), users(:child_2)]
+ expect(ancestors).to eq(child.ancestors)
+ end
+
+ it "self_and_siblings" do
+ child = users(:child_2)
+ self_and_siblings = [users(:child_1), child, users(:child_3)]
+ expect(self_and_siblings).to eq(child.self_and_siblings)
+ expect do
+ tops = [users(:top_level), users(:top_level_2)]
+ assert_equal tops, users(:top_level).self_and_siblings
+ end.not_to raise_exception
+ end
+
+ it "siblings" do
+ child = users(:child_2)
+ siblings = [users(:child_1), users(:child_3)]
+ expect(siblings).to eq(child.siblings)
+ end
+
+ it "leaves" do
+ leaves = [users(:child_1), users(:child_2_1), users(:child_3)]
+ expect(users(:top_level).leaves).to eq(leaves)
+ end
+ end
+
+ describe "level" do
+ it "returns the correct level" do
+ expect(users(:top_level).level).to eq(0)
+ expect(users(:child_1).level).to eq(1)
+ expect(users(:child_2_1).level).to eq(2)
+ end
+
+ context "given parent associations are loaded" do
+ it "returns the correct level" do
+ child = users(:child_1)
+ if child.respond_to?(:association)
+ child.association(:parent).load_target
+ child.parent.association(:parent).load_target
+ expect(child.level).to eq(1)
+ else
+ skip 'associations not used where child#association is not a method'
+ end
+ end
+ end
+ end
+
+ describe "depth" do
+ context "in general" do
+ let(:ceo) { User.create!(:name => "CEO") }
+ let(:district_manager) { User.create!(:name => "District Manager") }
+ let(:store_manager) { User.create!(:name => "Store Manager") }
+ let(:cashier) { User.create!(:name => "Cashier") }
+
+ before(:each) do
+ # ceo > district_manager > store_manager > cashier
+ district_manager.move_to_child_of(ceo)
+ store_manager.move_to_child_of(district_manager)
+ cashier.move_to_child_of(store_manager)
+ [ceo, district_manager, store_manager, cashier].each(&:reload)
+ end
+
+ it "updates depth when moved into child position" do
+ expect(ceo.depth).to eq(0)
+ expect(district_manager.depth).to eq(1)
+ expect(store_manager.depth).to eq(2)
+ expect(cashier.depth).to eq(3)
+ end
+
+ it "updates depth of all descendants when parent is moved" do
+ # ceo
+ # district_manager > store_manager > cashier
+ district_manager.move_to_right_of(ceo)
+ [ceo, district_manager, store_manager, cashier].each(&:reload)
+ expect(district_manager.depth).to eq(0)
+ expect(store_manager.depth).to eq(1)
+ expect(cashier.depth).to eq(2)
+ end
+ end
+
+ it "is magic and does not apply when column is missing" do
+ expect { NoDepth.create!(:name => "shallow") }.not_to raise_error
+ expect { NoDepth.first.save }.not_to raise_error
+ expect { NoDepth.rebuild! }.not_to raise_error
+
+ expect(NoDepth.method_defined?(:depth)).to be_falsey
+ expect(NoDepth.first.respond_to?(:depth)).to be_falsey
+ end
+ end
+
+ it "has_children?" do
+ expect(users(:child_2_1).children.empty?).to be_truthy
+ expect(users(:child_2).children.empty?).to be_falsey
+ expect(users(:top_level).children.empty?).to be_falsey
+ end
+
+ it "self_and_descendants" do
+ parent = users(:top_level)
+ self_and_descendants = [
+ parent,
+ users(:child_1),
+ users(:child_2),
+ users(:child_2_1),
+ users(:child_3)
+ ]
+ expect(self_and_descendants).to eq(parent.self_and_descendants)
+ expect(self_and_descendants.count).to eq(parent.self_and_descendants.count)
+ end
+
+ it "descendants" do
+ lawyers = User.create!(:name => "lawyers")
+ us = User.create!(:name => "United States")
+ us.move_to_child_of(lawyers)
+ patent = User.create!(:name => "Patent Law")
+ patent.move_to_child_of(us)
+ lawyers.reload
+
+ expect(lawyers.children.size).to eq(1)
+ expect(us.children.size).to eq(1)
+ expect(lawyers.descendants.size).to eq(2)
+ end
+
+ it "self_and_descendants" do
+ parent = users(:top_level)
+ descendants = [
+ users(:child_1),
+ users(:child_2),
+ users(:child_2_1),
+ users(:child_3)
+ ]
+ expect(descendants).to eq(parent.descendants)
+ end
+
+ it "children" do
+ user = users(:top_level)
+ user.children.each {|c| expect(user.uuid).to eq(c.parent_uuid) }
+ end
+
+ it "order_of_children" do
+ users(:child_2).move_left
+ expect(users(:child_2)).to eq(users(:top_level).children[0])
+ expect(users(:child_1)).to eq(users(:top_level).children[1])
+ expect(users(:child_3)).to eq(users(:top_level).children[2])
+ end
+
+ it "is_or_is_ancestor_of?" do
+ expect(users(:top_level).is_or_is_ancestor_of?(users(:child_1))).to be_truthy
+ expect(users(:top_level).is_or_is_ancestor_of?(users(:child_2_1))).to be_truthy
+ expect(users(:child_2).is_or_is_ancestor_of?(users(:child_2_1))).to be_truthy
+ expect(users(:child_2_1).is_or_is_ancestor_of?(users(:child_2))).to be_falsey
+ expect(users(:child_1).is_or_is_ancestor_of?(users(:child_2))).to be_falsey
+ expect(users(:child_1).is_or_is_ancestor_of?(users(:child_1))).to be_truthy
+ end
+
+ it "is_ancestor_of?" do
+ expect(users(:top_level).is_ancestor_of?(users(:child_1))).to be_truthy
+ expect(users(:top_level).is_ancestor_of?(users(:child_2_1))).to be_truthy
+ expect(users(:child_2).is_ancestor_of?(users(:child_2_1))).to be_truthy
+ expect(users(:child_2_1).is_ancestor_of?(users(:child_2))).to be_falsey
+ expect(users(:child_1).is_ancestor_of?(users(:child_2))).to be_falsey
+ expect(users(:child_1).is_ancestor_of?(users(:child_1))).to be_falsey
+ end
+
+ it "is_or_is_ancestor_of_with_scope" do
+ root = ScopedUser.root
+ child = root.children.first
+ expect(root.is_or_is_ancestor_of?(child)).to be_truthy
+ child.update_attribute :organization_id, 999999999
+ expect(root.is_or_is_ancestor_of?(child)).to be_falsey
+ end
+
+ it "is_or_is_descendant_of?" do
+ expect(users(:child_1).is_or_is_descendant_of?(users(:top_level))).to be_truthy
+ expect(users(:child_2_1).is_or_is_descendant_of?(users(:top_level))).to be_truthy
+ expect(users(:child_2_1).is_or_is_descendant_of?(users(:child_2))).to be_truthy
+ expect(users(:child_2).is_or_is_descendant_of?(users(:child_2_1))).to be_falsey
+ expect(users(:child_2).is_or_is_descendant_of?(users(:child_1))).to be_falsey
+ expect(users(:child_1).is_or_is_descendant_of?(users(:child_1))).to be_truthy
+ end
+
+ it "is_descendant_of?" do
+ expect(users(:child_1).is_descendant_of?(users(:top_level))).to be_truthy
+ expect(users(:child_2_1).is_descendant_of?(users(:top_level))).to be_truthy
+ expect(users(:child_2_1).is_descendant_of?(users(:child_2))).to be_truthy
+ expect(users(:child_2).is_descendant_of?(users(:child_2_1))).to be_falsey
+ expect(users(:child_2).is_descendant_of?(users(:child_1))).to be_falsey
+ expect(users(:child_1).is_descendant_of?(users(:child_1))).to be_falsey
+ end
+
+ it "is_or_is_descendant_of_with_scope" do
+ root = ScopedUser.root
+ child = root.children.first
+ expect(child.is_or_is_descendant_of?(root)).to be_truthy
+ child.update_attribute :organization_id, 999999999
+ expect(child.is_or_is_descendant_of?(root)).to be_falsey
+ end
+
+ it "same_scope?" do
+ root = ScopedUser.root
+ child = root.children.first
+ expect(child.same_scope?(root)).to be_truthy
+ child.update_attribute :organization_id, 999999999
+ expect(child.same_scope?(root)).to be_falsey
+ end
+
+ it "left_sibling" do
+ expect(users(:child_1)).to eq(users(:child_2).left_sibling)
+ expect(users(:child_2)).to eq(users(:child_3).left_sibling)
+ end
+
+ it "left_sibling_of_root" do
+ expect(users(:top_level).left_sibling).to be_nil
+ end
+
+ it "left_sibling_without_siblings" do
+ expect(users(:child_2_1).left_sibling).to be_nil
+ end
+
+ it "left_sibling_of_leftmost_node" do
+ expect(users(:child_1).left_sibling).to be_nil
+ end
+
+ it "right_sibling" do
+ expect(users(:child_3)).to eq(users(:child_2).right_sibling)
+ expect(users(:child_2)).to eq(users(:child_1).right_sibling)
+ end
+
+ it "right_sibling_of_root" do
+ expect(users(:top_level_2)).to eq(users(:top_level).right_sibling)
+ expect(users(:top_level_2).right_sibling).to be_nil
+ end
+
+ it "right_sibling_without_siblings" do
+ expect(users(:child_2_1).right_sibling).to be_nil
+ end
+
+ it "right_sibling_of_rightmost_node" do
+ expect(users(:child_3).right_sibling).to be_nil
+ end
+
+ it "move_left" do
+ users(:child_2).move_left
+ expect(users(:child_2).left_sibling).to be_nil
+ expect(users(:child_1)).to eq(users(:child_2).right_sibling)
+ expect(User.valid?).to be_truthy
+ end
+
+ it "move_right" do
+ users(:child_2).move_right
+ expect(users(:child_2).right_sibling).to be_nil
+ expect(users(:child_3)).to eq(users(:child_2).left_sibling)
+ expect(User.valid?).to be_truthy
+ end
+
+ it "move_to_left_of" do
+ users(:child_3).move_to_left_of(users(:child_1))
+ expect(users(:child_3).left_sibling).to be_nil
+ expect(users(:child_1)).to eq(users(:child_3).right_sibling)
+ expect(User.valid?).to be_truthy
+ end
+
+ it "move_to_right_of" do
+ users(:child_1).move_to_right_of(users(:child_3))
+ expect(users(:child_1).right_sibling).to be_nil
+ expect(users(:child_3)).to eq(users(:child_1).left_sibling)
+ expect(User.valid?).to be_truthy
+ end
+
+ it "move_to_root" do
+ users(:child_2).move_to_root
+ expect(users(:child_2).parent).to be_nil
+ expect(users(:child_2).level).to eq(0)
+ expect(users(:child_2_1).level).to eq(1)
+ expect(users(:child_2).left).to eq(9)
+ expect(users(:child_2).right).to eq(12)
+ expect(User.valid?).to be_truthy
+ end
+
+ it "move_to_child_of" do
+ users(:child_1).move_to_child_of(users(:child_3))
+ expect(users(:child_3).uuid).to eq(users(:child_1).parent_uuid)
+ expect(User.valid?).to be_truthy
+ end
+
+ describe "#move_to_child_with_index" do
+ it "move to a node without child" do
+ users(:child_1).move_to_child_with_index(users(:child_3), 0)
+ expect(users(:child_3).uuid).to eq(users(:child_1).parent_uuid)
+ expect(users(:child_1).left).to eq(7)
+ expect(users(:child_1).right).to eq(8)
+ expect(users(:child_3).left).to eq(6)
+ expect(users(:child_3).right).to eq(9)
+ expect(User.valid?).to be_truthy
+ end
+
+ it "move to a node to the left child" do
+ users(:child_1).move_to_child_with_index(users(:child_2), 0)
+ expect(users(:child_1).parent_uuid).to eq(users(:child_2).uuid)
+ expect(users(:child_2_1).left).to eq(5)
+ expect(users(:child_2_1).right).to eq(6)
+ expect(users(:child_1).left).to eq(3)
+ expect(users(:child_1).right).to eq(4)
+ users(:child_2).reload
+ expect(users(:child_2).left).to eq(2)
+ expect(users(:child_2).right).to eq(7)
+ end
+
+ it "move to a node to the right child" do
+ users(:child_1).move_to_child_with_index(users(:child_2), 1)
+ expect(users(:child_1).parent_uuid).to eq(users(:child_2).uuid)
+ expect(users(:child_2_1).left).to eq(3)
+ expect(users(:child_2_1).right).to eq(4)
+ expect(users(:child_1).left).to eq(5)
+ expect(users(:child_1).right).to eq(6)
+ users(:child_2).reload
+ expect(users(:child_2).left).to eq(2)
+ expect(users(:child_2).right).to eq(7)
+ end
+
+ end
+
+ it "move_to_child_of_appends_to_end" do
+ child = User.create! :name => 'New Child'
+ child.move_to_child_of users(:top_level)
+ expect(child).to eq(users(:top_level).children.last)
+ end
+
+ it "subtree_move_to_child_of" do
+ expect(users(:child_2).left).to eq(4)
+ expect(users(:child_2).right).to eq(7)
+
+ expect(users(:child_1).left).to eq(2)
+ expect(users(:child_1).right).to eq(3)
+
+ users(:child_2).move_to_child_of(users(:child_1))
+ expect(User.valid?).to be_truthy
+ expect(users(:child_1).uuid).to eq(users(:child_2).parent_uuid)
+
+ expect(users(:child_2).left).to eq(3)
+ expect(users(:child_2).right).to eq(6)
+ expect(users(:child_1).left).to eq(2)
+ expect(users(:child_1).right).to eq(7)
+ end
+
+ it "slightly_difficult_move_to_child_of" do
+ expect(users(:top_level_2).left).to eq(11)
+ expect(users(:top_level_2).right).to eq(12)
+
+ # create a new top-level node and move single-node top-level tree inside it.
+ new_top = User.create(:name => 'New Top')
+ expect(new_top.left).to eq(13)
+ expect(new_top.right).to eq(14)
+
+ users(:top_level_2).move_to_child_of(new_top)
+
+ expect(User.valid?).to be_truthy
+ expect(new_top.uuid).to eq(users(:top_level_2).parent_uuid)
+
+ expect(users(:top_level_2).left).to eq(12)
+ expect(users(:top_level_2).right).to eq(13)
+ expect(new_top.left).to eq(11)
+ expect(new_top.right).to eq(14)
+ end
+
+ it "difficult_move_to_child_of" do
+ expect(users(:top_level).left).to eq(1)
+ expect(users(:top_level).right).to eq(10)
+ expect(users(:child_2_1).left).to eq(5)
+ expect(users(:child_2_1).right).to eq(6)
+
+ # create a new top-level node and move an entire top-level tree inside it.
+ new_top = User.create(:name => 'New Top')
+ users(:top_level).move_to_child_of(new_top)
+ users(:child_2_1).reload
+ expect(User.valid?).to be_truthy
+ expect(new_top.uuid).to eq(users(:top_level).parent_uuid)
+
+ expect(users(:top_level).left).to eq(4)
+ expect(users(:top_level).right).to eq(13)
+ expect(users(:child_2_1).left).to eq(8)
+ expect(users(:child_2_1).right).to eq(9)
+ end
+
+ #rebuild swaps the position of the 2 children when added using move_to_child twice onto same parent
+ it "move_to_child_more_than_once_per_parent_rebuild" do
+ root1 = User.create(:name => 'Root1')
+ root2 = User.create(:name => 'Root2')
+ root3 = User.create(:name => 'Root3')
+
+ root2.move_to_child_of root1
+ root3.move_to_child_of root1
+
+ output = User.roots.last.to_text
+ User.update_all('lft = null, rgt = null')
+ User.rebuild!
+
+ expect(User.roots.last.to_text).to eq(output)
+ end
+
+ # doing move_to_child twice onto same parent from the furthest right first
+ it "move_to_child_more_than_once_per_parent_outside_in" do
+ node1 = User.create(:name => 'Node-1')
+ node2 = User.create(:name => 'Node-2')
+ node3 = User.create(:name => 'Node-3')
+
+ node2.move_to_child_of node1
+ node3.move_to_child_of node1
+
+ output = User.roots.last.to_text
+ User.update_all('lft = null, rgt = null')
+ User.rebuild!
+
+ expect(User.roots.last.to_text).to eq(output)
+ end
+
+ it "should_move_to_ordered_child" do
+ node1 = User.create(:name => 'Node-1')
+ node2 = User.create(:name => 'Node-2')
+ node3 = User.create(:name => 'Node-3')
+
+ node2.move_to_ordered_child_of(node1, "name")
+
+ assert_equal node1, node2.parent
+ assert_equal 1, node1.children.count
+
+ node3.move_to_ordered_child_of(node1, "name", true) # ascending
+
+ expect(node3.parent).to eq(node1)
+ expect(node1.children.count).to be(2)
+ expect(node1.children[0].name).to eq(node2.name)
+ expect(node1.children[1].name).to eq(node3.name)
+
+ node3.move_to_ordered_child_of(node1, "name", false) # descending
+ node1.reload
+
+ expect(node3.parent).to eq(node1)
+ expect(node1.children.count).to be(2)
+ expect(node1.children[0].name).to eq(node3.name)
+ expect(node1.children[1].name).to eq(node2.name)
+ end
+
+ it "should be able to rebuild without validating each record" do
+ root1 = User.create(:name => 'Root1')
+ root2 = User.create(:name => 'Root2')
+ root3 = User.create(:name => 'Root3')
+
+ root2.move_to_child_of root1
+ root3.move_to_child_of root1
+
+ root2.name = nil
+ root2.save!(:validate => false)
+
+ output = User.roots.last.to_text
+ User.update_all('lft = null, rgt = null')
+ User.rebuild!(false)
+
+ expect(User.roots.last.to_text).to eq(output)
+ end
+
+ it "valid_with_null_lefts" do
+ expect(User.valid?).to be_truthy
+ User.update_all('lft = null')
+ expect(User.valid?).to be_falsey
+ end
+
+ it "valid_with_null_rights" do
+ expect(User.valid?).to be_truthy
+ User.update_all('rgt = null')
+ expect(User.valid?).to be_falsey
+ end
+
+ it "valid_with_missing_intermediate_node" do
+ # Even though child_2_1 will still exist, it is a sign of a sloppy delete, not an invalid tree.
+ expect(User.valid?).to be_truthy
+ User.where(uuid: users(:child_2).uuid).delete_all
+ expect(User.valid?).to be_truthy
+ end
+
+ it "valid_with_overlapping_and_rights" do
+ expect(User.valid?).to be_truthy
+ users(:top_level_2)['lft'] = 0
+ users(:top_level_2).save
+ expect(User.valid?).to be_falsey
+ end
+
+ it "rebuild" do
+ expect(User.valid?).to be_truthy
+ before_text = User.root.to_text
+ User.update_all('lft = null, rgt = null')
+ User.rebuild!
+ expect(User.valid?).to be_truthy
+ expect(before_text).to eq(User.root.to_text)
+ end
+
+ it "move_possible_for_sibling" do
+ expect(users(:child_2).move_possible?(users(:child_1))).to be_truthy
+ end
+
+ it "move_not_possible_to_self" do
+ expect(users(:top_level).move_possible?(users(:top_level))).to be_falsey
+ end
+
+ it "move_not_possible_to_parent" do
+ users(:top_level).descendants.each do |descendant|
+ expect(users(:top_level).move_possible?(descendant)).to be_falsey
+ expect(descendant.move_possible?(users(:top_level))).to be_truthy
+ end
+ end
+
+ it "is_or_is_ancestor_of?" do
+ [:child_1, :child_2, :child_2_1, :child_3].each do |c|
+ expect(users(:top_level).is_or_is_ancestor_of?(users(c))).to be_truthy
+ end
+ expect(users(:top_level).is_or_is_ancestor_of?(users(:top_level_2))).to be_falsey
+ end
+
+ it "left_and_rights_valid_with_blank_left" do
+ expect(User.left_and_rights_valid?).to be_truthy
+ users(:child_2)[:lft] = nil
+ users(:child_2).save(:validate => false)
+ expect(User.left_and_rights_valid?).to be_falsey
+ end
+
+ it "left_and_rights_valid_with_blank_right" do
+ expect(User.left_and_rights_valid?).to be_truthy
+ users(:child_2)[:rgt] = nil
+ users(:child_2).save(:validate => false)
+ expect(User.left_and_rights_valid?).to be_falsey
+ end
+
+ it "left_and_rights_valid_with_equal" do
+ expect(User.left_and_rights_valid?).to be_truthy
+ users(:top_level_2)[:lft] = users(:top_level_2)[:rgt]
+ users(:top_level_2).save(:validate => false)
+ expect(User.left_and_rights_valid?).to be_falsey
+ end
+
+ it "left_and_rights_valid_with_left_equal_to_parent" do
+ expect(User.left_and_rights_valid?).to be_truthy
+ users(:child_2)[:lft] = users(:top_level)[:lft]
+ users(:child_2).save(:validate => false)
+ expect(User.left_and_rights_valid?).to be_falsey
+ end
+
+ it "left_and_rights_valid_with_right_equal_to_parent" do
+ expect(User.left_and_rights_valid?).to be_truthy
+ users(:child_2)[:rgt] = users(:top_level)[:rgt]
+ users(:child_2).save(:validate => false)
+ expect(User.left_and_rights_valid?).to be_falsey
+ end
+
+ it "moving_dirty_objects_doesnt_invalidate_tree" do
+ r1 = User.create :name => "Test 1"
+ r2 = User.create :name => "Test 2"
+ r3 = User.create :name => "Test 3"
+ r4 = User.create :name => "Test 4"
+ nodes = [r1, r2, r3, r4]
+
+ r2.move_to_child_of(r1)
+ expect(User.valid?).to be_truthy
+
+ r3.move_to_child_of(r1)
+ expect(User.valid?).to be_truthy
+
+ r4.move_to_child_of(r2)
+ expect(User.valid?).to be_truthy
+ end
+
+ it "delete_does_not_invalidate" do
+ User.acts_as_nested_set_options[:dependent] = :delete
+ users(:child_2).destroy
+ expect(User.valid?).to be_truthy
+ end
+
+ it "destroy_does_not_invalidate" do
+ User.acts_as_nested_set_options[:dependent] = :destroy
+ users(:child_2).destroy
+ expect(User.valid?).to be_truthy
+ end
+
+ it "destroy_multiple_times_does_not_invalidate" do
+ User.acts_as_nested_set_options[:dependent] = :destroy
+ users(:child_2).destroy
+ users(:child_2).destroy
+ expect(User.valid?).to be_truthy
+ end
+
+ it "assigning_parent_uuid_on_create" do
+ user = User.create!(:name => "Child", :parent_uuid => users(:child_2).uuid)
+ expect(users(:child_2)).to eq(user.parent)
+ expect(users(:child_2).uuid).to eq(user.parent_uuid)
+ expect(user.left).not_to be_nil
+ expect(user.right).not_to be_nil
+ expect(User.valid?).to be_truthy
+ end
+
+ it "assigning_parent_on_create" do
+ user = User.create!(:name => "Child", :parent => users(:child_2))
+ expect(users(:child_2)).to eq(user.parent)
+ expect(users(:child_2).uuid).to eq(user.parent_uuid)
+ expect(user.left).not_to be_nil
+ expect(user.right).not_to be_nil
+ expect(User.valid?).to be_truthy
+ end
+
+ it "assigning_parent_uuid_to_nil_on_create" do
+ user = User.create!(:name => "New Root", :parent_uuid => nil)
+ expect(user.parent).to be_nil
+ expect(user.parent_uuid).to be_nil
+ expect(user.left).not_to be_nil
+ expect(user.right).not_to be_nil
+ expect(User.valid?).to be_truthy
+ end
+
+ it "assigning_parent_uuid_on_update" do
+ user = users(:child_2_1)
+ user.parent_uuid = users(:child_3).uuid
+ user.save
+ user.reload
+ users(:child_3).reload
+ expect(users(:child_3)).to eq(user.parent)
+ expect(users(:child_3).uuid).to eq(user.parent_uuid)
+ expect(User.valid?).to be_truthy
+ end
+
+ it "assigning_parent_on_update" do
+ user = users(:child_2_1)
+ user.parent = users(:child_3)
+ user.save
+ user.reload
+ users(:child_3).reload
+ expect(users(:child_3)).to eq(user.parent)
+ expect(users(:child_3).uuid).to eq(user.parent_uuid)
+ expect(User.valid?).to be_truthy
+ end
+
+ it "assigning_parent_uuid_to_nil_on_update" do
+ user = users(:child_2_1)
+ user.parent_uuid = nil
+ user.save
+ expect(user.parent).to be_nil
+ expect(user.parent_uuid).to be_nil
+ expect(User.valid?).to be_truthy
+ end
+
+ it "creating_child_from_parent" do
+ user = users(:child_2).children.create!(:name => "Child")
+ expect(users(:child_2)).to eq(user.parent)
+ expect(users(:child_2).uuid).to eq(user.parent_uuid)
+ expect(user.left).not_to be_nil
+ expect(user.right).not_to be_nil
+ expect(User.valid?).to be_truthy
+ end
+
+ it 'creates user when clause where provided' do
+ parent = User.first
+
+ expect do
+ User.where(name: "Chris-#{Time.current.to_f}").first_or_create! do |user|
+ user.parent = parent
+ end
+ end.to change { User.count }.by 1
+ end
+
+ def check_structure(entries, structure)
+ structure = structure.dup
+ User.each_with_level(entries) do |user, level|
+ expected_level, expected_name = structure.shift
+ expect(expected_name).to eq(user.name)
+ expect(expected_level).to eq(level)
+ end
+ end
+
+ it "each_with_level" do
+ levels = [
+ [0, "Top Level"],
+ [1, "Child 1"],
+ [1, "Child 2"],
+ [2, "Child 2.1"],
+ [1, "Child 3" ]
+ ]
+
+ check_structure(User.root.self_and_descendants, levels)
+
+ # test some deeper structures
+ user = User.find_by_name("Child 1")
+ c1 = User.new(:name => "Child 1.1")
+ c2 = User.new(:name => "Child 1.1.1")
+ c3 = User.new(:name => "Child 1.1.1.1")
+ c4 = User.new(:name => "Child 1.2")
+ [c1, c2, c3, c4].each(&:save!)
+
+ c1.move_to_child_of(user)
+ c2.move_to_child_of(c1)
+ c3.move_to_child_of(c2)
+ c4.move_to_child_of(user)
+
+ levels = [
+ [0, "Top Level"],
+ [1, "Child 1"],
+ [2, "Child 1.1"],
+ [3, "Child 1.1.1"],
+ [4, "Child 1.1.1.1"],
+ [2, "Child 1.2"],
+ [1, "Child 2"],
+ [2, "Child 2.1"],
+ [1, "Child 3" ]
+ ]
+
+ check_structure(User.root.self_and_descendants, levels)
+ end
+
+ describe "before_move_callback" do
+ it "should fire the callback" do
+ expect(users(:child_2)).to receive(:custom_before_move)
+ users(:child_2).move_to_root
+ end
+
+ it "should stop move when callback returns false" do
+ User.test_allows_move = false
+ expect(users(:child_3).move_to_root).to be_falsey
+ expect(users(:child_3).root?).to be_falsey
+ end
+
+ it "should not halt save actions" do
+ User.test_allows_move = false
+ users(:child_3).parent_uuid = nil
+ expect(users(:child_3).save).to be_truthy
+ end
+ end
+
+ describe 'associate_parents' do
+ it 'assigns parent' do
+ root = User.root
+ users = root.self_and_descendants
+ users = User.associate_parents users
+ expect(users[1].parent).to be users.first
+ end
+
+ it 'adds children on inverse of association' do
+ root = User.root
+ users = root.self_and_descendants
+ users = User.associate_parents users
+ expect(users[0].children.first).to be users[1]
+ end
+ end
+
+ describe 'option dependent' do
+ it 'destroy should destroy children and node' do
+ User.acts_as_nested_set_options[:dependent] = :destroy
+ root = User.root
+ root.destroy!
+ expect(User.where(uuid: root.uuid)).to be_empty
+ expect(User.where(parent_uuid: root.uuid)).to be_empty
+ end
+
+ it 'delete should delete children and node' do
+ User.acts_as_nested_set_options[:dependent] = :delete
+ root = User.root
+ root.destroy!
+ expect(User.where(uuid: root.uuid)).to be_empty
+ expect(User.where(parent_uuid: root.uuid)).to be_empty
+ end
+
+ describe 'restrict_with_exception' do
+ it 'raises an exception' do
+ User.acts_as_nested_set_options[:dependent] = :restrict_with_exception
+ root = User.root
+ expect { root.destroy! }.to raise_error ActiveRecord::DeleteRestrictionError, 'Cannot delete record because of dependent children'
+ end
+
+ it 'deletes the leaf' do
+ User.acts_as_nested_set_options[:dependent] = :restrict_with_exception
+ leaf = User.last
+ expect(leaf.destroy).to eq(leaf)
+ end
+ end
+
+ describe 'restrict_with_error' do
+ it 'adds the error to the parent' do
+ User.acts_as_nested_set_options[:dependent] = :restrict_with_error
+ root = User.root
+ root.destroy
+ expect(root.errors[:base]).to eq(["Cannot delete record because dependent children exist"])
+ end
+
+ it 'deletes the leaf' do
+ User.acts_as_nested_set_options[:dependent] = :restrict_with_error
+ leaf = User.last
+ expect(leaf.destroy).to eq(leaf)
+ end
+ end
+ end
+end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
new file mode 100644
index 0000000..6e4fdd7
--- /dev/null
+++ b/spec/spec_helper.rb
@@ -0,0 +1,42 @@
+plugin_test_dir = File.dirname(__FILE__)
+
+require 'rubygems'
+require 'bundler/setup'
+require 'pry'
+
+require 'logger'
+require 'active_record'
+ActiveRecord::Base.logger = Logger.new(plugin_test_dir + "/debug.log")
+
+require 'yaml'
+require 'erb'
+db_config = YAML::load(ERB.new(IO.read(plugin_test_dir + "/db/database.yml")).result)
+ActiveRecord::Base.configurations = db_config
+ActiveRecord::Base.establish_connection((ENV["DB"] ||= "sqlite3mem").to_sym)
+ActiveRecord::Migration.verbose = false
+
+unless /sqlite/ === ENV['DB']
+ ActiveRecord::Tasks::DatabaseTasks.create db_config[ENV['DB']]
+end
+
+load(File.join(plugin_test_dir, "db", "schema.rb"))
+
+require 'awesome_nested_set'
+require 'support/models'
+
+begin
+ require 'action_view'
+rescue LoadError; end # action_view doesn't exist in Rails 4.0, but we need this for the tests to run with Rails 4.1
+
+require 'action_controller'
+require 'rspec/rails'
+require 'database_cleaner'
+RSpec.configure do |config|
+ config.fixture_path = "#{plugin_test_dir}/fixtures"
+ config.use_transactional_fixtures = true
+ config.after(:suite) do
+ unless /sqlite/ === ENV['DB']
+ ActiveRecord::Tasks::DatabaseTasks.drop db_config[ENV['DB']]
+ end
+ end
+end
diff --git a/spec/support/models.rb b/spec/support/models.rb
new file mode 100644
index 0000000..7ae1fad
--- /dev/null
+++ b/spec/support/models.rb
@@ -0,0 +1,171 @@
+class Note < ActiveRecord::Base
+ acts_as_nested_set :scope => [:notable_id, :notable_type]
+
+ belongs_to :user, inverse_of: :notes
+end
+
+class DefaultScopedModel < ActiveRecord::Base
+ acts_as_nested_set
+end
+
+class Default < ActiveRecord::Base
+ self.table_name = 'categories'
+ acts_as_nested_set
+end
+
+class ScopedCategory < ActiveRecord::Base
+ self.table_name = 'categories'
+ acts_as_nested_set :scope => :organization
+end
+
+class OrderedCategory < ActiveRecord::Base
+ self.table_name = 'categories'
+ acts_as_nested_set :order_column => 'name'
+end
+
+class HashOrderedCategory < ActiveRecord::Base
+ self.table_name = 'categories'
+ acts_as_nested_set :order_column => { :name => :desc }
+end
+
+class RenamedColumns < ActiveRecord::Base
+ acts_as_nested_set :parent_column => 'mother_id',
+ :left_column => 'red',
+ :right_column => 'black',
+ :depth_column => 'pitch'
+end
+
+class Category < ActiveRecord::Base
+ acts_as_nested_set
+
+ validates_presence_of :name
+
+ # Setup a callback that we can switch to true or false per-test
+ set_callback :move, :before, :custom_before_move
+ cattr_accessor :test_allows_move
+ @@test_allows_move = true
+ def custom_before_move
+ if !@@test_allows_move
+ if Rails::VERSION::MAJOR < 5
+ false
+ else
+ throw :abort
+ end
+ end
+ end
+
+ def to_s
+ name
+ end
+
+ def recurse &block
+ block.call self, lambda{
+ self.children.each do |child|
+ child.recurse &block
+ end
+ }
+ end
+end
+
+class Thing < ActiveRecord::Base
+ acts_as_nested_set :counter_cache => 'children_count'
+end
+
+class DefaultWithCallbacks < ActiveRecord::Base
+
+ self.table_name = 'categories'
+
+ attr_accessor :before_add, :after_add, :before_remove, :after_remove
+
+ acts_as_nested_set :before_add => :do_before_add_stuff,
+ :after_add => :do_after_add_stuff,
+ :before_remove => :do_before_remove_stuff,
+ :after_remove => :do_after_remove_stuff
+
+ private
+
+ [ :before_add, :after_add, :before_remove, :after_remove ].each do |hook_name|
+ define_method "do_#{hook_name}_stuff" do |child_node|
+ self.send("#{hook_name}=", child_node)
+ end
+ end
+
+end
+
+class Broken < ActiveRecord::Base
+ acts_as_nested_set
+end
+
+class Order < ActiveRecord::Base
+ acts_as_nested_set
+
+ default_scope -> { order(name: :asc) }
+end
+
+class Position < ActiveRecord::Base
+ acts_as_nested_set
+
+ default_scope -> { order(position: :asc) }
+end
+
+class NoDepth < ActiveRecord::Base
+ acts_as_nested_set
+end
+
+class User < ActiveRecord::Base
+ acts_as_nested_set :parent_column => 'parent_uuid', :primary_column => 'uuid'
+
+ validates_presence_of :name
+ validates_presence_of :uuid
+ validates_uniqueness_of :uuid
+
+ after_initialize :ensure_uuid
+
+ has_many :notes, dependent: :destroy, inverse_of: :user
+
+ # Setup a callback that we can switch to true or false per-test
+ set_callback :move, :before, :custom_before_move
+ cattr_accessor :test_allows_move
+ @@test_allows_move = true
+ def custom_before_move
+ if !@@test_allows_move
+ if Rails::VERSION::MAJOR < 5
+ false
+ else
+ throw :abort
+ end
+ end
+ end
+
+ def to_s
+ name
+ end
+
+ def recurse &block
+ block.call self, lambda{
+ self.children.each do |child|
+ child.recurse &block
+ end
+ }
+ end
+
+ def ensure_uuid
+ self.uuid ||= SecureRandom.hex
+ end
+end
+
+class ScopedUser < ActiveRecord::Base
+ self.table_name = 'users'
+ acts_as_nested_set :parent_column => 'parent_uuid', :primary_column => 'uuid', :scope => :organization
+end
+
+class Superclass < ActiveRecord::Base
+ acts_as_nested_set counter_cache: :children_count
+ self.table_name = 'single_table_inheritance'
+end
+
+class Subclass1 < Superclass
+end
+
+class Subclass2 < Superclass
+end
Debdiff
File lists identical (after any substitutions)
No differences were encountered in the control files