Created
April 16, 2012 20:12
-
-
Save philtr/2401203 to your computer and use it in GitHub Desktop.
Acts as Whateverable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/client.rb | |
class Client < ActiveRecord::Base | |
acts_as_whateverable | |
# ... | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/whateverable.rb | |
module Whateverable | |
def self.included(base) | |
base.send(:extend, ClassMethods) | |
end | |
module ClassMethods | |
def acts_as_whateverable | |
class_eval do | |
has_many :whatevers, :as => :whateverable, :dependent => :destroy, :order => 'whatevers.created_at ASC' | |
accepts_nested_attributes_for :whatevers, :reject_if => :all_blank, :allow_destroy => true | |
end | |
end | |
end | |
end | |
class ActiveRecord::Base | |
include Whateverable | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment