Created
November 22, 2011 12:25
-
-
Save camallen/1385549 to your computer and use it in GitHub Desktop.
Extend the fields of a Mongoid document from a .yml file
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 ExtendFields | |
require "yaml" | |
private | |
def extend_fields | |
dynamic_fields.each { |name, type| self.class.field name, type: type } | |
end | |
def dynamic_fields | |
YAML.load_file( File.join( Rails.root, 'config', 'class_fields', "#{self.class.name.underscore}.yml" ) ) rescue {} | |
end | |
end | |
#And then include this module in the class you want to extend | |
class DynamicDocument | |
include ExtendFields | |
after_initialize :extend_fields | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment