From b71ebb3982c9d0d22a3cb00e766a2bec3206c206 Mon Sep 17 00:00:00 2001 From: Fu Matthew Date: Mon, 7 Jan 2013 12:09:36 +0800 Subject: [PATCH] clean default widget --- app/helpers/default_widget_helper.rb | 42 +++++++++++++++---- lib/tasks/migrate.rake | 2 +- .../announcement/app/models/bulletin.rb | 5 --- .../announcement/config/locales/zh_tw.yml | 2 +- 4 files changed, 35 insertions(+), 16 deletions(-) diff --git a/app/helpers/default_widget_helper.rb b/app/helpers/default_widget_helper.rb index df98abe34..acab557c5 100644 --- a/app/helpers/default_widget_helper.rb +++ b/app/helpers/default_widget_helper.rb @@ -8,21 +8,20 @@ module DefaultWidgetHelper if method_ary.has_key? field url = case method_ary[field]["args"] when nil # no args - eval(method_ary[field]["method"]) + get_data_link(method_ary[field]["method"]) when :self # passing self - eval "#{method_ary[field]['method']}('#{row_data.id}')" + get_data_link(method_ary[field]['method'],row_data.id) else ary = method_ary[field]["args"].clone object_hash = ary.each do |key,val| - ary[key]= row_data.send(val[0]).send(val[1]).to_s + ary[key]= type_trans_or_to_s(row_data.send(val[0]).send(val[1])) end - binding.pry - eval "#{method_ary[field]['method']}(#{object_hash})" + get_data_link(method_ary[field]['method'],object_hash) end link_to row_data.send(field),url else - row_data.send(field) + type_trans_or_to_s(row_data.send(field) ) end end @@ -32,17 +31,42 @@ module DefaultWidgetHelper # # row_data.send(method_ary[field]["args"]) # end + def type_trans_or_to_s(object) + case object + when Date + display_date(object) + when DateTime + display_date_time(object) + when String + object + else + object.to_s + end + end + + def get_data_link(method_entry,object = nil) + case object + when nil + eval method_entry + else + eval("#{method_entry}('#{object}')" ) + end + end + + def get_display(row_data,field_setting) + label = row_data.send(field_setting[:method]) + type_trans_or_to_s(label) + end def get_row_data(row_data,field) field_is_link = (field[0][1]== 'false' ? false : true ) field_setting = {:class=>field[0][1],:method=>field[0][0]} if field_is_link field_link = field[0][1].to_s + '_path' - binding.pry - link = link_to(row_data.send(field_setting[:method]),field_link.send(row_data)) + link = link_to(get_display(row_data,field_setting),field_link.send(row_data)) content_tag(:span,link,:class=>field_setting[:class]) else - content_tag(:span,row_data.send(field_setting[:method]),:class=>field_setting[:class]) + content_tag(:span,get_display(row_data,field_setting),:class=>field_setting[:class]) end end end \ No newline at end of file diff --git a/lib/tasks/migrate.rake b/lib/tasks/migrate.rake index a4323fd47..639943888 100644 --- a/lib/tasks/migrate.rake +++ b/lib/tasks/migrate.rake @@ -357,7 +357,7 @@ namespace :migrate do a.widget_fields = [ ["title","announcement.default_widget.title"], ["bulletin_category_with_title","announcement.default_widget.bulletin_category_with_title"], - ["postdate_with_format","announcement.default_widget.postdate_with_format"] + ["postdate","announcement.default_widget.postdate"] ] a.get_default_widget = {:query=>'Bulletin.all',:image=> 'image'} a.widget_fields_link_method = { diff --git a/vendor/built_in_modules/announcement/app/models/bulletin.rb b/vendor/built_in_modules/announcement/app/models/bulletin.rb index 92199875a..602ad865f 100644 --- a/vendor/built_in_modules/announcement/app/models/bulletin.rb +++ b/vendor/built_in_modules/announcement/app/models/bulletin.rb @@ -77,11 +77,6 @@ class Bulletin string :bulletin_category_id end - - def postdate_with_format - self[:postdate].strftime("%m/%d/%Y") - end - def bulletin_category_with_title self.bulletin_category.title end diff --git a/vendor/built_in_modules/announcement/config/locales/zh_tw.yml b/vendor/built_in_modules/announcement/config/locales/zh_tw.yml index 34b6160be..cf2de1415 100644 --- a/vendor/built_in_modules/announcement/config/locales/zh_tw.yml +++ b/vendor/built_in_modules/announcement/config/locales/zh_tw.yml @@ -18,4 +18,4 @@ zh_tw: default_widget: bulletin_category_with_title: 分類 title: 標題 - postdate_with_format: 張貼日期 + postdate: 張貼日期