upgrade has_many_polymorphs

This commit is contained in:
Reinier Balt 2008-12-21 22:15:57 +01:00
parent 2d11109b8b
commit f97ca2f6af
178 changed files with 132 additions and 108 deletions

View file

@ -1,14 +1,13 @@
Has_many_polymorphs Has_many_polymorphs
An ActiveRecord plugin for self-referential and double-sided polymorphic associations. An ActiveRecord plugin for self-referential and double-sided polymorphic associations.
== License == License
Copyright 2006-2008 Cloudburst, LLC. Licensed under the AFL 3. See the included LICENSE file. Copyright 2007 Cloudburst, LLC. Licensed under the AFL 3. See the included LICENSE file.
The public certificate for the gem is here[http://rubyforge.org/frs/download.php/25331/evan_weaver-original-public_cert.pem]. The public certificate for this gem is at http://rubyforge.org/frs/download.php/25331/evan_weaver-original-public_cert.pem.
If you use this software, please {make a donation}[http://blog.evanweaver.com/donate/], or {recommend Evan}[http://www.workingwithrails.com/person/7739-evan-weaver] at Working with Rails.
== Description == Description
@ -36,7 +35,7 @@ The plugin also includes a generator for a tagging system, a common use case (se
== Installation == Installation
To install the Rails plugin, run: To install the Rails plugin, run:
script/plugin install git://github.com/fauna/has_many_polymorphs.git script/plugin install svn://rubyforge.org/var/svn/fauna/has_many_polymorphs/trunk
There's also a gem version. To install it instead, run: There's also a gem version. To install it instead, run:
sudo gem install has_many_polymorphs sudo gem install has_many_polymorphs
@ -188,7 +187,7 @@ Note that because of the way Rails reloads model classes, the plugin can sometim
== Reporting problems == Reporting problems
The support forum is here[http://rubyforge.org/forum/forum.php?forum_id=16450]. * http://rubyforge.org/forum/forum.php?forum_id=16450
Patches and contributions are very welcome. Please note that contributors are required to assign copyright for their additions to Cloudburst, LLC. Patches and contributions are very welcome. Please note that contributors are required to assign copyright for their additions to Cloudburst, LLC.

View file

@ -101,14 +101,13 @@ class ActiveRecord::Base #:nodoc:
end end
module TaggingFinders module TaggingFinders
#
# Find all the objects tagged with the supplied list of tags # Find all the objects tagged with the supplied list of tags
# #
# Usage : Model.tagged_with("ruby") # Usage : Model.tagged_with("ruby")
# Model.tagged_with("hello", "world") # Model.tagged_with("hello", "world")
# Model.tagged_with("hello", "world", :limit => 10) # Model.tagged_with("hello", "world", :limit => 10)
# #
# XXX This query strategy is not performant, and needs to be rewritten as an inverted join or a series of unions
#
def tagged_with(*tag_list) def tagged_with(*tag_list)
options = tag_list.last.is_a?(Hash) ? tag_list.pop : {} options = tag_list.last.is_a?(Hash) ? tag_list.pop : {}
tag_list = parse_tags(tag_list) tag_list = parse_tags(tag_list)
@ -126,16 +125,11 @@ class ActiveRecord::Base #:nodoc:
sql << "AND taggings.taggable_type = '#{ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s}' " sql << "AND taggings.taggable_type = '#{ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s}' "
sql << "AND taggings.tag_id = tags.id " sql << "AND taggings.tag_id = tags.id "
tag_list_condition = tag_list.map {|name| "'#{name}'"}.join(", ") tag_list_condition = tag_list.map {|t| "'#{t}'"}.join(", ")
sql << "AND (tags.name IN (#{sanitize_sql(tag_list_condition)})) " sql << "AND (tags.name IN (#{sanitize_sql(tag_list_condition)})) "
sql << "AND #{sanitize_sql(options[:conditions])} " if options[:conditions] sql << "AND #{sanitize_sql(options[:conditions])} " if options[:conditions]
sql << "GROUP BY #{table_name}.id "
columns = column_names.map do |column|
"#{table_name}.#{column}"
end.join(", ")
sql << "GROUP BY #{columns} "
sql << "HAVING COUNT(taggings.tag_id) = #{tag_list.size}" sql << "HAVING COUNT(taggings.tag_id) = #{tag_list.size}"
add_order!(sql, options[:order], scope) add_order!(sql, options[:order], scope)

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,17 @@
=begin rdoc
Access the <tt>has_many_polymorphs_options</tt> hash in your Rails::Initializer.run#after_initialize block if you need to modify the behavior of Rails::Initializer::HasManyPolymorphsAutoload.
=end
class Rails::Configuration
def has_many_polymorphs_options
::HasManyPolymorphs.options
end
def has_many_polymorphs_options=(hash)
::HasManyPolymorphs.options = HashWithIndifferentAccess.new(hash)
end
end

View file

@ -2,19 +2,18 @@ module ActiveRecord #:nodoc:
module Reflection #:nodoc: module Reflection #:nodoc:
module ClassMethods #:nodoc: module ClassMethods #:nodoc:
# Update the default reflection switch so that <tt>:has_many_polymorphs</tt> types get instantiated. # 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.
# It's not a composed method so we have to override the whole thing.
def create_reflection(macro, name, options, active_record) def create_reflection(macro, name, options, active_record)
case macro case macro
when :has_many, :belongs_to, :has_one, :has_and_belongs_to_many when :has_many, :belongs_to, :has_one, :has_and_belongs_to_many
klass = options[:through] ? ThroughReflection : AssociationReflection reflection = AssociationReflection.new(macro, name, options, active_record)
reflection = klass.new(macro, name, options, active_record)
when :composed_of when :composed_of
reflection = AggregateReflection.new(macro, name, options, active_record) reflection = AggregateReflection.new(macro, name, options, active_record)
# added by has_many_polymorphs # # added by has_many_polymorphs #
when :has_many_polymorphs when :has_many_polymorphs
reflection = PolymorphicReflection.new(macro, name, options, active_record) reflection = PolymorphicReflection.new(macro, name, options, active_record)
# end added #
end end
write_inheritable_hash :reflections, name => reflection write_inheritable_hash :reflections, name => reflection
reflection reflection

View file

@ -0,0 +1,44 @@
# This file is autogenerated. Instead of editing this file, please use the
# migrations feature of ActiveRecord to incrementally modify your database, and
# then regenerate this schema definition.
ActiveRecord::Schema.define(:version => 9) do
create_table "double_sti_parent_relationships", :force => true do |t|
t.column "the_bone_type", :string, :default => "", :null => false
t.column "the_bone_id", :integer, :null => false
t.column "parent_type", :string, :default => "", :null => false
t.column "parent_id", :integer, :null => false
end
create_table "double_sti_parents", :force => true do |t|
t.column "name", :string
end
create_table "library_models", :force => true do |t|
t.column "name", :string
end
create_table "organic_substances", :force => true do |t|
t.column "type", :string
end
create_table "single_sti_parent_relationships", :force => true do |t|
t.column "the_bone_type", :string, :default => "", :null => false
t.column "the_bone_id", :integer, :null => false
t.column "single_sti_parent_id", :integer, :null => false
end
create_table "single_sti_parents", :force => true do |t|
t.column "name", :string
end
create_table "sticks", :force => true do |t|
t.column "name", :string
end
create_table "stones", :force => true do |t|
t.column "name", :string
end
end

Some files were not shown because too many files have changed in this diff Show more