Sha256: 1da5f82dc2f663459ad40a394b9f231f3d05c2e07752653cd64aab67c7d71422

Contents?: true

Size: 1.63 KB

Versions: 21

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

require "rails/generators/resource_helpers"

module Rails
  module Generators
    class ScaffoldControllerGenerator < NamedBase # :nodoc:
      include ResourceHelpers

      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 :api, type: :boolean,
                         desc: "Generates API controller"

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

      def create_controller_files
        template_file = options.api? ? "api_controller.rb" : "controller.rb"
        template template_file, File.join("app/controllers", controller_class_path, "#{controller_file_name}_controller.rb")
      end

      hook_for :template_engine, as: :scaffold do |template_engine|
        invoke template_engine unless options.api?
      end

      hook_for :test_framework, as: :scaffold

      # Invoke the helper using the controller name (pluralized)
      hook_for :helper, 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

21 entries across 19 versions & 5 rubygems

Version Path
railties-6.0.2.2 lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb
argon-1.3.1 vendor/bundle/ruby/2.7.0/gems/railties-6.0.2.1/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb
symbolic_enum-1.1.5 vendor/bundle/ruby/2.7.0/gems/railties-6.0.2.1/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb
railties-6.0.2.1 lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb
railties-6.0.2 lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb
railties-6.0.2.rc2 lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/railties-6.0.0/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/railties-6.0.1/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb
railties-6.0.2.rc1 lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/railties-6.0.0/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/railties-6.0.1/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb
railties-6.0.1 lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/railties-6.0.0/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb
railties-6.0.1.rc1 lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb
chatops-rpc-0.0.1 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/railties-6.0.0/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/railties-6.0.0/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/railties-6.0.0/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb
zuora_connect_ui-0.8.3 vendor/ruby/2.6.0/gems/railties-6.0.0/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb
zuora_connect_ui-0.8.2 vendor/ruby/2.6.0/gems/railties-6.0.0/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb
railties-6.0.0 lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb