Skip to content

Instantly share code, notes, and snippets.

@parndt
Forked from fauxparse/gist:131622
Created June 18, 2009 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parndt/131624 to your computer and use it in GitHub Desktop.
Save parndt/131624 to your computer and use it in GitHub Desktop.
# 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