Last active
December 22, 2015 20:39
-
-
Save jordaaash/6528397 to your computer and use it in GitHub Desktop.
This file contains 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
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 |
This file contains 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
class Company < Organization | |
belongs_to :type, | |
:class_name => 'CompanyType', | |
:inverse_of => :companies | |
belongs_to :size, | |
:class_name => 'CompanySize', | |
:inverse_of => :companies | |
#...snip... | |
end |
This file contains 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
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