Sha256: e13193f3137c77299727272a392ff9f482045a6f6b08228dfccd671ffa838ac8

Contents?: true

Size: 1.83 KB

Versions: 39

Compression:

Stored size: 1.83 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?
        route generate_routing_code
      end

      hook_for :template_engine, :test_framework, :helper, :assets

      private

        # This method creates nested route entry for namespaced resources.
        # For eg. rails g controller foo/bar/baz index show
        # Will generate -
        # namespace :foo do
        #   namespace :bar do
        #     get 'baz/index'
        #     get 'baz/show'
        #   end
        # end
        def generate_routing_code
          depth = 0
          lines = []

          # Create 'namespace' ladder
          # namespace :foo do
          #   namespace :bar do
          regular_class_path.each do |ns|
            lines << indent("namespace :#{ns} do\n", depth * 2)
            depth += 1
          end

          # Create route
          #     get 'baz/index'
          #     get 'baz/show'
          actions.each do |action|
            lines << indent(%{get '#{file_name}/#{action}'\n}, depth * 2)
          end

          # Create `end` ladder
          #   end
          # end
          until depth.zero?
            depth -= 1
            lines << indent("end\n", depth * 2)
          end

          lines.join
        end
    end
  end
end

Version data entries

39 entries across 39 versions & 3 rubygems

Version Path
railties-5.2.8.1 lib/rails/generators/rails/controller/controller_generator.rb
railties-5.2.8 lib/rails/generators/rails/controller/controller_generator.rb
railties-5.2.7.1 lib/rails/generators/rails/controller/controller_generator.rb
railties-5.2.7 lib/rails/generators/rails/controller/controller_generator.rb
railties-5.2.6.3 lib/rails/generators/rails/controller/controller_generator.rb
railties-5.2.6.2 lib/rails/generators/rails/controller/controller_generator.rb
railties-5.2.6.1 lib/rails/generators/rails/controller/controller_generator.rb
railties-5.2.6 lib/rails/generators/rails/controller/controller_generator.rb
railties-5.2.4.6 lib/rails/generators/rails/controller/controller_generator.rb
railties-5.2.5 lib/rails/generators/rails/controller/controller_generator.rb
railties-5.2.4.5 lib/rails/generators/rails/controller/controller_generator.rb
railties-5.2.4.4 lib/rails/generators/rails/controller/controller_generator.rb
railties-5.2.4.3 lib/rails/generators/rails/controller/controller_generator.rb
railties-5.2.4.2 lib/rails/generators/rails/controller/controller_generator.rb
railties-5.2.4.1 lib/rails/generators/rails/controller/controller_generator.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/railties-5.2.3/lib/rails/generators/rails/controller/controller_generator.rb
railties-5.2.4 lib/rails/generators/rails/controller/controller_generator.rb
railties-5.2.4.rc1 lib/rails/generators/rails/controller/controller_generator.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/railties-5.2.3/lib/rails/generators/rails/controller/controller_generator.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/railties-5.2.3/lib/rails/generators/rails/controller/controller_generator.rb