Sha256: 18a31310881ccd1754f6623e288003c7645c857925920664165e494908adfe1a

Contents?: true

Size: 1.99 KB

Versions: 6

Compression:

Stored size: 1.99 KB

Contents

require "rails/generators/resource_helpers"
require "inertia_rails_contrib/generators_helper"

module Inertia
  module Generators
    # This class is a modified copy of Rails::Generators::ScaffoldControllerGenerator.
    # We don't use inheritance because some gems (i.e. jsbuilder) monkey-patch it.
    class ScaffoldControllerGenerator < Rails::Generators::NamedBase
      include InertiaRailsContrib::GeneratorsHelper
      include Rails::Generators::ResourceHelpers

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

      check_class_collision suffix: "Controller"

      class_option :helper, type: :boolean
      class_option :orm, banner: "NAME", type: :string, required: true,
        desc: "ORM to generate the controller for"

      class_option :skip_routes, type: :boolean, desc: "Don't add routes to config/routes.rb."

      argument :attributes, type: :array, default: [], banner: "field:type field:type"

      def create_controller_files
        template "controller.rb", File.join("app/controllers", controller_class_path, "#{controller_file_name}_controller.rb")
      end

      hook_for :inertia_templates, as: :scaffold, required: true, default: InertiaRailsContrib::GeneratorsHelper.guess_inertia_template

      hook_for :resource_route, in: :rails, required: true do |route|
        invoke route unless options.skip_routes?
      end

      hook_for :test_framework, in: :rails, as: :scaffold

      # Invoke the helper using the controller name (pluralized)
      hook_for :helper, in: :rails, as: :scaffold do |invoked|
        invoke invoked, [controller_name]
      end

      private

      def permitted_params
        attachments, others = attributes_names.partition { |name| attachments?(name) }
        params = others.map { |name| ":#{name}" }
        params += attachments.map { |name| "#{name}: []" }
        params.join(", ")
      end

      def attachments?(name)
        attribute = attributes.find { |attr| attr.name == name }
        attribute&.attachments?
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
inertia_rails-contrib-0.3.0 lib/generators/inertia/scaffold_controller/scaffold_controller_generator.rb
inertia_rails-contrib-0.2.2 lib/generators/inertia/scaffold_controller/scaffold_controller_generator.rb
inertia_rails-contrib-0.2.1 lib/generators/inertia/scaffold_controller/scaffold_controller_generator.rb
inertia_rails-contrib-0.2.0 lib/generators/inertia/scaffold_controller/scaffold_controller_generator.rb
inertia_rails-contrib-0.1.1 lib/generators/inertia/scaffold_controller/scaffold_controller_generator.rb
inertia_rails-contrib-0.1.0 lib/generators/inertia/scaffold_controller/scaffold_controller_generator.rb