Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
generator for partials, i am so lazy :)
  • Loading branch information
kfl62 committed Feb 3, 2009
1 parent 1b329d3 commit f559e48
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/generators/task_partial/USAGE
@@ -0,0 +1,13 @@
Description:
Generating list,edit,save and show partials for tasks from TrstSysTask table, based on 'exe_id' value.
Convention:
base_path /app/views/trst_sys (change with --controller_path)
partial_path base_path/xxx/yyy/zzz/_*.html.erb with "./script/generate task_partial xxx_yyy_zzz"
or partial_path base_path/xxx/yyy/_zzz.html.erb with "./script/generate task_partial xxx_yyy_zzz --single"
Example:
"./script/generate task_partial db_sys_menu" =>
/app/views/trst_sys/db/sys/menu/_list.html.erb, _edit.html.rb ....
"./script/generate task_partial db_sys_menu --single" =>
/app/views/trst_sys/db/sys/_menu.html.erb
"./script/generate task_partial db_sys_menu --single --controller_path "other_path"" =>
/app/views/other_path/db/sys/_menu.html.erb
62 changes: 62 additions & 0 deletions lib/generators/task_partial/task_partial_generator.rb
@@ -0,0 +1,62 @@
class TaskPartialGenerator < Rails::Generator::NamedBase
default_options :single => false, :create => false, :controller_path => "trst_sys"

def initialize(runtime_args, runtime_options = {})
super
if TrstSysTask.find(:first, :conditions => ['exe_id = ?', @name ]).nil?
logger.error("Task with exe_id='#{@name}' doesn't exists in trst_sys_tasks table !
Create a new record in table with exe_id='#{@name}' and try again ...
or run with --create_record (if you are sure you didn't misspelled)")
exit
end unless options[:create]
end

def manifest
record do |m|
if options[:single]
m.directory File.join("app/views/#{options[:controller_path]}",task_path)
m.template(
"single.html.erb",
File.join("app/views/#{options[:controller_path]}",task_path,"_#{single_file_name}.html.erb"))
else
m.directory File.join("app/views/#{options[:controller_path]}",task_path)
for action in task_views
m.template(
"#{action}.html.erb",
File.join("app/views/#{options[:controller_path]}", task_path, "_#{action}.html.erb")
)
end
end
end
end

private

def banner
"Usage: #{$0} task_partial some_task_exe_id"
end

def task_path
file_name.gsub(/_/, "/")
end

def single_file_name
file_name.split("_").last
end

def task_views
%w[ edit list save show ]
end

def add_options!(opt)
opt.separator ''
opt.separator 'Options:'
opt.on("--single",
"Generate only one partial") { |v| options[:single] = v }
opt.on("--create",
"Force creating partials even without required task") { |v| options[:create] = v }
opt.on("--controller_path",
"Change default ('trst_sys') controller") { |v| options[:controller_path] = args[0] }
end

end
6 changes: 6 additions & 0 deletions lib/generators/task_partial/templates/edit.html.erb
@@ -0,0 +1,6 @@
<%#
# To change this template, choose Tools | Templates
# and open the template in the editor.
%>
<%= "edit" %>
6 changes: 6 additions & 0 deletions lib/generators/task_partial/templates/list.html.erb
@@ -0,0 +1,6 @@
<%#
# To change this template, choose Tools | Templates
# and open the template in the editor.
%>
<%= "list" %>
6 changes: 6 additions & 0 deletions lib/generators/task_partial/templates/save.html.erb
@@ -0,0 +1,6 @@
<%#
# To change this template, choose Tools | Templates
# and open the template in the editor.
%>
<%= "save" %>
6 changes: 6 additions & 0 deletions lib/generators/task_partial/templates/show.html.erb
@@ -0,0 +1,6 @@
<%#
# To change this template, choose Tools | Templates
# and open the template in the editor.
%>
<%= "show" %>
6 changes: 6 additions & 0 deletions lib/generators/task_partial/templates/single.html.erb
@@ -0,0 +1,6 @@
<%#
# To change this template, choose Tools | Templates
# and open the template in the editor.
%>
<%= "single.html" %>

0 comments on commit f559e48

Please sign in to comment.