[Git][noosfero/noosfero][master] 3 commits: Add filters in pagination links

Rodrigo Souto gitlab at mg.gitlab.com
Fri Jul 13 15:48:55 BRT 2018


Rodrigo Souto pushed to branch master at Noosfero / noosfero


Commits:
8aca6957 by mendesiasmin at 2018-07-10T22:27:55Z
Add filters in pagination links

Fix functional test

- - - - -
d8b80262 by mendesiasmin at 2018-07-10T22:28:07Z
Get filters to pg_search in pagination

Fix order param

Clean enterprises before test

- - - - -
c64dd11b by Rodrigo Souto at 2018-07-13T18:48:43Z
Merge branch 'filter-pagination' into 'master'

Add filters in pagination links

See merge request noosfero/noosfero!1559
- - - - -


5 changed files:

- app/concerns/find_by_contents.rb
- app/controllers/public/search_controller.rb
- app/helpers/application_helper.rb
- lib/noosfero/plugin.rb
- test/functional/search_controller_test.rb


Changes:

=====================================
app/concerns/find_by_contents.rb
=====================================
@@ -22,5 +22,20 @@ module FindByContents
     {:results => scope.paginate(paginate_options)}
   end
 
+  def load_filters filters
+    selected_filters = {}
+    filters.each do |key, value|
+      if value.is_a?(Hash)
+        result = load_filters value
+        selected_filters[key] = result unless result.empty?
+      else
+        if value.present? && value != "0" && value != ""
+          selected_filters[key] = value
+        end
+      end
+    end
+    selected_filters
+  end
+
 end
 


=====================================
app/controllers/public/search_controller.rb
=====================================
@@ -238,9 +238,13 @@ class SearchController < PublicController
   end
 
   def full_text_search
-    @searches[@asset] = find_by_contents(@asset, environment, @scope, @query, paginate_options,
-      {:category => @category, :tag => @tag, :filter => @order, :template_id => params[:template_id],
-       :facets => params[:facets], :periods => params[:periods]})
+    options = {:category => @category, :tag => @tag, :order => @order,
+               :display => params[:display], :template_id => params[:template_id],
+               :facets => params[:facets], :periods => params[:periods]}
+
+    @filters = load_filters options
+    @searches[@asset] = find_by_contents(@asset, environment, @scope, @query,
+                          paginate_options, options)
   end
 
   private
@@ -281,5 +285,4 @@ class SearchController < PublicController
       @events = environment.events.by_month(@date)
     end
   end
-
 end


=====================================
app/helpers/application_helper.rb
=====================================
@@ -870,7 +870,8 @@ module ApplicationHelper
     options = { previous_label: content_tag(:span, font_awesome('long-arrow-left', _('Previous'))),
                 next_label:     content_tag(:span, "#{_('Next')} #{font_awesome('long-arrow-right')}".html_safe),
                 inner_window: 1,
-                outer_window: 0 }.merge(options)
+                outer_window: 0,
+                params: @filters }.merge(options)
     will_paginate(collection, options)
   end
 


=====================================
lib/noosfero/plugin.rb
=====================================
@@ -589,7 +589,7 @@ class Noosfero::Plugin
   # own use in specific views
   def find_by_contents(asset, scope, query, paginate_options={}, options={})
     scope = scope.like_search(query, options) unless query.blank?
-    scope = scope.send(options[:filter]) unless options[:filter].blank? || options[:filter] == 'more_relevant'
+    scope = scope.send(options[:order]) unless options[:order].blank? || options[:order] == 'more_relevant'
     {:results => scope.paginate(paginate_options)}
   end
 


=====================================
test/functional/search_controller_test.rb
=====================================
@@ -143,6 +143,26 @@ class SearchControllerTest < ActionController::TestCase
     assert_equal 1, assigns(:searches)[:enterprises][:results].size
   end
 
+  should 'return pagination links with filters' do
+    Enterprise.destroy_all
+    @controller.expects(:limit).returns(2).at_least_once
+    ent1 = create_profile_with_optional_category(Enterprise, 'teste 1')
+    ent2 = create_profile_with_optional_category(Enterprise, 'teste 2')
+    ent3 = create_profile_with_optional_category(Enterprise, 'teste 3')
+
+    get :enterprises, :page => '1', :order => 'more_active'
+
+    assert_equal 2, assigns(:searches)[:enterprises][:results].size
+    assert_tag :tag => 'a', :attributes => { :rel => 'next',
+                 :href => '/search/enterprises?order=more_active&page=2'}
+
+    get :enterprises, :page => '2', :order => 'more_active'
+
+    assert_equal 1, assigns(:searches)[:enterprises][:results].size
+    assert_tag :tag => 'a', :attributes => {
+                 :href => '/search/enterprises?order=more_active&page=1'}
+  end
+
   should 'display a given category' do
     get :category_index, :category_path => [ 'my-category' ]
     assert_equal @category, assigns(:category)



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/0278a486374e66fc3f446ca672caecd96f001031...c64dd11b1fbd3d5eaa69f05f021b3393725dff91

-- 
View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/0278a486374e66fc3f446ca672caecd96f001031...c64dd11b1fbd3d5eaa69f05f021b3393725dff91
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20180713/655f28b0/attachment-0001.html>


More information about the Noosfero-dev mailing list