Sha256: 8899f4c79da826783b6aa1840cb6298fe8d094cc79f4201e43748fab2680d215

Contents?: true

Size: 1.94 KB

Versions: 13

Compression:

Stored size: 1.94 KB

Contents

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
        unless options[:skip_routes]
          actions.reverse_each do |action|
            # route prepends two spaces onto the front of the string that is passed, this corrects that.
            route generate_routing_code(action)[2..-1]
          end
        end
      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
        # Will generate -
        # namespace :foo do
        #   namespace :bar do
        #     get 'baz/index'
        #   end
        # end
        def generate_routing_code(action)
          depth = regular_class_path.length
          # Create 'namespace' ladder
          # namespace :foo do
          #   namespace :bar do
          namespace_ladder = regular_class_path.each_with_index.map do |ns, i|
            indent("  namespace :#{ns} do\n", i * 2)
          end.join

          # Create route
          #     get 'baz/index'
          route = indent(%{  get '#{file_name}/#{action}'\n}, depth * 2)

          # Create `end` ladder
          #   end
          # end
          end_ladder = (1..depth).reverse_each.map do |i|
            indent("end\n", i * 2)
          end.join

          # Combine the 3 parts to generate complete route entry
          namespace_ladder + route + end_ladder
        end
    end
  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
railties-5.0.7.2 lib/rails/generators/rails/controller/controller_generator.rb
railties-5.0.7.1 lib/rails/generators/rails/controller/controller_generator.rb
railties-5.0.7 lib/rails/generators/rails/controller/controller_generator.rb
railties-5.0.6 lib/rails/generators/rails/controller/controller_generator.rb
railties-5.0.6.rc1 lib/rails/generators/rails/controller/controller_generator.rb
railties-5.0.5 lib/rails/generators/rails/controller/controller_generator.rb
railties-5.0.5.rc2 lib/rails/generators/rails/controller/controller_generator.rb
railties-5.0.5.rc1 lib/rails/generators/rails/controller/controller_generator.rb
railties-5.0.4 lib/rails/generators/rails/controller/controller_generator.rb
railties-5.0.4.rc1 lib/rails/generators/rails/controller/controller_generator.rb
railties-5.0.3 lib/rails/generators/rails/controller/controller_generator.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/railties-5.0.2/lib/rails/generators/rails/controller/controller_generator.rb
railties-5.0.2 lib/rails/generators/rails/controller/controller_generator.rb