From 572e512a1d17518774306e6ab706e9ed4c0ad758 Mon Sep 17 00:00:00 2001 From: Fu Matthew Date: Mon, 18 Feb 2013 17:03:18 +0800 Subject: [PATCH] fix link for default widget --- app/controllers/default_widget_controller.rb | 2 +- app/helpers/default_widget_helper.rb | 15 ++++++++------- app/models/module_app.rb | 3 ++- app/views/default_widget/typeA.html.erb | 2 +- lib/orbit_app/module/registration.rb | 4 ++++ lib/orbit_app/module/widget.rb | 12 ++++++++++++ 6 files changed, 28 insertions(+), 10 deletions(-) diff --git a/app/controllers/default_widget_controller.rb b/app/controllers/default_widget_controller.rb index 6580b70f..956a2a53 100644 --- a/app/controllers/default_widget_controller.rb +++ b/app/controllers/default_widget_controller.rb @@ -23,7 +23,7 @@ class DefaultWidgetController< OrbitWidgetController @fields = @page_part.widget_field @page_part.widget_data_count.is_a?(Fixnum) ? @page_part.widget_data_count : (@page_part.widget_data_count.to_i rescue 3) end - @data = eval(@default_widget["query"]).limit(data_limit).includes(@widget_image_field) + @data = eval(@default_widget["query"]).limit(data_limit).includes(@widget_image_field).desc(:created_at) case params[:type] when "typeA" @tag_class = 'default_widget_typeA' diff --git a/app/helpers/default_widget_helper.rb b/app/helpers/default_widget_helper.rb index 7f917449..913c77ee 100644 --- a/app/helpers/default_widget_helper.rb +++ b/app/helpers/default_widget_helper.rb @@ -3,20 +3,21 @@ module DefaultWidgetHelper I18n.t("#{@page_part.module_app.key}.default_widget.#{field}") end - def link_to_field(row_data,field) + def link_to_field(row_data,field,switch) method_ary = @page_part.module_app.widget_fields_link_method - if method_ary.has_key? field - url = case method_ary[field]["args"] + field = field.to_sym + if(method_ary.has_key?(field) and (switch == "true")) + url = case method_ary[field][:args] when nil # no args - get_data_link(method_ary[field]["method"]) + get_data_link(method_ary[field][:method]) when :self # passing self - get_data_link(method_ary[field]['method'],row_data.id) + get_data_link(method_ary[field][:method],row_data.id) else - ary = method_ary[field]["args"].clone + ary = method_ary[field][:args].clone object_hash = ary.each do |key,val| ary[key]= type_trans_or_to_s(row_data.send(val[0]).send(val[1])) end - get_data_link(method_ary[field]['method'],object_hash) + get_data_link(method_ary[field][:method],object_hash) end link_to row_data.send(field),url diff --git a/app/models/module_app.rb b/app/models/module_app.rb index b10d581f..5aa5a11d 100644 --- a/app/models/module_app.rb +++ b/app/models/module_app.rb @@ -98,7 +98,8 @@ class ModuleApp end def widget_fields_link_method - raise 'Need to link with OrbitApp [type: Hash]' + get_registration.get_link_methods + # raise 'Need to link with OrbitApp [type: Hash]' end diff --git a/app/views/default_widget/typeA.html.erb b/app/views/default_widget/typeA.html.erb index f6e0eb54..20ce3dbd 100644 --- a/app/views/default_widget/typeA.html.erb +++ b/app/views/default_widget/typeA.html.erb @@ -11,7 +11,7 @@ <% @data.each do |row_data| %> <% @fields.each do |field|%> - <%= content_tag(:span,link_to_field(row_data,field[0]),:class=>field[1])%> + <%= content_tag(:span,link_to_field(row_data,field[0],field[2]),:class=>field[1])%> <% end %> <% end %> diff --git a/lib/orbit_app/module/registration.rb b/lib/orbit_app/module/registration.rb index 4911042b..9856f7d3 100644 --- a/lib/orbit_app/module/registration.rb +++ b/lib/orbit_app/module/registration.rb @@ -64,6 +64,10 @@ module OrbitApp return @widget_set.get_fields end + def get_link_methods + return @widget_set.get_link_methods + end + def get_default_widget if @widget_set.nil? # Init not defining widget return {} diff --git a/lib/orbit_app/module/widget.rb b/lib/orbit_app/module/widget.rb index 42e02a4e..7e99b2a9 100644 --- a/lib/orbit_app/module/widget.rb +++ b/lib/orbit_app/module/widget.rb @@ -75,6 +75,10 @@ module OrbitApp get_default_widget.get_fields end + def get_link_methods + get_default_widget.get_link_methods + end + def get_default_widget return @default_widget end @@ -117,6 +121,10 @@ module OrbitApp @fields end + def get_link_methods + return flatten_hashes(@fields.select{|t| t[2]==:link}.map{|t| Hash[t[0].to_sym,t[1]] }) + end + def query(var) @query = var end @@ -125,6 +133,10 @@ module OrbitApp @image = var end + def flatten_hashes(ary) + Hash[*ary.map(&:to_a).flatten] + end + end class Widget