Skip to content

Instantly share code, notes, and snippets.

@googya
Forked from mfazekas/test-children.rb
Created February 22, 2017 08:31
Show Gist options
  • Save googya/1550b8a64dc50134e3c5ed12e0d6c9a8 to your computer and use it in GitHub Desktop.
Save googya/1550b8a64dc50134e3c5ed12e0d6c9a8 to your computer and use it in GitHub Desktop.
awesome-nested-set-children
# 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