blob: 822f35fb424c815da3ee1db37ae3a9418a34e311 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
module Rails
module Generators
class ControllerGenerator < NamedBase # :nodoc:
argument :actions, type: :array, default: [], banner: "action action"
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
actions.reverse.each do |action|
route generate_routing_code(action)
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 = class_path.length
# Create 'namespace' ladder
# namespace :foo do
# namespace :bar do
namespace_ladder = class_path.each_with_index.map do |ns, i|
%{#{" " * i * 2}namespace :#{ns} do\n }
end.join
# Create route
# get "baz/index"
route = %{#{" " * depth * 2}get "#{file_name}/#{action}"\n}
# Create `end` ladder
# end
# end
end_ladder = (1..depth).reverse_each.map do |i|
"#{" " * i * 2}end\n"
end.join
# Combine the 3 parts to generate complete route entry
namespace_ladder + route + end_ladder
end
end
end
end
|