Skip to content

Instantly share code, notes, and snippets.

@jordaaash
Last active December 22, 2015 20:39
Show Gist options
  • Save jordaaash/6528397 to your computer and use it in GitHub Desktop.
Save jordaaash/6528397 to your computer and use it in GitHub Desktop.
module AbstractClass
def self.extended (base)
base.instance_eval do
self.abstract_class = true if respond_to?(:abstract_class=)
@abstract_class = true unless instance_variable_defined?(:@abstract_class)
end
end
def new (*)
if @abstract_class
raise NotImplementedError,
"#{self} is an abstract class and cannot be instantiated."
end
super
end
def inherited (subclass = nil, &block)
if subclass.nil?
(@_inherited_blocks ||= []) << block
else
super
if instance_variable_defined?(:@_inherited_blocks)
@_inherited_blocks.each { |inherit| subclass.instance_eval(&inherit) }
end
end
end
end
class Company < Organization
belongs_to :type,
:class_name => 'CompanyType',
:inverse_of => :companies
belongs_to :size,
:class_name => 'CompanySize',
:inverse_of => :companies
#...snip...
end
class Organization < ActiveRecord::Base
extend AbstractClass
inherited do
# ...snip...
self.inheritance_column = nil
scope :default, -> { exists.shown }
has_many :users,
-> { exists }
validates :type,
:presence => true
validates :size,
:allow_nil => true,
:presence => true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment