Skip to content

Instantly share code, notes, and snippets.

@philtr
Created April 16, 2012 20:12
Show Gist options
  • Save philtr/2401203 to your computer and use it in GitHub Desktop.
Save philtr/2401203 to your computer and use it in GitHub Desktop.
Acts as Whateverable
# app/models/client.rb
class Client < ActiveRecord::Base
acts_as_whateverable
# ...
end
# 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