From 06fd305bf90affb694fca79b9be7fcc396ea22bf Mon Sep 17 00:00:00 2001 From: "Matt K. Fu" Date: Wed, 5 Jun 2013 13:00:21 +0800 Subject: [PATCH] default widget show search --- app/controllers/default_widget_controller.rb | 34 +++++++++++++------ .../default_widget/default_widget.html.erb | 9 ++++- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/app/controllers/default_widget_controller.rb b/app/controllers/default_widget_controller.rb index 0e0a0ddf..1f946165 100644 --- a/app/controllers/default_widget_controller.rb +++ b/app/controllers/default_widget_controller.rb @@ -12,25 +12,34 @@ class DefaultWidgetController< OrbitWidgetController def query_for_default_widget - ori_class_name = @default_widget["query"].split('.')[0] + @ori_class_name = @default_widget["query"].split('.')[0] result = nil result_objects = nil - + if !params["tag_id"].blank? if params["category_id"].blank? #has tag no cate - result = ori_class_name.constantize.where(:tagged_ids.in => params["tag_id"]) + result = @ori_class_name.constantize.where(:tagged_ids.in => params["tag_id"]) else #has tag and cate - result = eval("#{ori_class_name}.where(:#{get_category_field_name(ori_class_name)}.in=>params['category_id'], :tagged_ids.in => params['tag_id'])") + result = eval("#{@ori_class_name}.where(:#{get_category_field_name(@ori_class_name)}.in=>params['category_id'], :tagged_ids.in => params['tag_id'])") end elsif params["category_id"].blank? #no tag no cate result = eval(@default_widget["query"]) else #no tag has cate - result = eval("#{ori_class_name}.where(:#{get_category_field_name(ori_class_name)}.in=>params['category_id'])") + result = eval("#{@ori_class_name}.where(:#{get_category_field_name(@ori_class_name)}.in=>params['category_id'])") end result.available_for_lang(I18n.locale).can_display.desc(:is_top, :postdate) end + def search_result + @search = query_for_default_widget.solr_search do + fulltext params[:search_query] + end + search_result = @search.results.collect{|result| result.id} + @items = eval(@ori_class_name).all.available_for_lang(I18n.locale).can_display.any_in(_id:search_result) + end + def default_widget + @search = params["search_query"].blank? ? false : true if !params[:id].blank? clicked_field_name = params["clicked_field_name"].to_sym redirect_to eval("#{@page_part.module_app.widget_fields_link_method[clicked_field_name][:method]}('#{params[:id]}', {inner: #{params[:inner] || true}})") @@ -55,8 +64,13 @@ class DefaultWidgetController< OrbitWidgetController @fields = @page_part.widget_field @data_limit = @page_part.widget_data_count.is_a?(Fixnum) ? @page_part.widget_data_count : (@page_part.widget_data_count.to_i rescue 3) end - if @paginate - @data = query_for_default_widget.includes(@widget_image_field).page(params[:page_main]).per(@data_limit) + + if @paginate || @search + if @search + @data = search_result.page(params[:page_main]).per(@data_limit) rescue [] + else #regular listing + @data = query_for_default_widget.includes(@widget_image_field).page(params[:page_main]).per(@data_limit) + end # @data = query_for_default_widget.limit(@data_limit).includes(@widget_image_field) else @data = query_for_default_widget.includes(@widget_image_field).limit(@data_limit) @@ -76,9 +90,9 @@ class DefaultWidgetController< OrbitWidgetController end end - def get_category_field_name(ori_class_name) - ori_class_name = ori_class_name.constantize - ori_class_name.fields.each_key do |key| + def get_category_field_name() + @ori_class_name = @ori_class_name.constantize + @ori_class_name.fields.each_key do |key| return key if key.include?('category_id') end nil diff --git a/app/views/default_widget/default_widget.html.erb b/app/views/default_widget/default_widget.html.erb index 1b02e7f6..5817673c 100644 --- a/app/views/default_widget/default_widget.html.erb +++ b/app/views/default_widget/default_widget.html.erb @@ -1,5 +1,12 @@ + <% if @data.blank? and @search %> + <%=render :partial => 'shared/search_not_found' %> + <% elsif @search %> + <%= t("search.result_get",:search_word => params[:search_query],:item_num=>@data.count) unless (@data.count == 30) %> + <%= t("search.too_many",:search_word => params[:search_query],:exceed_num=>@data.count) if (@data.count ==30) %> + <% end %> + <%= render @partial %> -<% if @paginate %> +<% if @paginate and !@data.blank? %> <%= paginate @data, :param_name => :page_main, :params => {:same_page_id => @page_id} %> <% elsif @page_part.module_app %> <%= @page_part.module_app.get_registration.default_widget_setting.link_to_more_tag(request,params) %>