Sha256: 95101b85fada190bf21db3622ff193e4ca294d5c41408b3dc70c4116970a5a21

Contents?: true

Size: 1.18 KB

Versions: 37

Compression:

Stored size: 1.18 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 :assets, type: :boolean

      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, :assets do |generator|
        invoke generator, [ remove_possible_suffix(name), actions ]
      end

      private
        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

37 entries across 37 versions & 4 rubygems

Version Path
railties-6.1.7.10 lib/rails/generators/rails/controller/controller_generator.rb
railties-6.1.7.9 lib/rails/generators/rails/controller/controller_generator.rb
railties-6.1.7.8 lib/rails/generators/rails/controller/controller_generator.rb
railties-6.1.7.7 lib/rails/generators/rails/controller/controller_generator.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/railties-6.1.6.1/lib/rails/generators/rails/controller/controller_generator.rb
railties-6.1.7.6 lib/rails/generators/rails/controller/controller_generator.rb
railties-6.1.7.5 lib/rails/generators/rails/controller/controller_generator.rb
railties-6.1.7.4 lib/rails/generators/rails/controller/controller_generator.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/railties-6.1.6.1/lib/rails/generators/rails/controller/controller_generator.rb
railties-6.1.7.3 lib/rails/generators/rails/controller/controller_generator.rb
railties-6.1.7.2 lib/rails/generators/rails/controller/controller_generator.rb
railties-6.1.7.1 lib/rails/generators/rails/controller/controller_generator.rb
railties-6.1.7 lib/rails/generators/rails/controller/controller_generator.rb
railties-6.1.6.1 lib/rails/generators/rails/controller/controller_generator.rb
railties-6.1.6 lib/rails/generators/rails/controller/controller_generator.rb
railties-6.1.5.1 lib/rails/generators/rails/controller/controller_generator.rb
railties-6.1.5 lib/rails/generators/rails/controller/controller_generator.rb
railties-6.1.4.7 lib/rails/generators/rails/controller/controller_generator.rb
railties-6.1.4.6 lib/rails/generators/rails/controller/controller_generator.rb
railties-6.1.4.5 lib/rails/generators/rails/controller/controller_generator.rb