imagine_cms 3.0.0.beta2 → 3.0.0.beta3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +7 -4
- data/app/controllers/cms/content_controller.rb +2 -2
- data/app/helpers/cms_application_helper.rb +5 -5
- data/imagine_cms.gemspec +4 -6
- data/lib/extensions/action_controller.rb +28 -0
- data/lib/extensions/array.rb +11 -0
- data/lib/imagine_cms.rb +8 -0
- data/lib/imagine_cms/engine.rb +24 -9
- data/lib/imagine_cms/version.rb +1 -1
- data/vendor/.DS_Store +0 -0
- data/vendor/gems/.DS_Store +0 -0
- data/vendor/gems/acts_as_tree/README +26 -0
- data/vendor/gems/acts_as_tree/Rakefile +22 -0
- data/vendor/gems/acts_as_tree/init.rb +1 -0
- data/vendor/gems/acts_as_tree/lib/active_record/acts/tree.rb +96 -0
- data/vendor/gems/acts_as_tree/test/abstract_unit.rb +0 -0
- data/vendor/gems/acts_as_tree/test/acts_as_tree_test.rb +219 -0
- data/vendor/gems/acts_as_tree/test/database.yml +0 -0
- data/vendor/gems/acts_as_tree/test/fixtures/mixin.rb +0 -0
- data/vendor/gems/acts_as_tree/test/fixtures/mixins.yml +0 -0
- data/vendor/gems/acts_as_tree/test/schema.rb +0 -0
- data/vendor/gems/acts_as_versioned/.document +5 -0
- data/vendor/gems/acts_as_versioned/.gitignore +6 -0
- data/vendor/gems/acts_as_versioned/CHANGELOG +84 -0
- data/vendor/gems/acts_as_versioned/Gemfile +7 -0
- data/vendor/gems/acts_as_versioned/MIT-LICENSE +20 -0
- data/vendor/gems/acts_as_versioned/README +24 -0
- data/vendor/gems/acts_as_versioned/RUNNING_UNIT_TESTS +41 -0
- data/vendor/gems/acts_as_versioned/Rakefile +146 -0
- data/vendor/gems/acts_as_versioned/acts_as_versioned.gemspec +85 -0
- data/vendor/gems/acts_as_versioned/init.rb +1 -0
- data/vendor/gems/acts_as_versioned/lib/acts_as_versioned.rb +494 -0
- data/vendor/gems/acts_as_versioned/test/abstract_unit.rb +49 -0
- data/vendor/gems/acts_as_versioned/test/database.yml +18 -0
- data/vendor/gems/acts_as_versioned/test/fixtures/authors.yml +6 -0
- data/vendor/gems/acts_as_versioned/test/fixtures/landmark.rb +3 -0
- data/vendor/gems/acts_as_versioned/test/fixtures/landmark_versions.yml +7 -0
- data/vendor/gems/acts_as_versioned/test/fixtures/landmarks.yml +7 -0
- data/vendor/gems/acts_as_versioned/test/fixtures/locked_pages.yml +10 -0
- data/vendor/gems/acts_as_versioned/test/fixtures/locked_pages_revisions.yml +27 -0
- data/vendor/gems/acts_as_versioned/test/fixtures/migrations/1_add_versioned_tables.rb +15 -0
- data/vendor/gems/acts_as_versioned/test/fixtures/page.rb +43 -0
- data/vendor/gems/acts_as_versioned/test/fixtures/page_versions.yml +16 -0
- data/vendor/gems/acts_as_versioned/test/fixtures/pages.yml +8 -0
- data/vendor/gems/acts_as_versioned/test/fixtures/widget.rb +6 -0
- data/vendor/gems/acts_as_versioned/test/migration_test.rb +46 -0
- data/vendor/gems/acts_as_versioned/test/schema.rb +82 -0
- data/vendor/gems/acts_as_versioned/test/versioned_test.rb +370 -0
- metadata +58 -32
data/README.rdoc
CHANGED
@@ -23,14 +23,17 @@ be a good time to get on board.
|
|
23
23
|
|
24
24
|
Here's what's coming up:
|
25
25
|
|
26
|
-
|
27
|
-
* Imagine 3.1: 100% restored functionality, may require a few simple migrations
|
28
|
-
* Imagine 4.0: Refactoring & rewriting of the worst parts (substitutions); move away from ERB templating, to something like Liquid; switch to a cross-browser editor; switch to jquery
|
26
|
+
Current status: Original functionality 25% restored
|
29
27
|
|
30
|
-
|
28
|
+
* Imagine 3.0 (Rails 3.2): 90% restored functionality, can function on existing sites with no database changes
|
29
|
+
* Imagine 3.1 (Rails 3.2): 100% restored functionality, may require a few simple migrations
|
30
|
+
* Imagine 4.0 (Rails 4.0): Refactoring & rewriting of the worst parts (substitutions); move away from ERB templating, to something like Liquid; switch to a cross-browser editor; switch to jQuery
|
31
|
+
|
32
|
+
(Imagine 1.x and 2.x version numbers have already been used internally, so might as well start at 3.0.)
|
31
33
|
|
32
34
|
Dependencies:
|
33
35
|
* Rails 3.2
|
34
36
|
* acts_as_tree
|
35
37
|
* acts_as_versioned
|
36
38
|
* mini_magick
|
39
|
+
* net-dns
|
@@ -35,7 +35,7 @@ module Cms # :nodoc:
|
|
35
35
|
if e.message =~ /string contains null byte/
|
36
36
|
# do nothing
|
37
37
|
else
|
38
|
-
|
38
|
+
raise e
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
@@ -73,7 +73,7 @@ module Cms # :nodoc:
|
|
73
73
|
helper_method :template_exists?
|
74
74
|
|
75
75
|
def url_for_current
|
76
|
-
|
76
|
+
request.fullpath
|
77
77
|
end
|
78
78
|
helper_method :url_for_current
|
79
79
|
|
@@ -76,13 +76,13 @@ module CmsApplicationHelper
|
|
76
76
|
#
|
77
77
|
# NOTE: @error and @notice are deprecated, use flash.now[:error] and flash.now[:notice] instead.
|
78
78
|
def flash_message(message = 'Please review the following messages:')
|
79
|
-
output = ''
|
79
|
+
output = ''.html_safe
|
80
80
|
if (flash[:error] || @error || '') != ''
|
81
|
-
output <<
|
82
|
-
output <<
|
81
|
+
output << content_tag('p') { message }
|
82
|
+
output << content_tag('p', :class => 'error') { flash[:error] || @error }
|
83
83
|
end
|
84
84
|
if (flash[:notice] || @notice || '') != ''
|
85
|
-
output <<
|
85
|
+
output << content_tag('p', :class => 'notice') { flash[:notice] || @notice }
|
86
86
|
end
|
87
87
|
output
|
88
88
|
end
|
@@ -426,7 +426,7 @@ EOF
|
|
426
426
|
def copyright_year(year)
|
427
427
|
year_str, this_year = year.to_s, Time.now.year.to_s
|
428
428
|
year_str << "–#{this_year}" if ((year_str.to_i.to_s == year_str) and (year_str.to_i != this_year.to_i))
|
429
|
-
year_str
|
429
|
+
year_str.html_safe
|
430
430
|
end
|
431
431
|
|
432
432
|
end
|
data/imagine_cms.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |s|
|
|
10
10
|
s.email = ["aaron@biggerbird.com"]
|
11
11
|
s.homepage = "https://github.com/anamba/imagine_cms"
|
12
12
|
s.summary = %q{Imagine Content Management System for Rails}
|
13
|
-
s.description = %q{
|
13
|
+
s.description = %q{Don't use this for now. See README for details.}
|
14
14
|
|
15
15
|
s.rubyforge_project = "imagine_cms"
|
16
16
|
|
@@ -19,9 +19,7 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
20
|
s.require_paths = ["lib"]
|
21
21
|
|
22
|
-
s.add_dependency "rails", "~> 3.2.
|
23
|
-
|
24
|
-
s.add_dependency "
|
25
|
-
s.add_dependency "acts_as_versioned", "~> 0.6.0"
|
26
|
-
s.add_dependency "mini_magick" #, "~> 3.4"
|
22
|
+
s.add_dependency "rails", "~> 3.2.3"
|
23
|
+
s.add_dependency "mini_magick", "~> 3.4"
|
24
|
+
s.add_dependency "net-dns", "~> 0.6.1"
|
27
25
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module ActionControllerExtensions
|
2
|
+
module ClassMethods
|
3
|
+
end
|
4
|
+
|
5
|
+
module InstanceMethods
|
6
|
+
# Determines whether the input string is a valid email address per RFC specification
|
7
|
+
def valid_email_address?(addr, perform_mx_lookup = false)
|
8
|
+
valid = true
|
9
|
+
valid = valid && addr.to_s =~ /\A([\w\d]+(?:[\w\d\!\#\$\%\&\*\+\-\/\=\?\^\`\{\|\}\~\.]*[\w\d]+)*)@((?:[\w\d]+\.)+[\w]{2,})\z/
|
10
|
+
user, host = $1, $2
|
11
|
+
|
12
|
+
# blacklist
|
13
|
+
# return false if ContactEmailBlacklist.include?(addr.to_s.strip)
|
14
|
+
|
15
|
+
if perform_mx_lookup
|
16
|
+
begin
|
17
|
+
# require 'net/dns'
|
18
|
+
res = Net::DNS::Resolver.new
|
19
|
+
valid = valid && res.mx(host).size > 0
|
20
|
+
rescue Exception => e
|
21
|
+
logger.error(e)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
valid
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'active_support/core_ext/string/output_safety'
|
2
|
+
require 'active_support/core_ext/module/aliasing.rb'
|
3
|
+
|
4
|
+
class Array
|
5
|
+
def safe_join(sep = $,)
|
6
|
+
sep ||= "".html_safe
|
7
|
+
sep = ERB::Util.html_escape(sep)
|
8
|
+
|
9
|
+
map { |i| ERB::Util.html_escape(i) }.join(sep).html_safe
|
10
|
+
end
|
11
|
+
end
|
data/lib/imagine_cms.rb
CHANGED
@@ -14,3 +14,11 @@ end
|
|
14
14
|
|
15
15
|
# Require our engine
|
16
16
|
require "imagine_cms/engine"
|
17
|
+
|
18
|
+
|
19
|
+
# Require vendored gems
|
20
|
+
# $:.push File.expand_path("../../vendor/gems", __FILE__)
|
21
|
+
# $:.push File.expand_path("../../vendor/gems/acts_as_tree/lib", __FILE__)
|
22
|
+
# require "acts_as_tree/init"
|
23
|
+
# $:.push File.expand_path("../../vendor/gems/acts_as_versioned/lib", __FILE__)
|
24
|
+
# require "acts_as_versioned/init"
|
data/lib/imagine_cms/engine.rb
CHANGED
@@ -3,17 +3,32 @@ module ImagineCms
|
|
3
3
|
class Engine < Rails::Engine
|
4
4
|
engine_name "imagine_cms"
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
config.app_root = app.root
|
9
|
-
# end
|
10
|
-
# end
|
6
|
+
config.app_root = root
|
7
|
+
middleware.use ::ActionDispatch::Static, "#{root}/public"
|
11
8
|
|
12
|
-
#
|
13
|
-
|
14
|
-
|
15
|
-
|
9
|
+
#
|
10
|
+
# load unusual gems
|
11
|
+
#
|
12
|
+
require 'net/dns'
|
16
13
|
|
14
|
+
#
|
15
|
+
# load provided classes
|
16
|
+
#
|
17
|
+
require 'hash_object'
|
18
|
+
require 'hash_wrapper'
|
19
|
+
|
20
|
+
#
|
21
|
+
# load extensions
|
22
|
+
#
|
23
|
+
require 'extensions/array'
|
24
|
+
|
25
|
+
ActiveSupport.on_load(:action_controller) do
|
26
|
+
require 'extensions/action_controller'
|
27
|
+
extend ActionControllerExtensions::ClassMethods
|
28
|
+
include ActionControllerExtensions::InstanceMethods
|
29
|
+
# before_filter :create_settings_object, :set_default_session_values, :check_ssl_requirement, :expire_session_data
|
30
|
+
# after_filter :compress_output
|
31
|
+
end
|
17
32
|
end
|
18
33
|
|
19
34
|
end
|
data/lib/imagine_cms/version.rb
CHANGED
data/vendor/.DS_Store
ADDED
Binary file
|
Binary file
|
@@ -0,0 +1,26 @@
|
|
1
|
+
acts_as_tree
|
2
|
+
============
|
3
|
+
|
4
|
+
Specify this +acts_as+ extension if you want to model a tree structure by providing a parent association and a children
|
5
|
+
association. This requires that you have a foreign key column, which by default is called +parent_id+.
|
6
|
+
|
7
|
+
class Category < ActiveRecord::Base
|
8
|
+
acts_as_tree :order => "name"
|
9
|
+
end
|
10
|
+
|
11
|
+
Example:
|
12
|
+
root
|
13
|
+
\_ child1
|
14
|
+
\_ subchild1
|
15
|
+
\_ subchild2
|
16
|
+
|
17
|
+
root = Category.create("name" => "root")
|
18
|
+
child1 = root.children.create("name" => "child1")
|
19
|
+
subchild1 = child1.children.create("name" => "subchild1")
|
20
|
+
|
21
|
+
root.parent # => nil
|
22
|
+
child1.parent # => root
|
23
|
+
root.children # => [child1]
|
24
|
+
root.children.first.children.first # => subchild1
|
25
|
+
|
26
|
+
Copyright (c) 2007 David Heinemeier Hansson, released under the MIT license
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test acts_as_tree plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for acts_as_tree plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'acts_as_tree'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
ActiveRecord::Base.send :include, ActiveRecord::Acts::Tree
|
@@ -0,0 +1,96 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Acts
|
3
|
+
module Tree
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
# Specify this +acts_as+ extension if you want to model a tree structure by providing a parent association and a children
|
9
|
+
# association. This requires that you have a foreign key column, which by default is called +parent_id+.
|
10
|
+
#
|
11
|
+
# class Category < ActiveRecord::Base
|
12
|
+
# acts_as_tree :order => "name"
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# Example:
|
16
|
+
# root
|
17
|
+
# \_ child1
|
18
|
+
# \_ subchild1
|
19
|
+
# \_ subchild2
|
20
|
+
#
|
21
|
+
# root = Category.create("name" => "root")
|
22
|
+
# child1 = root.children.create("name" => "child1")
|
23
|
+
# subchild1 = child1.children.create("name" => "subchild1")
|
24
|
+
#
|
25
|
+
# root.parent # => nil
|
26
|
+
# child1.parent # => root
|
27
|
+
# root.children # => [child1]
|
28
|
+
# root.children.first.children.first # => subchild1
|
29
|
+
#
|
30
|
+
# In addition to the parent and children associations, the following instance methods are added to the class
|
31
|
+
# after calling <tt>acts_as_tree</tt>:
|
32
|
+
# * <tt>siblings</tt> - Returns all the children of the parent, excluding the current node (<tt>[subchild2]</tt> when called on <tt>subchild1</tt>)
|
33
|
+
# * <tt>self_and_siblings</tt> - Returns all the children of the parent, including the current node (<tt>[subchild1, subchild2]</tt> when called on <tt>subchild1</tt>)
|
34
|
+
# * <tt>ancestors</tt> - Returns all the ancestors of the current node (<tt>[child1, root]</tt> when called on <tt>subchild2</tt>)
|
35
|
+
# * <tt>root</tt> - Returns the root of the current node (<tt>root</tt> when called on <tt>subchild2</tt>)
|
36
|
+
module ClassMethods
|
37
|
+
# Configuration options are:
|
38
|
+
#
|
39
|
+
# * <tt>foreign_key</tt> - specifies the column name to use for tracking of the tree (default: +parent_id+)
|
40
|
+
# * <tt>order</tt> - makes it possible to sort the children according to this SQL snippet.
|
41
|
+
# * <tt>counter_cache</tt> - keeps a count in a +children_count+ column if set to +true+ (default: +false+).
|
42
|
+
def acts_as_tree(options = {})
|
43
|
+
configuration = { :foreign_key => "parent_id", :order => nil, :counter_cache => nil }
|
44
|
+
configuration.update(options) if options.is_a?(Hash)
|
45
|
+
|
46
|
+
belongs_to :parent, :class_name => name, :foreign_key => configuration[:foreign_key], :counter_cache => configuration[:counter_cache]
|
47
|
+
has_many :children, :class_name => name, :foreign_key => configuration[:foreign_key], :order => configuration[:order], :dependent => :destroy
|
48
|
+
|
49
|
+
class_eval <<-EOV
|
50
|
+
include ActiveRecord::Acts::Tree::InstanceMethods
|
51
|
+
|
52
|
+
def self.roots
|
53
|
+
find(:all, :conditions => "#{configuration[:foreign_key]} IS NULL", :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}})
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.root
|
57
|
+
find(:first, :conditions => "#{configuration[:foreign_key]} IS NULL", :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}})
|
58
|
+
end
|
59
|
+
EOV
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
module InstanceMethods
|
64
|
+
# Returns list of ancestors, starting from parent until root.
|
65
|
+
#
|
66
|
+
# subchild1.ancestors # => [child1, root]
|
67
|
+
def ancestors
|
68
|
+
node, nodes = self, []
|
69
|
+
nodes << node = node.parent while node.parent
|
70
|
+
nodes
|
71
|
+
end
|
72
|
+
|
73
|
+
# Returns the root node of the tree.
|
74
|
+
def root
|
75
|
+
node = self
|
76
|
+
node = node.parent while node.parent
|
77
|
+
node
|
78
|
+
end
|
79
|
+
|
80
|
+
# Returns all siblings of the current node.
|
81
|
+
#
|
82
|
+
# subchild1.siblings # => [subchild2]
|
83
|
+
def siblings
|
84
|
+
self_and_siblings - [self]
|
85
|
+
end
|
86
|
+
|
87
|
+
# Returns all siblings and a reference to the current node.
|
88
|
+
#
|
89
|
+
# subchild1.self_and_siblings # => [subchild1, subchild2]
|
90
|
+
def self_and_siblings
|
91
|
+
parent ? parent.children : self.class.roots
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
File without changes
|
@@ -0,0 +1,219 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'active_record'
|
5
|
+
|
6
|
+
$:.unshift File.dirname(__FILE__) + '/../lib'
|
7
|
+
require File.dirname(__FILE__) + '/../init'
|
8
|
+
|
9
|
+
class Test::Unit::TestCase
|
10
|
+
def assert_queries(num = 1)
|
11
|
+
$query_count = 0
|
12
|
+
yield
|
13
|
+
ensure
|
14
|
+
assert_equal num, $query_count, "#{$query_count} instead of #{num} queries were executed."
|
15
|
+
end
|
16
|
+
|
17
|
+
def assert_no_queries(&block)
|
18
|
+
assert_queries(0, &block)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
|
23
|
+
|
24
|
+
# AR keeps printing annoying schema statements
|
25
|
+
$stdout = StringIO.new
|
26
|
+
|
27
|
+
def setup_db
|
28
|
+
ActiveRecord::Base.logger
|
29
|
+
ActiveRecord::Schema.define(:version => 1) do
|
30
|
+
create_table :mixins do |t|
|
31
|
+
t.column :type, :string
|
32
|
+
t.column :parent_id, :integer
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def teardown_db
|
38
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
39
|
+
ActiveRecord::Base.connection.drop_table(table)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class Mixin < ActiveRecord::Base
|
44
|
+
end
|
45
|
+
|
46
|
+
class TreeMixin < Mixin
|
47
|
+
acts_as_tree :foreign_key => "parent_id", :order => "id"
|
48
|
+
end
|
49
|
+
|
50
|
+
class TreeMixinWithoutOrder < Mixin
|
51
|
+
acts_as_tree :foreign_key => "parent_id"
|
52
|
+
end
|
53
|
+
|
54
|
+
class RecursivelyCascadedTreeMixin < Mixin
|
55
|
+
acts_as_tree :foreign_key => "parent_id"
|
56
|
+
has_one :first_child, :class_name => 'RecursivelyCascadedTreeMixin', :foreign_key => :parent_id
|
57
|
+
end
|
58
|
+
|
59
|
+
class TreeTest < Test::Unit::TestCase
|
60
|
+
|
61
|
+
def setup
|
62
|
+
setup_db
|
63
|
+
@root1 = TreeMixin.create!
|
64
|
+
@root_child1 = TreeMixin.create! :parent_id => @root1.id
|
65
|
+
@child1_child = TreeMixin.create! :parent_id => @root_child1.id
|
66
|
+
@root_child2 = TreeMixin.create! :parent_id => @root1.id
|
67
|
+
@root2 = TreeMixin.create!
|
68
|
+
@root3 = TreeMixin.create!
|
69
|
+
end
|
70
|
+
|
71
|
+
def teardown
|
72
|
+
teardown_db
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_children
|
76
|
+
assert_equal @root1.children, [@root_child1, @root_child2]
|
77
|
+
assert_equal @root_child1.children, [@child1_child]
|
78
|
+
assert_equal @child1_child.children, []
|
79
|
+
assert_equal @root_child2.children, []
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_parent
|
83
|
+
assert_equal @root_child1.parent, @root1
|
84
|
+
assert_equal @root_child1.parent, @root_child2.parent
|
85
|
+
assert_nil @root1.parent
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_delete
|
89
|
+
assert_equal 6, TreeMixin.count
|
90
|
+
@root1.destroy
|
91
|
+
assert_equal 2, TreeMixin.count
|
92
|
+
@root2.destroy
|
93
|
+
@root3.destroy
|
94
|
+
assert_equal 0, TreeMixin.count
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_insert
|
98
|
+
@extra = @root1.children.create
|
99
|
+
|
100
|
+
assert @extra
|
101
|
+
|
102
|
+
assert_equal @extra.parent, @root1
|
103
|
+
|
104
|
+
assert_equal 3, @root1.children.size
|
105
|
+
assert @root1.children.include?(@extra)
|
106
|
+
assert @root1.children.include?(@root_child1)
|
107
|
+
assert @root1.children.include?(@root_child2)
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_ancestors
|
111
|
+
assert_equal [], @root1.ancestors
|
112
|
+
assert_equal [@root1], @root_child1.ancestors
|
113
|
+
assert_equal [@root_child1, @root1], @child1_child.ancestors
|
114
|
+
assert_equal [@root1], @root_child2.ancestors
|
115
|
+
assert_equal [], @root2.ancestors
|
116
|
+
assert_equal [], @root3.ancestors
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_root
|
120
|
+
assert_equal @root1, TreeMixin.root
|
121
|
+
assert_equal @root1, @root1.root
|
122
|
+
assert_equal @root1, @root_child1.root
|
123
|
+
assert_equal @root1, @child1_child.root
|
124
|
+
assert_equal @root1, @root_child2.root
|
125
|
+
assert_equal @root2, @root2.root
|
126
|
+
assert_equal @root3, @root3.root
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_roots
|
130
|
+
assert_equal [@root1, @root2, @root3], TreeMixin.roots
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_siblings
|
134
|
+
assert_equal [@root2, @root3], @root1.siblings
|
135
|
+
assert_equal [@root_child2], @root_child1.siblings
|
136
|
+
assert_equal [], @child1_child.siblings
|
137
|
+
assert_equal [@root_child1], @root_child2.siblings
|
138
|
+
assert_equal [@root1, @root3], @root2.siblings
|
139
|
+
assert_equal [@root1, @root2], @root3.siblings
|
140
|
+
end
|
141
|
+
|
142
|
+
def test_self_and_siblings
|
143
|
+
assert_equal [@root1, @root2, @root3], @root1.self_and_siblings
|
144
|
+
assert_equal [@root_child1, @root_child2], @root_child1.self_and_siblings
|
145
|
+
assert_equal [@child1_child], @child1_child.self_and_siblings
|
146
|
+
assert_equal [@root_child1, @root_child2], @root_child2.self_and_siblings
|
147
|
+
assert_equal [@root1, @root2, @root3], @root2.self_and_siblings
|
148
|
+
assert_equal [@root1, @root2, @root3], @root3.self_and_siblings
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
class TreeTestWithEagerLoading < Test::Unit::TestCase
|
153
|
+
|
154
|
+
def setup
|
155
|
+
teardown_db
|
156
|
+
setup_db
|
157
|
+
@root1 = TreeMixin.create!
|
158
|
+
@root_child1 = TreeMixin.create! :parent_id => @root1.id
|
159
|
+
@child1_child = TreeMixin.create! :parent_id => @root_child1.id
|
160
|
+
@root_child2 = TreeMixin.create! :parent_id => @root1.id
|
161
|
+
@root2 = TreeMixin.create!
|
162
|
+
@root3 = TreeMixin.create!
|
163
|
+
|
164
|
+
@rc1 = RecursivelyCascadedTreeMixin.create!
|
165
|
+
@rc2 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc1.id
|
166
|
+
@rc3 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc2.id
|
167
|
+
@rc4 = RecursivelyCascadedTreeMixin.create! :parent_id => @rc3.id
|
168
|
+
end
|
169
|
+
|
170
|
+
def teardown
|
171
|
+
teardown_db
|
172
|
+
end
|
173
|
+
|
174
|
+
def test_eager_association_loading
|
175
|
+
roots = TreeMixin.find(:all, :include => :children, :conditions => "mixins.parent_id IS NULL", :order => "mixins.id")
|
176
|
+
assert_equal [@root1, @root2, @root3], roots
|
177
|
+
assert_no_queries do
|
178
|
+
assert_equal 2, roots[0].children.size
|
179
|
+
assert_equal 0, roots[1].children.size
|
180
|
+
assert_equal 0, roots[2].children.size
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
def test_eager_association_loading_with_recursive_cascading_three_levels_has_many
|
185
|
+
root_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :children => { :children => :children } }, :order => 'mixins.id')
|
186
|
+
assert_equal @rc4, assert_no_queries { root_node.children.first.children.first.children.first }
|
187
|
+
end
|
188
|
+
|
189
|
+
def test_eager_association_loading_with_recursive_cascading_three_levels_has_one
|
190
|
+
root_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :first_child => { :first_child => :first_child } }, :order => 'mixins.id')
|
191
|
+
assert_equal @rc4, assert_no_queries { root_node.first_child.first_child.first_child }
|
192
|
+
end
|
193
|
+
|
194
|
+
def test_eager_association_loading_with_recursive_cascading_three_levels_belongs_to
|
195
|
+
leaf_node = RecursivelyCascadedTreeMixin.find(:first, :include => { :parent => { :parent => :parent } }, :order => 'mixins.id DESC')
|
196
|
+
assert_equal @rc1, assert_no_queries { leaf_node.parent.parent.parent }
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
class TreeTestWithoutOrder < Test::Unit::TestCase
|
201
|
+
|
202
|
+
def setup
|
203
|
+
setup_db
|
204
|
+
@root1 = TreeMixinWithoutOrder.create!
|
205
|
+
@root2 = TreeMixinWithoutOrder.create!
|
206
|
+
end
|
207
|
+
|
208
|
+
def teardown
|
209
|
+
teardown_db
|
210
|
+
end
|
211
|
+
|
212
|
+
def test_root
|
213
|
+
assert [@root1, @root2].include?(TreeMixinWithoutOrder.root)
|
214
|
+
end
|
215
|
+
|
216
|
+
def test_roots
|
217
|
+
assert_equal [], [@root1, @root2] - TreeMixinWithoutOrder.roots
|
218
|
+
end
|
219
|
+
end
|