From f67866fa6c66ad54ce764f373ba1151f782ab30a Mon Sep 17 00:00:00 2001 From: Ferenc-Lorant Kiss Date: Sat, 7 Feb 2009 07:19:25 +0200 Subject: [PATCH] still working on partial generator (i18n) some before generate checks --- lib/generators/task_partial/USAGE | 13 +-- lib/generators/task_partial/USAGE_RO | 16 +++ lib/generators/task_partial/messages.yml | 62 ++++++++++++ .../task_partial/task_partial_generator.rb | 99 ++++++++++++------- 4 files changed, 145 insertions(+), 45 deletions(-) create mode 100644 lib/generators/task_partial/USAGE_RO create mode 100644 lib/generators/task_partial/messages.yml diff --git a/lib/generators/task_partial/USAGE b/lib/generators/task_partial/USAGE index d1920e9..a6cf40d 100644 --- a/lib/generators/task_partial/USAGE +++ b/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 \ No newline at end of file + Avaible only in RO for moment ! \ No newline at end of file diff --git a/lib/generators/task_partial/USAGE_RO b/lib/generators/task_partial/USAGE_RO new file mode 100644 index 0000000..3b8026d --- /dev/null +++ b/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... \ No newline at end of file diff --git a/lib/generators/task_partial/messages.yml b/lib/generators/task_partial/messages.yml new file mode 100644 index 0000000..a9b93c1 --- /dev/null +++ b/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')" + \ No newline at end of file diff --git a/lib/generators/task_partial/task_partial_generator.rb b/lib/generators/task_partial/task_partial_generator.rb index 72219eb..d13d59d 100644 --- a/lib/generators/task_partial/task_partial_generator.rb +++ b/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) @@ -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 \ No newline at end of file