Last active
December 31, 2015 16:59
-
-
Save tikijian/8017486 to your computer and use it in GitHub Desktop.
Code snippet for dynamic model definition within a specific module.
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
# class Post < ActiveRecord::Base | |
# #code | |
# end | |
# | |
# class Backend::Post < Post # we don't want to define it in each model file. | |
# end | |
module Backend | |
def self.const_missing(klass) | |
klass = klass.to_s | |
if Backend.const_defined?(klass) | |
const_get(klass) | |
else | |
const_set(klass, ActiveRecord.const_get(klass)) | |
end | |
end | |
end | |
# Backend::Post | |
# #=> Post | |
# | |
# Backend::Foo | |
# #=> NameError: uninitialized constant Backend::Foo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment