-
-
Save parndt/131624 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
# Custom types are scoped to a particular site instance, | |
# so their class names are suffixed with the site ID. | |
# This also helps Content#const_missing figure out when we're | |
# trying to load a custom type, because it gets numbers at the | |
# end of the class name. | |
# Note: I've put some validation logic in CustomType to ensure | |
# that the name in the database is a valid Ruby constant name. | |
# new_custom_type = Content::CustomType.create(:site_id => 1, :name => "My custom type") | |
# Content::MyCustomType1 #=> Content::MyCustomType1(...) | |
module Content | |
# ... | |
def self.const_missing(sym) | |
if sym.to_s =~ /\A([A-Z][a-zA-Z]*)(\d+)\z/ and custom_type = CustomType.find_by_name_and_site_id($1, $2) | |
custom_type.to_class | |
else | |
super | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment