Skip to content

Instantly share code, notes, and snippets.

@HungYuHei
Forked from camallen/dynamic_document.rb
Created July 17, 2012 09:15
Show Gist options
  • Save HungYuHei/3128267 to your computer and use it in GitHub Desktop.
Save HungYuHei/3128267 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