mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Optimize routes indentation
This commit is contained in:
parent
788f46d486
commit
d1dabde8a2
5 changed files with 38 additions and 33 deletions
|
@ -1,3 +1,7 @@
|
||||||
|
* Optimize routes indentation.
|
||||||
|
|
||||||
|
*Yoshiyuki Hirano*
|
||||||
|
|
||||||
* Optimize indentation for generator actions.
|
* Optimize indentation for generator actions.
|
||||||
|
|
||||||
*Yoshiyuki Hirano*
|
*Yoshiyuki Hirano*
|
||||||
|
|
|
@ -244,7 +244,7 @@ module Rails
|
||||||
sentinel = /\.routes\.draw do\s*\n/m
|
sentinel = /\.routes\.draw do\s*\n/m
|
||||||
|
|
||||||
in_root do
|
in_root do
|
||||||
inject_into_file "config/routes.rb", " #{routing_code}\n", after: sentinel, verbose: false, force: false
|
inject_into_file "config/routes.rb", optimize_indentation(routing_code, 2), after: sentinel, verbose: false, force: false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -13,12 +13,8 @@ module Rails
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_routes
|
def add_routes
|
||||||
unless options[:skip_routes]
|
return if options[:skip_routes]
|
||||||
actions.reverse_each do |action|
|
route generate_routing_code(actions)
|
||||||
# route prepends two spaces onto the front of the string that is passed, this corrects that.
|
|
||||||
route indent(generate_routing_code(action), 2)[2..-1]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
hook_for :template_engine, :test_framework, :helper, :assets
|
hook_for :template_engine, :test_framework, :helper, :assets
|
||||||
|
@ -26,11 +22,12 @@ module Rails
|
||||||
private
|
private
|
||||||
|
|
||||||
# This method creates nested route entry for namespaced resources.
|
# This method creates nested route entry for namespaced resources.
|
||||||
# For eg. rails g controller foo/bar/baz index
|
# For eg. rails g controller foo/bar/baz index show
|
||||||
# Will generate -
|
# Will generate -
|
||||||
# namespace :foo do
|
# namespace :foo do
|
||||||
# namespace :bar do
|
# namespace :bar do
|
||||||
# get 'baz/index'
|
# get 'baz/index'
|
||||||
|
# get 'baz/show'
|
||||||
# end
|
# end
|
||||||
# end
|
# end
|
||||||
def generate_routing_code(action)
|
def generate_routing_code(action)
|
||||||
|
@ -47,7 +44,9 @@ module Rails
|
||||||
|
|
||||||
# Create route
|
# Create route
|
||||||
# get 'baz/index'
|
# get 'baz/index'
|
||||||
|
actions.each do |action|
|
||||||
lines << indent(%{get '#{file_name}/#{action}'\n}, depth * 2)
|
lines << indent(%{get '#{file_name}/#{action}'\n}, depth * 2)
|
||||||
|
end
|
||||||
|
|
||||||
# Create `end` ladder
|
# Create `end` ladder
|
||||||
# end
|
# end
|
||||||
|
|
|
@ -15,36 +15,31 @@ module Rails
|
||||||
def add_resource_route
|
def add_resource_route
|
||||||
return if options[:actions].present?
|
return if options[:actions].present?
|
||||||
|
|
||||||
# iterates over all namespaces and opens up blocks
|
depth = 0
|
||||||
regular_class_path.each_with_index do |namespace, index|
|
lines = []
|
||||||
write("namespace :#{namespace} do", index + 1)
|
|
||||||
|
# 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
|
end
|
||||||
|
|
||||||
# inserts the primary resource
|
# inserts the primary resource
|
||||||
write("resources :#{file_name.pluralize}", route_length + 1)
|
# Create route
|
||||||
|
# resources 'products'
|
||||||
|
lines << indent(%{resources :#{file_name.pluralize}\n}, depth * 2)
|
||||||
|
|
||||||
# ends blocks
|
# Create `end` ladder
|
||||||
regular_class_path.each_index do |index|
|
# end
|
||||||
write("end", route_length - index)
|
# end
|
||||||
|
until depth.zero?
|
||||||
|
depth -= 1
|
||||||
|
lines << indent("end\n", depth * 2)
|
||||||
end
|
end
|
||||||
|
|
||||||
# route prepends two spaces onto the front of the string that is passed, this corrects that.
|
route lines.join
|
||||||
# Also it adds a \n to the end of each line, as route already adds that
|
|
||||||
# we need to correct that too.
|
|
||||||
route route_string[2..-2]
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
def route_string
|
|
||||||
@route_string ||= ""
|
|
||||||
end
|
|
||||||
|
|
||||||
def write(str, indent)
|
|
||||||
route_string << "#{" " * indent}#{str}\n"
|
|
||||||
end
|
|
||||||
|
|
||||||
def route_length
|
|
||||||
regular_class_path.length
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -100,4 +100,11 @@ class ControllerGeneratorTest < Rails::Generators::TestCase
|
||||||
assert_match(/^ namespace :admin do\n get 'dashboard\/index'\n end$/, route)
|
assert_match(/^ namespace :admin do\n get 'dashboard\/index'\n end$/, route)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_namespaced_routes_with_multiple_actions_are_created_in_routes
|
||||||
|
run_generator ["admin/dashboard", "index", "show"]
|
||||||
|
assert_file "config/routes.rb" do |route|
|
||||||
|
assert_match(/^ namespace :admin do\n get 'dashboard\/index'\n get 'dashboard\/show'\n end$/, route)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue