svn propset svn:eol-style LF lib/plugins/awesome_nested_set/spec/*
git-svn-id: http://svn.redmine.org/redmine/trunk@12676 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
parent
df9da6750d
commit
77f29deb29
|
@ -1,67 +1,67 @@
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
describe "Helper" do
|
describe "Helper" do
|
||||||
include CollectiveIdea::Acts::NestedSet::Helper
|
include CollectiveIdea::Acts::NestedSet::Helper
|
||||||
|
|
||||||
before(:all) do
|
before(:all) do
|
||||||
self.class.fixtures :categories
|
self.class.fixtures :categories
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "nested_set_options" do
|
describe "nested_set_options" do
|
||||||
it "test_nested_set_options" do
|
it "test_nested_set_options" do
|
||||||
expected = [
|
expected = [
|
||||||
[" Top Level", 1],
|
[" Top Level", 1],
|
||||||
["- Child 1", 2],
|
["- Child 1", 2],
|
||||||
['- Child 2', 3],
|
['- Child 2', 3],
|
||||||
['-- Child 2.1', 4],
|
['-- Child 2.1', 4],
|
||||||
['- Child 3', 5],
|
['- Child 3', 5],
|
||||||
[" Top Level 2", 6]
|
[" Top Level 2", 6]
|
||||||
]
|
]
|
||||||
actual = nested_set_options(Category) do |c|
|
actual = nested_set_options(Category) do |c|
|
||||||
"#{'-' * c.level} #{c.name}"
|
"#{'-' * c.level} #{c.name}"
|
||||||
end
|
end
|
||||||
actual.should == expected
|
actual.should == expected
|
||||||
end
|
end
|
||||||
|
|
||||||
it "test_nested_set_options_with_mover" do
|
it "test_nested_set_options_with_mover" do
|
||||||
expected = [
|
expected = [
|
||||||
[" Top Level", 1],
|
[" Top Level", 1],
|
||||||
["- Child 1", 2],
|
["- Child 1", 2],
|
||||||
['- Child 3', 5],
|
['- Child 3', 5],
|
||||||
[" Top Level 2", 6]
|
[" Top Level 2", 6]
|
||||||
]
|
]
|
||||||
actual = nested_set_options(Category, categories(:child_2)) do |c|
|
actual = nested_set_options(Category, categories(:child_2)) do |c|
|
||||||
"#{'-' * c.level} #{c.name}"
|
"#{'-' * c.level} #{c.name}"
|
||||||
end
|
end
|
||||||
actual.should == expected
|
actual.should == expected
|
||||||
end
|
end
|
||||||
|
|
||||||
it "test_nested_set_options_with_array_as_argument_without_mover" do
|
it "test_nested_set_options_with_array_as_argument_without_mover" do
|
||||||
expected = [
|
expected = [
|
||||||
[" Top Level", 1],
|
[" Top Level", 1],
|
||||||
["- Child 1", 2],
|
["- Child 1", 2],
|
||||||
['- Child 2', 3],
|
['- Child 2', 3],
|
||||||
['-- Child 2.1', 4],
|
['-- Child 2.1', 4],
|
||||||
['- Child 3', 5],
|
['- Child 3', 5],
|
||||||
[" Top Level 2", 6]
|
[" Top Level 2", 6]
|
||||||
]
|
]
|
||||||
actual = nested_set_options(Category.all) do |c|
|
actual = nested_set_options(Category.all) do |c|
|
||||||
"#{'-' * c.level} #{c.name}"
|
"#{'-' * c.level} #{c.name}"
|
||||||
end
|
end
|
||||||
actual.should == expected
|
actual.should == expected
|
||||||
end
|
end
|
||||||
|
|
||||||
it "test_nested_set_options_with_array_as_argument_with_mover" do
|
it "test_nested_set_options_with_array_as_argument_with_mover" do
|
||||||
expected = [
|
expected = [
|
||||||
[" Top Level", 1],
|
[" Top Level", 1],
|
||||||
["- Child 1", 2],
|
["- Child 1", 2],
|
||||||
['- Child 3', 5],
|
['- Child 3', 5],
|
||||||
[" Top Level 2", 6]
|
[" Top Level 2", 6]
|
||||||
]
|
]
|
||||||
actual = nested_set_options(Category.all, categories(:child_2)) do |c|
|
actual = nested_set_options(Category.all, categories(:child_2)) do |c|
|
||||||
"#{'-' * c.level} #{c.name}"
|
"#{'-' * c.level} #{c.name}"
|
||||||
end
|
end
|
||||||
actual.should == expected
|
actual.should == expected
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,18 +1,18 @@
|
||||||
sqlite3:
|
sqlite3:
|
||||||
adapter: <%= "jdbc" if defined? JRUBY_VERSION %>sqlite3
|
adapter: <%= "jdbc" if defined? JRUBY_VERSION %>sqlite3
|
||||||
database: awesome_nested_set.sqlite3.db
|
database: awesome_nested_set.sqlite3.db
|
||||||
sqlite3mem:
|
sqlite3mem:
|
||||||
adapter: <%= "jdbc" if defined? JRUBY_VERSION %>sqlite3
|
adapter: <%= "jdbc" if defined? JRUBY_VERSION %>sqlite3
|
||||||
database: ":memory:"
|
database: ":memory:"
|
||||||
postgresql:
|
postgresql:
|
||||||
adapter: postgresql
|
adapter: postgresql
|
||||||
username: postgres
|
username: postgres
|
||||||
password: postgres
|
password: postgres
|
||||||
database: awesome_nested_set_plugin_test
|
database: awesome_nested_set_plugin_test
|
||||||
min_messages: ERROR
|
min_messages: ERROR
|
||||||
mysql:
|
mysql:
|
||||||
adapter: mysql2
|
adapter: mysql2
|
||||||
host: localhost
|
host: localhost
|
||||||
username: root
|
username: root
|
||||||
password:
|
password:
|
||||||
database: awesome_nested_set_plugin_test
|
database: awesome_nested_set_plugin_test
|
|
@ -1,45 +1,45 @@
|
||||||
ActiveRecord::Schema.define(:version => 0) do
|
ActiveRecord::Schema.define(:version => 0) do
|
||||||
|
|
||||||
create_table :categories, :force => true do |t|
|
create_table :categories, :force => true do |t|
|
||||||
t.column :name, :string
|
t.column :name, :string
|
||||||
t.column :parent_id, :integer
|
t.column :parent_id, :integer
|
||||||
t.column :lft, :integer
|
t.column :lft, :integer
|
||||||
t.column :rgt, :integer
|
t.column :rgt, :integer
|
||||||
t.column :organization_id, :integer
|
t.column :organization_id, :integer
|
||||||
end
|
end
|
||||||
|
|
||||||
create_table :departments, :force => true do |t|
|
create_table :departments, :force => true do |t|
|
||||||
t.column :name, :string
|
t.column :name, :string
|
||||||
end
|
end
|
||||||
|
|
||||||
create_table :notes, :force => true do |t|
|
create_table :notes, :force => true do |t|
|
||||||
t.column :body, :text
|
t.column :body, :text
|
||||||
t.column :parent_id, :integer
|
t.column :parent_id, :integer
|
||||||
t.column :lft, :integer
|
t.column :lft, :integer
|
||||||
t.column :rgt, :integer
|
t.column :rgt, :integer
|
||||||
t.column :notable_id, :integer
|
t.column :notable_id, :integer
|
||||||
t.column :notable_type, :string
|
t.column :notable_type, :string
|
||||||
end
|
end
|
||||||
|
|
||||||
create_table :renamed_columns, :force => true do |t|
|
create_table :renamed_columns, :force => true do |t|
|
||||||
t.column :name, :string
|
t.column :name, :string
|
||||||
t.column :mother_id, :integer
|
t.column :mother_id, :integer
|
||||||
t.column :red, :integer
|
t.column :red, :integer
|
||||||
t.column :black, :integer
|
t.column :black, :integer
|
||||||
end
|
end
|
||||||
|
|
||||||
create_table :things, :force => true do |t|
|
create_table :things, :force => true do |t|
|
||||||
t.column :body, :text
|
t.column :body, :text
|
||||||
t.column :parent_id, :integer
|
t.column :parent_id, :integer
|
||||||
t.column :lft, :integer
|
t.column :lft, :integer
|
||||||
t.column :rgt, :integer
|
t.column :rgt, :integer
|
||||||
t.column :children_count, :integer
|
t.column :children_count, :integer
|
||||||
end
|
end
|
||||||
|
|
||||||
create_table :brokens, :force => true do |t|
|
create_table :brokens, :force => true do |t|
|
||||||
t.column :name, :string
|
t.column :name, :string
|
||||||
t.column :parent_id, :integer
|
t.column :parent_id, :integer
|
||||||
t.column :lft, :integer
|
t.column :lft, :integer
|
||||||
t.column :rgt, :integer
|
t.column :rgt, :integer
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
one:
|
one:
|
||||||
id: 1
|
id: 1
|
||||||
name: One
|
name: One
|
|
@ -1,34 +1,34 @@
|
||||||
top_level:
|
top_level:
|
||||||
id: 1
|
id: 1
|
||||||
name: Top Level
|
name: Top Level
|
||||||
lft: 1
|
lft: 1
|
||||||
rgt: 10
|
rgt: 10
|
||||||
child_1:
|
child_1:
|
||||||
id: 2
|
id: 2
|
||||||
name: Child 1
|
name: Child 1
|
||||||
parent_id: 1
|
parent_id: 1
|
||||||
lft: 2
|
lft: 2
|
||||||
rgt: 3
|
rgt: 3
|
||||||
child_2:
|
child_2:
|
||||||
id: 3
|
id: 3
|
||||||
name: Child 2
|
name: Child 2
|
||||||
parent_id: 1
|
parent_id: 1
|
||||||
lft: 4
|
lft: 4
|
||||||
rgt: 7
|
rgt: 7
|
||||||
child_2_1:
|
child_2_1:
|
||||||
id: 4
|
id: 4
|
||||||
name: Child 2.1
|
name: Child 2.1
|
||||||
parent_id: 3
|
parent_id: 3
|
||||||
lft: 5
|
lft: 5
|
||||||
rgt: 6
|
rgt: 6
|
||||||
child_3:
|
child_3:
|
||||||
id: 5
|
id: 5
|
||||||
name: Child 3
|
name: Child 3
|
||||||
parent_id: 1
|
parent_id: 1
|
||||||
lft: 8
|
lft: 8
|
||||||
rgt: 9
|
rgt: 9
|
||||||
top_level_2:
|
top_level_2:
|
||||||
id: 6
|
id: 6
|
||||||
name: Top Level 2
|
name: Top Level 2
|
||||||
lft: 11
|
lft: 11
|
||||||
rgt: 12
|
rgt: 12
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
top:
|
top:
|
||||||
id: 1
|
id: 1
|
||||||
name: Top
|
name: Top
|
|
@ -1,38 +1,38 @@
|
||||||
scope1:
|
scope1:
|
||||||
id: 1
|
id: 1
|
||||||
body: Top Level
|
body: Top Level
|
||||||
lft: 1
|
lft: 1
|
||||||
rgt: 10
|
rgt: 10
|
||||||
notable_id: 1
|
notable_id: 1
|
||||||
notable_type: Category
|
notable_type: Category
|
||||||
child_1:
|
child_1:
|
||||||
id: 2
|
id: 2
|
||||||
body: Child 1
|
body: Child 1
|
||||||
parent_id: 1
|
parent_id: 1
|
||||||
lft: 2
|
lft: 2
|
||||||
rgt: 3
|
rgt: 3
|
||||||
notable_id: 1
|
notable_id: 1
|
||||||
notable_type: Category
|
notable_type: Category
|
||||||
child_2:
|
child_2:
|
||||||
id: 3
|
id: 3
|
||||||
body: Child 2
|
body: Child 2
|
||||||
parent_id: 1
|
parent_id: 1
|
||||||
lft: 4
|
lft: 4
|
||||||
rgt: 7
|
rgt: 7
|
||||||
notable_id: 1
|
notable_id: 1
|
||||||
notable_type: Category
|
notable_type: Category
|
||||||
child_3:
|
child_3:
|
||||||
id: 4
|
id: 4
|
||||||
body: Child 3
|
body: Child 3
|
||||||
parent_id: 1
|
parent_id: 1
|
||||||
lft: 8
|
lft: 8
|
||||||
rgt: 9
|
rgt: 9
|
||||||
notable_id: 1
|
notable_id: 1
|
||||||
notable_type: Category
|
notable_type: Category
|
||||||
scope2:
|
scope2:
|
||||||
id: 5
|
id: 5
|
||||||
body: Top Level 2
|
body: Top Level 2
|
||||||
lft: 1
|
lft: 1
|
||||||
rgt: 2
|
rgt: 2
|
||||||
notable_id: 1
|
notable_id: 1
|
||||||
notable_type: Departments
|
notable_type: Departments
|
||||||
|
|
|
@ -1,27 +1,27 @@
|
||||||
parent1:
|
parent1:
|
||||||
id: 1
|
id: 1
|
||||||
body: Top Level
|
body: Top Level
|
||||||
lft: 1
|
lft: 1
|
||||||
rgt: 10
|
rgt: 10
|
||||||
children_count: 2
|
children_count: 2
|
||||||
child_1:
|
child_1:
|
||||||
id: 2
|
id: 2
|
||||||
body: Child 1
|
body: Child 1
|
||||||
parent_id: 1
|
parent_id: 1
|
||||||
lft: 2
|
lft: 2
|
||||||
rgt: 3
|
rgt: 3
|
||||||
children_count: 0
|
children_count: 0
|
||||||
child_2:
|
child_2:
|
||||||
id: 3
|
id: 3
|
||||||
body: Child 2
|
body: Child 2
|
||||||
parent_id: 1
|
parent_id: 1
|
||||||
lft: 4
|
lft: 4
|
||||||
rgt: 7
|
rgt: 7
|
||||||
children_count: 0
|
children_count: 0
|
||||||
child_2_1:
|
child_2_1:
|
||||||
id: 4
|
id: 4
|
||||||
body: Child 2.1
|
body: Child 2.1
|
||||||
parent_id: 3
|
parent_id: 3
|
||||||
lft: 8
|
lft: 8
|
||||||
rgt: 9
|
rgt: 9
|
||||||
children_count: 0
|
children_count: 0
|
||||||
|
|
|
@ -1,32 +1,32 @@
|
||||||
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
||||||
plugin_test_dir = File.dirname(__FILE__)
|
plugin_test_dir = File.dirname(__FILE__)
|
||||||
|
|
||||||
require 'rubygems'
|
require 'rubygems'
|
||||||
require 'bundler/setup'
|
require 'bundler/setup'
|
||||||
|
|
||||||
require 'rspec'
|
require 'rspec'
|
||||||
require 'logger'
|
require 'logger'
|
||||||
|
|
||||||
require 'active_support'
|
require 'active_support'
|
||||||
require 'active_model'
|
require 'active_model'
|
||||||
require 'active_record'
|
require 'active_record'
|
||||||
require 'action_controller'
|
require 'action_controller'
|
||||||
|
|
||||||
require 'awesome_nested_set'
|
require 'awesome_nested_set'
|
||||||
|
|
||||||
ActiveRecord::Base.logger = Logger.new(plugin_test_dir + "/debug.log")
|
ActiveRecord::Base.logger = Logger.new(plugin_test_dir + "/debug.log")
|
||||||
|
|
||||||
require 'yaml'
|
require 'yaml'
|
||||||
require 'erb'
|
require 'erb'
|
||||||
ActiveRecord::Base.configurations = YAML::load(ERB.new(IO.read(plugin_test_dir + "/db/database.yml")).result)
|
ActiveRecord::Base.configurations = YAML::load(ERB.new(IO.read(plugin_test_dir + "/db/database.yml")).result)
|
||||||
ActiveRecord::Base.establish_connection(ENV["DB"] || "sqlite3mem")
|
ActiveRecord::Base.establish_connection(ENV["DB"] || "sqlite3mem")
|
||||||
ActiveRecord::Migration.verbose = false
|
ActiveRecord::Migration.verbose = false
|
||||||
load(File.join(plugin_test_dir, "db", "schema.rb"))
|
load(File.join(plugin_test_dir, "db", "schema.rb"))
|
||||||
|
|
||||||
require 'support/models'
|
require 'support/models'
|
||||||
|
|
||||||
require 'rspec/rails'
|
require 'rspec/rails'
|
||||||
RSpec.configure do |config|
|
RSpec.configure do |config|
|
||||||
config.fixture_path = "#{plugin_test_dir}/fixtures"
|
config.fixture_path = "#{plugin_test_dir}/fixtures"
|
||||||
config.use_transactional_fixtures = true
|
config.use_transactional_fixtures = true
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,72 +1,72 @@
|
||||||
class Note < ActiveRecord::Base
|
class Note < ActiveRecord::Base
|
||||||
acts_as_nested_set :scope => [:notable_id, :notable_type]
|
acts_as_nested_set :scope => [:notable_id, :notable_type]
|
||||||
end
|
end
|
||||||
|
|
||||||
class Default < ActiveRecord::Base
|
class Default < ActiveRecord::Base
|
||||||
self.table_name = 'categories'
|
self.table_name = 'categories'
|
||||||
acts_as_nested_set
|
acts_as_nested_set
|
||||||
end
|
end
|
||||||
|
|
||||||
class ScopedCategory < ActiveRecord::Base
|
class ScopedCategory < ActiveRecord::Base
|
||||||
self.table_name = 'categories'
|
self.table_name = 'categories'
|
||||||
acts_as_nested_set :scope => :organization
|
acts_as_nested_set :scope => :organization
|
||||||
end
|
end
|
||||||
|
|
||||||
class RenamedColumns < ActiveRecord::Base
|
class RenamedColumns < ActiveRecord::Base
|
||||||
acts_as_nested_set :parent_column => 'mother_id', :left_column => 'red', :right_column => 'black'
|
acts_as_nested_set :parent_column => 'mother_id', :left_column => 'red', :right_column => 'black'
|
||||||
end
|
end
|
||||||
|
|
||||||
class Category < ActiveRecord::Base
|
class Category < ActiveRecord::Base
|
||||||
acts_as_nested_set
|
acts_as_nested_set
|
||||||
|
|
||||||
validates_presence_of :name
|
validates_presence_of :name
|
||||||
|
|
||||||
# Setup a callback that we can switch to true or false per-test
|
# Setup a callback that we can switch to true or false per-test
|
||||||
set_callback :move, :before, :custom_before_move
|
set_callback :move, :before, :custom_before_move
|
||||||
cattr_accessor :test_allows_move
|
cattr_accessor :test_allows_move
|
||||||
@@test_allows_move = true
|
@@test_allows_move = true
|
||||||
def custom_before_move
|
def custom_before_move
|
||||||
@@test_allows_move
|
@@test_allows_move
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
name
|
name
|
||||||
end
|
end
|
||||||
|
|
||||||
def recurse &block
|
def recurse &block
|
||||||
block.call self, lambda{
|
block.call self, lambda{
|
||||||
self.children.each do |child|
|
self.children.each do |child|
|
||||||
child.recurse &block
|
child.recurse &block
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Thing < ActiveRecord::Base
|
class Thing < ActiveRecord::Base
|
||||||
acts_as_nested_set :counter_cache => 'children_count'
|
acts_as_nested_set :counter_cache => 'children_count'
|
||||||
end
|
end
|
||||||
|
|
||||||
class DefaultWithCallbacks < ActiveRecord::Base
|
class DefaultWithCallbacks < ActiveRecord::Base
|
||||||
|
|
||||||
self.table_name = 'categories'
|
self.table_name = 'categories'
|
||||||
|
|
||||||
attr_accessor :before_add, :after_add, :before_remove, :after_remove
|
attr_accessor :before_add, :after_add, :before_remove, :after_remove
|
||||||
|
|
||||||
acts_as_nested_set :before_add => :do_before_add_stuff,
|
acts_as_nested_set :before_add => :do_before_add_stuff,
|
||||||
:after_add => :do_after_add_stuff,
|
:after_add => :do_after_add_stuff,
|
||||||
:before_remove => :do_before_remove_stuff,
|
:before_remove => :do_before_remove_stuff,
|
||||||
:after_remove => :do_after_remove_stuff
|
:after_remove => :do_after_remove_stuff
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
[ :before_add, :after_add, :before_remove, :after_remove ].each do |hook_name|
|
[ :before_add, :after_add, :before_remove, :after_remove ].each do |hook_name|
|
||||||
define_method "do_#{hook_name}_stuff" do |child_node|
|
define_method "do_#{hook_name}_stuff" do |child_node|
|
||||||
self.send("#{hook_name}=", child_node)
|
self.send("#{hook_name}=", child_node)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class Broken < ActiveRecord::Base
|
class Broken < ActiveRecord::Base
|
||||||
acts_as_nested_set
|
acts_as_nested_set
|
||||||
end
|
end
|
Loading…
Reference in New Issue