Skip to content

Commit

Permalink
still working on partial generator
Browse files Browse the repository at this point in the history
(i18n) some before generate checks
  • Loading branch information
Ferenc-Lorant Kiss committed Feb 7, 2009
1 parent e854aca commit f67866f
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 45 deletions.
13 changes: 1 addition & 12 deletions lib/generators/task_partial/USAGE
@@ -1,13 +1,2 @@
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
Avaible only in RO for moment !
16 changes: 16 additions & 0 deletions lib/generators/task_partial/USAGE_RO
@@ -0,0 +1,16 @@
Descriere:
Se generează parţialele (list,edit,save şi show) ptr. taskul din tabelul
TrstSysTask, bazat pe valoarea câmpului 'exe_id'.
Convenţie:
calea_de_bază este /app/views/trst_sys controllerul implicit fiind "trst_sys"
(se poate schimba cu --controller_path=valoare) la aceasta se adaugă valoarea
câmpului prefix din tabel sau implicit "sys" în cazul în care câmpul este gol
şi în final numele taskului.
"/app/views/controller_path/prefix/final/_*.html.erb" sau
"/app/views/controller_path/prefix/final.html.erb" în cazul --single
Dacă la generare aţi dat numele tabelului din baza de date aceasta se va
descompune după cum urmează: trst(se elimină)_prefix(dacă există)_final
"/app/views/controller_path/prefix/final/_*.html.erb"
Exemple:
Nu este cazul. Înainte de a se genera parţialele veţi fi informat despre
modul de aranjare a lor şi asupra posibilităţilor de modificare...
62 changes: 62 additions & 0 deletions lib/generators/task_partial/messages.yml
@@ -0,0 +1,62 @@
ro:
msg1:
"
\nInfo:
\n Taskul există
\n"
msg2:
"
\nInfo:
\n Clasa există tabelul există
\n {{prefix}}
\n"
msg3:
"
\nInfo:
\n Clasa există tabelul nu există
\n
\n
\n"
msg4:
"
\nInfo:
\n Clasa nu este definită
\n
"
msg5:
"
\nInfo:
\n Taskul nu este definit
\n
"
continue: "Continuaţi ('d','n')"
en:
msg1:
"
\nInfo:
\n RO only
\n"
msg2:
"
\nInfo:
\n RO only
\n"
msg3:
"
\nInfo:
\n RO only
\n"
msg4:
"
\nInfo:
\n RO only
\n
"
msg5:
"
\nInfo:
\n RO only
\n
"
continue: "Continue ('y','n')"

99 changes: 66 additions & 33 deletions lib/generators/task_partial/task_partial_generator.rb
@@ -1,51 +1,73 @@
class TaskPartialGenerator < Rails::Generator::NamedBase
class TaskPartialGenerator < Rails::Generator::Base
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]
I18n.load_path=(File.join(spec.path, 'messages.yml'))
usage if args.empty?
@name = args.shift
@message = usage_message
msg(@name)
print @message
print I18n.t("continue")
r = gets
exit unless r == 'y' || r == "d"
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
# if options[:single]
# m.directory File.join("app/views/#{options[:controller_path]}",task_path[0,task_path.rindex(/\//)])
# m.template(
# "single.html.erb",
# File.join("app/views/#{options[:controller_path]}",task_path[0,task_path.rindex(/\//)],"_#{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 usage_message
File.exists?(File.join(spec.path, "USAGE_#{I18n.default_locale.to_s.upcase}")) ?
File.read(File.join(spec.path, "USAGE_#{I18n.default_locale.to_s.upcase}")) :
File.read(File.join(spec.path, "USAGE"))
end

def banner
"Usage: #{$0} task_partial some_task_exe_id"
def task_exists?(name)
TrstSysTask.exists?(:exe_id => name)
end

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

def inflect_names(name)
camel = name.camelize
under = camel.underscore
plural = under.pluralize
singular = under.singularize
[camel, under, plural, singular]
end

def single_file_name
file_name.split("_").last
def msg(name)
if task_exists?(name)
message("msg1",name)
elsif task_exists?(name.split('_').last) && name.split('_').length > 1
name.classify.constantize.table_exists? ? message("msg2",name) : message("msg3",name) rescue message("msg4",name)
else
message("msg5",name)
end
end

def task_views
%w[ edit list save show ]
def prefix(name)
ary = name.split('_')
ary.pop
prefix = options[:prefix]
prefix ||= ary.last unless ary.last == "trst"
prefix ||= "sys"
end

def add_options!(opt)
Expand All @@ -55,8 +77,19 @@ def add_options!(opt)
"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] }
opt.on("--controller_path=name",
"Change default ('trst_sys') controller") { |v| options[:controller_path] = v }
opt.on("--prefix=name",
"Change default ('sys') prefix") { |v| options[:prefix] = v }
end

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

def message (msg,name)
camel, under, plural, singular = inflect_names(name)
@message << I18n.t(msg, :class_name => camel, :table_name => plural, :task_name => singular.last, :prefix => prefix(under))
end

end

0 comments on commit f67866f

Please sign in to comment.