mirror of
https://github.com/TracksApp/tracks.git
synced 2025-12-20 09:10:12 +01:00
rollback to previous has_many_polymorphs version. upgrading had issues.
git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@468 a4c988fc-2ded-0310-b66e-134b36920a42
This commit is contained in:
parent
38eae1eb08
commit
178e1c70cd
29 changed files with 1513 additions and 0 deletions
184
tracks/vendor/plugins/has_many_polymorphs/LICENSE
vendored
Normal file
184
tracks/vendor/plugins/has_many_polymorphs/LICENSE
vendored
Normal file
|
|
@ -0,0 +1,184 @@
|
|||
Academic Free License (AFL) v. 3.0
|
||||
|
||||
This Academic Free License (the "License") applies to any original work
|
||||
of authorship (the "Original Work") whose owner (the "Licensor") has
|
||||
placed the following licensing notice adjacent to the copyright notice
|
||||
for the Original Work:
|
||||
|
||||
Licensed under the Academic Free License version 3.0
|
||||
|
||||
1) Grant of Copyright License. Licensor grants You a worldwide,
|
||||
royalty-free, non-exclusive, sublicensable license, for the duration of
|
||||
the copyright, to do the following:
|
||||
|
||||
a) to reproduce the Original Work in copies, either alone or as part of
|
||||
a collective work;
|
||||
|
||||
b) to translate, adapt, alter, transform, modify, or arrange the
|
||||
Original Work, thereby creating derivative works ("Derivative Works")
|
||||
based upon the Original Work;
|
||||
|
||||
c) to distribute or communicate copies of the Original Work and
|
||||
Derivative Works to the public, under any license of your choice that
|
||||
does not contradict the terms and conditions, including Licensor's
|
||||
reserved rights and remedies, in this Academic Free License;
|
||||
|
||||
d) to perform the Original Work publicly; and
|
||||
|
||||
e) to display the Original Work publicly.
|
||||
|
||||
2) Grant of Patent License. Licensor grants You a worldwide,
|
||||
royalty-free, non-exclusive, sublicensable license, under patent claims
|
||||
owned or controlled by the Licensor that are embodied in the Original
|
||||
Work as furnished by the Licensor, for the duration of the patents, to
|
||||
make, use, sell, offer for sale, have made, and import the Original Work
|
||||
and Derivative Works.
|
||||
|
||||
3) Grant of Source Code License. The term "Source Code" means the
|
||||
preferred form of the Original Work for making modifications to it and
|
||||
all available documentation describing how to modify the Original Work.
|
||||
Licensor agrees to provide a machine-readable copy of the Source Code of
|
||||
the Original Work along with each copy of the Original Work that
|
||||
Licensor distributes. Licensor reserves the right to satisfy this
|
||||
obligation by placing a machine-readable copy of the Source Code in an
|
||||
information repository reasonably calculated to permit inexpensive and
|
||||
convenient access by You for as long as Licensor continues to distribute
|
||||
the Original Work.
|
||||
|
||||
4) Exclusions From License Grant. Neither the names of Licensor, nor the
|
||||
names of any contributors to the Original Work, nor any of their
|
||||
trademarks or service marks, may be used to endorse or promote products
|
||||
derived from this Original Work without express prior permission of the
|
||||
Licensor. Except as expressly stated herein, nothing in this License
|
||||
grants any license to Licensor's trademarks, copyrights, patents, trade
|
||||
secrets or any other intellectual property. No patent license is granted
|
||||
to make, use, sell, offer for sale, have made, or import embodiments of
|
||||
any patent claims other than the licensed claims defined in Section 2.
|
||||
No license is granted to the trademarks of Licensor even if such marks
|
||||
are included in the Original Work. Nothing in this License shall be
|
||||
interpreted to prohibit Licensor from licensing under terms different
|
||||
from this License any Original Work that Licensor otherwise would have a
|
||||
right to license.
|
||||
|
||||
5) External Deployment. The term "External Deployment" means the use,
|
||||
distribution, or communication of the Original Work or Derivative Works
|
||||
in any way such that the Original Work or Derivative Works may be used
|
||||
by anyone other than You, whether those works are distributed or
|
||||
communicated to those persons or made available as an application
|
||||
intended for use over a network. As an express condition for the grants
|
||||
of license hereunder, You must treat any External Deployment by You of
|
||||
the Original Work or a Derivative Work as a distribution under section
|
||||
1(c).
|
||||
|
||||
6) Attribution Rights. You must retain, in the Source Code of any
|
||||
Derivative Works that You create, all copyright, patent, or trademark
|
||||
notices from the Source Code of the Original Work, as well as any
|
||||
notices of licensing and any descriptive text identified therein as an
|
||||
"Attribution Notice." You must cause the Source Code for any Derivative
|
||||
Works that You create to carry a prominent Attribution Notice reasonably
|
||||
calculated to inform recipients that You have modified the Original
|
||||
Work.
|
||||
|
||||
7) Warranty of Provenance and Disclaimer of Warranty. Licensor warrants
|
||||
that the copyright in and to the Original Work and the patent rights
|
||||
granted herein by Licensor are owned by the Licensor or are sublicensed
|
||||
to You under the terms of this License with the permission of the
|
||||
contributor(s) of those copyrights and patent rights. Except as
|
||||
expressly stated in the immediately preceding sentence, the Original
|
||||
Work is provided under this License on an "AS IS" BASIS and WITHOUT
|
||||
WARRANTY, either express or implied, including, without limitation, the
|
||||
warranties of non-infringement, merchantability or fitness for a
|
||||
particular purpose. THE ENTIRE RISK AS TO THE QUALITY OF THE ORIGINAL
|
||||
WORK IS WITH YOU. This DISCLAIMER OF WARRANTY constitutes an essential
|
||||
part of this License. No license to the Original Work is granted by this
|
||||
License except under this disclaimer.
|
||||
|
||||
8) Limitation of Liability. Under no circumstances and under no legal
|
||||
theory, whether in tort (including negligence), contract, or otherwise,
|
||||
shall the Licensor be liable to anyone for any indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or the use of the Original Work including,
|
||||
without limitation, damages for loss of goodwill, work stoppage,
|
||||
computer failure or malfunction, or any and all other commercial damages
|
||||
or losses. This limitation of liability shall not apply to the extent
|
||||
applicable law prohibits such limitation.
|
||||
|
||||
9) Acceptance and Termination. If, at any time, You expressly assented
|
||||
to this License, that assent indicates your clear and irrevocable
|
||||
acceptance of this License and all of its terms and conditions. If You
|
||||
distribute or communicate copies of the Original Work or a Derivative
|
||||
Work, You must make a reasonable effort under the circumstances to
|
||||
obtain the express assent of recipients to the terms of this License.
|
||||
This License conditions your rights to undertake the activities listed
|
||||
in Section 1, including your right to create Derivative Works based upon
|
||||
the Original Work, and doing so without honoring these terms and
|
||||
conditions is prohibited by copyright law and international treaty.
|
||||
Nothing in this License is intended to affect copyright exceptions and
|
||||
limitations (including "fair use" or "fair dealing"). This License shall
|
||||
terminate immediately and You may no longer exercise any of the rights
|
||||
granted to You by this License upon your failure to honor the conditions
|
||||
in Section 1(c).
|
||||
|
||||
10) Termination for Patent Action. This License shall terminate
|
||||
automatically and You may no longer exercise any of the rights granted
|
||||
to You by this License as of the date You commence an action, including
|
||||
a cross-claim or counterclaim, against Licensor or any licensee alleging
|
||||
that the Original Work infringes a patent. This termination provision
|
||||
shall not apply for an action alleging patent infringement by
|
||||
combinations of the Original Work with other software or hardware.
|
||||
|
||||
11) Jurisdiction, Venue and Governing Law. Any action or suit relating
|
||||
to this License may be brought only in the courts of a jurisdiction
|
||||
wherein the Licensor resides or in which Licensor conducts its primary
|
||||
business, and under the laws of that jurisdiction excluding its
|
||||
conflict-of-law provisions. The application of the United Nations
|
||||
Convention on Contracts for the International Sale of Goods is expressly
|
||||
excluded. Any use of the Original Work outside the scope of this License
|
||||
or after its termination shall be subject to the requirements and
|
||||
penalties of copyright or patent law in the appropriate jurisdiction.
|
||||
This section shall survive the termination of this License.
|
||||
|
||||
12) Attorneys' Fees. In any action to enforce the terms of this License
|
||||
or seeking damages relating thereto, the prevailing party shall be
|
||||
entitled to recover its costs and expenses, including, without
|
||||
limitation, reasonable attorneys' fees and costs incurred in connection
|
||||
with such action, including any appeal of such action. This section
|
||||
shall survive the termination of this License.
|
||||
|
||||
13) Miscellaneous. If any provision of this License is held to be
|
||||
unenforceable, such provision shall be reformed only to the extent
|
||||
necessary to make it enforceable.
|
||||
|
||||
14) Definition of "You" in This License. "You" throughout this License,
|
||||
whether in upper or lower case, means an individual or a legal entity
|
||||
exercising rights under, and complying with all of the terms of, this
|
||||
License. For legal entities, "You" includes any entity that controls, is
|
||||
controlled by, or is under common control with you. For purposes of this
|
||||
definition, "control" means (i) the power, direct or indirect, to cause
|
||||
the direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
15) Right to Use. You may use the Original Work in all ways not
|
||||
otherwise restricted or conditioned by this License or by law, and
|
||||
Licensor promises not to interfere with or be responsible for such uses
|
||||
by You.
|
||||
|
||||
16) Modification of This License. This License is Copyright (c) 2005
|
||||
Lawrence Rosen. Permission is granted to copy, distribute, or
|
||||
communicate this License without modification. Nothing in this License
|
||||
permits You to modify this License as applied to the Original Work or to
|
||||
Derivative Works. However, You may modify the text of this License and
|
||||
copy, distribute or communicate your modified version (the "Modified
|
||||
License") and apply it to other original works of authorship subject to
|
||||
the following conditions: (i) You may not indicate in any way that your
|
||||
Modified License is the "Academic Free License" or "AFL" and you may not
|
||||
use those names in the name of your Modified License; (ii) You must
|
||||
replace the notice specified in the first paragraph above with the
|
||||
notice "Licensed under <insert your license name here>" or with a notice
|
||||
of your own that is not confusingly similar to the notice in this
|
||||
License; and (iii) You may not claim that your original works are open
|
||||
source software unless your Modified License has been approved by Open
|
||||
Source Initiative (OSI) and You comply with its license review and
|
||||
certification process.
|
||||
|
||||
46
tracks/vendor/plugins/has_many_polymorphs/README
vendored
Normal file
46
tracks/vendor/plugins/has_many_polymorphs/README
vendored
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
|
||||
Self-referential, polymorphic has_many :through helper
|
||||
|
||||
Copyright 2006 Evan Weaver (see the LICENSE file)
|
||||
|
||||
"model :parent_class" may be required in some controllers or perhaps models in order for reloading to work properly, since the parent setup must be executed on the child every time the child class is reloaded.
|
||||
|
||||
Usage and help:
|
||||
http://blog.evanweaver.com/articles/2006/06/02/has_many_polymorphs
|
||||
|
||||
Also see the source code, although it's probably not going to be super helpful to you.
|
||||
|
||||
Changelog:
|
||||
|
||||
22. api change; prefix on methods is now singular when using :rename_individual_collections
|
||||
21. add configuration option to cache polymorphic classes in development mode
|
||||
20. collection methods (push, delete, clear) now on individual collections
|
||||
19.2. disjoint collection sides bugfix, don't raise on new records
|
||||
19.1. double classify bugfix
|
||||
19. large changes to properly support double polymorphism
|
||||
18.2. bugfix to make sure the type gets checked on doubly polymorphic parents
|
||||
18.1. bugfix for sqlite3 child attribute retrieval
|
||||
18. bugfix for instantiating attributes of namespaced models
|
||||
17.1. bugfix for double polymorphic relationships
|
||||
17. double polymorphic relationships (includes new API method)
|
||||
16. namespaced model support
|
||||
15. bugfix for postgres and mysql under 1.1.6; refactored tests (thanks hildofur); properly handles legacy table names set with set_table_name()
|
||||
14. sti support added (use the child class names, not the base class)
|
||||
13. bug regarding table names with underscores in SQL query fixed
|
||||
12.1. license change
|
||||
12. file_column bug fixed
|
||||
11. tests written; after_find and after_initialize now correctly called
|
||||
10. bugfix
|
||||
9. rollback
|
||||
8. SQL performance enhancements added
|
||||
7. rewrote singletons as full-fledged proxy class so that marshalling works (e.g. in the session)
|
||||
6. caching added
|
||||
5. fixed dependency reloading problem in development mode
|
||||
4. license change
|
||||
3. added :dependent support on the join table
|
||||
1-2. no changelog
|
||||
|
||||
Known problems:
|
||||
|
||||
1. Plugin's test fixtures do not load properly for non-edge postgres, invalidating the tests.
|
||||
2. quote_value() hack is stupid.
|
||||
1
tracks/vendor/plugins/has_many_polymorphs/init.rb
vendored
Normal file
1
tracks/vendor/plugins/has_many_polymorphs/init.rb
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
require 'has_many_polymorphs'
|
||||
581
tracks/vendor/plugins/has_many_polymorphs/lib/has_many_polymorphs.rb
vendored
Normal file
581
tracks/vendor/plugins/has_many_polymorphs/lib/has_many_polymorphs.rb
vendored
Normal file
|
|
@ -0,0 +1,581 @@
|
|||
|
||||
# self-referential, polymorphic has_many :through plugin
|
||||
# http://blog.evanweaver.com/articles/2006/06/02/has_many_polymorphs
|
||||
# operates via magic dust, and courage
|
||||
|
||||
if defined? Rails::Configuration
|
||||
class Rails::Configuration
|
||||
def has_many_polymorphs_cache_classes= *args
|
||||
::ActiveRecord::Associations::ClassMethods.has_many_polymorphs_cache_classes = *args
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module ActiveRecord
|
||||
|
||||
if ENV['RAILS_ENV'] =~ /development|test/ and ENV['USER'] == 'eweaver'
|
||||
# enable this condition to get awesome association debugging
|
||||
# you will get a folder "generated_models" in the current dir containing valid Ruby files
|
||||
# explaining all ActiveRecord relationships set up by the plugin, as well as listing the
|
||||
# line in the plugin that made each particular macro call
|
||||
class << Base
|
||||
COLLECTION_METHODS = [:belongs_to, :has_many, :has_and_belongs_to_many, :has_one].each do |method_name|
|
||||
alias_method "original_#{method_name}".to_sym, method_name
|
||||
undef_method method_name
|
||||
end
|
||||
|
||||
unless defined? GENERATED_CODE_DIR
|
||||
# automatic code generation for debugging... bitches
|
||||
GENERATED_CODE_DIR = "generated_models"
|
||||
system "rm -rf #{GENERATED_CODE_DIR}"
|
||||
Dir.mkdir GENERATED_CODE_DIR
|
||||
|
||||
alias :original_method_missing :method_missing
|
||||
def method_missing(method_name, *args, &block)
|
||||
if COLLECTION_METHODS.include? method_name.to_sym
|
||||
Dir.chdir GENERATED_CODE_DIR do
|
||||
filename = "#{ActiveRecord::Associations::ClassMethods.demodulate(self.name.underscore)}.rb"
|
||||
contents = File.open(filename).read rescue "\nclass #{self.name}\n\nend\n"
|
||||
line = caller[1][/\:(\d+)\:/, 1]
|
||||
contents[-5..-5] = "\n #{method_name} #{args[0..-2].inspect[1..-2]},\n #{args[-1].inspect[1..-2].gsub(" :", "\n :").gsub("=>", " => ")}\n#{ block ? " #{block.inspect.sub(/\@.*\//, '@')}\n" : ""} # called from line #{line}\n\n"
|
||||
File.open(filename, "w") do |file|
|
||||
file.puts contents
|
||||
end
|
||||
end
|
||||
# doesn't handle blocks
|
||||
self.send("original_#{method_name}", *args, &block)
|
||||
else
|
||||
self.send(:original_method_missing, method_name, *args, &block)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# and we want to track the reloader's shenanigans
|
||||
(::Dependencies.log_activity = true) rescue nil
|
||||
end
|
||||
|
||||
module Associations
|
||||
module ClassMethods
|
||||
mattr_accessor :has_many_polymorphs_cache_classes
|
||||
|
||||
def acts_as_double_polymorphic_join opts
|
||||
raise RuntimeError, "Couldn't understand #{opts.inspect} options in acts_as_double_polymorphic_join. Please only specify the two relationships and their member classes; there are no options to set. " unless opts.length == 2
|
||||
|
||||
join_name = self.name.tableize.to_sym
|
||||
opts.each do |polymorphs, children|
|
||||
parent_hash_key = (opts.keys - [polymorphs]).first # parents are the entries in the _other_ children array
|
||||
|
||||
begin
|
||||
parent_foreign_key = self.reflect_on_association(parent_hash_key.to_s.singularize.to_sym).primary_key_name
|
||||
rescue NoMethodError
|
||||
raise RuntimeError, "Couldn't find 'belongs_to' association for :#{parent_hash_key.to_s.singularize} in #{self.name}." unless parent_foreign_key
|
||||
end
|
||||
|
||||
parents = opts[parent_hash_key]
|
||||
conflicts = (children & parents) # set intersection
|
||||
parents.each do |parent_name|
|
||||
|
||||
parent_class = parent_name.to_s.classify.constantize
|
||||
reverse_polymorph = parent_hash_key.to_s.singularize
|
||||
polymorph = polymorphs.to_s.singularize
|
||||
|
||||
parent_class.send(:has_many_polymorphs,
|
||||
polymorphs, {:double => true,
|
||||
:from => children,
|
||||
:as => parent_hash_key.to_s.singularize.to_sym,
|
||||
:through => join_name,
|
||||
:dependent => :destroy,
|
||||
:foreign_key => parent_foreign_key,
|
||||
:foreign_type_key => parent_foreign_key.to_s.sub(/_id$/, '_type'),
|
||||
:reverse_polymorph => reverse_polymorph,
|
||||
:conflicts => conflicts,
|
||||
:rename_individual_collections => false})
|
||||
|
||||
if conflicts.include? parent_name
|
||||
# unify the alternate sides of the conflicting children
|
||||
(conflicts).each do |method_name|
|
||||
unless parent_class.instance_methods.include?(method_name)
|
||||
parent_class.send(:define_method, method_name) do
|
||||
(self.send("#{reverse_polymorph}_#{method_name}") +
|
||||
self.send("#{polymorph}_#{method_name}")).freeze
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# unify the join model
|
||||
unless parent_class.instance_methods.include?(join_name)
|
||||
parent_class.send(:define_method, join_name) do
|
||||
(self.send("#{join_name}_as_#{reverse_polymorph}") +
|
||||
self.send("#{join_name}_as_#{polymorph}")).freeze
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def has_many_polymorphs(polymorphs, options, &block)
|
||||
options.assert_valid_keys(:from, :acts_as, :as, :through, :foreign_key, :dependent, :double,
|
||||
:rename_individual_collections, :foreign_type_key, :reverse_polymorph, :conflicts)
|
||||
|
||||
# the way this deals with extra parameters to the associations could use some work
|
||||
options[:as] ||= options[:acts_as] ||= self.table_name.singularize.to_sym
|
||||
|
||||
# foreign keys follow the table name, not the class name in Rails 2.0
|
||||
options[:foreign_key] ||= "#{options[:as].to_s}_id"
|
||||
|
||||
# no conflicts by default
|
||||
options[:conflicts] ||= []
|
||||
|
||||
# construct the join table name
|
||||
options[:through] ||= join_table((options[:as].to_s.pluralize or self.table_name), polymorphs)
|
||||
if options[:reverse_polymorph]
|
||||
options[:through_with_reverse_polymorph] = "#{options[:through]}_as_#{options[:reverse_polymorph]}".to_sym
|
||||
else
|
||||
options[:through_with_reverse_polymorph] = options[:through]
|
||||
end
|
||||
|
||||
options[:join_class_name] ||= options[:through].to_s.classify
|
||||
|
||||
# the class must have_many on the join_table
|
||||
opts = {:foreign_key => options[:foreign_key], :dependent => options[:dependent],
|
||||
:class_name => options[:join_class_name]}
|
||||
if options[:foreign_type_key]
|
||||
opts[:conditions] = "#{options[:foreign_type_key]} = #{quote_value self.base_class.name}"
|
||||
end
|
||||
|
||||
has_many demodulate(options[:through_with_reverse_polymorph]), opts
|
||||
|
||||
polymorph = polymorphs.to_s.singularize.to_sym
|
||||
|
||||
# add the base_class method to the join_table so that STI will work transparently
|
||||
inject_before_save_into_join_table(options[:join_class_name], polymorph)
|
||||
|
||||
# get some reusable info
|
||||
children, child_associations = {}, {}
|
||||
options[:from].each do |child_plural|
|
||||
children[child_plural] = child_plural.to_s.singularize.to_sym
|
||||
child_associations[child_plural] = (options[:rename_individual_collections] ? "#{polymorph}_#{child_plural}".to_sym : child_plural)
|
||||
end
|
||||
|
||||
# get our models out of the reloadable lists, if requested
|
||||
if self.has_many_polymorphs_cache_classes
|
||||
klasses = [self.name, options[:join_class_name], *children.values.map{|x| x.to_s.classify}]
|
||||
klasses += basify_sti_classnames(klasses).keys.to_a.compact.uniq.map{|x| x.to_s.classify}
|
||||
klasses.uniq!
|
||||
klasses.each {|s| logger.debug "Ejecting #{s.inspect} from the autoload lists"}
|
||||
begin
|
||||
Dependencies.autoloaded_constants -= klasses
|
||||
Dependencies.explicitly_unloadable_constants -= klasses
|
||||
rescue NoMethodError
|
||||
raise "Rails 1.2.0 or later is required to set config.has_many_polymorphs_cache_classes = true"
|
||||
end
|
||||
end
|
||||
|
||||
# auto-inject individually named associations for the children into the join model
|
||||
create_virtual_associations_for_join_to_individual_children(children, polymorph, options)
|
||||
|
||||
# iterate through the polymorphic children, running the parent class's :has_many on each one
|
||||
create_has_many_through_associations_for_parent_to_children(children, child_associations, polymorphs, polymorph, options)
|
||||
|
||||
# auto-inject the regular polymorphic associations into the child classes
|
||||
create_has_many_through_associations_for_children_to_parent(children, polymorph, options)
|
||||
|
||||
create_general_collection_association_for_parent(polymorphs, polymorph, basify_sti_classnames(children), options, &block)
|
||||
end
|
||||
|
||||
def self.demodulate(s)
|
||||
s.to_s.gsub('/', '_').to_sym
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def demodulate(s)
|
||||
ActiveRecord::Associations::ClassMethods.demodulate(s)
|
||||
end
|
||||
|
||||
def basify_sti_classnames(hash)
|
||||
# this blows
|
||||
result = {}
|
||||
hash.each do |plural, singular|
|
||||
klass = plural.to_s.classify.constantize
|
||||
if klass != klass.base_class
|
||||
result[klass.base_class.table_name.to_sym] = klass.base_class.table_name.singularize.to_sym
|
||||
else
|
||||
result[plural] = singular
|
||||
end
|
||||
end
|
||||
result
|
||||
end
|
||||
|
||||
def inject_before_save_into_join_table(join_class_name, polymorph)
|
||||
sti_hook = "sti_class_rewrite"
|
||||
rewrite_procedure = %[
|
||||
self.send(:#{polymorph}_type=, self.#{polymorph}_type.constantize.base_class.name)
|
||||
]
|
||||
|
||||
# this also blows, and should be abstracted. alias_method_chain is not enough.
|
||||
join_class_name.constantize.class_eval %[
|
||||
unless instance_methods.include? "before_save_with_#{sti_hook}"
|
||||
if instance_methods.include? "before_save"
|
||||
alias_method :before_save_without_#{sti_hook}, :before_save
|
||||
def before_save_with_#{sti_hook}
|
||||
before_save_without_#{sti_hook}
|
||||
#{rewrite_procedure}
|
||||
end
|
||||
else
|
||||
def before_save_with_#{sti_hook}
|
||||
#{rewrite_procedure}
|
||||
end
|
||||
end
|
||||
alias_method :before_save, :before_save_with_#{sti_hook}
|
||||
end
|
||||
]
|
||||
|
||||
end
|
||||
|
||||
def create_virtual_associations_for_join_to_individual_children(children, polymorph, options)
|
||||
children.each do |child_plural, child|
|
||||
options[:join_class_name].constantize.instance_eval do
|
||||
|
||||
association_name = child.to_s
|
||||
association_name += "_as_#{polymorph}" if options[:conflicts].include?(child_plural)
|
||||
association = demodulate(association_name)
|
||||
|
||||
opts = {:class_name => child.to_s.classify,
|
||||
:foreign_key => "#{polymorph}_id" }
|
||||
|
||||
unless self.reflect_on_all_associations.map(&:name).include? association
|
||||
belongs_to association, opts
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def create_has_many_through_associations_for_children_to_parent(children, polymorph, options)
|
||||
children.each do |child_plural, child|
|
||||
|
||||
if child == options[:as]
|
||||
raise RuntimeError, "You can't have a self-referential polymorphic has_many :through without renaming the non-polymorphic foreign key in the join model."
|
||||
end
|
||||
|
||||
parent = self
|
||||
child.to_s.classify.constantize.instance_eval do
|
||||
|
||||
# this shouldn't be called at all during doubles; there is no way to traverse to a
|
||||
# double polymorphic parent (XXX is that right?)
|
||||
unless options[:double] or options[:conflicts].include? self.name.tableize.to_sym
|
||||
begin
|
||||
require_dependency parent.name.underscore # XXX why is this here?
|
||||
rescue MissingSourceFile
|
||||
end
|
||||
|
||||
# the join table
|
||||
through = demodulate(options[:through_with_reverse_polymorph]).to_s
|
||||
through += "_as_child" if parent == self
|
||||
through = through.to_sym
|
||||
|
||||
has_many through, :as => polymorph,
|
||||
:class_name => options[:through].to_s.classify,
|
||||
:dependent => options[:dependent]
|
||||
|
||||
association = options[:as].to_s.pluralize
|
||||
association += "_of_#{polymorph.to_s.pluralize}" if options[:rename_individual_collections] # XXX check this
|
||||
|
||||
# the polymorphic parent association
|
||||
has_many association.to_sym, :through => through,
|
||||
:class_name => parent.name,
|
||||
:source => options[:as],
|
||||
:foreign_key => options[:foreign_key]
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def create_has_many_through_associations_for_parent_to_children(children, child_associations, polymorphs, polymorph, options)
|
||||
children.each do |child_plural, child|
|
||||
#puts ":source => #{child}"
|
||||
association = demodulate(child_associations[child_plural]).to_s
|
||||
source = demodulate(child).to_s
|
||||
|
||||
if options[:conflicts].include? child_plural
|
||||
# XXX what?
|
||||
association = "#{polymorph}_#{association}" if options[:conflicts].include? self.name.tableize.to_sym
|
||||
source += "_as_#{polymorph}"
|
||||
end
|
||||
|
||||
# activerecord is broken when you try to anonymously extend an association in a namespaced model,
|
||||
extension = self.class_eval %[
|
||||
module #{association.classify + "AssociationExtension"}
|
||||
def push *args
|
||||
proxy_owner.send(:#{polymorphs}).send(:push, *args).select{|x| x.is_a? #{child.to_s.classify}}
|
||||
end
|
||||
alias :<< :push
|
||||
def delete *args
|
||||
proxy_owner.send(:#{polymorphs}).send(:delete, *args)
|
||||
end
|
||||
def clear
|
||||
proxy_owner.send(:#{polymorphs}).send(:clear, #{child.to_s.classify})
|
||||
end
|
||||
self # required
|
||||
end]
|
||||
|
||||
has_many association.to_sym, :through => demodulate(options[:through_with_reverse_polymorph]),
|
||||
:source => source.to_sym,
|
||||
:conditions => ["#{options[:join_class_name].constantize.table_name}.#{polymorph}_type = ?", child.to_s.classify.constantize.base_class.name],
|
||||
:extend => extension
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
def create_general_collection_association_for_parent(collection_name, polymorph, children, options, &block)
|
||||
# we need to explicitly rename all the columns because we are fetching all the children objects at once.
|
||||
# if multiple objects have a 'title' column, for instance, there will be a collision and we will potentially
|
||||
# lose data. if we alias the fields and then break them up later, there are no collisions.
|
||||
join_model = options[:through].to_s.classify.constantize
|
||||
|
||||
# figure out what fields we wanna grab
|
||||
select_fields = []
|
||||
children.each do |plural, singular|
|
||||
klass = plural.to_s.classify.constantize
|
||||
klass.columns.map(&:name).each do |name|
|
||||
select_fields << "#{klass.table_name}.#{name} as #{demodulate plural}_#{name}"
|
||||
end
|
||||
end
|
||||
|
||||
# now get the join model fields
|
||||
join_model.columns.map(&:name).each do |name|
|
||||
select_fields << "#{join_model.table_name}.#{name} as #{join_model.table_name}_#{name}"
|
||||
end
|
||||
|
||||
from_table = self.table_name
|
||||
left_joins = children.keys.map do |n|
|
||||
klass = n.to_s.classify.constantize
|
||||
"LEFT JOIN #{klass.table_name} ON #{join_model.table_name}.#{polymorph}_id = #{klass.table_name}.#{klass.primary_key} AND #{join_model.table_name}.#{polymorph}_type = '#{n.to_s.classify}'"
|
||||
end
|
||||
|
||||
sql_query = 'SELECT ' + select_fields.join(', ') + " FROM #{join_model.table_name}" +
|
||||
"\nJOIN #{from_table} as polymorphic_parent ON #{join_model.table_name}.#{options[:foreign_key]} = polymorphic_parent.#{self.primary_key}\n" +
|
||||
left_joins.join("\n") + "\nWHERE "
|
||||
|
||||
if options[:foreign_type_key]
|
||||
sql_query +="#{join_model.table_name}.#{options[:foreign_type_key]} = #{quote_value self.base_class.name} AND "
|
||||
end
|
||||
|
||||
# for sqlite3 you have to reference the left-most table in WHERE clauses or rows with NULL
|
||||
# join results sometimes get silently dropped. it's stupid.
|
||||
sql_query += "#{join_model.table_name}.#{options[:foreign_key]} "
|
||||
#puts("Built collection property query:\n #{sql_query}")
|
||||
|
||||
class_eval do
|
||||
attr_accessor "#{collection_name}_cache"
|
||||
cattr_accessor "#{collection_name}_options"
|
||||
|
||||
define_method(collection_name) do
|
||||
if collection_name_cache = instance_variable_get("@#{collection_name}_cache")
|
||||
#puts("Cache hit on #{collection_name}")
|
||||
collection_name_cache
|
||||
else
|
||||
#puts("Cache miss on #{collection_name}")
|
||||
rows = connection.select_all("#{sql_query}" + (new_record? ? "IS NULL" : "= #{self.id}"))
|
||||
# this gives us a hash with keys for each object type
|
||||
objectified = objectify_polymorphic_array(rows, "#{join_model}", "#{polymorph}_type")
|
||||
# locally cache the different object types found
|
||||
# this doesn't work... yet.
|
||||
objectified.each do |key, array|
|
||||
instance_variable_set("@#{ActiveRecord::Associations::ClassMethods.demodulate(key)}", array)
|
||||
end
|
||||
proxy_object = HasManyPolymorphsProxyCollection.new(objectified[:all], self, send("#{collection_name}_options"))
|
||||
(class << proxy_object; self end).send(:class_eval, &block) if block_given?
|
||||
instance_variable_set("@#{collection_name}_cache", proxy_object)
|
||||
end
|
||||
end
|
||||
|
||||
# in order not to break tests, see if we have been defined already
|
||||
unless instance_methods.include? "reload_with_#{collection_name}"
|
||||
define_method("reload_with_#{collection_name}") do
|
||||
send("reload_without_#{collection_name}")
|
||||
instance_variable_set("@#{collection_name}_cache", nil)
|
||||
self
|
||||
end
|
||||
|
||||
alias_method "reload_without_#{collection_name}", :reload
|
||||
alias_method :reload, "reload_with_#{collection_name}"
|
||||
end
|
||||
end
|
||||
|
||||
send("#{collection_name}_options=",
|
||||
options.merge(:collection_name => collection_name,
|
||||
:type_key => "#{polymorph}_type",
|
||||
:id_key => "#{polymorph}_id"))
|
||||
|
||||
# puts("Defined the collection proxy.\n#{collection_name}\n")
|
||||
end
|
||||
|
||||
def join_table(a, b)
|
||||
[a.to_s, b.to_s].sort.join("_").to_sym
|
||||
end
|
||||
|
||||
unless self.respond_to? :quote_value
|
||||
# hack it in (very badly) for Rails 1.1.6 people
|
||||
def quote_value s
|
||||
"'#{s.inspect[1..-2]}'"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
################################################
|
||||
|
||||
# decided to leave this alone unless it becomes clear that there is some benefit
|
||||
# in deriving from AssociationProxy
|
||||
#
|
||||
# the benefit would be custom finders on the collection, perhaps...
|
||||
class HasManyPolymorphsProxyCollection < Array
|
||||
|
||||
alias :array_delete :delete
|
||||
alias :array_push :push
|
||||
alias :count :length
|
||||
|
||||
def initialize(contents, parent, options)
|
||||
@parent = parent
|
||||
@options = options
|
||||
@join_class = options[:join_class_name].constantize
|
||||
return if contents.blank?
|
||||
super(contents)
|
||||
end
|
||||
|
||||
def push(objs, args={})
|
||||
objs = [objs] unless objs.is_a? Array
|
||||
|
||||
objs.each do |obj|
|
||||
data = {@options[:foreign_key] => @parent.id,
|
||||
@options[:type_key] => obj.class.base_class.to_s, @options[:id_key] => obj.id}
|
||||
data.merge!({@options[:foreign_type_key] => @parent.class.base_class.to_s}) if @options[:foreign_type_key] # for double polymorphs
|
||||
conditions_string = data.keys.map(&:to_s).push("").join(" = ? AND ")[0..-6]
|
||||
if @join_class.find(:first, :conditions => [conditions_string] + data.values).blank?
|
||||
@join_class.new(data).save!
|
||||
end
|
||||
end
|
||||
|
||||
if args[:reload]
|
||||
reload
|
||||
else
|
||||
# we have to do this funky stuff instead of just array difference because +/.uniq returns a regular array,
|
||||
# which doesn't have our special methods and configuration anymore
|
||||
unless (difference = objs - collection).blank?
|
||||
@parent.send("#{@options[:collection_name]}_cache=".to_sym, collection.array_push(*difference))
|
||||
end
|
||||
end
|
||||
|
||||
@parent.send(@options[:collection_name])
|
||||
end
|
||||
|
||||
alias :<< :push
|
||||
|
||||
def delete(objs, args={})
|
||||
|
||||
if objs
|
||||
objs = [objs] unless objs.is_a? Array
|
||||
elsif args[:clear]
|
||||
objs = collection
|
||||
objs = objs.select{|obj| obj.is_a? args[:klass]} if args[:klass]
|
||||
else
|
||||
raise RuntimeError, "Invalid delete parameters (has_many_polymorphs)."
|
||||
end
|
||||
|
||||
records = []
|
||||
objs.each do |obj|
|
||||
records += join_records.select do |record|
|
||||
record.send(@options[:type_key]) == obj.class.base_class.to_s and
|
||||
record.send(@options[:id_key]) == obj.id
|
||||
end
|
||||
end
|
||||
|
||||
reload if args[:reload]
|
||||
unless records.blank?
|
||||
records.map(&:destroy)
|
||||
# XXX could be faster if we reversed the loops
|
||||
deleted_items = collection.select do |item|
|
||||
records.select {|join_record|
|
||||
join_record.send(@options[:type_key]) == item.class.base_class.name and
|
||||
join_record.send(@options[:id_key]) == item.id
|
||||
}.length > 0
|
||||
end
|
||||
# keep the cache fresh, while we're at it. see comment in .push
|
||||
deleted_items.each { |item| collection.array_delete(item) }
|
||||
@parent.send("#{@options[:collection_name]}_cache=", collection)
|
||||
|
||||
return deleted_items unless deleted_items.empty?
|
||||
end
|
||||
nil
|
||||
end
|
||||
|
||||
def clear(klass = nil)
|
||||
result = delete(nil, :clear => true, :klass => klass)
|
||||
return result if result
|
||||
collection
|
||||
end
|
||||
|
||||
def reload
|
||||
# reset the cache, postponing reloading from the db until we really need it
|
||||
@parent.reload
|
||||
end
|
||||
|
||||
private
|
||||
def join_records
|
||||
@parent.send(ActiveRecord::Associations::ClassMethods.demodulate(@options[:through]))
|
||||
end
|
||||
|
||||
def collection
|
||||
@parent.send(@options[:collection_name])
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
class Base
|
||||
# turns an array of hashes (db rows) into a hash consisting of :all (array of everything) and
|
||||
# a hash key for each class type it finds, e.g. :posts and :comments
|
||||
private
|
||||
def objectify_polymorphic_array(array, join_model, type_field)
|
||||
join_model = join_model.constantize
|
||||
arrays_hash = {}
|
||||
|
||||
array.each do |element|
|
||||
klass = element["#{join_model.table_name}_#{type_field}"].constantize
|
||||
association = ActiveRecord::Associations::ClassMethods.demodulate(klass.name.pluralize.underscore.downcase)
|
||||
hash = {}
|
||||
|
||||
# puts "Class #{klass.inspect}"
|
||||
# puts "Association name: #{association.inspect}"
|
||||
|
||||
element.each do |key, value|
|
||||
# puts "key #{key} - value #{value.inspect}"
|
||||
if key =~ /^#{association}_(.+)/
|
||||
hash[$1] = value
|
||||
# puts "#{$1.inspect} assigned #{value.inspect}"
|
||||
end
|
||||
end
|
||||
|
||||
object = klass.instantiate(hash)
|
||||
|
||||
arrays_hash[:all] ||= []
|
||||
arrays_hash[association] ||= []
|
||||
arrays_hash[:all] << object
|
||||
arrays_hash[association] << object
|
||||
end
|
||||
|
||||
arrays_hash
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
#require 'ruby-debug'
|
||||
#Debugger.start
|
||||
|
||||
8
tracks/vendor/plugins/has_many_polymorphs/test/fixtures/aquatic/fish.yml
vendored
Normal file
8
tracks/vendor/plugins/has_many_polymorphs/test/fixtures/aquatic/fish.yml
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
swimmy:
|
||||
id: 1
|
||||
name: Swimmy
|
||||
speed: 10
|
||||
jaws:
|
||||
id: 2
|
||||
name: Jaws
|
||||
speed: 20
|
||||
0
tracks/vendor/plugins/has_many_polymorphs/test/fixtures/aquatic/little_whale_pupils.yml
vendored
Normal file
0
tracks/vendor/plugins/has_many_polymorphs/test/fixtures/aquatic/little_whale_pupils.yml
vendored
Normal file
3
tracks/vendor/plugins/has_many_polymorphs/test/fixtures/aquatic/whales.yml
vendored
Normal file
3
tracks/vendor/plugins/has_many_polymorphs/test/fixtures/aquatic/whales.yml
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
shamu:
|
||||
id: 1
|
||||
name: Shamu
|
||||
6
tracks/vendor/plugins/has_many_polymorphs/test/fixtures/bow_wows.yml
vendored
Normal file
6
tracks/vendor/plugins/has_many_polymorphs/test/fixtures/bow_wows.yml
vendored
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
rover:
|
||||
id: 1
|
||||
name: Rover
|
||||
spot:
|
||||
id: 2
|
||||
name: Spot
|
||||
8
tracks/vendor/plugins/has_many_polymorphs/test/fixtures/cats.yml
vendored
Normal file
8
tracks/vendor/plugins/has_many_polymorphs/test/fixtures/cats.yml
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
chloe:
|
||||
id: 1
|
||||
cat_type: Kitten
|
||||
name: Chloe
|
||||
alice:
|
||||
id: 2
|
||||
cat_type: Kitten
|
||||
name: Alice
|
||||
0
tracks/vendor/plugins/has_many_polymorphs/test/fixtures/eaters_foodstuffs.yml
vendored
Normal file
0
tracks/vendor/plugins/has_many_polymorphs/test/fixtures/eaters_foodstuffs.yml
vendored
Normal file
3
tracks/vendor/plugins/has_many_polymorphs/test/fixtures/frogs.yml
vendored
Normal file
3
tracks/vendor/plugins/has_many_polymorphs/test/fixtures/frogs.yml
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
froggy:
|
||||
id: 1
|
||||
name: Froggy
|
||||
0
tracks/vendor/plugins/has_many_polymorphs/test/fixtures/keep_your_enemies_close.yml
vendored
Normal file
0
tracks/vendor/plugins/has_many_polymorphs/test/fixtures/keep_your_enemies_close.yml
vendored
Normal file
6
tracks/vendor/plugins/has_many_polymorphs/test/fixtures/petfoods.yml
vendored
Normal file
6
tracks/vendor/plugins/has_many_polymorphs/test/fixtures/petfoods.yml
vendored
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
kibbles:
|
||||
the_petfood_primary_key: 1
|
||||
name: Kibbles
|
||||
bits:
|
||||
the_petfood_primary_key: 2
|
||||
name: Bits
|
||||
6
tracks/vendor/plugins/has_many_polymorphs/test/fixtures/wild_boars.yml
vendored
Normal file
6
tracks/vendor/plugins/has_many_polymorphs/test/fixtures/wild_boars.yml
vendored
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
puma:
|
||||
id: 1
|
||||
name: Puma
|
||||
jacrazy:
|
||||
id: 2
|
||||
name: Jacrazy
|
||||
4
tracks/vendor/plugins/has_many_polymorphs/test/models/aquatic/fish.rb
vendored
Normal file
4
tracks/vendor/plugins/has_many_polymorphs/test/models/aquatic/fish.rb
vendored
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
class Aquatic::Fish < ActiveRecord::Base
|
||||
# attr_accessor :after_find_test, :after_initialize_test
|
||||
end
|
||||
|
||||
7
tracks/vendor/plugins/has_many_polymorphs/test/models/aquatic/pupils_whale.rb
vendored
Normal file
7
tracks/vendor/plugins/has_many_polymorphs/test/models/aquatic/pupils_whale.rb
vendored
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
|
||||
class Aquatic::PupilsWhale < ActiveRecord::Base
|
||||
set_table_name "little_whale_pupils"
|
||||
belongs_to :whale, :class_name => "Aquatic::Whale", :foreign_key => "whale_id"
|
||||
belongs_to :aquatic_pupil, :polymorphic => true
|
||||
end
|
||||
|
||||
11
tracks/vendor/plugins/has_many_polymorphs/test/models/aquatic/whale.rb
vendored
Normal file
11
tracks/vendor/plugins/has_many_polymorphs/test/models/aquatic/whale.rb
vendored
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
# see http://dev.rubyonrails.org/ticket/5935
|
||||
module Aquatic; end
|
||||
require 'aquatic/fish'
|
||||
require 'aquatic/pupils_whale'
|
||||
|
||||
class Aquatic::Whale < ActiveRecord::Base
|
||||
has_many_polymorphs(:aquatic_pupils, :from => [:dogs, :"aquatic/fish"],
|
||||
:through => "aquatic/pupils_whales") do
|
||||
def blow; "result"; end
|
||||
end
|
||||
end
|
||||
13
tracks/vendor/plugins/has_many_polymorphs/test/models/beautiful_fight_relationship.rb
vendored
Normal file
13
tracks/vendor/plugins/has_many_polymorphs/test/models/beautiful_fight_relationship.rb
vendored
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
|
||||
class BeautifulFightRelationship < ActiveRecord::Base
|
||||
set_table_name 'keep_your_enemies_close'
|
||||
|
||||
belongs_to :enemy, :polymorphic => true
|
||||
belongs_to :protector, :polymorphic => true
|
||||
# polymorphic relationships with column names different from the relationship name
|
||||
# are not supported by Rails
|
||||
|
||||
acts_as_double_polymorphic_join :enemies => [:dogs, :kittens, :frogs],
|
||||
:protectors => [:wild_boars, :kittens, :"aquatic/fish", :dogs]
|
||||
end
|
||||
|
||||
5
tracks/vendor/plugins/has_many_polymorphs/test/models/cat.rb
vendored
Normal file
5
tracks/vendor/plugins/has_many_polymorphs/test/models/cat.rb
vendored
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
class Cat < ActiveRecord::Base
|
||||
# STI base class
|
||||
self.inheritance_column = 'cat_type'
|
||||
end
|
||||
|
||||
16
tracks/vendor/plugins/has_many_polymorphs/test/models/dog.rb
vendored
Normal file
16
tracks/vendor/plugins/has_many_polymorphs/test/models/dog.rb
vendored
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
class Dog < ActiveRecord::Base
|
||||
attr_accessor :after_find_test, :after_initialize_test
|
||||
|
||||
set_table_name "bow_wows"
|
||||
|
||||
def after_find
|
||||
@after_find_test = true
|
||||
# puts "After find called on #{name}."
|
||||
end
|
||||
|
||||
def after_initialize
|
||||
@after_initialize_test = true
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
10
tracks/vendor/plugins/has_many_polymorphs/test/models/eaters_foodstuff.rb
vendored
Normal file
10
tracks/vendor/plugins/has_many_polymorphs/test/models/eaters_foodstuff.rb
vendored
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
|
||||
class EatersFoodstuff < ActiveRecord::Base
|
||||
belongs_to :foodstuff, :class_name => "Petfood", :foreign_key => "foodstuff_id"
|
||||
belongs_to :eater, :polymorphic => true
|
||||
|
||||
def before_save
|
||||
self.some_attribute = 3
|
||||
end
|
||||
end
|
||||
|
||||
4
tracks/vendor/plugins/has_many_polymorphs/test/models/frog.rb
vendored
Normal file
4
tracks/vendor/plugins/has_many_polymorphs/test/models/frog.rb
vendored
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
class Frog < ActiveRecord::Base
|
||||
|
||||
end
|
||||
|
||||
3
tracks/vendor/plugins/has_many_polymorphs/test/models/kitten.rb
vendored
Normal file
3
tracks/vendor/plugins/has_many_polymorphs/test/models/kitten.rb
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
class Kitten < Cat
|
||||
# has_many :eaters_parents, :dependent => true, :as => 'eater'
|
||||
end
|
||||
21
tracks/vendor/plugins/has_many_polymorphs/test/models/petfood.rb
vendored
Normal file
21
tracks/vendor/plugins/has_many_polymorphs/test/models/petfood.rb
vendored
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
# see http://dev.rubyonrails.org/ticket/5935
|
||||
require 'eaters_foodstuff'
|
||||
require 'petfood'
|
||||
require 'cat'
|
||||
module Aquatic; end
|
||||
require 'aquatic/fish'
|
||||
require 'dog'
|
||||
require 'wild_boar'
|
||||
require 'kitten'
|
||||
require 'tabby'
|
||||
|
||||
class Petfood < ActiveRecord::Base
|
||||
set_primary_key 'the_petfood_primary_key'
|
||||
has_many_polymorphs :eaters,
|
||||
:from => [:dogs, :petfoods, :wild_boars, :kittens,
|
||||
:tabbies, :"aquatic/fish"],
|
||||
:dependent => :destroy,
|
||||
:rename_individual_collections => true,
|
||||
:acts_as => :foodstuff,
|
||||
:foreign_key => "foodstuff_id"
|
||||
end
|
||||
2
tracks/vendor/plugins/has_many_polymorphs/test/models/tabby.rb
vendored
Normal file
2
tracks/vendor/plugins/has_many_polymorphs/test/models/tabby.rb
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
class Tabby < Cat
|
||||
end
|
||||
3
tracks/vendor/plugins/has_many_polymorphs/test/models/wild_boar.rb
vendored
Normal file
3
tracks/vendor/plugins/has_many_polymorphs/test/models/wild_boar.rb
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
class WildBoar < ActiveRecord::Base
|
||||
end
|
||||
|
||||
52
tracks/vendor/plugins/has_many_polymorphs/test/schema.rb
vendored
Normal file
52
tracks/vendor/plugins/has_many_polymorphs/test/schema.rb
vendored
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
ActiveRecord::Schema.define(:version => 0) do
|
||||
create_table :petfoods, :force => true, :primary_key => :the_petfood_primary_key do |t|
|
||||
t.column :name, :string
|
||||
end
|
||||
|
||||
create_table :bow_wows, :force => true do |t|
|
||||
t.column :name, :string
|
||||
end
|
||||
|
||||
create_table :cats, :force => true do |t|
|
||||
t.column :name, :string
|
||||
t.column :cat_type, :string
|
||||
end
|
||||
|
||||
create_table :frogs, :force => true do |t|
|
||||
t.column :name, :string
|
||||
end
|
||||
|
||||
create_table :wild_boars, :force => true do |t|
|
||||
t.column :name, :string
|
||||
end
|
||||
|
||||
create_table :eaters_foodstuffs, :force => true do |t|
|
||||
t.column :foodstuff_id, :integer
|
||||
t.column :eater_id, :integer
|
||||
t.column :some_attribute, :integer, :default => 0
|
||||
t.column :eater_type, :string
|
||||
end
|
||||
|
||||
create_table :fish, :force => true do |t|
|
||||
t.column :name, :string
|
||||
t.column :speed, :integer
|
||||
end
|
||||
|
||||
create_table :whales, :force => true do |t|
|
||||
t.column :name, :string
|
||||
end
|
||||
|
||||
create_table :little_whale_pupils, :force => true do |t|
|
||||
t.column :whale_id, :integer
|
||||
t.column :aquatic_pupil_id, :integer
|
||||
t.column :aquatic_pupil_type, :string
|
||||
end
|
||||
|
||||
create_table :keep_your_enemies_close, :force => true do |t|
|
||||
t.column :enemy_id, :integer
|
||||
t.column :enemy_type, :string
|
||||
t.column :protector_id, :integer
|
||||
t.column :protector_type, :string
|
||||
end
|
||||
|
||||
end
|
||||
23
tracks/vendor/plugins/has_many_polymorphs/test/test_helper.rb
vendored
Normal file
23
tracks/vendor/plugins/has_many_polymorphs/test/test_helper.rb
vendored
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
require 'pathname'
|
||||
# default test helper
|
||||
begin
|
||||
require File.dirname(__FILE__) + '/../../../../test/test_helper'
|
||||
rescue LoadError
|
||||
require '~/projects/miscellaneous/cookbook/test/test_helper'
|
||||
end
|
||||
|
||||
Inflector.inflections {|i| i.irregular 'fish', 'fish' }
|
||||
|
||||
# fixtures
|
||||
$LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/")
|
||||
# models
|
||||
$LOAD_PATH.unshift("#{Pathname.new(__FILE__).dirname.to_s}/models")
|
||||
|
||||
class Test::Unit::TestCase
|
||||
self.use_transactional_fixtures = true # must stay true for tests to run on postgres or sqlite3
|
||||
self.use_instantiated_fixtures = false
|
||||
end
|
||||
|
||||
# test schema
|
||||
load(File.dirname(__FILE__) + "/schema.rb")
|
||||
|
||||
487
tracks/vendor/plugins/has_many_polymorphs/test/unit/polymorph_test.rb
vendored
Normal file
487
tracks/vendor/plugins/has_many_polymorphs/test/unit/polymorph_test.rb
vendored
Normal file
|
|
@ -0,0 +1,487 @@
|
|||
require File.dirname(__FILE__) + '/../test_helper'
|
||||
|
||||
class PolymorphTest < Test::Unit::TestCase
|
||||
|
||||
fixtures :cats, :bow_wows, :frogs, :wild_boars, :eaters_foodstuffs, :petfoods,
|
||||
:"aquatic/fish", :"aquatic/whales", :"aquatic/little_whale_pupils",
|
||||
:keep_your_enemies_close
|
||||
require 'beautiful_fight_relationship'
|
||||
|
||||
# to-do: finder queries on the collection
|
||||
# order-mask column on the join table for polymorphic order
|
||||
# rework load order so you could push and pop without ever loading the whole collection
|
||||
# so that limit works in a sane way
|
||||
|
||||
def setup
|
||||
@kibbles = Petfood.find(1)
|
||||
@bits = Petfood.find(2)
|
||||
@shamu = Aquatic::Whale.find(1)
|
||||
@swimmy = Aquatic::Fish.find(1)
|
||||
@rover = Dog.find(1)
|
||||
@spot = Dog.find(2)
|
||||
@puma = WildBoar.find(1)
|
||||
@chloe = Kitten.find(1)
|
||||
@alice = Kitten.find(2)
|
||||
@froggy = Frog.find(1)
|
||||
|
||||
@join_count = EatersFoodstuff.count
|
||||
@l = @kibbles.eaters.length
|
||||
@m = @bits.eaters.count
|
||||
end
|
||||
|
||||
def test_all_relationship_validities
|
||||
# q = []
|
||||
# ObjectSpace.each_object(Class){|c| q << c if c.ancestors.include? ActiveRecord::Base }
|
||||
# q.each{|c| puts "#{c.name}.reflect_on_all_associations.map &:check_validity! "}
|
||||
Petfood.reflect_on_all_associations.map &:check_validity!
|
||||
Tabby.reflect_on_all_associations.map &:check_validity!
|
||||
Kitten.reflect_on_all_associations.map &:check_validity!
|
||||
Dog.reflect_on_all_associations.map &:check_validity!
|
||||
Aquatic::Fish.reflect_on_all_associations.map &:check_validity!
|
||||
EatersFoodstuff.reflect_on_all_associations.map &:check_validity!
|
||||
WildBoar.reflect_on_all_associations.map &:check_validity!
|
||||
Frog.reflect_on_all_associations.map &:check_validity!
|
||||
Aquatic::Whale.reflect_on_all_associations.map &:check_validity!
|
||||
Cat.reflect_on_all_associations.map &:check_validity!
|
||||
Aquatic::PupilsWhale.reflect_on_all_associations.map &:check_validity!
|
||||
BeautifulFightRelationship.reflect_on_all_associations.map &:check_validity!
|
||||
end
|
||||
|
||||
def test_assignment
|
||||
assert @kibbles.eaters.blank?
|
||||
assert @kibbles.eaters.push(Cat.find_by_name('Chloe'))
|
||||
assert_equal @l += 1, @kibbles.eaters.count
|
||||
|
||||
@kibbles.reload
|
||||
assert_equal @l, @kibbles.eaters.count
|
||||
|
||||
end
|
||||
|
||||
def test_duplicate_assignment
|
||||
# try to add a duplicate item
|
||||
@kibbles.eaters.push(@alice)
|
||||
assert @kibbles.eaters.include?(@alice)
|
||||
@kibbles.eaters.push(@alice)
|
||||
assert_equal @l + 1, @kibbles.eaters.count
|
||||
assert_equal @join_count + 1, EatersFoodstuff.count
|
||||
|
||||
@kibbles.reload
|
||||
assert_equal @l + 1, @kibbles.eaters.count
|
||||
assert_equal @join_count + 1, EatersFoodstuff.count
|
||||
end
|
||||
|
||||
def test_create_and_push
|
||||
assert @kibbles.eaters.push(@spot)
|
||||
assert_equal @l += 1, @kibbles.eaters.count
|
||||
assert @kibbles.eaters << @rover
|
||||
assert @kibbles.eaters << Kitten.create(:name => "Miranda")
|
||||
assert_equal @l += 2, @kibbles.eaters.length
|
||||
|
||||
@kibbles.reload
|
||||
assert_equal @l, @kibbles.eaters.length
|
||||
|
||||
# test that ids and new flags were set appropriately
|
||||
assert_not_nil @kibbles.eaters[0].id
|
||||
assert !@kibbles.eaters[1].new_record?
|
||||
end
|
||||
|
||||
def test_reload
|
||||
assert @kibbles.reload
|
||||
assert @kibbles.eaters.reload
|
||||
end
|
||||
|
||||
def test_add_join_record
|
||||
assert_equal Kitten, @chloe.class
|
||||
assert @join_record = EatersFoodstuff.new(:foodstuff_id => @bits.id, :eater_id => @chloe.id, :eater_type => @chloe.class.name )
|
||||
assert @join_record.save!
|
||||
assert @join_record.id
|
||||
assert_equal @join_count + 1, EatersFoodstuff.count
|
||||
|
||||
# has the parent changed if we don't reload?
|
||||
assert_equal @m, @bits.eaters.count
|
||||
|
||||
# if we do reload, is the new association there?
|
||||
# XXX no, because TestCase breaks reload. it works fine in the app.
|
||||
|
||||
assert_equal Petfood, @bits.eaters.reload.class
|
||||
assert_equal @m + 1, @bits.eaters.count
|
||||
assert @bits.eaters.include?(@chloe)
|
||||
|
||||
# puts "XXX #{EatersFoodstuff.count}"
|
||||
|
||||
end
|
||||
|
||||
def test_add_unsaved
|
||||
# add an unsaved item
|
||||
assert @bits.eaters << Kitten.new(:name => "Bridget")
|
||||
assert_nil Kitten.find_by_name("Bridget")
|
||||
assert_equal @m + 1, @bits.eaters.count
|
||||
|
||||
assert @bits.save
|
||||
@bits.reload
|
||||
assert_equal @m + 1, @bits.eaters.count
|
||||
|
||||
end
|
||||
|
||||
def test_self_reference
|
||||
assert @kibbles.eaters << @bits
|
||||
assert_equal @l += 1, @kibbles.eaters.count
|
||||
assert @kibbles.eaters.include?(@bits)
|
||||
@kibbles.reload
|
||||
assert @kibbles.foodstuffs_of_eaters.blank?
|
||||
|
||||
@bits.reload
|
||||
assert @bits.foodstuffs_of_eaters.include?(@kibbles)
|
||||
assert_equal [@kibbles], @bits.foodstuffs_of_eaters
|
||||
end
|
||||
|
||||
def test_remove
|
||||
assert @kibbles.eaters << @chloe
|
||||
@kibbles.reload
|
||||
assert @kibbles.eaters.delete(@kibbles.eaters[0])
|
||||
assert_equal @l, @kibbles.eaters.count
|
||||
end
|
||||
|
||||
def test_destroy
|
||||
assert @kibbles.eaters.push(@chloe)
|
||||
@kibbles.reload
|
||||
assert @kibbles.eaters.length > 0
|
||||
assert @kibbles.eaters[0].destroy
|
||||
@kibbles.reload
|
||||
assert_equal @l, @kibbles.eaters.count
|
||||
end
|
||||
|
||||
def test_clear
|
||||
@kibbles.eaters << [@chloe, @spot, @rover]
|
||||
@kibbles.reload
|
||||
assert_equal 3, @kibbles.eaters.clear.size
|
||||
assert @kibbles.eaters.blank?
|
||||
@kibbles.reload
|
||||
assert @kibbles.eaters.blank?
|
||||
assert_equal 0, @kibbles.eaters.clear.size
|
||||
end
|
||||
|
||||
def test_individual_collections
|
||||
assert @kibbles.eaters.push(@chloe)
|
||||
# check if individual collections work
|
||||
assert_equal @kibbles.eater_kittens.length, 1
|
||||
assert @kibbles.eater_dogs
|
||||
assert 1, @rover.eaters_foodstuffs.count
|
||||
end
|
||||
|
||||
def test_invididual_collections_push
|
||||
assert_equal [@chloe], (@kibbles.eater_kittens << @chloe)
|
||||
@kibbles.reload
|
||||
assert @kibbles.eaters.include?(@chloe)
|
||||
assert @kibbles.eater_kittens.include?(@chloe)
|
||||
assert !@kibbles.eater_dogs.include?(@chloe)
|
||||
end
|
||||
|
||||
def test_invididual_collections_delete
|
||||
@kibbles.eaters << [@chloe, @spot, @rover]
|
||||
@kibbles.reload
|
||||
assert_equal [@chloe], @kibbles.eater_kittens.delete(@chloe)
|
||||
assert @kibbles.eater_kittens.empty?
|
||||
assert !@kibbles.eater_kittens.delete(@chloe)
|
||||
|
||||
@kibbles.reload
|
||||
assert @kibbles.eater_kittens.empty?
|
||||
assert @kibbles.eater_dogs.include?(@spot)
|
||||
end
|
||||
|
||||
def test_invididual_collections_clear
|
||||
@kibbles.eaters << [@chloe, @spot, @rover]
|
||||
@kibbles.reload
|
||||
assert_equal [@chloe], @kibbles.eater_kittens.clear
|
||||
assert @kibbles.eater_kittens.empty?
|
||||
assert_equal 2, @kibbles.eaters.size
|
||||
@kibbles.reload
|
||||
assert @kibbles.eater_kittens.empty?
|
||||
assert_equal 2, @kibbles.eaters.size
|
||||
assert !@kibbles.eater_kittens.include?(@chloe)
|
||||
assert !@kibbles.eaters.include?(@chloe)
|
||||
end
|
||||
|
||||
def test_childrens_individual_collections
|
||||
assert Cat.find_by_name('Chloe').eaters_foodstuffs
|
||||
assert @kibbles.eaters_foodstuffs
|
||||
end
|
||||
|
||||
def test_self_referential_join_tables
|
||||
# check that the self-reference join tables go the right ways
|
||||
assert_equal @l, @kibbles.eaters_foodstuffs.count
|
||||
assert_equal @kibbles.eaters_foodstuffs.count, @kibbles.eaters_foodstuffs_as_child.count
|
||||
end
|
||||
|
||||
def test_dependent
|
||||
assert @kibbles.eaters << @chloe
|
||||
@kibbles.reload
|
||||
|
||||
# delete ourself and see if :dependent was obeyed
|
||||
dependent_rows = @kibbles.eaters_foodstuffs
|
||||
assert_equal dependent_rows.length, @kibbles.eaters.count
|
||||
@join_count = EatersFoodstuff.count
|
||||
|
||||
@kibbles.destroy
|
||||
assert_equal @join_count - dependent_rows.length, EatersFoodstuff.count
|
||||
assert_equal 0, EatersFoodstuff.find(:all, :conditions => ['foodstuff_id = ?', 1] ).length
|
||||
end
|
||||
|
||||
def test_normal_callbacks
|
||||
assert @rover.respond_to?(:after_initialize)
|
||||
assert @rover.respond_to?(:after_find)
|
||||
|
||||
assert @rover.after_initialize_test
|
||||
assert @rover.after_find_test
|
||||
end
|
||||
|
||||
def test_our_callbacks
|
||||
assert 0, @bits.eaters.count
|
||||
assert @bits.eaters.push(@rover)
|
||||
@bits.save
|
||||
|
||||
# puts "Testing callbacks."
|
||||
@bits2 = Petfood.find_by_name("Bits")
|
||||
@bits.reload
|
||||
|
||||
assert rover = @bits2.eaters.select { |x| x.name == "Rover" }[0]
|
||||
assert rover.after_initialize_test
|
||||
assert rover.after_find_test
|
||||
# puts "Done."
|
||||
|
||||
end
|
||||
|
||||
def test_number_of_join_records
|
||||
assert EatersFoodstuff.create(:foodstuff_id => 1, :eater_id => 1, :eater_type => "Cat")
|
||||
@join_count = EatersFoodstuff.count
|
||||
assert @join_count > 0
|
||||
end
|
||||
|
||||
def test_number_of_regular_records
|
||||
dogs = Dog.count
|
||||
assert Dog.new(:name => "Auggie").save!
|
||||
assert dogs + 1, Dog.count
|
||||
end
|
||||
|
||||
def test_attributes_come_through_when_child_has_underscore_in_table_name
|
||||
@join_record = EatersFoodstuff.new(:foodstuff_id => @bits.id, :eater_id => @puma.id, :eater_type => @puma.class.name)
|
||||
@join_record.save!
|
||||
@bits.eaters.reload
|
||||
|
||||
assert_equal 'Puma', @puma.name
|
||||
assert_equal 'Puma', @bits.eaters.first.name
|
||||
end
|
||||
|
||||
def test_before_save_on_join_table_is_not_clobbered_by_sti_base_class_fix
|
||||
assert @kibbles.eaters << @chloe
|
||||
assert_equal 3, @kibbles.eaters_foodstuffs.first.some_attribute
|
||||
end
|
||||
|
||||
def test_creating_namespaced_relationship
|
||||
assert @shamu.aquatic_pupils.empty?
|
||||
@shamu.aquatic_pupils << @swimmy
|
||||
assert_equal 1, @shamu.aquatic_pupils.length
|
||||
@shamu.reload
|
||||
assert_equal 1, @shamu.aquatic_pupils.length
|
||||
end
|
||||
|
||||
|
||||
def test_namespaced_polymorphic_collection
|
||||
@shamu.aquatic_pupils << @swimmy
|
||||
assert @shamu.aquatic_pupils.include?(@swimmy)
|
||||
@shamu.reload
|
||||
assert @shamu.aquatic_pupils.include?(@swimmy)
|
||||
|
||||
@shamu.aquatic_pupils << @spot
|
||||
assert @shamu.dogs.include?(@spot)
|
||||
assert @shamu.aquatic_pupils.include?(@swimmy)
|
||||
assert_equal @swimmy, @shamu.aquatic_fish.first
|
||||
assert_equal 10, @shamu.aquatic_fish.first.speed
|
||||
end
|
||||
|
||||
def test_deleting_namespaced_relationship
|
||||
@shamu.aquatic_pupils << @swimmy
|
||||
@shamu.aquatic_pupils << @spot
|
||||
|
||||
@shamu.reload
|
||||
@shamu.aquatic_pupils.delete @spot
|
||||
assert !@shamu.dogs.include?(@spot)
|
||||
assert !@shamu.aquatic_pupils.include?(@spot)
|
||||
assert_equal 1, @shamu.aquatic_pupils.length
|
||||
end
|
||||
|
||||
def test_unrenamed_parent_of_namespaced_child
|
||||
@shamu.aquatic_pupils << @swimmy
|
||||
assert_equal [@shamu], @swimmy.whales
|
||||
end
|
||||
|
||||
def test_empty_double_collections
|
||||
assert @puma.enemies.empty?
|
||||
assert @froggy.protectors.empty?
|
||||
assert @alice.enemies.empty?
|
||||
assert @spot.protectors.empty?
|
||||
assert @alice.beautiful_fight_relationships_as_enemy.empty?
|
||||
assert @alice.beautiful_fight_relationships_as_protector.empty?
|
||||
assert @alice.beautiful_fight_relationships.empty?
|
||||
end
|
||||
|
||||
def test_double_collection_assignment
|
||||
@alice.enemies << @spot
|
||||
@alice.reload
|
||||
@spot.reload
|
||||
assert @spot.protectors.include?(@alice)
|
||||
assert @alice.enemies.include?(@spot)
|
||||
assert !@alice.protectors.include?(@alice)
|
||||
assert_equal 1, @alice.beautiful_fight_relationships_as_protector.size
|
||||
assert_equal 0, @alice.beautiful_fight_relationships_as_enemy.size
|
||||
assert_equal 1, @alice.beautiful_fight_relationships.size
|
||||
|
||||
# self reference
|
||||
assert_equal 1, @alice.enemies.length
|
||||
@alice.enemies.push @alice
|
||||
assert @alice.enemies.include?(@alice)
|
||||
assert_equal 2, @alice.enemies.length
|
||||
@alice.reload
|
||||
assert_equal 2, @alice.beautiful_fight_relationships_as_protector.size
|
||||
assert_equal 1, @alice.beautiful_fight_relationships_as_enemy.size
|
||||
assert_equal 3, @alice.beautiful_fight_relationships.size
|
||||
end
|
||||
|
||||
def test_double_collection_deletion
|
||||
@alice.enemies << @spot
|
||||
@alice.reload
|
||||
assert @alice.enemies.include?(@spot)
|
||||
@alice.enemies.delete(@spot)
|
||||
assert !@alice.enemies.include?(@spot)
|
||||
assert @alice.enemies.empty?
|
||||
@alice.reload
|
||||
assert !@alice.enemies.include?(@spot)
|
||||
assert @alice.enemies.empty?
|
||||
assert_equal 0, @alice.beautiful_fight_relationships.size
|
||||
end
|
||||
|
||||
def test_double_collection_deletion_from_opposite_side
|
||||
@alice.protectors << @puma
|
||||
@alice.reload
|
||||
assert @alice.protectors.include?(@puma)
|
||||
@alice.protectors.delete(@puma)
|
||||
assert !@alice.protectors.include?(@puma)
|
||||
assert @alice.protectors.empty?
|
||||
@alice.reload
|
||||
assert !@alice.protectors.include?(@puma)
|
||||
assert @alice.protectors.empty?
|
||||
assert_equal 0, @alice.beautiful_fight_relationships.size
|
||||
end
|
||||
|
||||
def test_individual_collections_created_for_double_relationship
|
||||
assert @alice.dogs.empty?
|
||||
@alice.enemies << @spot
|
||||
|
||||
assert @alice.enemies.include?(@spot)
|
||||
assert !@alice.kittens.include?(@alice)
|
||||
|
||||
assert !@alice.dogs.include?(@spot)
|
||||
@alice.reload
|
||||
assert @alice.dogs.include?(@spot)
|
||||
assert !WildBoar.find(@alice.id).dogs.include?(@spot) # make sure the parent type is checked
|
||||
end
|
||||
|
||||
def test_individual_collections_created_for_double_relationship_from_opposite_side
|
||||
assert @alice.wild_boars.empty?
|
||||
@alice.protectors << @puma
|
||||
|
||||
assert @alice.protectors.include?(@puma)
|
||||
assert !@alice.wild_boars.include?(@puma)
|
||||
@alice.reload
|
||||
assert @alice.wild_boars.include?(@puma)
|
||||
|
||||
assert !Dog.find(@alice.id).wild_boars.include?(@puma) # make sure the parent type is checked
|
||||
end
|
||||
|
||||
def test_self_referential_individual_collections_created_for_double_relationship
|
||||
@alice.enemies << @alice
|
||||
@alice.reload
|
||||
assert @alice.enemy_kittens.include?(@alice)
|
||||
assert @alice.protector_kittens.include?(@alice)
|
||||
assert @alice.kittens.include?(@alice)
|
||||
assert_equal 2, @alice.kittens.size
|
||||
|
||||
@alice.enemies << (@chloe = Kitten.find_by_name('Chloe'))
|
||||
@alice.reload
|
||||
assert @alice.enemy_kittens.include?(@chloe)
|
||||
assert !@alice.protector_kittens.include?(@chloe)
|
||||
assert @alice.kittens.include?(@chloe)
|
||||
assert_equal 3, @alice.kittens.size
|
||||
end
|
||||
|
||||
def test_child_of_polymorphic_join_can_reach_parent
|
||||
@alice.enemies << @spot
|
||||
@alice.reload
|
||||
assert @spot.protectors.include?(@alice)
|
||||
end
|
||||
|
||||
def test_double_collection_deletion_from_child_polymorphic_join
|
||||
@alice.enemies << @spot
|
||||
@spot.protectors.delete(@alice)
|
||||
assert !@spot.protectors.include?(@alice)
|
||||
@alice.reload
|
||||
assert !@alice.enemies.include?(@spot)
|
||||
BeautifulFightRelationship.create(:protector_id => 2, :protector_type => "Dog", :enemy_id => @spot.id, :enemy_type => @spot.class.name)
|
||||
@alice.enemies << @spot
|
||||
@spot.protectors.delete(@alice)
|
||||
assert !@spot.protectors.include?(@alice)
|
||||
end
|
||||
|
||||
def test_hmp_passed_block_manipulates_proxy_class
|
||||
assert_equal "result", @shamu.aquatic_pupils.blow
|
||||
assert_raises(NoMethodError) { @kibbles.eaters.blow }
|
||||
end
|
||||
|
||||
def test_collection_query_on_unsaved_record
|
||||
assert Dog.new.enemies.empty?
|
||||
assert Dog.new.foodstuffs_of_eaters.empty?
|
||||
end
|
||||
|
||||
def test_double_invididual_collections_push
|
||||
assert_equal [@chloe], (@spot.protector_kittens << @chloe)
|
||||
@spot.reload
|
||||
assert @spot.protectors.include?(@chloe)
|
||||
assert @spot.protector_kittens.include?(@chloe)
|
||||
assert !@spot.protector_dogs.include?(@chloe)
|
||||
|
||||
assert_equal [@froggy], (@spot.frogs << @froggy)
|
||||
@spot.reload
|
||||
assert @spot.enemies.include?(@froggy)
|
||||
assert @spot.frogs.include?(@froggy)
|
||||
assert !@spot.enemy_dogs.include?(@froggy)
|
||||
end
|
||||
|
||||
def test_double_invididual_collections_delete
|
||||
@spot.protectors << [@chloe, @puma]
|
||||
@spot.reload
|
||||
assert_equal [@chloe], @spot.protector_kittens.delete(@chloe)
|
||||
assert @spot.protector_kittens.empty?
|
||||
assert !@spot.protector_kittens.delete(@chloe)
|
||||
|
||||
@spot.reload
|
||||
assert @spot.protector_kittens.empty?
|
||||
assert @spot.wild_boars.include?(@puma)
|
||||
end
|
||||
|
||||
def test_double_invididual_collections_clear
|
||||
@spot.protectors << [@chloe, @puma, @alice]
|
||||
@spot.reload
|
||||
assert_equal [@chloe, @alice], @spot.protector_kittens.clear.sort_by(&:id)
|
||||
assert @spot.protector_kittens.empty?
|
||||
assert_equal 1, @spot.protectors.size
|
||||
@spot.reload
|
||||
assert @spot.protector_kittens.empty?
|
||||
assert_equal 1, @spot.protectors.size
|
||||
assert !@spot.protector_kittens.include?(@chloe)
|
||||
assert !@spot.protectors.include?(@chloe)
|
||||
assert !@spot.protector_kittens.include?(@alice)
|
||||
assert !@spot.protectors.include?(@alice)
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue