From 71e8b3c04251199aadf5fa2ab2b2c6dd81e64c3d Mon Sep 17 00:00:00 2001 From: chris Date: Fri, 19 Jul 2013 18:39:13 +0800 Subject: [PATCH] Change get_item Add category and tags to default widget link to object --- app/controllers/pages_controller.rb | 63 ++++++++++--------- app/helpers/application_helper.rb | 4 ++ app/helpers/default_widget_helper.rb | 9 +++ app/views/default_widget/_typeA.html.erb | 31 ++++----- app/views/default_widget/_typeB.html.erb | 10 +-- app/views/default_widget/_typeC.html.erb | 10 +-- .../front_end/web_links_controller.rb | 1 - 7 files changed, 74 insertions(+), 54 deletions(-) diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index 97e4f7fa4..a77854374 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -44,29 +44,13 @@ class PagesController < ApplicationController # render :file => "#{Rails.root}/public/404.html", :status => :not_found #end end - - def convert_array_param(key,array) - array.collect{|t| "#{key}[]=#{t}"}.join("&") - end def index_from_link url = "/#{@item.path}" options = '' options << "#{options.blank? ? '?' : '&'}page_main=#{params[:page_main]}" unless params[:page_main].blank? - - if params[:category_id].is_a? Array - options << "#{options.blank? ? '?' : '&'}#{convert_array_param('category_id',params[:category_id])}" unless params[:category_id].blank? - elsif params[:category_id].is_a? String - options << "#{options.blank? ? '?' : '&'}category_id=#{params[:category_id]}" unless params[:category_id].blank? - end - - if params[:tag_id].is_a? Array - options << "#{options.blank? ? '?' : '&'}#{convert_array_param('tag_id',params[:tag_id])}" unless params[:tag_id].blank? - elsif params[:category_id].is_a? String - options << "#{options.blank? ? '?' : '&'}tag_id=#{params[:tag_id]}" unless params[:tag_id].blank? - end - - + options << "#{options.blank? ? '?' : '&'}#{convert_array_param('category_id', params[:category_id])}" unless params[:category_id].blank? + options << "#{options.blank? ? '?' : '&'}#{convert_array_param('tag_id', params[:tag_id])}" unless params[:tag_id].blank? options << "#{options.blank? ? '?' : '&'}search_query=#{params[:search_query]}" unless params[:search_query].blank? options << "#{options.blank? ? '?' : '&'}name=#{params[:name]}" unless params[:name].blank? uri = URI::escape(url + options) @@ -78,8 +62,8 @@ class PagesController < ApplicationController url = "/#{@item.path}" options = '' options << "#{options.blank? ? '?' : '&'}id=#{params[:id]}" unless params[:id].blank? - options << "#{options.blank? ? '?' : '&'}category_id=#{params[:category_id]}" unless params[:category_id].blank? - options << "#{options.blank? ? '?' : '&'}tag_id=#{params[:tag_id]}" unless params[:tag_id].blank? + options << "#{options.blank? ? '?' : '&'}#{convert_array_param('category_id', params[:category_id])}" unless params[:category_id].blank? + options << "#{options.blank? ? '?' : '&'}#{convert_array_param('tag_id', params[:tag_id])}" unless params[:tag_id].blank? options << "#{options.blank? ? '?' : '&'}preview=#{params[:preview]}" unless params[:preview].blank? options << "#{options.blank? ? '?' : '&'}controller_action=#{params[:controller_action]}" unless params[:controller_action].blank? options << "#{options.blank? ? '?' : '&'}clicked_field_name=#{params[:clicked_field_name]}" unless params[:clicked_field_name].blank? @@ -98,22 +82,43 @@ class PagesController < ApplicationController elsif params[:same_page_id] @item = Item.find(params[:same_page_id]) else - category_ids = params[:category_id].is_a?(Array) ? params[:category_id] : [params[:category_id]] unless params[:category_id].nil? - tag_ids = params[:tag_id].is_a?(Array) ? params[:tag_id] : [params[:tag_id]] unless params[:tag_id].nil? + category_ids = Array(params[:category_id]) unless params[:category_id].nil? + tag_ids = Array(params[:tag_id]) unless params[:tag_id].nil? module_app = ModuleApp.first(:conditions => {:key => params[:app_name]}) + + default_query = { is_published: true, module_app_id: module_app.id, :app_frontend_url.in => [params[:app_action], 'default_widget'] } + specific_categories = { :category => category_ids } + in_categories = { :category.in => category_ids } + nil_categories = { :category.in => [nil, []] } + specific_tags = { :tag => tag_ids } + in_tags = { :tag.in => tag_ids } + nil_tags = { :tag.in => [nil, []] } + if !category_ids.blank? && !tag_ids.blank? - @item = Item.first(:conditions => {is_published: true, :module_app_id => module_app.id, :app_frontend_url.in => [params[:app_action], 'default_widget'], :category.in => category_ids, :tag.in => tag_ids}) - @item = Item.first(:conditions => {is_published: true, :module_app_id => module_app.id, :app_frontend_url.in => [params[:app_action], 'default_widget'], :category.in => category_ids, :tag.in => [nil, []]}) unless @item + @item = Item.where(default_query.merge(specific_categories).merge(specific_tags)).first + @item = Item.where(default_query.merge(specific_categories).merge(in_tags)).first unless @item + @item = Item.where(default_query.merge(specific_categories).merge(nil_tags)).first unless @item + @item = Item.where(default_query.merge(specific_tags).merge(in_categories)).first unless @item + @item = Item.where(default_query.merge(specific_tags).merge(nil_categories)).first unless @item + @item = Item.where(default_query.merge(in_categories).merge(in_tags)).first unless @item + @item = Item.where(default_query.merge(in_categories).merge(nil_tags)).first unless @item + @item = Item.where(default_query.merge(in_tags).merge(nil_categories)).first unless @item elsif !category_ids.blank? - @item = Item.where(is_published: true, module_app_id: module_app.id, :app_frontend_url.in => [params[:app_action], 'default_widget'], :category.in => category_ids).any_in(tag: [nil, []]).first + @item = Item.where(default_query.merge(specific_categories).merge(nil_tags)).first + @item = Item.where(default_query.merge(specific_categories)).first unless @item + @item = Item.where(default_query.merge(in_categories).merge(nil_tags)).first unless @item + @item = Item.where(default_query.merge(in_categories)).first unless @item elsif !tag_ids.blank? - @item = Item.where(is_published: true, module_app_id: module_app.id, :app_frontend_url.in => [params[:app_action], 'default_widget'], :tag.in => tag_ids).any_in(category: [nil, []]).first + @item = Item.where(default_query.merge(specific_tags).merge(nil_categories)).first + @item = Item.where(default_query.merge(specific_tags)).first unless @item + @item = Item.where(default_query.merge(in_tags).merge(nil_categories)).first unless @item + @item = Item.where(default_query.merge(in_tags)).first unless @item end @item = Item.find(params[:orig_page]) unless @item || params[:orig_page].blank? - @item = Item.where(is_published: true, module_app_id: module_app.id, :app_frontend_url.in => [params[:app_action], 'default_widget']).all_of("tag" => {"$in" => [nil, []]},"category" => { "$in" => [nil, []]}).first unless @item - @item = Item.where(is_published: true, module_app_id: module_app.id, :app_frontend_url.in => [params[:app_action], 'default_widget']).first unless @item + @item = Item.where(default_query.merge(nil_categories).merge(nil_tags)).first unless @item + @item = Item.where(default_query).first unless @item end - raise PageError,'Missing Frontend Page' if @item.nil? + raise PageError, 'Missing Frontend Page' if @item.nil? end def save_from_no_lang_for_page diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index bfaed2ca3..d486c479a 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -315,4 +315,8 @@ module ApplicationHelper link_to title, params.merge({direction: direction, sort: column, sort_options: column}), {:class => "#{css_class} sortable", :remote => options[:remote]} end + def convert_array_param(key, array) + Array(array).collect{|t| "#{key}[]=#{t}"}.join("&") + end + end diff --git a/app/helpers/default_widget_helper.rb b/app/helpers/default_widget_helper.rb index 3536525ca..364054199 100644 --- a/app/helpers/default_widget_helper.rb +++ b/app/helpers/default_widget_helper.rb @@ -84,4 +84,13 @@ module DefaultWidgetHelper content_tag(:span,get_display(row_data,field_setting),:class=>field_setting[:class]) end end + + def get_row_category(row_data) + row_data.send("#{row_data.class.to_s.underscore}_category_id") rescue nil + end + + def get_row_tags(row_data) + row_data.tagged_ids rescue nil + end + end \ No newline at end of file diff --git a/app/views/default_widget/_typeA.html.erb b/app/views/default_widget/_typeA.html.erb index 910d23b6a..7d5b995d2 100644 --- a/app/views/default_widget/_typeA.html.erb +++ b/app/views/default_widget/_typeA.html.erb @@ -1,19 +1,22 @@ -<%= content_tag :div,:class=>@tag_class do%> +<%= content_tag :div, class: @tag_class do %>
- - - <% @frontend_field_names.each_with_index do |field,index|%> - <%= content_tag(:th,content_tag(:span,get_field_header(field),:class=>@frontend_classes[index]))unless field.blank?%> - <% end %> - - - <% @data.each do |row_data| %> - - <% @frontend_field_names.each_with_index do |field,index|%> - <%= content_tag(:td,content_tag(:span,link_to_field(row_data,field, @frontend_sat_to_links[index], {orig_page: @page_id.to_s}),:class=>@frontend_classes[index]))unless field.blank?%> + + + <% @frontend_field_names.each_with_index do |field, index| %> + <%= content_tag(:th, content_tag(:span, get_field_header(field), class: @frontend_classes[index])) unless field.blank? %> <% end %> - <% end %> + + + <% @data.each do |row_data| %> + + <% @frontend_field_names.each_with_index do |field, index|%> + <%= content_tag(:td, content_tag(:span, link_to_field(row_data, field, @frontend_sat_to_links[index], {orig_page: @page_id.to_s, category_id: get_row_category(row_data), tag_id: get_row_tags(row_data)}), class: @frontend_classes[index])) unless field.blank? %> + <% end %> + + <% end %> +
- <% end %> +
+<% end %> diff --git a/app/views/default_widget/_typeB.html.erb b/app/views/default_widget/_typeB.html.erb index 4a394a116..ed458864d 100644 --- a/app/views/default_widget/_typeB.html.erb +++ b/app/views/default_widget/_typeB.html.erb @@ -1,13 +1,13 @@ -<%= content_tag :div,:class=>@tag_class do%> +<%= content_tag :div, class: @tag_class do %>