Sha256: 7e280a8806abc7235b3ddf00a9322f77ef4fbb352f6373bd79eab38cd47b09ce

Contents?: true

Size: 1.32 KB

Versions: 21

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

module Rails
  module Generators
    class ControllerGenerator < NamedBase # :nodoc:
      argument :actions, type: :array, default: [], banner: "action action"
      class_option :skip_routes, type: :boolean, desc: "Don't add routes to config/routes.rb."
      class_option :helper, type: :boolean
      class_option :parent, type: :string, default: "ApplicationController", desc: "The parent class for the generated controller"

      check_class_collision suffix: "Controller"

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

      def add_routes
        return if options[:skip_routes]
        return if actions.empty?
        routing_code = actions.map { |action| %(get "#{file_name}/#{action}") }.join("\n")
        route routing_code, namespace: regular_class_path
      end

      hook_for :template_engine, :test_framework, :helper do |generator|
        invoke generator, [ remove_possible_suffix(name), actions ]
      end

      private
        def parent_class_name
          options[:parent]
        end

        def file_name
          @_file_name ||= remove_possible_suffix(super)
        end

        def remove_possible_suffix(name)
          name.sub(/_?controller$/i, "")
        end
    end
  end
end

Version data entries

21 entries across 21 versions & 2 rubygems

Version Path
railties-8.0.1 lib/rails/generators/rails/controller/controller_generator.rb
railties-8.0.0.1 lib/rails/generators/rails/controller/controller_generator.rb
railties-7.2.2.1 lib/rails/generators/rails/controller/controller_generator.rb
railties-8.0.0 lib/rails/generators/rails/controller/controller_generator.rb
railties-7.2.2 lib/rails/generators/rails/controller/controller_generator.rb
railties-8.0.0.rc2 lib/rails/generators/rails/controller/controller_generator.rb
railties-7.2.1.2 lib/rails/generators/rails/controller/controller_generator.rb
railties-8.0.0.rc1 lib/rails/generators/rails/controller/controller_generator.rb
railties-7.2.1.1 lib/rails/generators/rails/controller/controller_generator.rb
railties-8.0.0.beta1 lib/rails/generators/rails/controller/controller_generator.rb
omg-railties-8.0.0.alpha9 lib/rails/generators/rails/controller/controller_generator.rb
omg-railties-8.0.0.alpha8 lib/rails/generators/rails/controller/controller_generator.rb
omg-railties-8.0.0.alpha7 lib/rails/generators/rails/controller/controller_generator.rb
omg-railties-8.0.0.alpha4 lib/rails/generators/rails/controller/controller_generator.rb
omg-railties-8.0.0.alpha3 lib/rails/generators/rails/controller/controller_generator.rb
railties-7.2.1 lib/rails/generators/rails/controller/controller_generator.rb
railties-7.2.0 lib/rails/generators/rails/controller/controller_generator.rb
railties-7.2.0.rc1 lib/rails/generators/rails/controller/controller_generator.rb
railties-7.2.0.beta3 lib/rails/generators/rails/controller/controller_generator.rb
railties-7.2.0.beta2 lib/rails/generators/rails/controller/controller_generator.rb