Permalink
Browse files

added layout option to themed generator

  • Loading branch information...
pilu committed Sep 27, 2010
1 parent 77f8fc0 commit 39409644eb4852ac0543cf7432ace0d856aa964e
Showing with 27 additions and 8 deletions.
  1. +4 −2 README.md
  2. +23 −6 lib/generators/web_app_theme/themed/themed_generator.rb
View
@@ -58,11 +58,13 @@ If you have a controller named like the plural of the used model you can specify
rails g web_app_theme:themed posts # you have a model named Post and a controller named PostsController
- script/generate themed admin/gallery_pictures # you have a model named GalleryPicture and a controller named Admin::GalleryPicturesController
+ rails g web_app_theme:themed admin/gallery_pictures # you have a model named GalleryPicture and a controller named Admin::GalleryPicturesController
+
+ rails g web_app_theme:themed admin/pictures GalleryPicture # you have a model named GalleryPicture and a controller named Admin::PicturesController
Use the `--layout` option specifying the previously generated layout to add a link to the controller you are working on:
- script/generate themed posts --layout=admin # you will see the `Posts` link in the navigation
+ rails g web_app_theme:themed posts --layout=admin # you will see the `Posts` link in the navigation
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:
@@ -3,27 +3,44 @@
module WebAppTheme
class ThemedGenerator < Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)
+
+ argument :controller_path, :type => :string
+ argument :model_name, :type => :string, :required => false
+
+ class_option :layout, :type => :string, :desc => 'Specify the layout name'
+ class_option :engine, :type => :string, :default => 'erb', :desc => 'Specify the template engine'
def initialize(args, *options)
super(args, *options)
- initialize_views_variables(args)
+ initialize_views_variables
end
- def copy_views
+ def copy_views
template 'view_tables.html.erb', File.join('app/views', @controller_file_path, 'index.html.erb')
template 'view_new.html.erb', File.join('app/views', @controller_file_path, 'new.html.erb')
template 'view_edit.html.erb', File.join('app/views', @controller_file_path, 'edit.html.erb')
template 'view_form.html.erb', File.join('app/views', @controller_file_path, '_form.html.erb')
template 'view_show.html.erb', File.join('app/views', @controller_file_path, 'show.html.erb')
template 'view_sidebar.html.erb', File.join('app/views', @controller_file_path, '_sidebar.html.erb')
+ unless options.layout.blank?
+ gsub_file(File.join('app/views/layouts', "#{options[:layout]}.html.#{options.engine}"), /\<div\s+id=\"main-navigation\">.*\<\/ul\>/mi) do |match|
+ match.gsub!(/\<\/ul\>/, "")
+ if @engine.to_s =~ /haml/
+ %|#{match}
+ %li{:class => controller.controller_path == '#{@controller_file_path}' ? 'active' : '' }
+ %a{:href => #{controller_routing_path}_path} #{plural_model_name}
+ </ul>|
+ else
+ %|#{match} <li class="<%= controller.controller_path == '#{@controller_file_path}' ? 'active' : '' %>"><a href="<%= #{controller_routing_path}_path %>">#{plural_model_name}</a></li></ul>|
+ end
+ end
+ end
end
protected
- def initialize_views_variables(args)
- @controller_path = args.shift
- @model_name = args.shift
- @base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_path)
+ def initialize_views_variables
+ @base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(controller_path)
@controller_routing_path = @controller_file_path.gsub(/\//, '_')
@model_name = @base_name.singularize unless @model_name
@model_name = @model_name.camelize

0 comments on commit 3940964

Please sign in to comment.