|
3 | 3 | module WebAppTheme |
4 | 4 | class ThemedGenerator < Rails::Generators::Base |
5 | 5 | source_root File.expand_path('../templates', __FILE__) |
| 6 | + |
| 7 | + argument :controller_path, :type => :string |
| 8 | + argument :model_name, :type => :string, :required => false |
| 9 | + |
| 10 | + class_option :layout, :type => :string, :desc => 'Specify the layout name' |
| 11 | + class_option :engine, :type => :string, :default => 'erb', :desc => 'Specify the template engine' |
6 | 12 |
|
7 | 13 | def initialize(args, *options) |
8 | 14 | super(args, *options) |
9 | | - initialize_views_variables(args) |
| 15 | + initialize_views_variables |
10 | 16 | end |
11 | 17 |
|
12 | | - def copy_views |
| 18 | + def copy_views |
13 | 19 | template 'view_tables.html.erb', File.join('app/views', @controller_file_path, 'index.html.erb') |
14 | 20 | template 'view_new.html.erb', File.join('app/views', @controller_file_path, 'new.html.erb') |
15 | 21 | template 'view_edit.html.erb', File.join('app/views', @controller_file_path, 'edit.html.erb') |
16 | 22 | template 'view_form.html.erb', File.join('app/views', @controller_file_path, '_form.html.erb') |
17 | 23 | template 'view_show.html.erb', File.join('app/views', @controller_file_path, 'show.html.erb') |
18 | 24 | template 'view_sidebar.html.erb', File.join('app/views', @controller_file_path, '_sidebar.html.erb') |
| 25 | + unless options.layout.blank? |
| 26 | + gsub_file(File.join('app/views/layouts', "#{options[:layout]}.html.#{options.engine}"), /\<div\s+id=\"main-navigation\">.*\<\/ul\>/mi) do |match| |
| 27 | + match.gsub!(/\<\/ul\>/, "") |
| 28 | + if @engine.to_s =~ /haml/ |
| 29 | + %|#{match} |
| 30 | + %li{:class => controller.controller_path == '#{@controller_file_path}' ? 'active' : '' } |
| 31 | + %a{:href => #{controller_routing_path}_path} #{plural_model_name} |
| 32 | + </ul>| |
| 33 | + else |
| 34 | + %|#{match} <li class="<%= controller.controller_path == '#{@controller_file_path}' ? 'active' : '' %>"><a href="<%= #{controller_routing_path}_path %>">#{plural_model_name}</a></li></ul>| |
| 35 | + end |
| 36 | + end |
| 37 | + end |
19 | 38 | end |
20 | 39 |
|
21 | 40 | protected |
22 | 41 |
|
23 | | - def initialize_views_variables(args) |
24 | | - @controller_path = args.shift |
25 | | - @model_name = args.shift |
26 | | - @base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_path) |
| 42 | + def initialize_views_variables |
| 43 | + @base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(controller_path) |
27 | 44 | @controller_routing_path = @controller_file_path.gsub(/\//, '_') |
28 | 45 | @model_name = @base_name.singularize unless @model_name |
29 | 46 | @model_name = @model_name.camelize |
|
0 commit comments