-
-
Save HungYuHei/3128267 to your computer and use it in GitHub Desktop.
Extend the fields of a Mongoid document from a .yml file
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
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