Code Monkey home page Code Monkey logo

acts_as_polymorphic's Introduction

ActsAsPolymorphic
=================

TODO: Add documentation.

This is important because the introduction of a polymorphic association means that more than one model is going to have a 'has_many :as'.  Now, while this may not seem too repetitive to add to a few different classes, it easily can be.  This is especially true if you start caring about the :dependent relationship.  Or, if you add new methods based on the association.


Example
=======

TODO: This example isn't great because it illustrates something that is common with a many-to-many

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

class Movie < ActiveRecord::Base
  acts_as_taggable
end

class Photo < ActiveRecord::Base
	acts_as_taggable
end

class Song < ActiveRecord::Base
  acts_as_taggable
end

All you need to do is run this:
script/generate acts_as_polyorphic taggable tags
or
script/generate acts_as_polyorphic taggable tags tag destroy

which generates (in the former case)

module ActiveRecord
  module Acts
    module Taggable
      def self.included(base)
        base.extend(ClassMethods)
      end

      module ClassMethods
        def acts_as_taggable
          class_eval do
            include ActiveRecord::Acts::Taggable::InstanceMethods

            has_many :tags, :class_name => "Tag",
                            :as         => :taggable,
                            :dependent  => :nullify
          end
        end
      end

      module InstanceMethods
        # Add instance methods here for the taggables
      end 
    end
  end
end

ActiveRecord::Base.class_eval { include ActiveRecord::Acts::Taggable }


Copyright (c) 2010 Chris Morris, released under the MIT license

acts_as_polymorphic's People

Contributors

hitsvilleusa avatar

Stargazers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.