Sha256: 0e0c7fb85a2ebb2ed9445ca7ae3cb632f1b857a203cb63674a258fed9b3205e9

Contents?: true

Size: 1.13 KB

Versions: 19

Compression:

Stored size: 1.13 KB

Contents

require 'rails/generators/named_base'
require 'rails/generators/active_model'
require File.join(Gem::Specification.find_by_name('mongoid').gem_dir, 'lib/rails/generators/mongoid/model/model_generator.rb')

module Rails
  module Generators
    # Extend this class to add the "default" field used in the generator.
    class GeneratedAttribute
      attr_accessor :default
    end
  end
end

module CustomMongo
  module Generators
    # Extend this class to add the "default" field.
    class ModelGenerator < ::Mongoid::Generators::ModelGenerator
      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

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

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
cqm-models-4.2.0 lib/generators/custom_mongo/model_generator.rb
cqm-models-3.1.2 lib/generators/custom_mongo/model_generator.rb
cqm-models-4.1.1 lib/generators/custom_mongo/model_generator.rb
cqm-models-4.1.0 lib/generators/custom_mongo/model_generator.rb
cqm-models-4.0.2 lib/generators/custom_mongo/model_generator.rb
cqm-models-4.0.1 lib/generators/custom_mongo/model_generator.rb
cqm-models-4.0.0 lib/generators/custom_mongo/model_generator.rb
cqm-models-3.1.1 lib/generators/custom_mongo/model_generator.rb
cqm-models-3.1.0 lib/generators/custom_mongo/model_generator.rb
cqm-models-3.0.6 lib/generators/custom_mongo/model_generator.rb
cqm-models-3.0.5 lib/generators/custom_mongo/model_generator.rb
cqm-models-3.0.4 lib/generators/custom_mongo/model_generator.rb
cqm-models-3.0.3 lib/generators/custom_mongo/model_generator.rb
cqm-models-3.0.2 lib/generators/custom_mongo/model_generator.rb
cqm-models-3.0.1 lib/generators/custom_mongo/model_generator.rb
cqm-models-2.0.1 lib/generators/custom_mongo/model_generator.rb
cqm-models-3.0.0 lib/generators/custom_mongo/model_generator.rb
cqm-models-2.0.0 lib/generators/custom_mongo/model_generator.rb
cqm-models-1.1.1.0 lib/generators/custom_mongo/model_generator.rb