Sha256: 253df104781ada93cd75d8cf2815227da2c636d6736a5c776e927183d7e1ed8d

Contents?: true

Size: 1.73 KB

Versions: 2

Compression:

Stored size: 1.73 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 write_factory
        insert_into_file 'spec/factories/factories.rb', <<-FACTORY, before: /end\n\z/
  factory #{ model.name(:symbol) }

        FACTORY
      end

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

      no_commands do
        def belongs_tos
          model.application_model.get_belongs_tos_for model.name
        end

        def has_manys
          model.application_model.get_has_manys_for model.name
        end

        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.4.1 lib/generators/katapult/model/model_generator.rb
katapult-0.4.0 lib/generators/katapult/model/model_generator.rb