From ed4c32338710f676f46a837188cec4efa9b46219 Mon Sep 17 00:00:00 2001 From: Andrea Franz Date: Mon, 27 Sep 2010 21:39:54 +0200 Subject: [PATCH] basic themed generator --- README.md | 3 +- .../templates/layout_admin.html.erb | 45 ------------ .../templates/layout_sign.html.erb | 15 ---- .../web_app_theme/theme/theme_generator.rb | 4 +- .../themed/templates/view_edit.html.erb | 19 +++++ .../themed/templates/view_form.html.erb | 13 ++++ .../themed/templates/view_new.html.erb | 18 +++++ .../themed/templates/view_show.html.erb | 25 +++++++ .../themed/templates/view_sidebar.html.erb | 13 ++++ .../themed/templates/view_signin.html.erb | 36 ++++++++++ .../themed/templates/view_signup.html.erb | 52 ++++++++++++++ .../themed/templates/view_tables.html.erb | 54 ++++++++++++++ .../themed/templates/view_text.html.erb | 18 +++++ .../web_app_theme/themed/themed_generator.rb | 70 +++++++++++++++++++ 14 files changed, 322 insertions(+), 63 deletions(-) delete mode 100644 lib/generators/web_app_theme/templates/layout_admin.html.erb delete mode 100644 lib/generators/web_app_theme/templates/layout_sign.html.erb create mode 100644 lib/generators/web_app_theme/themed/templates/view_edit.html.erb create mode 100644 lib/generators/web_app_theme/themed/templates/view_form.html.erb create mode 100644 lib/generators/web_app_theme/themed/templates/view_new.html.erb create mode 100644 lib/generators/web_app_theme/themed/templates/view_show.html.erb create mode 100644 lib/generators/web_app_theme/themed/templates/view_sidebar.html.erb create mode 100644 lib/generators/web_app_theme/themed/templates/view_signin.html.erb create mode 100644 lib/generators/web_app_theme/themed/templates/view_signup.html.erb create mode 100644 lib/generators/web_app_theme/themed/templates/view_tables.html.erb create mode 100644 lib/generators/web_app_theme/themed/templates/view_text.html.erb create mode 100644 lib/generators/web_app_theme/themed/themed_generator.rb diff --git a/README.md b/README.md index 4d094ed..bbcb04e 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ Start creating your controllers manually or with a scaffold, and then use the `t If you have a controller named like the plural of the used model you can specify just the first parameter: - script/generate themed posts # you have a model named Post and a controller named PostsController + 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 @@ -93,6 +93,7 @@ If you want to show form error messages inside the generated forms, use the foll end If you want to have translated pages, simple create in your locale.yml the keys just like config/locales/en_us.yml example. + en_us: web-app-theme: save: Save diff --git a/lib/generators/web_app_theme/templates/layout_admin.html.erb b/lib/generators/web_app_theme/templates/layout_admin.html.erb deleted file mode 100644 index e3e660a..0000000 --- a/lib/generators/web_app_theme/templates/layout_admin.html.erb +++ /dev/null @@ -1,45 +0,0 @@ - - - - <%= options.app_name %> - <%%= stylesheet_link_tag "web-app-theme/base", "web-app-theme/themes/<%= options.theme %>/style", "web-app-theme/override", :cache => true %> - <%%= csrf_meta_tag %> - - -
- -
-
- <%% flash.each do |type, message| -%> -
-

<%%= message %>

-
- <%% end -%> -
-
- <%%= yield %> - -
- -
-
- - diff --git a/lib/generators/web_app_theme/templates/layout_sign.html.erb b/lib/generators/web_app_theme/templates/layout_sign.html.erb deleted file mode 100644 index 18cc3c7..0000000 --- a/lib/generators/web_app_theme/templates/layout_sign.html.erb +++ /dev/null @@ -1,15 +0,0 @@ - - - - <%= options.app_name %> - <%%= stylesheet_link_tag "web-app-theme/base", "web-app-theme/themes/<%= options.theme %>/style", "web-app-theme/override", :cache => true %> - <%%= csrf_meta_tag %> - - -
-
- <%%= yield %> -
-
- - diff --git a/lib/generators/web_app_theme/theme/theme_generator.rb b/lib/generators/web_app_theme/theme/theme_generator.rb index 0c5157e..20f67d4 100644 --- a/lib/generators/web_app_theme/theme/theme_generator.rb +++ b/lib/generators/web_app_theme/theme/theme_generator.rb @@ -1,6 +1,6 @@ module WebAppTheme class ThemeGenerator < Rails::Generators::Base - source_root File.expand_path('../../templates', __FILE__) + source_root File.expand_path('../templates', __FILE__) argument :layout_name, :type => :string, :default => 'application' @@ -33,7 +33,7 @@ def copy_theme_stylesheets protected def stylesheets_path - "../../../../stylesheets" + "../../../../../stylesheets" end def generate_haml_layout(admin_layout_name) diff --git a/lib/generators/web_app_theme/themed/templates/view_edit.html.erb b/lib/generators/web_app_theme/themed/templates/view_edit.html.erb new file mode 100644 index 0000000..52914e4 --- /dev/null +++ b/lib/generators/web_app_theme/themed/templates/view_edit.html.erb @@ -0,0 +1,19 @@ +
+
+
    +
  • <%%= link_to "#{t("web-app-theme.list", :default => "List")}", <%= controller_routing_path %>_path %>
  • +
  • <%%= link_to "#{t("web-app-theme.new", :default => "New")}", new_<%= singular_controller_routing_path %>_path %>
  • +
  • <%%= link_to "#{t("web-app-theme.edit", :default => "Edit")}", edit_<%= singular_controller_routing_path %>_path %>
  • +
+
+
+

<%%= t("web-app-theme.edit", :default => "Edit") %> <%= model_name %>

+
+ <%% form_for @<%= model_name.underscore %>, :url => <%= singular_controller_routing_path %>_path(@<%= resource_name %>), :html => { :class => :form } do |f| -%> + <%%= render :partial => "form", :locals => {:f => f} %> + <%% end -%> +
+
+
+ +<%% content_for :sidebar, render(:partial => 'sidebar') -%> \ No newline at end of file diff --git a/lib/generators/web_app_theme/themed/templates/view_form.html.erb b/lib/generators/web_app_theme/themed/templates/view_form.html.erb new file mode 100644 index 0000000..fac28ca --- /dev/null +++ b/lib/generators/web_app_theme/themed/templates/view_form.html.erb @@ -0,0 +1,13 @@ +<% columns.each do |column| %> +
+ <%%= f.label :<%= column.name %>, t("activerecord.attributes.<%= model_name.underscore %>.<%= column.name %>", :default => "<%= column.name.humanize %>"), :class => :label %> + <%%= f.<%= column.field_type %> :<%= column.name %>, :class => '<%= column.field_type %>' %> + Ex: a simple text +
+<%- end -%> + \ No newline at end of file diff --git a/lib/generators/web_app_theme/themed/templates/view_new.html.erb b/lib/generators/web_app_theme/themed/templates/view_new.html.erb new file mode 100644 index 0000000..d272cbe --- /dev/null +++ b/lib/generators/web_app_theme/themed/templates/view_new.html.erb @@ -0,0 +1,18 @@ +
+
+
    +
  • <%%= link_to "#{t("web-app-theme.list", :default => "List")}", <%= controller_routing_path %>_path %>
  • +
  • <%%= link_to "#{t("web-app-theme.new", :default => "New")}", new_<%= singular_controller_routing_path %>_path %>
  • +
+
+
+

<%%= t("web-app-theme.new", :default => "New")%> <%= model_name %>

+
+ <%% form_for :<%= model_name.underscore %>, :url => <%= controller_routing_path %>_path, :html => { :class => :form } do |f| -%> + <%%= render :partial => "form", :locals => {:f => f} %> + <%% end -%> +
+
+
+ +<%% content_for :sidebar, render(:partial => 'sidebar') -%> \ No newline at end of file diff --git a/lib/generators/web_app_theme/themed/templates/view_show.html.erb b/lib/generators/web_app_theme/themed/templates/view_show.html.erb new file mode 100644 index 0000000..484a771 --- /dev/null +++ b/lib/generators/web_app_theme/themed/templates/view_show.html.erb @@ -0,0 +1,25 @@ +
+
+
    +
  • <%%= link_to "#{t("web-app-theme.list", :default => "List")}", <%= controller_routing_path %>_path %>
  • +
  • <%%= link_to "#{t("web-app-theme.new", :default => "New")}", new_<%= singular_controller_routing_path %>_path %>
  • +
  • <%%= link_to "#{t("web-app-theme.show", :default => "Show")}", <%= singular_controller_routing_path %>_path %>
  • +
+
+
+
+ <% columns.each do |column| %> +

+ <%%= t("activerecord.attributes.<%= singular_controller_routing_path %>.<%= column.name %>", :default => t("activerecord.labels.<%= column.name %>", :default => "<%= column.name.humanize %>")) %>: + <%%= @<%= resource_name %>.<%= column.name %> %> +

+ <%- end -%> +
+ <%%= link_to "#{image_tag("web-app-theme/application_edit.png", :alt => "#{t("web-app-theme.edit", :default=> "Edit")}")} #{t("web-app-theme.edit", :default=> "Edit")}", edit_<%= singular_controller_routing_path %>_path(@<%= resource_name %>), :class => "button" %> + <%%= link_to "#{image_tag("web-app-theme/cross.png", :alt => "#{t("web-app-theme.delete", :default=> "Delete")}")} #{t("web-app-theme.delete", :default => "Delete")}", <%= singular_controller_routing_path %>_path(@<%= resource_name %>), :method => "delete", :class => "button", :confirm => "#{t("web-app-theme.confirm", :default => "Are you sure?")}" %> +
+
+
+
+ +<%% content_for :sidebar, render(:partial => 'sidebar') -%> \ No newline at end of file diff --git a/lib/generators/web_app_theme/themed/templates/view_sidebar.html.erb b/lib/generators/web_app_theme/themed/templates/view_sidebar.html.erb new file mode 100644 index 0000000..2148fbf --- /dev/null +++ b/lib/generators/web_app_theme/themed/templates/view_sidebar.html.erb @@ -0,0 +1,13 @@ +
+

Simple Block

+
+

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

+
+
+
+

Links

+ +
\ No newline at end of file diff --git a/lib/generators/web_app_theme/themed/templates/view_signin.html.erb b/lib/generators/web_app_theme/themed/templates/view_signin.html.erb new file mode 100644 index 0000000..8bacfdd --- /dev/null +++ b/lib/generators/web_app_theme/themed/templates/view_signin.html.erb @@ -0,0 +1,36 @@ +

<%= options[:app_name] %>

+
+

Login Box

+ +
\ No newline at end of file diff --git a/lib/generators/web_app_theme/themed/templates/view_signup.html.erb b/lib/generators/web_app_theme/themed/templates/view_signup.html.erb new file mode 100644 index 0000000..a2b9d0b --- /dev/null +++ b/lib/generators/web_app_theme/themed/templates/view_signup.html.erb @@ -0,0 +1,52 @@ +

<%= options[:app_name] %>

+
+

Sign up

+
+
+ <%% flash.each do |type, message| -%> +
+

<%%= message %>

+
+ <%% end -%> +
+ <%% form_for :<%= resource_name %>, :url => <%= controller_routing_path %>_path, :html => { :class => 'form' } do |f| -%> +
+
+ +
+
+ <%%= f.text_field :login, :class => 'text_field' %> +
+
+
+
+ +
+
+ <%%= f.text_field :email, :class => 'text_field' %> +
+
+
+
+ +
+
+ <%%= f.password_field :password, :class => 'text_field' %> +
+
+ +
+
+ +
+
+ <%%= f.password_field :password_confirmation, :class => 'text_field' %> +
+
+ + + <%% end -%> +
+
\ No newline at end of file diff --git a/lib/generators/web_app_theme/themed/templates/view_tables.html.erb b/lib/generators/web_app_theme/themed/templates/view_tables.html.erb new file mode 100644 index 0000000..e159229 --- /dev/null +++ b/lib/generators/web_app_theme/themed/templates/view_tables.html.erb @@ -0,0 +1,54 @@ +
+
+
    +
  • <%%= link_to "#{t("web-app-theme.list", :default => "List")}", <%= controller_routing_path %>_path %>
  • +
  • <%%= link_to "#{t("web-app-theme.new", :default => "New")}", new_<%= singular_controller_routing_path %>_path %>
  • +
+
+
+

<%%= t("web-app-theme.all", :default => "All") %> <%= plural_model_name %>

+
+ + + + <% unless columns.empty? -%> + + <% end -%> + + + + <%% @<%= plural_resource_name %>.each do |<%= resource_name %>| -%> + "> + + <% unless columns.empty? -%> + + <% end -%> + + + + <%% end -%> +
ID + <%%= t("activerecord.attributes.<%= singular_controller_routing_path %>.<%= columns.first.name %>", :default => t("activerecord.labels.<%= columns.first.name %>", :default => "<%= columns.first.name.capitalize %>")) %> + <%%= t("web-app-theme.created_at", :default => "Created at") %> 
+ <%%= <%= resource_name %>.id %> + + <%%= link_to <%= resource_name %>.<%= columns.first.name %>, <%= singular_controller_routing_path %>_path(<%= resource_name %>) %> + + <%%= <%= resource_name %>.created_at %> + + <%%= link_to "#{t("web-app-theme.show", :default => "Show")}", <%= singular_controller_routing_path %>_path(<%= resource_name %>) %> | + <%%= link_to "#{t("web-app-theme.edit", :default => "Edit")}", edit_<%= singular_controller_routing_path %>_path(<%= resource_name %>) %> | + <%%= link_to "#{t("web-app-theme.delete", :default => "Delete")}", <%= singular_controller_routing_path %>_path(<%= resource_name %>), :method => :delete, :confirm => "#{t("web-app-theme.confirm", :default => "Are you sure?")}" %> +
+
+
+
+ <% if options[:will_paginate] %> + <%%= will_paginate @<%= plural_resource_name %> %> + <% end %> +
+
+
+
+ +<%% content_for :sidebar, render(:partial => 'sidebar') -%> diff --git a/lib/generators/web_app_theme/themed/templates/view_text.html.erb b/lib/generators/web_app_theme/themed/templates/view_text.html.erb new file mode 100644 index 0000000..a28f81b --- /dev/null +++ b/lib/generators/web_app_theme/themed/templates/view_text.html.erb @@ -0,0 +1,18 @@ +
+
+

<%= resource_name.capitalize %>

+
+

+ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. +

+

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore

+

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore

+
+

+ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. +

+
+
+
+ +<%% content_for :sidebar, render(:partial => 'sidebar') -%> \ No newline at end of file diff --git a/lib/generators/web_app_theme/themed/themed_generator.rb b/lib/generators/web_app_theme/themed/themed_generator.rb new file mode 100644 index 0000000..6afe119 --- /dev/null +++ b/lib/generators/web_app_theme/themed/themed_generator.rb @@ -0,0 +1,70 @@ +require 'rails/generators/generated_attribute' + +module WebAppTheme + class ThemedGenerator < Rails::Generators::Base + source_root File.expand_path('../templates', __FILE__) + + def initialize(args, *options) + super(args, *options) + initialize_views_variables(args) + end + + 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') + 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) + @controller_routing_path = @controller_file_path.gsub(/\//, '_') + @model_name = @base_name.singularize unless @model_name + @model_name = @model_name.camelize + end + + def controller_routing_path + @controller_routing_path + end + + def singular_controller_routing_path + @controller_routing_path.singularize + end + + def model_name + @model_name + end + + def plural_model_name + @model_name.pluralize + end + + def resource_name + @model_name.underscore + end + + def plural_resource_name + resource_name.pluralize + end + + def columns + excluded_column_names = %w[id created_at updated_at] + Kernel.const_get(@model_name).columns.reject{|c| excluded_column_names.include?(c.name) }.collect{|c| Rails::Generators::GeneratedAttribute.new(c.name, c.type)} + end + + def extract_modules(name) + modules = name.include?('/') ? name.split('/') : name.split('::') + name = modules.pop + path = modules.map { |m| m.underscore } + file_path = (path + [name.underscore]).join('/') + nesting = modules.map { |m| m.camelize }.join('::') + [name, path, file_path, nesting, modules.size] + end + end +end \ No newline at end of file