From 5793f0fe5d502ff7e53c2c0f8fac31b5760ca430 Mon Sep 17 00:00:00 2001 From: Fu Matthew Date: Fri, 4 Jan 2013 18:42:25 +0800 Subject: [PATCH] default widget link. make sure to run rake migrate:make_default_widget_work_config for changing announcement module app --- app/controllers/default_widget_controller.rb | 2 +- app/helpers/admin/page_parts_helper.rb | 11 +++++ app/helpers/default_widget_helper.rb | 48 +++++++++++++++++++ app/helpers/default_widgets_helper.rb | 14 ------ app/models/module_app.rb | 10 ++-- .../admin/page_parts/_widget_fields.html.erb | 4 +- app/views/default_widget/typeA.html.erb | 10 +--- app/views/default_widget/typeB.html.erb | 8 +--- app/views/default_widget/typeC.html.erb | 8 +--- .../module_app_error_handler.rb | 3 +- .../object_auth_error_handler.rb | 2 +- .../error_handlers/page_error_handler.rb | 2 +- lib/parsers/parser_common.rb | 1 + lib/tasks/migrate.rake | 14 +++++- .../announcement/app/models/bulletin.rb | 9 ++++ .../announcement/config/locales/zh_tw.yml | 4 ++ 16 files changed, 100 insertions(+), 50 deletions(-) create mode 100644 app/helpers/default_widget_helper.rb delete mode 100644 app/helpers/default_widgets_helper.rb diff --git a/app/controllers/default_widget_controller.rb b/app/controllers/default_widget_controller.rb index e4c80587..55953805 100644 --- a/app/controllers/default_widget_controller.rb +++ b/app/controllers/default_widget_controller.rb @@ -11,7 +11,7 @@ class DefaultWidgetController< OrbitWidgetController @default_widget = @page_part.module_app.get_default_widget @widget_image_field = @default_widget[:image] data_limit = @page_part.widget_data_count.is_a?(Fixnum) ? @page_part.widget_data_count : (@page_part.widget_data_count.to_i rescue 3) - @data = eval(@default_widget[:query]).limit(data_limit).includes(@widget_image_field) + @data = eval(@default_widget["query"]).limit(data_limit).includes(@widget_image_field) @fields = @page_part.widget_field case params[:type] diff --git a/app/helpers/admin/page_parts_helper.rb b/app/helpers/admin/page_parts_helper.rb index 33fff309..8e2951f5 100644 --- a/app/helpers/admin/page_parts_helper.rb +++ b/app/helpers/admin/page_parts_helper.rb @@ -8,4 +8,15 @@ module Admin::PagePartsHelper res.html_safe end end + + def widget_field_options(i) + options = @module_app.widget_fields.collect{|widget_field| [I18n.t(widget_field[1]), widget_field[0]]} + options_for_select(options, (@part[:widget_field][i][0] if (@part && !@part[:widget_field].blank? && !@part[:widget_field][i].blank?)) ) + end + + def widget_fiield_type_options(i) + options = LIST[:widget_field_type].collect{|widget_field| [widget_field.humanize, widget_field]} + options_for_select(options, (@part[:widget_field][i][1] if (@part && !@part[:widget_field].blank? && !@part[:widget_field][i].blank?)) ) + end + end \ No newline at end of file diff --git a/app/helpers/default_widget_helper.rb b/app/helpers/default_widget_helper.rb new file mode 100644 index 00000000..df98abe3 --- /dev/null +++ b/app/helpers/default_widget_helper.rb @@ -0,0 +1,48 @@ +module DefaultWidgetHelper + def get_field_header(field) + I18n.t(@page_part.module_app.widget_fields.select{|t|t[0]==field}[0][1]) + end + + def link_to_field(row_data,field) + method_ary = @page_part.module_app.widget_fields_link_method + if method_ary.has_key? field + url = case method_ary[field]["args"] + when nil # no args + eval(method_ary[field]["method"]) + when :self # passing self + eval "#{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 + end + binding.pry + eval "#{method_ary[field]['method']}(#{object_hash})" + end + + link_to row_data.send(field),url + else + row_data.send(field) + end + end + + # def get_args_mapping(object,row_hash) + # first_hash = row_hash.first + + # # row_data.send(method_ary[field]["args"]) + # 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)) + content_tag(:span,link,:class=>field_setting[:class]) + else + content_tag(:span,row_data.send(field_setting[:method]),:class=>field_setting[:class]) + end + end +end \ No newline at end of file diff --git a/app/helpers/default_widgets_helper.rb b/app/helpers/default_widgets_helper.rb deleted file mode 100644 index b4be8141..00000000 --- a/app/helpers/default_widgets_helper.rb +++ /dev/null @@ -1,14 +0,0 @@ -module DefaultWidgetsHelper - 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)) - content_tag(:span,link,:class=>field_setting[:class]) - else - content_tag(:span,row_data.send(field_setting[:method]),:class=>field_setting[:class]) - end - end -end \ No newline at end of file diff --git a/app/models/module_app.rb b/app/models/module_app.rb index d0cd8886..15657b2c 100644 --- a/app/models/module_app.rb +++ b/app/models/module_app.rb @@ -19,6 +19,9 @@ class ModuleApp field :widget_fields ,type: Array field :widget_options,type:Hash field :widget_options_fields_i18n,type:Hash + field :widget_fields_link_method,type:Hash + field :get_default_widget,type:Hash + has_many :managers,as: :managing_app ,:class_name => "AppManager" ,:dependent => :destroy#,:foreign_key => "managing_app_id",:inverse_of => :managing_app has_many :sub_managers,as: :sub_managing_app ,:class_name => "AppManager", :dependent => :destroy#,:foreign_key => "sub_managing_app_id",:inverse_of => :sub_managing_app @@ -30,13 +33,6 @@ class ModuleApp before_save :set_key - #>>>>>>>>>>>>> remove after app config applied - def get_default_widget - {:query=>'Bulletin.all',:image=> 'image'} - end - #<<<<<<<<<<<<@@ - - def is_manager?(user) managing_users.include?(user) end diff --git a/app/views/admin/page_parts/_widget_fields.html.erb b/app/views/admin/page_parts/_widget_fields.html.erb index 81b49992..99f41328 100644 --- a/app/views/admin/page_parts/_widget_fields.html.erb +++ b/app/views/admin/page_parts/_widget_fields.html.erb @@ -4,8 +4,8 @@ <% @module_app.widget_fields.each_with_index do |widget_field, i| %> <%= i+1 %> - <%= select_tag "page_part[widget_field][]", options_for_select(@module_app.widget_fields.collect{|widget_field| [widget_field.humanize, widget_field]}, (@part[:widget_field][i][0] if (@part && !@part[:widget_field].blank? && !@part[:widget_field][i].blank?)) ), :include_blank => true %> - <%= select_tag "page_part[widget_field_type][]", options_for_select(LIST[:widget_field_type].collect{|widget_field| [widget_field.humanize, widget_field]}, (@part[:widget_field][i][1] if (@part && !@part[:widget_field].blank? && !@part[:widget_field][i].blank?)) ), :include_blank => true %>
+ <%= select_tag "page_part[widget_field][]", widget_field_options(i), :include_blank => true %> + <%= select_tag "page_part[widget_field_type][]", widget_fiield_type_options(i), :include_blank => true %>
<% end %> <%= label_tag :widget_data_count %> diff --git a/app/views/default_widget/typeA.html.erb b/app/views/default_widget/typeA.html.erb index 1b6a0cce..c797ce73 100644 --- a/app/views/default_widget/typeA.html.erb +++ b/app/views/default_widget/typeA.html.erb @@ -4,20 +4,14 @@ <% @fields.each do |field|%> - <%= content_tag(:span,field[0],:class=>field[1])%> + <%= content_tag(:span,get_field_header(field[0]),:class=>field[1])%> <% end %> <% @data.each do |row_data| %> <% @fields.each do |field|%> - <% if field[0] == "bulletin_category"%> - <%= content_tag(:span,row_data.send(field[0]).title,:class=>field[1])%> - <% elsif field[0] == "title" %> - <%= content_tag(:span, link_to(row_data.send(field[0]), panel_announcement_front_end_bulletin_path(row_data)), :class=>field[1]) %> - <% else %> - <%= content_tag(:span,row_data.send(field[0]),:class=>field[1])%> - <% end %> + <%= content_tag(:span,link_to_field(row_data,field[0]),:class=>field[1])%> <% end %> <% end %> diff --git a/app/views/default_widget/typeB.html.erb b/app/views/default_widget/typeB.html.erb index e8058e79..3d1fe462 100644 --- a/app/views/default_widget/typeB.html.erb +++ b/app/views/default_widget/typeB.html.erb @@ -7,13 +7,7 @@
<% @fields.each do |field|%> - <% if field[0] == "bulletin_category"%> - <%= content_tag(:span,row_data.send(field[0]).title,:class=>field[1])%> - <% elsif field[0] == "title" %> - <%= content_tag(:span, link_to(row_data.send(field[0]), panel_announcement_front_end_bulletin_path(row_data)), :class=>field[1]) %> - <% else %> - <%= content_tag(:span,row_data.send(field[0]),:class=>field[1])%> - <% end %> + <%= content_tag(:span,link_to_field(row_data,field[0]),:class=>field[1])%> <% end %>
<% end %> diff --git a/app/views/default_widget/typeC.html.erb b/app/views/default_widget/typeC.html.erb index 045d6ce1..26b50f34 100644 --- a/app/views/default_widget/typeC.html.erb +++ b/app/views/default_widget/typeC.html.erb @@ -6,13 +6,7 @@ <% @data.each do |row_data| %> <%= content_tag(:li) do %> <% @fields.each do |field|%> - <% if field[0] == "bulletin_category"%> - <%= content_tag(:span,row_data.send(field[0]).title,:class=>field[1])%> - <% elsif field[0] == "title" %> - <%= content_tag(:span, link_to(row_data.send(field[0]), panel_announcement_front_end_bulletin_path(row_data)), :class=>field[1]) %> - <% else %> - <%= content_tag(:span,row_data.send(field[0]),:class=>field[1])%> - <% end %> + <%= content_tag(:span,link_to_field(row_data,field[0]),:class=>field[1])%> <% end %> <% end %> <% end %> diff --git a/lib/orbit_app/error_handlers/module_app_error_handler.rb b/lib/orbit_app/error_handlers/module_app_error_handler.rb index 74001d02..8cf5decd 100644 --- a/lib/orbit_app/error_handlers/module_app_error_handler.rb +++ b/lib/orbit_app/error_handlers/module_app_error_handler.rb @@ -6,8 +6,9 @@ module OrbitApp def render_module_app_error(exception = nil) default_message = 'ModuleAppErrorHandler' meaasge = '' + if exception - meaasge = default_message + exception.message + meaasge = default_message +" " + exception.message end render :text=>meaasge end diff --git a/lib/orbit_app/error_handlers/object_auth_error_handler.rb b/lib/orbit_app/error_handlers/object_auth_error_handler.rb index 8d65b4bc..62eb137c 100644 --- a/lib/orbit_app/error_handlers/object_auth_error_handler.rb +++ b/lib/orbit_app/error_handlers/object_auth_error_handler.rb @@ -7,7 +7,7 @@ module OrbitApp default_message = 'This is a render_object_auth_error' meaasge = '' if exception - meaasge = default_message + exception.message + meaasge = default_message +" " + exception.message end render :text=>meaasge end diff --git a/lib/orbit_app/error_handlers/page_error_handler.rb b/lib/orbit_app/error_handlers/page_error_handler.rb index 5bf76ba6..44ecda1f 100644 --- a/lib/orbit_app/error_handlers/page_error_handler.rb +++ b/lib/orbit_app/error_handlers/page_error_handler.rb @@ -7,7 +7,7 @@ module OrbitApp default_message = 'ModuleAppErrorHandler' meaasge = '' if exception - meaasge = default_message + exception.message + meaasge = default_message +" " + exception.message end render :text=>meaasge end diff --git a/lib/parsers/parser_common.rb b/lib/parsers/parser_common.rb index 1f996cec..1075fe75 100644 --- a/lib/parsers/parser_common.rb +++ b/lib/parsers/parser_common.rb @@ -210,6 +210,7 @@ module ParserCommon when 'default_widget' "/panel/orbit_app/widget/#{part.widget_style}?inner=true" else + raise ModuleAppError,"PagePart can't find ModuleApp" if part.module_app.nil? "/panel/#{part.module_app.key}/widget/#{part.widget_path}?inner=true" end options = "&part_id=#{part.id}&category_id=#{!part[:category].blank? ? part[:category].blank? : category}&tag_id=#{!part[:tag].blank? ? part[:tag] : tag}&page=#{params[:page]}&search_query=#{params[:search_query]}&part_title=#{Rack::Utils.escape(part_title).gsub("+", "%20") rescue nil}" diff --git a/lib/tasks/migrate.rake b/lib/tasks/migrate.rake index fb7e1f77..a4323fd4 100644 --- a/lib/tasks/migrate.rake +++ b/lib/tasks/migrate.rake @@ -354,7 +354,19 @@ namespace :migrate do task :make_default_widget_work_config => :environment do a = ModuleApp.where(:key=>'announcement').first a.widgets[:default_widget] = ['typeA','typeC','typeB_style2','typeB_style3','typeB_style4'] - a.widget_fields = ["title","bulletin_category","postdate"] + 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"] + ] + a.get_default_widget = {:query=>'Bulletin.all',:image=> 'image'} + a.widget_fields_link_method = { + "title"=>{:method => 'panel_announcement_front_end_bulletin_path', + :args=>:self}, + "bulletin_category_with_title"=>{ + :method => 'panel_announcement_front_end_bulletins_path', + :args=>{:category_id => [:bulletin_category,:id]}} + } a.save end diff --git a/vendor/built_in_modules/announcement/app/models/bulletin.rb b/vendor/built_in_modules/announcement/app/models/bulletin.rb index 85d1b654..92199875 100644 --- a/vendor/built_in_modules/announcement/app/models/bulletin.rb +++ b/vendor/built_in_modules/announcement/app/models/bulletin.rb @@ -77,6 +77,15 @@ 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 + def share_item Item.first(conditions:{name: 'announcement'}) 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 4814586b..34b6160b 100644 --- a/vendor/built_in_modules/announcement/config/locales/zh_tw.yml +++ b/vendor/built_in_modules/announcement/config/locales/zh_tw.yml @@ -15,3 +15,7 @@ zh_tw: search: 搜尋公告 update_bulletin_success: 公告已成功更新 update_bulletin_category_success: 公告類別已成功更新 + default_widget: + bulletin_category_with_title: 分類 + title: 標題 + postdate_with_format: 張貼日期