Sha256: 93d42b139861e7f0a0c5e2ea8f3bfaf4316a532f9e6179e35cd795c8e0151970

Contents?: true

Size: 1.37 KB

Versions: 2

Compression:

Stored size: 1.37 KB

Contents

# Generate a Rails model, including migration and spec.

require 'katapult/generator'
require 'generators/katapult/model_specs/model_specs_generator'

module Katapult
  module Generators
    class ModelGenerator < Katapult::Generator

      desc 'Generate a Rails Model'

      check_class_collision
      source_root File.expand_path('../templates', __FILE__)


      def create_migration_file
        migration_name = "create_#{table_name}"
        migration_attributes = model.db_fields.map(&:for_migration)

        args = [migration_name] + migration_attributes
        options = { timestamps: true, force: true }
        invoke 'active_record:migration', args, options
      end

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

      def write_traits
        template 'app/models/shared/does_flag.rb' if flag_attrs.any?
      end

      def generate_unit_tests
        Generators::ModelSpecsGenerator.new(model).invoke_all
      end

      no_commands do
        def flag_attrs
          model.attrs.select(&:flag?)
        end

        def defaults
          {}.tap do |defaults|
            model.attrs.select(&:has_defaults?).each do |attr|
              defaults[attr.name.to_sym] = attr.default
            end
          end
        end
      end

      private

      def model
        @element
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
katapult-0.3.0 lib/generators/katapult/model/model_generator.rb
katapult-0.2.0 lib/generators/katapult/model/model_generator.rb