Permalink
Browse files

Initial revision.

  • Loading branch information...
0 parents commit 430774972fccef5501c68de5b86f359d7cecc642 @obrie obrie committed Feb 9, 2007
@@ -0,0 +1,2 @@
+*SVN*
+
@@ -0,0 +1,20 @@
+Copyright (c) 2005-2006 Aaron Pfeifer & Neil Abraham
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 README
@@ -0,0 +1,24 @@
+= polymorphic_identity
+
+polymorphic_identity .
+
+== Resources
+
+Wiki
+
+* http://wiki.pluginaweek.org/Polymorphic_Identity
+
+Announcement
+
+* http://www.pluginaweek.org/
+
+Source
+
+* http://svn.pluginaweek.org/trunk/plugins/active_record/associations/polymorphic_identity
+
+Development
+
+* http://dev.pluginaweek.org/browser/trunk/plugins/active_record/associations/polymorphic_identity
+
+== Description
+
@@ -0,0 +1,80 @@
+require 'rake/testtask'
+require 'rake/rdoctask'
+require 'rake/gempackagetask'
+require 'rake/contrib/sshpublisher'
+
+PKG_NAME = 'polymorphic_identity'
+PKG_VERSION = '0.0.1'
+PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
+RUBY_FORGE_PROJECT = 'pluginaweek'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the polymorphic_identity plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/unit/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the polymorphic_identity plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'ClassAssociations'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
+
+spec = Gem::Specification.new do |s|
+ s.name = PKG_NAME
+ s.version = PKG_VERSION
+ s.platform = Gem::Platform::RUBY
+ s.summary = ''
+
+ s.files = FileList['{lib,tasks,test}/**/*'].to_a + %w(init.rb MIT-LICENSE Rakefile README)
+ s.require_path = 'lib'
+ s.autorequire = 'polymorphic_identity'
+ s.has_rdoc = true
+ s.test_files = Dir['test/unit/**/*_test.rb']
+ s.add_dependency 'activerecord', '>= 1.15.0'
+
+ s.author = 'Aaron Pfeifer and Neil Abraham'
+ s.email = 'info@pluginaweek.org'
+ s.homepage = 'http://www.pluginaweek.org'
+end
+
+Rake::GemPackageTask.new(spec) do |p|
+ p.gem_spec = spec
+ p.need_tar = true
+ p.need_zip = true
+end
+
+desc 'Publish the beta gem'
+task :pgem => [:package] do
+ Rake::SshFilePublisher.new('pluginaweek@pluginaweek.org', '/home/pluginaweek/gems.pluginaweek.org/gems', 'pkg', "#{PKG_FILE_NAME}.gem").upload
+end
+
+desc 'Publish the API documentation'
+task :pdoc => [:rdoc] do
+ Rake::SshDirPublisher.new('pluginaweek@pluginaweek.org', "/home/pluginaweek/api.pluginaweek.org/#{PKG_NAME}", 'rdoc').upload
+end
+
+desc 'Publish the API docs and gem'
+task :publish => [:pdoc, :release]
+
+desc 'Publish the release files to RubyForge.'
+task :release => [:gem, :package] do
+ require 'rubyforge'
+
+ ruby_forge = RubyForge.new
+ ruby_forge.login
+
+ %w( gem tgz zip ).each do |ext|
+ file = "pkg/#{PKG_FILE_NAME}.#{ext}"
+ puts "Releasing #{File.basename(file)}..."
+
+ ruby_forge.add_release(RUBY_FORGE_PROJECT, PKG_NAME, PKG_VERSION, file)
+ end
+end
@@ -0,0 +1 @@
+require 'polymorphic_identity'
@@ -0,0 +1,35 @@
+module PluginAWeek
+ #
+ module PolymorphicIdentity
+ def self.included(base)
+ base.class_eval do
+ alias_method_chain :method_missing, :polymorphic_identity
+ alias_method_chain :respond_to?, :polymorphic_identity
+ end
+ end
+
+ def method_missing_with_polymorphic_identity(method_id, *args, &block) #:nodoc:
+ polymorphic_reflection = self.class.reflections.values.find do |reflection|
+ reflection.options[:polymorphic] && read_attribute(reflection.options[:foreign_type]) == method_id.to_s.camelize
+ end
+
+ if polymorphic_reflection
+ send(polymorphic_reflection.name, *args, &block)
+ else
+ method_missing_witouth_polymorphic_identity(method_id, *args, &block)
+ end
+ end
+
+ def respond_to_with_polymorphic_identity?(method, include_priv = false) #:nodoc:
+ return true if respond_to_without_polymorphic_identity?(method, include_priv)
+
+ self.class.reflections.values.detect do |reflection|
+ reflection.options[:polymorphic] && read_attribute(reflection.options[:foreign_type]) == method.to_s.camelize
+ end
+ end
+ end
+end
+
+ActiveRecord::Base.class_eval do
+ include PluginAWeek::PolymorphicIdentity
+end
@@ -0,0 +1,4 @@
+class Article < ActiveRecord::Base
+ has_many :comments,
+ :as => :commentable
+end
@@ -0,0 +1,4 @@
+class Author < ActiveRecord::Base
+ has_many :comments,
+ :as => :commenter
+end
@@ -0,0 +1,6 @@
+class Comment < ActiveRecord::Base
+ belongs_to :commentable,
+ :polymorphic => true
+ belongs_to :commenter,
+ :polymorphic => true
+end
@@ -0,0 +1,4 @@
+class Page < ActiveRecord::Base
+ has_many :comments,
+ :as => :commentable
+end
@@ -0,0 +1,4 @@
+class User < ActiveRecord::Base
+ has_many :comments,
+ :as => :commenter
+end
@@ -0,0 +1,13 @@
+unless defined?(APP_ROOT)
+ root_path = File.join(File.dirname(__FILE__), '..')
+
+ unless RUBY_PLATFORM =~ /mswin32/
+ require 'pathname'
+ root_path = Pathname.new(root_path).cleanpath(true).to_s
+ end
+
+ APP_ROOT = root_path
+end
+
+require 'rubygems'
+require 'active_record'
@@ -0,0 +1,22 @@
+in_memory:
+ adapter: sqlite3
+ database: ":memory:"
+ verbosity: quiet
+sqlite:
+ adapter: sqlite
+ dbfile: polymorphic_identity_test.sqlite.db
+sqlite3:
+ adapter: sqlite3
+ dbfile: polymorphic_identity_test.sqlite3.db
+postgresql:
+ adapter: postgresql
+ username: postgres
+ password: postgres
+ database: polymorphic_identity_test
+ min_messages: ERROR
+mysql:
+ adapter: mysql
+ host: localhost
+ username: root
+ password:
+ database: polymorphic_identity_test
@@ -0,0 +1,45 @@
+require File.join(File.dirname(__FILE__), 'boot')
+
+# set_load_path
+load_paths = %w(app app/models config vendor).collect {|dir| "#{APP_ROOT}/#{dir}"}
+load_paths.reverse_each {|dir| $LOAD_PATH.unshift("#{APP_ROOT}/#{dir}") if File.directory?(dir)}
+$LOAD_PATH.uniq!
+
+# set_autoload_paths
+Dependencies.load_paths = load_paths
+
+# load_environment
+APP_ENV = ENV['DB']
+
+# initialize_database
+ActiveRecord::Base.configurations = YAML::load(IO.read("#{APP_ROOT}/config/database.yml"))
+ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[APP_ENV])
+
+# initializer_logger
+log_path = "#{APP_ROOT}/log/#{APP_ENV}.log"
+begin
+ logger = Logger.new(log_path)
+ logger.level = Logger::DEBUG
+rescue StandardError
+ logger = Logger.new(STDERR)
+ logger.level = Logger::WARN
+ logger.warn(
+ "Logger Error: Unable to access log file. Please ensure that #{log_path} exists and is chmod 0666. " +
+ "The log level has been raised to WARN and the output directed to STDERR until the problem is fixed."
+ )
+end
+
+# initialize_framework_logging
+ActiveRecord::Base.logger = logger
+
+# initialize_dependency_mechanism
+Dependencies.mechanism = :require
+
+# initialize_breakpoints
+require 'active_support/breakpoint'
+
+# initialize_whiny_nils
+# require('active_support/whiny_nil')
+
+# load_observers
+ActiveRecord::Base.instantiate_observers
@@ -0,0 +1,11 @@
+class CreateAuthors < ActiveRecord::Migration
+ def self.up
+ create_table :authors do |t|
+ t.column :name, :string
+ end
+ end
+
+ def self.down
+ drop_table :authors
+ end
+end
@@ -0,0 +1,12 @@
+class CreateArticles < ActiveRecord::Migration
+ def self.up
+ create_table :articles do |t|
+ t.column :author_id, :integer
+ t.column :content, :text
+ end
+ end
+
+ def self.down
+ drop_table :articles
+ end
+end
@@ -0,0 +1,12 @@
+class CreatePages < ActiveRecord::Migration
+ def self.up
+ create_table :pages do |t|
+ t.column :author_id, :integer
+ t.column :content, :text
+ end
+ end
+
+ def self.down
+ drop_table :pages
+ end
+end
@@ -0,0 +1,11 @@
+class CreateUsers < ActiveRecord::Migration
+ def self.up
+ create_table :users do |t|
+ t.column :name, :string
+ end
+ end
+
+ def self.down
+ drop_table :users
+ end
+end
@@ -0,0 +1,14 @@
+class CreateComments < ActiveRecord::Migration
+ def self.up
+ create_table :comments do |t|
+ t.column :commentable_id, :integer
+ t.column :commentable_type, :string
+ t.column :commenter_id, :string
+ t.column :commenter_type, :string
+ end
+ end
+
+ def self.down
+ drop_table :comments
+ end
+end
@@ -0,0 +1,9 @@
+test:
+ id: 1
+ author_id: 1
+ content: This is a test of the Emergency Broadcast System. If this had been an actual emergency...
+
+welcome:
+ id: 2
+ author_id: 2
+ content: Welcome!
@@ -0,0 +1,7 @@
+john_doe:
+ id: 1
+ name: John Doe
+
+jane_doe:
+ id: 2
+ name: Jane Doe
Oops, something went wrong.

0 comments on commit 4307749

Please sign in to comment.