has_many_polymorphs 2.2 → 2.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +0 -9
- data/README +6 -52
- data/TODO +1 -0
- data/generators/tagging/tagging_generator.rb +1 -14
- data/generators/tagging/templates/tag_test.rb +3 -8
- data/generators/tagging/templates/tagging.rb +2 -2
- data/generators/tagging/templates/tagging_extensions.rb +13 -106
- data/generators/tagging/templates/tagging_test.rb +14 -41
- data/generators/tagging/templates/taggings.yml +3 -5
- data/generators/tagging/templates/tags.yml +2 -2
- data/has_many_polymorphs.gemspec +36 -29
- data/lib/has_many_polymorphs.rb +5 -6
- data/lib/has_many_polymorphs/association.rb +8 -9
- data/lib/has_many_polymorphs/autoload.rb +16 -52
- data/lib/has_many_polymorphs/base.rb +3 -13
- data/lib/has_many_polymorphs/class_methods.rb +102 -181
- data/lib/has_many_polymorphs/debugging_tools.rb +0 -2
- data/lib/has_many_polymorphs/dependencies.rb +36 -0
- data/lib/has_many_polymorphs/rake_task_redefine_task.rb +1 -9
- data/lib/has_many_polymorphs/reflection.rb +6 -7
- data/lib/has_many_polymorphs/support_methods.rb +4 -10
- data/test/fixtures/{fish.yml → aquatic/fish.yml} +0 -0
- data/test/fixtures/{little_whale_pupils.yml → aquatic/little_whale_pupils.yml} +0 -0
- data/test/fixtures/{whales.yml → aquatic/whales.yml} +0 -0
- data/test/models/aquatic/fish.rb +1 -2
- data/test/models/aquatic/whale.rb +0 -2
- data/test/models/eaters_foodstuff.rb +3 -1
- data/test/models/petfood.rb +1 -2
- data/test/schema.rb +0 -15
- data/test/test_helper.rb +18 -39
- data/test/unit/{has_many_polymorphs_test.rb → polymorph_test.rb} +109 -181
- metadata +92 -265
- data.tar.gz.sig +0 -1
- data/Manifest +0 -173
- data/Rakefile +0 -28
- data/examples/hmph.rb +0 -69
- data/lib/has_many_polymorphs/configuration.rb +0 -19
- data/test/fixtures/people.yml +0 -7
- data/test/generator/tagging_generator_test.rb +0 -42
- data/test/integration/app/README +0 -182
- data/test/integration/app/Rakefile +0 -19
- data/test/integration/app/app/controllers/application.rb +0 -7
- data/test/integration/app/app/controllers/bones_controller.rb +0 -5
- data/test/integration/app/app/helpers/addresses_helper.rb +0 -2
- data/test/integration/app/app/helpers/application_helper.rb +0 -3
- data/test/integration/app/app/helpers/bones_helper.rb +0 -2
- data/test/integration/app/app/helpers/sellers_helper.rb +0 -28
- data/test/integration/app/app/helpers/states_helper.rb +0 -2
- data/test/integration/app/app/helpers/users_helper.rb +0 -2
- data/test/integration/app/app/models/bone.rb +0 -2
- data/test/integration/app/app/models/double_sti_parent.rb +0 -2
- data/test/integration/app/app/models/double_sti_parent_relationship.rb +0 -2
- data/test/integration/app/app/models/organic_substance.rb +0 -2
- data/test/integration/app/app/models/single_sti_parent.rb +0 -4
- data/test/integration/app/app/models/single_sti_parent_relationship.rb +0 -4
- data/test/integration/app/app/models/stick.rb +0 -2
- data/test/integration/app/app/models/stone.rb +0 -2
- data/test/integration/app/app/views/addresses/edit.html.erb +0 -12
- data/test/integration/app/app/views/addresses/index.html.erb +0 -18
- data/test/integration/app/app/views/addresses/new.html.erb +0 -11
- data/test/integration/app/app/views/addresses/show.html.erb +0 -3
- data/test/integration/app/app/views/bones/index.rhtml +0 -5
- data/test/integration/app/app/views/layouts/addresses.html.erb +0 -17
- data/test/integration/app/app/views/layouts/sellers.html.erb +0 -17
- data/test/integration/app/app/views/layouts/states.html.erb +0 -17
- data/test/integration/app/app/views/layouts/users.html.erb +0 -17
- data/test/integration/app/app/views/sellers/edit.html.erb +0 -12
- data/test/integration/app/app/views/sellers/index.html.erb +0 -20
- data/test/integration/app/app/views/sellers/new.html.erb +0 -11
- data/test/integration/app/app/views/sellers/show.html.erb +0 -3
- data/test/integration/app/app/views/states/edit.html.erb +0 -12
- data/test/integration/app/app/views/states/index.html.erb +0 -19
- data/test/integration/app/app/views/states/new.html.erb +0 -11
- data/test/integration/app/app/views/states/show.html.erb +0 -3
- data/test/integration/app/app/views/users/edit.html.erb +0 -12
- data/test/integration/app/app/views/users/index.html.erb +0 -22
- data/test/integration/app/app/views/users/new.html.erb +0 -11
- data/test/integration/app/app/views/users/show.html.erb +0 -3
- data/test/integration/app/config/boot.rb +0 -110
- data/test/integration/app/config/database.yml +0 -17
- data/test/integration/app/config/environment.rb +0 -19
- data/test/integration/app/config/environment.rb.canonical +0 -19
- data/test/integration/app/config/environments/development.rb +0 -9
- data/test/integration/app/config/environments/production.rb +0 -18
- data/test/integration/app/config/environments/test.rb +0 -19
- data/test/integration/app/config/locomotive.yml +0 -6
- data/test/integration/app/config/routes.rb +0 -33
- data/test/integration/app/config/ultrasphinx/default.base +0 -56
- data/test/integration/app/config/ultrasphinx/development.conf.canonical +0 -155
- data/test/integration/app/db/migrate/001_create_sticks.rb +0 -11
- data/test/integration/app/db/migrate/002_create_stones.rb +0 -11
- data/test/integration/app/db/migrate/003_create_organic_substances.rb +0 -11
- data/test/integration/app/db/migrate/004_create_bones.rb +0 -8
- data/test/integration/app/db/migrate/005_create_single_sti_parents.rb +0 -11
- data/test/integration/app/db/migrate/006_create_double_sti_parents.rb +0 -11
- data/test/integration/app/db/migrate/007_create_single_sti_parent_relationships.rb +0 -13
- data/test/integration/app/db/migrate/008_create_double_sti_parent_relationships.rb +0 -14
- data/test/integration/app/db/migrate/009_create_library_model.rb +0 -11
- data/test/integration/app/doc/README_FOR_APP +0 -2
- data/test/integration/app/generators/commenting_generator_test.rb +0 -83
- data/test/integration/app/lib/library_model.rb +0 -2
- data/test/integration/app/public/404.html +0 -30
- data/test/integration/app/public/500.html +0 -30
- data/test/integration/app/public/dispatch.cgi +0 -10
- data/test/integration/app/public/dispatch.fcgi +0 -24
- data/test/integration/app/public/dispatch.rb +0 -10
- data/test/integration/app/public/favicon.ico +0 -0
- data/test/integration/app/public/images/rails.png +0 -0
- data/test/integration/app/public/index.html +0 -277
- data/test/integration/app/public/javascripts/application.js +0 -2
- data/test/integration/app/public/javascripts/controls.js +0 -833
- data/test/integration/app/public/javascripts/dragdrop.js +0 -942
- data/test/integration/app/public/javascripts/effects.js +0 -1088
- data/test/integration/app/public/javascripts/prototype.js +0 -2515
- data/test/integration/app/public/robots.txt +0 -1
- data/test/integration/app/public/stylesheets/scaffold.css +0 -74
- data/test/integration/app/script/about +0 -3
- data/test/integration/app/script/breakpointer +0 -3
- data/test/integration/app/script/console +0 -3
- data/test/integration/app/script/destroy +0 -3
- data/test/integration/app/script/generate +0 -3
- data/test/integration/app/script/performance/benchmarker +0 -3
- data/test/integration/app/script/performance/profiler +0 -3
- data/test/integration/app/script/plugin +0 -3
- data/test/integration/app/script/process/inspector +0 -3
- data/test/integration/app/script/process/reaper +0 -3
- data/test/integration/app/script/process/spawner +0 -3
- data/test/integration/app/script/runner +0 -3
- data/test/integration/app/script/server +0 -3
- data/test/integration/app/test/fixtures/double_sti_parent_relationships.yml +0 -7
- data/test/integration/app/test/fixtures/double_sti_parents.yml +0 -7
- data/test/integration/app/test/fixtures/organic_substances.yml +0 -5
- data/test/integration/app/test/fixtures/single_sti_parent_relationships.yml +0 -7
- data/test/integration/app/test/fixtures/single_sti_parents.yml +0 -7
- data/test/integration/app/test/fixtures/sticks.yml +0 -7
- data/test/integration/app/test/fixtures/stones.yml +0 -7
- data/test/integration/app/test/functional/addresses_controller_test.rb +0 -57
- data/test/integration/app/test/functional/bones_controller_test.rb +0 -8
- data/test/integration/app/test/functional/sellers_controller_test.rb +0 -57
- data/test/integration/app/test/functional/states_controller_test.rb +0 -57
- data/test/integration/app/test/functional/users_controller_test.rb +0 -57
- data/test/integration/app/test/test_helper.rb +0 -8
- data/test/integration/app/test/unit/bone_test.rb +0 -8
- data/test/integration/app/test/unit/double_sti_parent_relationship_test.rb +0 -8
- data/test/integration/app/test/unit/double_sti_parent_test.rb +0 -8
- data/test/integration/app/test/unit/organic_substance_test.rb +0 -8
- data/test/integration/app/test/unit/single_sti_parent_relationship_test.rb +0 -8
- data/test/integration/app/test/unit/single_sti_parent_test.rb +0 -8
- data/test/integration/app/test/unit/stick_test.rb +0 -8
- data/test/integration/app/test/unit/stone_test.rb +0 -8
- data/test/integration/server_test.rb +0 -43
- data/test/models/parentship.rb +0 -4
- data/test/models/person.rb +0 -9
- data/test/patches/symlinked_plugins_1.2.6.diff +0 -46
- data/test/setup.rb +0 -14
- metadata.gz.sig +0 -0
|
@@ -31,8 +31,6 @@ Turns on Rails' default dependency logging.
|
|
|
31
31
|
|
|
32
32
|
=end
|
|
33
33
|
|
|
34
|
-
_logger_warn "debug mode enabled"
|
|
35
|
-
|
|
36
34
|
class << ActiveRecord::Base
|
|
37
35
|
COLLECTION_METHODS = [:belongs_to, :has_many, :has_and_belongs_to_many, :has_one,
|
|
38
36
|
:has_many_polymorphs, :acts_as_double_polymorphic_join].each do |method_name|
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
|
|
2
|
+
=begin rdoc
|
|
3
|
+
Adds a minimal dependency injection framework so that owners of polymorphic relationships reload after their children, reinjecting the child helper methods.
|
|
4
|
+
|
|
5
|
+
Overrides Dependencies#<tt>new_constants_in</tt>.
|
|
6
|
+
=end
|
|
7
|
+
|
|
8
|
+
module Dependencies
|
|
9
|
+
|
|
10
|
+
mattr_accessor :injection_graph
|
|
11
|
+
self.injection_graph = Hash.new([])
|
|
12
|
+
|
|
13
|
+
# Add a dependency for this target.
|
|
14
|
+
def inject_dependency(target, *requirements)
|
|
15
|
+
target, requirements = target.to_s, requirements.map(&:to_s)
|
|
16
|
+
injection_graph[target] = ((injection_graph[target] + requirements).uniq - [target])
|
|
17
|
+
requirements.each {|requirement| mark_for_unload requirement }
|
|
18
|
+
# _logger_debug "has_many_polymorphs: injection graph: #{injection_graph.inspect}"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Make sure any dependent constants of the constants added by <tt>yield</tt> are reloaded.
|
|
22
|
+
def new_constants_in_with_injection(*descs, &block) # chain
|
|
23
|
+
# _logger_debug "has_many_polymorphs: NEW: autoloaded constants: #{autoloaded_constants.inspect}; #{explicitly_unloadable_constants.inspect}" if (autoloaded_constants + explicitly_unloadable_constants).any?
|
|
24
|
+
returning(new_constants_in_without_injection(*descs, &block)) do |found|
|
|
25
|
+
# _logger_debug "has_many_polymorphs: new constants: #{found.inspect}" if found.any?
|
|
26
|
+
found.each do |constant|
|
|
27
|
+
injection_graph[constant].each do |requirement|
|
|
28
|
+
requirement.constantize
|
|
29
|
+
# _logger_debug "has_many_polymorphs: constantized #{requirement}"
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
alias_method_chain :new_constants_in, :injection
|
|
35
|
+
|
|
36
|
+
end
|
|
@@ -2,19 +2,18 @@ module ActiveRecord #:nodoc:
|
|
|
2
2
|
module Reflection #:nodoc:
|
|
3
3
|
|
|
4
4
|
module ClassMethods #:nodoc:
|
|
5
|
-
|
|
6
|
-
# Update the default reflection switch so that <tt>:has_many_polymorphs</tt> types get instantiated.
|
|
7
|
-
# It's not a composed method so we have to override the whole thing.
|
|
5
|
+
|
|
6
|
+
# Update the default reflection switch so that <tt>:has_many_polymorphs</tt> types get instantiated. It's not a composed method so we have to override the whole thing.
|
|
8
7
|
def create_reflection(macro, name, options, active_record)
|
|
9
8
|
case macro
|
|
10
9
|
when :has_many, :belongs_to, :has_one, :has_and_belongs_to_many
|
|
11
|
-
|
|
12
|
-
reflection = klass.new(macro, name, options, active_record)
|
|
10
|
+
reflection = AssociationReflection.new(macro, name, options, active_record)
|
|
13
11
|
when :composed_of
|
|
14
12
|
reflection = AggregateReflection.new(macro, name, options, active_record)
|
|
15
|
-
|
|
13
|
+
# added by has_many_polymorphs #
|
|
16
14
|
when :has_many_polymorphs
|
|
17
15
|
reflection = PolymorphicReflection.new(macro, name, options, active_record)
|
|
16
|
+
# end added #
|
|
18
17
|
end
|
|
19
18
|
write_inheritable_hash :reflections, name => reflection
|
|
20
19
|
reflection
|
|
@@ -33,7 +32,7 @@ Inherits from ActiveRecord::Reflection::AssociationReflection.
|
|
|
33
32
|
|
|
34
33
|
=end
|
|
35
34
|
|
|
36
|
-
class PolymorphicReflection <
|
|
35
|
+
class PolymorphicReflection < AssociationReflection
|
|
37
36
|
# Stub out the validity check. Has_many_polymorphs checks validity on macro creation, not on reflection.
|
|
38
37
|
def check_validity!
|
|
39
38
|
# nothing
|
|
@@ -45,13 +45,9 @@ class Hash
|
|
|
45
45
|
|
|
46
46
|
# An implementation of select that returns a Hash.
|
|
47
47
|
def _select
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
Hash[*self.select do |key, value|
|
|
52
|
-
yield key, value
|
|
53
|
-
end._flatten_once]
|
|
54
|
-
end
|
|
48
|
+
Hash[*self.select do |key, value|
|
|
49
|
+
yield key, value
|
|
50
|
+
end._flatten_once]
|
|
55
51
|
end
|
|
56
52
|
end
|
|
57
53
|
|
|
@@ -62,17 +58,15 @@ class Object
|
|
|
62
58
|
|
|
63
59
|
# Logger shortcut.
|
|
64
60
|
def _logger_debug s
|
|
65
|
-
s = "** has_many_polymorphs: #{s}"
|
|
66
61
|
RAILS_DEFAULT_LOGGER.debug(s) if RAILS_DEFAULT_LOGGER
|
|
67
62
|
end
|
|
68
63
|
|
|
69
64
|
# Logger shortcut.
|
|
70
65
|
def _logger_warn s
|
|
71
|
-
s = "** has_many_polymorphs: #{s}"
|
|
72
66
|
if RAILS_DEFAULT_LOGGER
|
|
73
67
|
RAILS_DEFAULT_LOGGER.warn(s)
|
|
74
68
|
else
|
|
75
|
-
$stderr.puts(s)
|
|
69
|
+
$stderr.puts("has_many_polymorphs: #{s}")
|
|
76
70
|
end
|
|
77
71
|
end
|
|
78
72
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
data/test/models/aquatic/fish.rb
CHANGED
|
@@ -3,6 +3,8 @@ class EatersFoodstuff < ActiveRecord::Base
|
|
|
3
3
|
belongs_to :foodstuff, :class_name => "Petfood", :foreign_key => "foodstuff_id"
|
|
4
4
|
belongs_to :eater, :polymorphic => true
|
|
5
5
|
|
|
6
|
-
before_save
|
|
6
|
+
def before_save
|
|
7
|
+
self.some_attribute = 3
|
|
8
|
+
end
|
|
7
9
|
end
|
|
8
10
|
|
data/test/models/petfood.rb
CHANGED
|
@@ -23,8 +23,7 @@ class Petfood < ActiveRecord::Base
|
|
|
23
23
|
:ignore_duplicates => false,
|
|
24
24
|
:conditions => "NULL IS NULL",
|
|
25
25
|
:order => "eaters_foodstuffs.updated_at ASC",
|
|
26
|
-
:parent_order => "
|
|
27
|
-
:parent_conditions => "petfoods.name IS NULL OR petfoods.name != 'Snausages'",
|
|
26
|
+
:parent_order => "the_petfood_primary_key DESC",
|
|
28
27
|
:extend => [ExtensionModule, OtherExtensionModule, proc {}],
|
|
29
28
|
:join_extend => proc {
|
|
30
29
|
def a_method
|
data/test/schema.rb
CHANGED
|
@@ -67,21 +67,6 @@ ActiveRecord::Schema.define(:version => 0) do
|
|
|
67
67
|
t.column :protector_type, :string
|
|
68
68
|
t.column :created_at, :datetime, :null => false
|
|
69
69
|
t.column :updated_at, :datetime, :null => false
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
create_table :parentships, :force => true do |t|
|
|
73
|
-
t.column :parent_id, :integer
|
|
74
|
-
t.column :child_type, :string
|
|
75
|
-
t.column :kid_id, :integer
|
|
76
|
-
t.column :created_at, :datetime, :null => false
|
|
77
|
-
t.column :updated_at, :datetime, :null => false
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
create_table :people, :force => true do |t|
|
|
81
|
-
t.column :name, :string
|
|
82
|
-
t.column :age, :integer
|
|
83
|
-
t.column :created_at, :datetime, :null => false
|
|
84
|
-
t.column :updated_at, :datetime, :null => false
|
|
85
70
|
end
|
|
86
71
|
|
|
87
72
|
end
|
data/test/test_helper.rb
CHANGED
|
@@ -1,52 +1,31 @@
|
|
|
1
1
|
|
|
2
|
-
|
|
3
|
-
require 'rubygems'
|
|
4
|
-
require '
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
require 'multi_rails_init'
|
|
8
|
-
#require 'ruby-debug' # uncomment if needed (for Ruby >= 1.9 use require 'debug' where needed)
|
|
9
|
-
|
|
10
|
-
if defined? ENV['MULTIRAILS_RAILS_VERSION']
|
|
11
|
-
ENV['RAILS_GEM_VERSION'] = ENV['MULTIRAILS_RAILS_VERSION']
|
|
2
|
+
begin
|
|
3
|
+
require 'rubygems'
|
|
4
|
+
require 'ruby-debug'
|
|
5
|
+
Debugger.start
|
|
6
|
+
rescue Object
|
|
12
7
|
end
|
|
13
8
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
9
|
+
# load the applicaiton's test helper
|
|
10
|
+
begin
|
|
11
|
+
require File.dirname(__FILE__) + '/../../../../test/test_helper'
|
|
12
|
+
rescue LoadError
|
|
13
|
+
require '~/projects/miscellaneous/cookbook/test/test_helper'
|
|
18
14
|
end
|
|
19
15
|
|
|
20
|
-
|
|
16
|
+
WORKING_DIR = File.dirname(__FILE__)
|
|
21
17
|
|
|
22
|
-
|
|
18
|
+
Inflector.inflections {|i| i.irregular 'fish', 'fish' }
|
|
23
19
|
|
|
24
|
-
|
|
25
|
-
|
|
20
|
+
$LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path = WORKING_DIR + "/fixtures")
|
|
21
|
+
$LOAD_PATH.unshift(WORKING_DIR + "/models")
|
|
22
|
+
$LOAD_PATH.unshift(WORKING_DIR + "/modules")
|
|
26
23
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
$LOAD_PATH.unshift(ActiveSupport::TestCase.fixture_path = HERE + "/fixtures")
|
|
30
|
-
$LOAD_PATH.unshift(HERE + "/models")
|
|
31
|
-
$LOAD_PATH.unshift(HERE + "/modules")
|
|
32
|
-
|
|
33
|
-
class ActiveSupport::TestCase
|
|
24
|
+
class Test::Unit::TestCase
|
|
34
25
|
self.use_transactional_fixtures = !(ActiveRecord::Base.connection.is_a? ActiveRecord::ConnectionAdapters::MysqlAdapter rescue false)
|
|
35
26
|
self.use_instantiated_fixtures = false
|
|
36
27
|
end
|
|
37
28
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
### For integration tests
|
|
29
|
+
# test schema
|
|
30
|
+
load(File.dirname(__FILE__) + "/schema.rb")
|
|
43
31
|
|
|
44
|
-
def truncate
|
|
45
|
-
system("> #{LOG}")
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
def log
|
|
49
|
-
File.open(LOG, 'r') do |f|
|
|
50
|
-
f.read
|
|
51
|
-
end
|
|
52
|
-
end
|
|
@@ -1,25 +1,11 @@
|
|
|
1
1
|
require File.dirname(__FILE__) + '/../test_helper'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
require 'wild_boar'
|
|
5
|
-
require 'frog'
|
|
6
|
-
require 'cat'
|
|
7
|
-
require 'kitten'
|
|
8
|
-
require 'aquatic/whale'
|
|
9
|
-
require 'aquatic/fish'
|
|
10
|
-
require 'aquatic/pupils_whale'
|
|
11
|
-
require 'beautiful_fight_relationship'
|
|
12
|
-
|
|
13
|
-
class PolymorphTest < ActiveSupport::TestCase
|
|
14
|
-
|
|
15
|
-
set_fixture_class :bow_wows => Dog
|
|
16
|
-
set_fixture_class :keep_your_enemies_close => BeautifulFightRelationship
|
|
17
|
-
set_fixture_class :whales => Aquatic::Whale
|
|
18
|
-
set_fixture_class :fish => Aquatic::Fish
|
|
19
|
-
set_fixture_class :little_whale_pupils => Aquatic::PupilsWhale
|
|
3
|
+
class PolymorphTest < Test::Unit::TestCase
|
|
20
4
|
|
|
21
5
|
fixtures :cats, :bow_wows, :frogs, :wild_boars, :eaters_foodstuffs, :petfoods,
|
|
22
|
-
:fish, :whales, :little_whale_pupils,
|
|
6
|
+
:"aquatic/fish", :"aquatic/whales", :"aquatic/little_whale_pupils",
|
|
7
|
+
:keep_your_enemies_close
|
|
8
|
+
require 'beautiful_fight_relationship'
|
|
23
9
|
|
|
24
10
|
def setup
|
|
25
11
|
@association_error = ActiveRecord::Associations::PolymorphicError
|
|
@@ -36,61 +22,61 @@ class PolymorphTest < ActiveSupport::TestCase
|
|
|
36
22
|
@froggy = Frog.find(1)
|
|
37
23
|
|
|
38
24
|
@join_count = EatersFoodstuff.count
|
|
39
|
-
@
|
|
40
|
-
@
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
25
|
+
@l = @kibbles.eaters.size
|
|
26
|
+
@m = @bits.eaters.size
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def d
|
|
30
|
+
require 'ruby-debug'
|
|
31
|
+
Debugger.start
|
|
32
|
+
debugger
|
|
33
|
+
end
|
|
45
34
|
|
|
46
35
|
def test_all_relationship_validities
|
|
47
36
|
# q = []
|
|
48
37
|
# ObjectSpace.each_object(Class){|c| q << c if c.ancestors.include? ActiveRecord::Base }
|
|
49
|
-
# q.each{|c| puts "#{c.name}.reflect_on_all_associations.map
|
|
50
|
-
Petfood.reflect_on_all_associations.map
|
|
51
|
-
Tabby.reflect_on_all_associations.map
|
|
52
|
-
Kitten.reflect_on_all_associations.map
|
|
53
|
-
Dog.reflect_on_all_associations.map
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
Cat.reflect_on_all_associations.map
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
Parentship.reflect_on_all_associations.map(&:check_validity!)
|
|
63
|
-
Aquatic::Whale.reflect_on_all_associations.map(&:check_validity!)
|
|
64
|
-
Aquatic::PupilsWhale.reflect_on_all_associations.map(&:check_validity!)
|
|
38
|
+
# q.each{|c| puts "#{c.name}.reflect_on_all_associations.map &:check_validity! "}
|
|
39
|
+
Petfood.reflect_on_all_associations.map &:check_validity!
|
|
40
|
+
Tabby.reflect_on_all_associations.map &:check_validity!
|
|
41
|
+
Kitten.reflect_on_all_associations.map &:check_validity!
|
|
42
|
+
Dog.reflect_on_all_associations.map &:check_validity!
|
|
43
|
+
Aquatic::Fish.reflect_on_all_associations.map &:check_validity!
|
|
44
|
+
EatersFoodstuff.reflect_on_all_associations.map &:check_validity!
|
|
45
|
+
WildBoar.reflect_on_all_associations.map &:check_validity!
|
|
46
|
+
Frog.reflect_on_all_associations.map &:check_validity!
|
|
47
|
+
Aquatic::Whale.reflect_on_all_associations.map &:check_validity!
|
|
48
|
+
Cat.reflect_on_all_associations.map &:check_validity!
|
|
49
|
+
Aquatic::PupilsWhale.reflect_on_all_associations.map &:check_validity!
|
|
50
|
+
BeautifulFightRelationship.reflect_on_all_associations.map &:check_validity!
|
|
65
51
|
end
|
|
66
52
|
|
|
67
53
|
def test_assignment
|
|
68
54
|
assert @kibbles.eaters.blank?
|
|
69
55
|
assert @kibbles.eaters.push(Cat.find_by_name('Chloe'))
|
|
70
|
-
assert_equal @
|
|
56
|
+
assert_equal @l += 1, @kibbles.eaters.count
|
|
71
57
|
|
|
72
58
|
@kibbles.reload
|
|
73
|
-
assert_equal @
|
|
59
|
+
assert_equal @l, @kibbles.eaters.count
|
|
74
60
|
end
|
|
75
61
|
|
|
76
62
|
def test_duplicate_assignment
|
|
77
63
|
# try to add a duplicate item when :ignore_duplicates is false
|
|
78
64
|
@kibbles.eaters.push(@alice)
|
|
79
|
-
assert @kibbles.eaters.
|
|
65
|
+
assert @kibbles.eaters.include?(@alice)
|
|
80
66
|
@kibbles.eaters.push(@alice)
|
|
81
|
-
assert_equal @
|
|
67
|
+
assert_equal @l + 2, @kibbles.eaters.count
|
|
82
68
|
assert_equal @join_count + 2, EatersFoodstuff.count
|
|
83
69
|
end
|
|
84
70
|
|
|
85
71
|
def test_create_and_push
|
|
86
72
|
assert @kibbles.eaters.push(@spot)
|
|
87
|
-
assert_equal @
|
|
73
|
+
assert_equal @l += 1, @kibbles.eaters.count
|
|
88
74
|
assert @kibbles.eaters << @rover
|
|
89
75
|
assert @kibbles.eaters << Kitten.create(:name => "Miranda")
|
|
90
|
-
assert_equal @
|
|
76
|
+
assert_equal @l += 2, @kibbles.eaters.length
|
|
91
77
|
|
|
92
78
|
@kibbles.reload
|
|
93
|
-
assert_equal @
|
|
79
|
+
assert_equal @l, @kibbles.eaters.length
|
|
94
80
|
|
|
95
81
|
# test that ids and new flags were set appropriately
|
|
96
82
|
assert_not_nil @kibbles.eaters[0].id
|
|
@@ -104,29 +90,18 @@ class PolymorphTest < ActiveSupport::TestCase
|
|
|
104
90
|
|
|
105
91
|
def test_add_join_record
|
|
106
92
|
assert_equal Kitten, @chloe.class
|
|
107
|
-
assert
|
|
108
|
-
assert
|
|
109
|
-
assert
|
|
93
|
+
assert @join_record = EatersFoodstuff.new(:foodstuff_id => @bits.id, :eater_id => @chloe.id, :eater_type => @chloe.class.name )
|
|
94
|
+
assert @join_record.save!
|
|
95
|
+
assert @join_record.id
|
|
110
96
|
assert_equal @join_count + 1, EatersFoodstuff.count
|
|
111
97
|
|
|
112
|
-
#
|
|
113
|
-
assert_equal @
|
|
98
|
+
# not reloaded
|
|
99
|
+
#assert_equal @m, @bits.eaters.size # Doesn't behave this way on latest edge anymore
|
|
100
|
+
assert_equal @m + 1, @bits.eaters.count # SQL
|
|
114
101
|
|
|
115
102
|
# reload; is the new association there?
|
|
116
103
|
assert @bits.eaters.reload
|
|
117
|
-
assert @bits.eaters.
|
|
118
|
-
end
|
|
119
|
-
|
|
120
|
-
def test_build_join_record_on_association
|
|
121
|
-
assert_equal Kitten, @chloe.class
|
|
122
|
-
assert join = @chloe.eaters_foodstuffs.build(:foodstuff => @bits)
|
|
123
|
-
# assert_equal join.eater_type, @chloe.class.name # will be STI parent type
|
|
124
|
-
assert join.save!
|
|
125
|
-
assert join.id
|
|
126
|
-
assert_equal @join_count + 1, EatersFoodstuff.count
|
|
127
|
-
|
|
128
|
-
assert @bits.eaters.reload
|
|
129
|
-
assert @bits.eaters.any? {|obj| obj == @chloe}
|
|
104
|
+
assert @bits.eaters.include?(@chloe)
|
|
130
105
|
end
|
|
131
106
|
|
|
132
107
|
# not supporting this, since has_many :through doesn't support it either
|
|
@@ -134,23 +109,23 @@ class PolymorphTest < ActiveSupport::TestCase
|
|
|
134
109
|
# # add an unsaved item
|
|
135
110
|
# assert @bits.eaters << Kitten.new(:name => "Bridget")
|
|
136
111
|
# assert_nil Kitten.find_by_name("Bridget")
|
|
137
|
-
# assert_equal @
|
|
112
|
+
# assert_equal @m + 1, @bits.eaters.count
|
|
138
113
|
#
|
|
139
114
|
# assert @bits.save
|
|
140
115
|
# @bits.reload
|
|
141
|
-
# assert_equal @
|
|
116
|
+
# assert_equal @m + 1, @bits.eaters.count
|
|
142
117
|
#
|
|
143
118
|
# end
|
|
144
119
|
|
|
145
120
|
def test_self_reference
|
|
146
121
|
assert @kibbles.eaters << @bits
|
|
147
|
-
assert_equal @
|
|
148
|
-
assert @kibbles.eaters.
|
|
122
|
+
assert_equal @l += 1, @kibbles.eaters.count
|
|
123
|
+
assert @kibbles.eaters.include?(@bits)
|
|
149
124
|
@kibbles.reload
|
|
150
125
|
assert @kibbles.foodstuffs_of_eaters.blank?
|
|
151
126
|
|
|
152
127
|
@bits.reload
|
|
153
|
-
assert @bits.foodstuffs_of_eaters.
|
|
128
|
+
assert @bits.foodstuffs_of_eaters.include?(@kibbles)
|
|
154
129
|
assert_equal [@kibbles], @bits.foodstuffs_of_eaters
|
|
155
130
|
end
|
|
156
131
|
|
|
@@ -158,7 +133,7 @@ class PolymorphTest < ActiveSupport::TestCase
|
|
|
158
133
|
assert @kibbles.eaters << @chloe
|
|
159
134
|
@kibbles.reload
|
|
160
135
|
assert @kibbles.eaters.delete(@kibbles.eaters[0])
|
|
161
|
-
assert_equal @
|
|
136
|
+
assert_equal @l, @kibbles.eaters.count
|
|
162
137
|
end
|
|
163
138
|
|
|
164
139
|
def test_destroy
|
|
@@ -167,7 +142,7 @@ class PolymorphTest < ActiveSupport::TestCase
|
|
|
167
142
|
assert @kibbles.eaters.length > 0
|
|
168
143
|
assert @kibbles.eaters[0].destroy
|
|
169
144
|
@kibbles.reload
|
|
170
|
-
assert_equal @
|
|
145
|
+
assert_equal @l, @kibbles.eaters.count
|
|
171
146
|
end
|
|
172
147
|
|
|
173
148
|
def test_clear
|
|
@@ -190,9 +165,9 @@ class PolymorphTest < ActiveSupport::TestCase
|
|
|
190
165
|
def test_individual_collections_push
|
|
191
166
|
assert_equal [@chloe], (@kibbles.eater_kittens << @chloe)
|
|
192
167
|
@kibbles.reload
|
|
193
|
-
assert @kibbles.eaters.
|
|
194
|
-
assert @kibbles.eater_kittens.
|
|
195
|
-
assert !@kibbles.eater_dogs.
|
|
168
|
+
assert @kibbles.eaters.include?(@chloe)
|
|
169
|
+
assert @kibbles.eater_kittens.include?(@chloe)
|
|
170
|
+
assert !@kibbles.eater_dogs.include?(@chloe)
|
|
196
171
|
end
|
|
197
172
|
|
|
198
173
|
def test_individual_collections_delete
|
|
@@ -204,7 +179,7 @@ class PolymorphTest < ActiveSupport::TestCase
|
|
|
204
179
|
|
|
205
180
|
@kibbles.reload
|
|
206
181
|
assert @kibbles.eater_kittens.empty?
|
|
207
|
-
assert @kibbles.eater_dogs.
|
|
182
|
+
assert @kibbles.eater_dogs.include?(@spot)
|
|
208
183
|
end
|
|
209
184
|
|
|
210
185
|
def test_individual_collections_clear
|
|
@@ -217,14 +192,14 @@ class PolymorphTest < ActiveSupport::TestCase
|
|
|
217
192
|
|
|
218
193
|
assert @kibbles.eater_kittens.empty?
|
|
219
194
|
assert_equal 2, @kibbles.eaters.size
|
|
220
|
-
assert !@kibbles.eater_kittens.
|
|
221
|
-
assert !@kibbles.eaters.
|
|
195
|
+
assert !@kibbles.eater_kittens.include?(@chloe)
|
|
196
|
+
assert !@kibbles.eaters.include?(@chloe)
|
|
222
197
|
|
|
223
198
|
@kibbles.reload
|
|
224
199
|
assert @kibbles.eater_kittens.empty?
|
|
225
200
|
assert_equal 2, @kibbles.eaters.size
|
|
226
|
-
assert !@kibbles.eater_kittens.
|
|
227
|
-
assert !@kibbles.eaters.
|
|
201
|
+
assert !@kibbles.eater_kittens.include?(@chloe)
|
|
202
|
+
assert !@kibbles.eaters.include?(@chloe)
|
|
228
203
|
end
|
|
229
204
|
|
|
230
205
|
def test_childrens_individual_collections
|
|
@@ -234,7 +209,7 @@ class PolymorphTest < ActiveSupport::TestCase
|
|
|
234
209
|
|
|
235
210
|
def test_self_referential_join_tables
|
|
236
211
|
# check that the self-reference join tables go the right ways
|
|
237
|
-
assert_equal @
|
|
212
|
+
assert_equal @l, @kibbles.eaters_foodstuffs.count
|
|
238
213
|
assert_equal @kibbles.eaters_foodstuffs.count, @kibbles.eaters_foodstuffs_as_child.count
|
|
239
214
|
end
|
|
240
215
|
|
|
@@ -283,9 +258,8 @@ class PolymorphTest < ActiveSupport::TestCase
|
|
|
283
258
|
end
|
|
284
259
|
|
|
285
260
|
def test_attributes_come_through_when_child_has_underscore_in_table_name
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
261
|
+
@join_record = EatersFoodstuff.new(:foodstuff_id => @bits.id, :eater_id => @puma.id, :eater_type => @puma.class.name)
|
|
262
|
+
@join_record.save!
|
|
289
263
|
@bits.eaters.reload
|
|
290
264
|
|
|
291
265
|
assert_equal "Puma", @puma.name
|
|
@@ -316,13 +290,13 @@ class PolymorphTest < ActiveSupport::TestCase
|
|
|
316
290
|
|
|
317
291
|
def test_namespaced_polymorphic_collection
|
|
318
292
|
@shamu.aquatic_pupils << @swimmy
|
|
319
|
-
assert @shamu.aquatic_pupils.
|
|
293
|
+
assert @shamu.aquatic_pupils.include?(@swimmy)
|
|
320
294
|
@shamu.reload
|
|
321
|
-
assert @shamu.aquatic_pupils.
|
|
295
|
+
assert @shamu.aquatic_pupils.include?(@swimmy)
|
|
322
296
|
|
|
323
297
|
@shamu.aquatic_pupils << @spot
|
|
324
|
-
assert @shamu.dogs.
|
|
325
|
-
assert @shamu.aquatic_pupils.
|
|
298
|
+
assert @shamu.dogs.include?(@spot)
|
|
299
|
+
assert @shamu.aquatic_pupils.include?(@swimmy)
|
|
326
300
|
assert_equal @swimmy, @shamu.aquatic_fish.first
|
|
327
301
|
assert_equal 10, @shamu.aquatic_fish.first.speed
|
|
328
302
|
end
|
|
@@ -333,8 +307,8 @@ class PolymorphTest < ActiveSupport::TestCase
|
|
|
333
307
|
|
|
334
308
|
@shamu.reload
|
|
335
309
|
@shamu.aquatic_pupils.delete @spot
|
|
336
|
-
assert !@shamu.dogs.
|
|
337
|
-
assert !@shamu.aquatic_pupils.
|
|
310
|
+
assert !@shamu.dogs.include?(@spot)
|
|
311
|
+
assert !@shamu.aquatic_pupils.include?(@spot)
|
|
338
312
|
assert_equal 1, @shamu.aquatic_pupils.length
|
|
339
313
|
end
|
|
340
314
|
|
|
@@ -357,9 +331,9 @@ class PolymorphTest < ActiveSupport::TestCase
|
|
|
357
331
|
@alice.enemies << @spot
|
|
358
332
|
@alice.reload
|
|
359
333
|
@spot.reload
|
|
360
|
-
assert @spot.protectors.
|
|
361
|
-
assert @alice.enemies.
|
|
362
|
-
assert !@alice.protectors.
|
|
334
|
+
assert @spot.protectors.include?(@alice)
|
|
335
|
+
assert @alice.enemies.include?(@spot)
|
|
336
|
+
assert !@alice.protectors.include?(@alice)
|
|
363
337
|
assert_equal 1, @alice.beautiful_fight_relationships_as_protector.size
|
|
364
338
|
assert_equal 0, @alice.beautiful_fight_relationships_as_enemy.size
|
|
365
339
|
assert_equal 1, @alice.beautiful_fight_relationships.size
|
|
@@ -367,7 +341,7 @@ class PolymorphTest < ActiveSupport::TestCase
|
|
|
367
341
|
# self reference
|
|
368
342
|
assert_equal 1, @alice.enemies.length
|
|
369
343
|
@alice.enemies.push @alice
|
|
370
|
-
assert @alice.enemies.
|
|
344
|
+
assert @alice.enemies.include?(@alice)
|
|
371
345
|
assert_equal 2, @alice.enemies.length
|
|
372
346
|
@alice.reload
|
|
373
347
|
assert_equal 2, @alice.beautiful_fight_relationships_as_protector.size
|
|
@@ -375,20 +349,6 @@ class PolymorphTest < ActiveSupport::TestCase
|
|
|
375
349
|
assert_equal 3, @alice.beautiful_fight_relationships.size
|
|
376
350
|
end
|
|
377
351
|
|
|
378
|
-
def test_double_collection_build_join_record_on_association
|
|
379
|
-
|
|
380
|
-
join = @alice.beautiful_fight_relationships_as_protector.build(:enemy => @spot)
|
|
381
|
-
|
|
382
|
-
assert_equal @alice.class.base_class.name, join.protector_type
|
|
383
|
-
assert_nothing_raised { join.save! }
|
|
384
|
-
|
|
385
|
-
assert join.id
|
|
386
|
-
assert_equal @double_join_count + 1, BeautifulFightRelationship.count
|
|
387
|
-
|
|
388
|
-
assert @alice.enemies.reload
|
|
389
|
-
assert @alice.enemies.any? {|obj| obj == @spot}
|
|
390
|
-
end
|
|
391
|
-
|
|
392
352
|
def test_double_dependency_injection
|
|
393
353
|
# breakpoint
|
|
394
354
|
end
|
|
@@ -396,12 +356,12 @@ class PolymorphTest < ActiveSupport::TestCase
|
|
|
396
356
|
def test_double_collection_deletion
|
|
397
357
|
@alice.enemies << @spot
|
|
398
358
|
@alice.reload
|
|
399
|
-
assert @alice.enemies.
|
|
359
|
+
assert @alice.enemies.include?(@spot)
|
|
400
360
|
@alice.enemies.delete(@spot)
|
|
401
|
-
assert !@alice.enemies.
|
|
361
|
+
assert !@alice.enemies.include?(@spot)
|
|
402
362
|
assert @alice.enemies.empty?
|
|
403
363
|
@alice.reload
|
|
404
|
-
assert !@alice.enemies.
|
|
364
|
+
assert !@alice.enemies.include?(@spot)
|
|
405
365
|
assert @alice.enemies.empty?
|
|
406
366
|
assert_equal 0, @alice.beautiful_fight_relationships.size
|
|
407
367
|
end
|
|
@@ -409,12 +369,12 @@ class PolymorphTest < ActiveSupport::TestCase
|
|
|
409
369
|
def test_double_collection_deletion_from_opposite_side
|
|
410
370
|
@alice.protectors << @puma
|
|
411
371
|
@alice.reload
|
|
412
|
-
assert @alice.protectors.
|
|
372
|
+
assert @alice.protectors.include?(@puma)
|
|
413
373
|
@alice.protectors.delete(@puma)
|
|
414
|
-
assert !@alice.protectors.
|
|
374
|
+
assert !@alice.protectors.include?(@puma)
|
|
415
375
|
assert @alice.protectors.empty?
|
|
416
376
|
@alice.reload
|
|
417
|
-
assert !@alice.protectors.
|
|
377
|
+
assert !@alice.protectors.include?(@puma)
|
|
418
378
|
assert @alice.protectors.empty?
|
|
419
379
|
assert_equal 0, @alice.beautiful_fight_relationships.size
|
|
420
380
|
end
|
|
@@ -423,58 +383,59 @@ class PolymorphTest < ActiveSupport::TestCase
|
|
|
423
383
|
assert @alice.dogs.empty?
|
|
424
384
|
@alice.enemies << @spot
|
|
425
385
|
|
|
426
|
-
assert @alice.enemies.
|
|
427
|
-
assert !@alice.kittens.
|
|
386
|
+
assert @alice.enemies.include?(@spot)
|
|
387
|
+
assert !@alice.kittens.include?(@alice)
|
|
428
388
|
|
|
429
|
-
assert !@alice.dogs.
|
|
389
|
+
assert !@alice.dogs.include?(@spot)
|
|
430
390
|
@alice.reload
|
|
431
|
-
assert @alice.dogs.
|
|
432
|
-
assert !WildBoar.find(@alice.id).dogs.
|
|
391
|
+
assert @alice.dogs.include?(@spot)
|
|
392
|
+
assert !WildBoar.find(@alice.id).dogs.include?(@spot) # make sure the parent type is checked
|
|
433
393
|
end
|
|
434
394
|
|
|
435
395
|
def test_individual_collections_created_for_double_relationship_from_opposite_side
|
|
436
396
|
assert @alice.wild_boars.empty?
|
|
437
397
|
@alice.protectors << @puma
|
|
438
|
-
@alice.reload
|
|
439
398
|
|
|
440
|
-
assert @alice.protectors.
|
|
441
|
-
assert
|
|
399
|
+
assert @alice.protectors.include?(@puma)
|
|
400
|
+
assert !@alice.wild_boars.include?(@puma)
|
|
401
|
+
@alice.reload
|
|
402
|
+
assert @alice.wild_boars.include?(@puma)
|
|
442
403
|
|
|
443
|
-
assert !Dog.find(@alice.id).wild_boars.
|
|
404
|
+
assert !Dog.find(@alice.id).wild_boars.include?(@puma) # make sure the parent type is checked
|
|
444
405
|
end
|
|
445
406
|
|
|
446
407
|
def test_self_referential_individual_collections_created_for_double_relationship
|
|
447
408
|
@alice.enemies << @alice
|
|
448
409
|
@alice.reload
|
|
449
|
-
assert @alice.enemy_kittens.
|
|
450
|
-
assert @alice.protector_kittens.
|
|
451
|
-
assert @alice.kittens.
|
|
410
|
+
assert @alice.enemy_kittens.include?(@alice)
|
|
411
|
+
assert @alice.protector_kittens.include?(@alice)
|
|
412
|
+
assert @alice.kittens.include?(@alice)
|
|
452
413
|
assert_equal 2, @alice.kittens.size
|
|
453
414
|
|
|
454
415
|
@alice.enemies << (@chloe = Kitten.find_by_name('Chloe'))
|
|
455
416
|
@alice.reload
|
|
456
|
-
assert @alice.enemy_kittens.
|
|
457
|
-
assert !@alice.protector_kittens.
|
|
458
|
-
assert @alice.kittens.
|
|
417
|
+
assert @alice.enemy_kittens.include?(@chloe)
|
|
418
|
+
assert !@alice.protector_kittens.include?(@chloe)
|
|
419
|
+
assert @alice.kittens.include?(@chloe)
|
|
459
420
|
assert_equal 3, @alice.kittens.size
|
|
460
421
|
end
|
|
461
422
|
|
|
462
423
|
def test_child_of_polymorphic_join_can_reach_parent
|
|
463
424
|
@alice.enemies << @spot
|
|
464
425
|
@alice.reload
|
|
465
|
-
assert @spot.protectors.
|
|
426
|
+
assert @spot.protectors.include?(@alice)
|
|
466
427
|
end
|
|
467
428
|
|
|
468
429
|
def test_double_collection_deletion_from_child_polymorphic_join
|
|
469
430
|
@alice.enemies << @spot
|
|
470
431
|
@spot.protectors.delete(@alice)
|
|
471
|
-
assert !@spot.protectors.
|
|
432
|
+
assert !@spot.protectors.include?(@alice)
|
|
472
433
|
@alice.reload
|
|
473
|
-
assert !@alice.enemies.
|
|
434
|
+
assert !@alice.enemies.include?(@spot)
|
|
474
435
|
BeautifulFightRelationship.create(:protector_id => 2, :protector_type => "Dog", :enemy_id => @spot.id, :enemy_type => @spot.class.name)
|
|
475
436
|
@alice.enemies << @spot
|
|
476
437
|
@spot.protectors.delete(@alice)
|
|
477
|
-
assert !@spot.protectors.
|
|
438
|
+
assert !@spot.protectors.include?(@alice)
|
|
478
439
|
end
|
|
479
440
|
|
|
480
441
|
def test_collection_query_on_unsaved_record
|
|
@@ -485,15 +446,15 @@ class PolymorphTest < ActiveSupport::TestCase
|
|
|
485
446
|
def test_double_individual_collections_push
|
|
486
447
|
assert_equal [@chloe], (@spot.protector_kittens << @chloe)
|
|
487
448
|
@spot.reload
|
|
488
|
-
assert @spot.protectors.
|
|
489
|
-
assert @spot.protector_kittens.
|
|
490
|
-
assert !@spot.protector_dogs.
|
|
449
|
+
assert @spot.protectors.include?(@chloe)
|
|
450
|
+
assert @spot.protector_kittens.include?(@chloe)
|
|
451
|
+
assert !@spot.protector_dogs.include?(@chloe)
|
|
491
452
|
|
|
492
453
|
assert_equal [@froggy], (@spot.frogs << @froggy)
|
|
493
454
|
@spot.reload
|
|
494
|
-
assert @spot.enemies.
|
|
495
|
-
assert @spot.frogs.
|
|
496
|
-
assert !@spot.enemy_dogs.
|
|
455
|
+
assert @spot.enemies.include?(@froggy)
|
|
456
|
+
assert @spot.frogs.include?(@froggy)
|
|
457
|
+
assert !@spot.enemy_dogs.include?(@froggy)
|
|
497
458
|
end
|
|
498
459
|
|
|
499
460
|
def test_double_individual_collections_delete
|
|
@@ -505,7 +466,7 @@ class PolymorphTest < ActiveSupport::TestCase
|
|
|
505
466
|
|
|
506
467
|
@spot.reload
|
|
507
468
|
assert @spot.protector_kittens.empty?
|
|
508
|
-
assert @spot.wild_boars.
|
|
469
|
+
assert @spot.wild_boars.include?(@puma)
|
|
509
470
|
end
|
|
510
471
|
|
|
511
472
|
def test_double_individual_collections_clear
|
|
@@ -517,12 +478,12 @@ class PolymorphTest < ActiveSupport::TestCase
|
|
|
517
478
|
@spot.reload
|
|
518
479
|
assert @spot.protector_kittens.empty?
|
|
519
480
|
assert_equal 1, @spot.protectors.size
|
|
520
|
-
assert !@spot.protector_kittens.
|
|
521
|
-
assert !@spot.protectors.
|
|
522
|
-
assert !@spot.protector_kittens.
|
|
523
|
-
assert !@spot.protectors.
|
|
524
|
-
assert @spot.protectors.
|
|
525
|
-
assert @spot.wild_boars.
|
|
481
|
+
assert !@spot.protector_kittens.include?(@chloe)
|
|
482
|
+
assert !@spot.protectors.include?(@chloe)
|
|
483
|
+
assert !@spot.protector_kittens.include?(@alice)
|
|
484
|
+
assert !@spot.protectors.include?(@alice)
|
|
485
|
+
assert @spot.protectors.include?(@puma)
|
|
486
|
+
assert @spot.wild_boars.include?(@puma)
|
|
526
487
|
end
|
|
527
488
|
|
|
528
489
|
def test_single_extensions
|
|
@@ -560,16 +521,9 @@ class PolymorphTest < ActiveSupport::TestCase
|
|
|
560
521
|
end" }
|
|
561
522
|
assert_raises(@association_error) {
|
|
562
523
|
eval "class SomeModel < ActiveRecord::Base
|
|
563
|
-
|
|
524
|
+
acts_as_double_polymorphic_join :polymorph => [:dogs, :cats], :unimorphs => [:dogs, :cats]
|
|
564
525
|
end" }
|
|
565
526
|
end
|
|
566
|
-
|
|
567
|
-
def test_error_message_on_namespaced_targets
|
|
568
|
-
assert_raises(@association_error) {
|
|
569
|
-
eval "class SomeModel < ActiveRecord::Base
|
|
570
|
-
has_many_polymorphs :polymorphs, :from => [:fish]
|
|
571
|
-
end" }
|
|
572
|
-
end
|
|
573
527
|
|
|
574
528
|
def test_single_custom_finders
|
|
575
529
|
[@kibbles, @alice, @puma, @spot, @bits].each {|record| @kibbles.eaters << record; sleep 1} # XXX yeah i know
|
|
@@ -634,38 +588,12 @@ class PolymorphTest < ActiveSupport::TestCase
|
|
|
634
588
|
assert_equal EatersFoodstuff.name, Petfood.reflect_on_association(:eaters).class_name
|
|
635
589
|
end
|
|
636
590
|
|
|
637
|
-
def
|
|
591
|
+
def test_parent_order_orders_parents
|
|
638
592
|
@alice.foodstuffs_of_eaters << Petfood.find(:all, :order => "the_petfood_primary_key ASC")
|
|
639
593
|
@alice.reload #not necessary
|
|
640
594
|
assert_equal [2,1], @alice.foodstuffs_of_eaters.map(&:id)
|
|
641
595
|
end
|
|
642
596
|
|
|
643
|
-
def test_parent_conditions
|
|
644
|
-
@kibbles.eaters << @alice
|
|
645
|
-
assert_equal [@alice], @kibbles.eaters
|
|
646
|
-
|
|
647
|
-
@snausages = Petfood.create(:name => 'Snausages')
|
|
648
|
-
@snausages.eaters << @alice
|
|
649
|
-
assert_equal [@alice], @snausages.eaters
|
|
650
|
-
|
|
651
|
-
assert_equal [@kibbles], @alice.foodstuffs_of_eaters
|
|
652
|
-
end
|
|
653
|
-
|
|
654
|
-
def test_self_referential_hmp_with_conditions
|
|
655
|
-
p = Person.find(:first)
|
|
656
|
-
kid = Person.create(:name => "Tim", :age => 3)
|
|
657
|
-
p.kids << kid
|
|
658
|
-
|
|
659
|
-
kid.reload; p.reload
|
|
660
|
-
|
|
661
|
-
# assert_equal [p], kid.parents
|
|
662
|
-
# assert Rails.has_one? Bug
|
|
663
|
-
# non-standard foreign_type key is not set properly when you are the polymorphic interface of a has_many going to a :through
|
|
664
|
-
|
|
665
|
-
assert_equal [kid], p.kids
|
|
666
|
-
assert_equal [kid], p.people
|
|
667
|
-
end
|
|
668
|
-
|
|
669
597
|
# def test_polymorphic_include
|
|
670
598
|
# @kibbles.eaters << [@kibbles, @alice, @puma, @spot, @bits]
|
|
671
599
|
# assert @kibbles.eaters.include?(@kibbles.eaters_foodstuffs.find(:all, :include => :eater).first.eater)
|