Skip to content

Commit 3940964

Browse files
committed
added layout option to themed generator
1 parent 77f8fc0 commit 3940964

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,13 @@ If you have a controller named like the plural of the used model you can specify
5858

5959
rails g web_app_theme:themed posts # you have a model named Post and a controller named PostsController
6060

61-
script/generate themed admin/gallery_pictures # you have a model named GalleryPicture and a controller named Admin::GalleryPicturesController
61+
rails g web_app_theme:themed admin/gallery_pictures # you have a model named GalleryPicture and a controller named Admin::GalleryPicturesController
62+
63+
rails g web_app_theme:themed admin/pictures GalleryPicture # you have a model named GalleryPicture and a controller named Admin::PicturesController
6264

6365
Use the `--layout` option specifying the previously generated layout to add a link to the controller you are working on:
6466

65-
script/generate themed posts --layout=admin # you will see the `Posts` link in the navigation
67+
rails g web_app_theme:themed posts --layout=admin # you will see the `Posts` link in the navigation
6668

6769
If the controller has a name different to the model used, specify the controller path in the first parameter and the model name in the second one:
6870

lib/generators/web_app_theme/themed/themed_generator.rb

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,44 @@
33
module WebAppTheme
44
class ThemedGenerator < Rails::Generators::Base
55
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'
612

713
def initialize(args, *options)
814
super(args, *options)
9-
initialize_views_variables(args)
15+
initialize_views_variables
1016
end
1117

12-
def copy_views
18+
def copy_views
1319
template 'view_tables.html.erb', File.join('app/views', @controller_file_path, 'index.html.erb')
1420
template 'view_new.html.erb', File.join('app/views', @controller_file_path, 'new.html.erb')
1521
template 'view_edit.html.erb', File.join('app/views', @controller_file_path, 'edit.html.erb')
1622
template 'view_form.html.erb', File.join('app/views', @controller_file_path, '_form.html.erb')
1723
template 'view_show.html.erb', File.join('app/views', @controller_file_path, 'show.html.erb')
1824
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
1938
end
2039

2140
protected
2241

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)
2744
@controller_routing_path = @controller_file_path.gsub(/\//, '_')
2845
@model_name = @base_name.singularize unless @model_name
2946
@model_name = @model_name.camelize

0 commit comments

Comments
 (0)