forked from rails/rails
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rails#6450 from iHiD/resource_generator_routes_master
Master branch: Fixed generated whitespace in routes when using namespaced resource. Merge pull request rails#7811 from iHiD/resource_generator_routes_master Fix the build (Broken scaffold routes test)
- Loading branch information
1 parent
628e38d
commit a02f67b
Showing
3 changed files
with
116 additions
and
7 deletions.
There are no files selected for viewing
45 changes: 41 additions & 4 deletions
45
railties/lib/rails/generators/rails/resource_route/resource_route_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,50 @@ | ||
module Rails | ||
module Generators | ||
class ResourceRouteGenerator < NamedBase | ||
|
||
# Properly nests namespaces passed into a generator | ||
# | ||
# $ rails generate resource admin/users/products | ||
# | ||
# should give you | ||
# | ||
# namespace :admin do | ||
# namespace :users | ||
# resources :products | ||
# end | ||
# end | ||
def add_resource_route | ||
return if options[:actions].present? | ||
route_config = regular_class_path.collect{ |namespace| "namespace :#{namespace} do " }.join(" ") | ||
route_config << "resources :#{file_name.pluralize}" | ||
route_config << " end" * regular_class_path.size | ||
route route_config | ||
|
||
# iterates over all namespaces and opens up blocks | ||
regular_class_path.each_with_index do |namespace, index| | ||
write("namespace :#{namespace} do", index + 1) | ||
end | ||
|
||
# inserts the primary resource | ||
write("resources :#{file_name.pluralize}", route_length + 1) | ||
|
||
# ends blocks | ||
regular_class_path.each_index do |index| | ||
write("end", route_length - index) | ||
end | ||
|
||
# route prepends two spaces onto the front of the string that is passed, this corrects that | ||
route route_string[2..-1] | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters