Sha256: 733104c2d89f2867418663777226b08c56eff01e19ea4b85d6c28946a6dbf734

Contents?: true

Size: 1.45 KB

Versions: 2

Compression:

Stored size: 1.45 KB

Contents

require "rails/generators/active_record"
require "generators/jobshop/orm_helpers"

module ActiveRecord
  module Generators
    class JobshopGenerator < ActiveRecord::Generators::Base
      include Jobshop::Generators::OrmHelpers

      source_root File.expand_path("../templates", __FILE__)

      def copy_jobshop_migration
        if model_exists_and_invoking? || migration_exists_and_revoking?
          migration_template "migration_existing.rb", "db/migrate/add_jobshop_to_#{table_name}.rb"
        else
          migration_template "migration.rb", "db/migrate/jobshop_create_#{table_name}.rb"
        end
      end

      def generate_model
        invoke "active_record:model", [name], migration: false unless model_exists_and_invoking?
        invoke "devise:install"
      end

      def inject_jobshop_content
        content = <<-CONTENT.strip_heredoc
          jobshop_user
        CONTENT

        class_path = namespaced? ? class_name.to_s.split("::") : [ class_name ]
        indent_depth = class_path.size - 1
        content = content.split("\n").map { |line| "  " * indent_depth + line } .join("\n") << "\n"
        inject_into_class(model_path, class_path.last, content) if model_exists?
      end

      no_tasks do
        def model_exists_and_invoking?
          model_exists? && behavior == :invoke
        end

        def migration_exists_and_revoking?
          migration_exists?(table_name) && behavior == :revoke
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jobshop-0.0.1 lib/generators/active_record/jobshop_generator.rb
jobshop-0.0.0 lib/generators/active_record/jobshop_generator.rb