github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

pluginaweek / polymorphic_identity

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 7
    • 1
  • Source
  • Commits
  • Network (1)
  • Downloads (6)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (6)
    • v0.1.0
    • v0.0.5
    • v0.0.4
    • v0.0.3
    • v0.0.2
    • v0.0.1
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Dynamically generates aliases for polymorphic associations based on the class names of those associations — Read more

  cancel

http://www.pluginaweek.org

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Add gemspec 
obrie (author)
Tue Jun 09 20:34:44 -0700 2009
commit  7578bae43c10006fc2dfaa2572b3fdef58fad6c8
tree    ecf722a8aaef6ac8549f30ec161058b4245f8ddb
parent  d13a1007da19927a4e7da69ed014aed8d9792172
polymorphic_identity /
name age
history
message
file .gitignore Loading commit data...
file CHANGELOG.rdoc
file LICENSE
file README.rdoc
file Rakefile
file init.rb
directory lib/
file polymorphic_identity.gemspec
directory test/
README.rdoc

polymorphic_identity

polymorphic_identity dynamically generates aliases for polymorphic associations based on the class names of those associations.

Resources

API

  • api.pluginaweek.org/polymorphic_identity

Bugs

  • pluginaweek.lighthouseapp.com/projects/13285-polymorphic_identity

Development

  • github.com/pluginaweek/polymorphic_identity

Source

  • git://github.com/pluginaweek/polymorphic_identity.git

Description

Polymorphic associations are not very descriptive when it comes to easily knowing the type of model your interacting with. For example, a typical polymorphic assocation looks like the following:

  class Tag < ActiveRecord::Base
    belongs_to :taggable, :polymorphic => true
  end

When getting the taggable record, you would normally have to call tag.taggable. However, if you know that the taggable record is just an instance of the Article model, then it would feel more comfortable if you could just call tag.article. polymoprhic_identity makes this possible by dynamically checking the name of the polymorphic record’s class and creating methods that allow you to access the polymorphic association based on that class name.

Usage

Example

  class Comment < ActiveRecord::Base
    belongs_to :commentable, :polymorphic => true
    belongs_to :commenter, :polymorphic => true
  end

  class Article < ActiveRecord::Base
    has_many :comments, :as => :commentable
  end

  class User < ActiveRecord::Base
    has_many :comments, :as => :commenter
  end

  c = Comment.find(1)   # => #<Tag id: 1, commentable_id: 1, commentable_type: "Article", commenter_id: 1, commenter_type: "User"}>
  c.commentable         # => #<Article id: 1>
  c.article             # => #<Article id: 1>
  c.commenter           # => #<User id: 1>
  c.user                # => #<User id: 1>

Testing

Before you can run any tests, the following gem must be installed:

  • plugin_test_helper

To run against a specific version of Rails:

  rake test RAILS_FRAMEWORK_ROOT=/path/to/rails

Dependencies

  • Rails 2.0 or later
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server