Skip to content

Instantly share code, notes, and snippets.

@camallen
Created November 22, 2011 12:25
Show Gist options
  • Save camallen/1385549 to your computer and use it in GitHub Desktop.
Save camallen/1385549 to your computer and use it in GitHub Desktop.
Extend the fields of a Mongoid document from a .yml file
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