Skip to content

Commit

Permalink
Added message generator
Browse files Browse the repository at this point in the history
  • Loading branch information
ashrafuzzaman committed Jun 7, 2012
1 parent 32cc26b commit 6173321
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 13 deletions.
4 changes: 0 additions & 4 deletions README.md

This file was deleted.

11 changes: 11 additions & 0 deletions lib/README.md
@@ -0,0 +1,11 @@
A custom template generator
===========================

rails3 template for customizing generators

Nested scaffold
===========================
rails g nested_scaffold ParentModel:ChildModel properties_hash

Example:
rails g nested_scaffold Blog:Comment title:string body:text
9 changes: 9 additions & 0 deletions lib/generators/messages/USAGE
@@ -0,0 +1,9 @@
Description:
This generates message partials for displaying messages/notifications

Example:
rails generate messages

This will create:
app/views/shared/_errors.erb.html
app/views/shared/_notifications.erb.html
16 changes: 16 additions & 0 deletions lib/generators/messages/messages_generator.rb
@@ -0,0 +1,16 @@
class MessagesGenerator < Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)

def copy_view_files
available_views.each do |view|
filename = "#{view}.html.erb"
template "erb/#{filename}", File.join("app/views/shared", filename)
end
end

private
def available_views
%w(_errors _notifications)
end

end
9 changes: 9 additions & 0 deletions lib/generators/messages/templates/erb/_errors.html.erb
@@ -0,0 +1,9 @@
<div class="alert-error alert">
<a class="close" href="#">&times;</a>
<h2><%%= t(:error_msg_title) %></h2>
<ul>
<%% model.errors.full_messages.uniq.each do |msg| %>
<li><%%= msg %></li>
<%% end %>
</ul>
</div>
13 changes: 13 additions & 0 deletions lib/generators/messages/templates/erb/_notifications.html.erb
@@ -0,0 +1,13 @@
<div class="alert-notify alert">
<a class="close" href="#">&times;</a>
<h2><%%= t(:notification_title) %></h2>
<ul>
<%% if message %>
<li><%%= msg %></li>
<%% else %>
<%% messages.each do |msg| %>
<li><%%= msg %></li>
<%% end %>
<%% end %>
</ul>
</div>
14 changes: 5 additions & 9 deletions lib/generators/nested_scaffold/nested_scaffold_generator.rb
Expand Up @@ -6,9 +6,9 @@ class NestedScaffoldGenerator < Rails::Generators::NamedBase
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type" argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"


def initialize(args, *options) #:nodoc: def initialize(args, *options) #:nodoc:
parse_reources!(args[0])
args[0] = r.name
super(args, *options) super(args, *options)
parse_reources!
assign_names!(r.name)
end end


def copy_view_files def copy_view_files
Expand All @@ -27,21 +27,17 @@ def copy_controller
# route %{get "#{file_name}/#{action}"} # route %{get "#{file_name}/#{action}"}
# end # end
#end #end
hook_for :template_engine, :test_framework, :as => :scaffold


# Invoke the helper using the controller name (pluralized) hook_for :test_framework, :messages, :as => :scaffold
hook_for :helper, :as => :scaffold do |invoked|
invoke invoked, [ controller_name ]
end


protected protected


def available_views def available_views
%w(index show new edit _form) %w(index show new edit _form)
end end


def parse_reources! def parse_reources!(name)
pr_name, r_name = file_name.split(':') pr_name, r_name = name.split(':')
@r ||= Resource.new(r_name.downcase) @r ||= Resource.new(r_name.downcase)
@pr ||= Resource.new(pr_name.downcase) @pr ||= Resource.new(pr_name.downcase)
end end
Expand Down

0 comments on commit 6173321

Please sign in to comment.