Created
February 20, 2017 08:47
-
-
Save mfazekas/a8f5c3e76a0630e00e87bbe7a7b5626d to your computer and use it in GitHub Desktop.
awesome-nested-set-children
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# test-children.rb | |
# This is a stand-alone test case. | |
# Run it in your console with: `rubytest-children.rb` | |
# If you change the gem dependencies, run it with: | |
# `rm gemfile* && ruby test-children.rb` | |
unless File.exist?('Gemfile') | |
File.write('Gemfile', <<-GEMFILE) | |
source 'https://rubygems.org' | |
# Rails master | |
gem 'rails', '~> 5.0.1' | |
# Rails last release | |
#gem 'rails', github: 'rails/rails' | |
gem 'sqlite3' | |
gem 'byebug' | |
gem 'awesome_nested_set', '~> 3.1.1' | |
GEMFILE | |
system 'bundle install' | |
end | |
require 'bundler' | |
Bundler.setup(:default) | |
require 'active_record' | |
require 'minitest/autorun' | |
require 'logger' | |
require 'awesome_nested_set' | |
# This connection will do for database-independent bug reports. | |
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
# Display versions. | |
message = "Running test case with Ruby #{RUBY_VERSION}, Active Record #{ | |
::ActiveRecord::VERSION::STRING}, Arel #{Arel::VERSION} and #{ | |
::ActiveRecord::Base.connection.adapter_name}" | |
line = '=' * message.length | |
puts line, message, line | |
ActiveRecord::Schema.define do | |
create_table :nodes do |t| | |
t.string :name | |
t.integer :parent_id | |
t.integer :lft | |
t.integer :rgt | |
end | |
end | |
class Node < ActiveRecord::Base | |
acts_as_nested_set dependent: :destroy | |
end | |
class BugTest < Minitest::Test | |
def test_append_to_children | |
n = Node.create! | |
n2 = Node.create! | |
n.children << n2 | |
assert_equal Node.find(n.id).descendants, [n2] | |
assert_equal n.descendants, [n2] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment