Sha256: 2eebe8bd664845960e216a00256ac98a0f4a89ef7999b9c24638c47f3813247a
Contents?: true
Size: 1.79 KB
Versions: 3
Compression:
Stored size: 1.79 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] 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
3 entries across 3 versions & 1 rubygems