Skip to content
Newer
Older
100644 55 lines (46 sloc) 1.58 KB
e234624 @josevalim Added Controller Generators.
josevalim authored
1 module Rails
2 module Generators
521e2ee update Rails::Generators docs [ci skip]
Francesco Rodriguez authored
3 class ControllerGenerator < NamedBase # :nodoc:
5ad7f8a @robin850 Use Ruby 1.9 Hash syntax in railties
robin850 authored
4 argument :actions, type: :array, default: [], banner: "action action"
5 check_class_collision suffix: "Controller"
e234624 @josevalim Added Controller Generators.
josevalim authored
6
7 def create_controller_files
bac0826 @drogus Refactor namespaced generators to use module_namespacing block helper
drogus authored
8 template 'controller.rb', File.join('app/controllers', class_path, "#{file_name}_controller.rb")
e234624 @josevalim Added Controller Generators.
josevalim authored
9 end
10
562154f @josevalim Controller generator now creates routes as well [#4233 state:resolved]
josevalim authored
11 def add_routes
12 actions.reverse.each do |action|
53c5794 @prathamesh-sonpatki Generate namespaced routes correctly for generators. Fix for #11532
prathamesh-sonpatki authored
13 route generate_routing_code(action)
562154f @josevalim Controller generator now creates routes as well [#4233 state:resolved]
josevalim authored
14 end
15 end
16
6c91b69 @dhh The controller and resource generators will now automatically produce…
dhh authored
17 hook_for :template_engine, :test_framework, :helper, :assets
53c5794 @prathamesh-sonpatki Generate namespaced routes correctly for generators. Fix for #11532
prathamesh-sonpatki authored
18
19 private
20
21 # This method creates nested route entry for namespaced resources.
22 # For eg. rails g controller foo/bar/baz index
23 # Will generate -
24 # namespace :foo do
25 # namespace :bar do
26 # get "baz/index"
27 # end
28 # end
29 def generate_routing_code(action)
30 depth = class_path.length
31 # Create 'namespace' ladder
32 # namespace :foo do
33 # namespace :bar do
34 namespace_ladder = class_path.each_with_index.map do |ns, i|
30aa2b9 @wangjohn Using indent method to refactor controller generator.
wangjohn authored
35 indent("namespace :#{ns} do\n", i * 2)
53c5794 @prathamesh-sonpatki Generate namespaced routes correctly for generators. Fix for #11532
prathamesh-sonpatki authored
36 end.join
37
38 # Create route
39 # get "baz/index"
30aa2b9 @wangjohn Using indent method to refactor controller generator.
wangjohn authored
40 route = indent(%{get "#{file_name}/#{action}"\n}, depth * 2)
53c5794 @prathamesh-sonpatki Generate namespaced routes correctly for generators. Fix for #11532
prathamesh-sonpatki authored
41
42 # Create `end` ladder
43 # end
44 # end
45 end_ladder = (1..depth).reverse_each.map do |i|
30aa2b9 @wangjohn Using indent method to refactor controller generator.
wangjohn authored
46 indent("end\n", i * 2)
53c5794 @prathamesh-sonpatki Generate namespaced routes correctly for generators. Fix for #11532
prathamesh-sonpatki authored
47 end.join
48
49 # Combine the 3 parts to generate complete route entry
50 namespace_ladder + route + end_ladder
51 end
e234624 @josevalim Added Controller Generators.
josevalim authored
52 end
53 end
54 end
Something went wrong with that request. Please try again.