Class: CustomMongo::Generators::ModelGenerator

Inherits:
Mongoid::Generators::ModelGenerator
  • Object
show all
Defined in:
lib/generators/custom_mongo/model_generator.rb

Overview

Extend this class to add the “default” field.

Instance Method Summary collapse

Constructor Details

#initialize(args, *options) ⇒ ModelGenerator

Returns a new instance of ModelGenerator.



18
19
20
21
22
23
24
25
26
27
# File 'lib/generators/custom_mongo/model_generator.rb', line 18

def initialize(args, *options)
  custom_attributes = args[1]
  args = [args[0]] # "super" expects the name as arg[0], then we custom parse the attributes
  super
  self.attributes = custom_attributes.map do |attribute|
    att = Rails::Generators::GeneratedAttribute.new(attribute[:name], attribute[:type].to_sym)
    att.default = attribute[:default]
    att
  end
end

Instance Method Details

#create_model_fileObject



29
30
31
# File 'lib/generators/custom_mongo/model_generator.rb', line 29

def create_model_file
  template File.join(File.dirname(__FILE__), 'model.rb.tt'), File.join('app/models', class_path, "#{file_name}.rb")
end