diff --git a/app/controllers/backoffice/dossiers/procedure_controller.rb b/app/controllers/backoffice/dossiers/procedure_controller.rb index d57a862a0..bbcc1aebe 100644 --- a/app/controllers/backoffice/dossiers/procedure_controller.rb +++ b/app/controllers/backoffice/dossiers/procedure_controller.rb @@ -1,14 +1,9 @@ -class Backoffice::Dossiers::ProcedureController < ApplicationController - include SmartListing::Helper::ControllerExtensions - helper SmartListing::Helper +class Backoffice::Dossiers::ProcedureController < Backoffice::DossiersListController - before_action :authenticate_gestionnaire! + def index + super - def show - cookies[:liste] = params[:liste] || cookies[:liste] || 'a_traiter' - smartlisting_dossier cookies[:liste] - - current_gestionnaire.update_column :procedure_filter, [params[:id]] + dossiers_list_facade.service.filter_procedure! params[:id] render 'backoffice/dossiers/index' rescue ActiveRecord::RecordNotFound @@ -18,19 +13,6 @@ class Backoffice::Dossiers::ProcedureController < ApplicationController private - def smartlisting_dossier liste - create_dossiers_list_facade liste - - @dossiers = smart_listing_create :dossiers, - @dossiers_list_facade.dossiers_to_display, - partial: "backoffice/dossiers/list", - array: true - end - - def create_dossiers_list_facade liste='a_traiter' - @dossiers_list_facade = DossiersListFacades.new current_gestionnaire, liste, retrieve_procedure - end - def retrieve_procedure current_gestionnaire.procedures.find params[:id] end diff --git a/app/controllers/backoffice/dossiers_controller.rb b/app/controllers/backoffice/dossiers_controller.rb index ec0e0206d..ec0a0efed 100644 --- a/app/controllers/backoffice/dossiers_controller.rb +++ b/app/controllers/backoffice/dossiers_controller.rb @@ -1,15 +1,9 @@ -class Backoffice::DossiersController < ApplicationController - include SmartListing::Helper::ControllerExtensions - helper SmartListing::Helper - - before_action :authenticate_gestionnaire! +class Backoffice::DossiersController < Backoffice::DossiersListController def index - cookies[:liste] = params[:liste] || cookies[:liste] || 'a_traiter' + super - current_gestionnaire.update_column :procedure_filter, [] - - smartlisting_dossier (cookies[:liste]) + dossiers_list_facade.service.filter_procedure_reset! end def show @@ -29,7 +23,7 @@ class Backoffice::DossiersController < ApplicationController @search_terms = params[:q] @dossiers_search, @dossier = Dossier.search(current_gestionnaire, @search_terms) - create_dossiers_list_facade + dossiers_list_facade unless @dossiers_search.empty? @dossiers_search = @dossiers_search.paginate(:page => params[:page]).decorate @@ -109,29 +103,17 @@ class Backoffice::DossiersController < ApplicationController begin @liste = URI(request.referer).query.split('=').second rescue NoMethodError - @liste = 'a_traiter' + @liste = cookies[:liste] || 'a_traiter' end - smartlisting_dossier @liste + dossiers_list_facade @liste + smartlisting_dossier render 'backoffice/dossiers/index', formats: :js end private - def smartlisting_dossier liste - create_dossiers_list_facade liste - - @dossiers = smart_listing_create :dossiers, - @dossiers_list_facade.dossiers_to_display, - partial: "backoffice/dossiers/list", - array: true - end - - def create_dossiers_list_facade liste='a_traiter' - @dossiers_list_facade = DossiersListFacades.new current_gestionnaire, liste, retrieve_procedure - end - def create_dossier_facade dossier_id @facade = DossierFacades.new dossier_id, current_gestionnaire.email @@ -140,7 +122,6 @@ class Backoffice::DossiersController < ApplicationController redirect_to url_for(controller: '/backoffice') end - def retrieve_procedure return if params[:procedure_id].blank? current_gestionnaire.procedures.find params[:procedure_id] diff --git a/app/controllers/backoffice/dossiers_list_controller.rb b/app/controllers/backoffice/dossiers_list_controller.rb new file mode 100644 index 000000000..69a5c0ac9 --- /dev/null +++ b/app/controllers/backoffice/dossiers_list_controller.rb @@ -0,0 +1,35 @@ +class Backoffice::DossiersListController < ApplicationController + include SmartListing::Helper::ControllerExtensions + helper SmartListing::Helper + + before_action :authenticate_gestionnaire! + + def index + liste = params[:liste] || cookies[:liste] || 'a_traiter' + cookies[:liste] = liste + + dossiers_list_facade liste + + dossiers_list_facade.service.change_sort! param_sort unless params[:dossiers_smart_listing].nil? + + smartlisting_dossier + end + + def dossiers_list_facade liste='a_traiter' + @dossiers_list_facade ||= DossiersListFacades.new current_gestionnaire, liste, retrieve_procedure + end + + def smartlisting_dossier + @dossiers = smart_listing_create :dossiers, + dossiers_list_facade.dossiers_to_display, + partial: "backoffice/dossiers/list", + array: true, + default_sort: dossiers_list_facade.service.default_sort + end + + private + + def param_sort + params[:dossiers_smart_listing][:sort] + end +end diff --git a/app/services/dossiers_list_gestionnaire_service.rb b/app/services/dossiers_list_gestionnaire_service.rb index 6e3cda519..cd6b04949 100644 --- a/app/services/dossiers_list_gestionnaire_service.rb +++ b/app/services/dossiers_list_gestionnaire_service.rb @@ -41,4 +41,48 @@ class DossiersListGestionnaireService def filter_dossiers @filter_dossiers ||= @procedure.nil? ? @current_devise_profil.dossiers : @procedure.dossiers end + + def filter_procedure_reset! + filter_procedure! nil + end + + def filter_procedure! procedure_id + @current_devise_profil.update_column :procedure_filter, procedure_id + end + + def default_sort + sort_preference = @current_devise_profil.preference_list_dossiers + .where(procedure: @procedure) + .where.not(order: nil).first + + return {'nil' => 'nil'} if sort_preference.nil? + + { + [sort_preference.table, sort_preference.attr] + .reject(&:nil?) + .join('.') => sort_preference.order + } + end + + def change_sort! new_sort + + raw_table_attr = new_sort.keys.first.split('.') + order = new_sort.values.first + + table = (raw_table_attr.size == 2 ? raw_table_attr.first : nil) + attr = (raw_table_attr.size == 2 ? raw_table_attr.second : raw_table_attr.first) + + reset_sort! + + @current_devise_profil.preference_list_dossiers + .find_by(table: table, attr: attr, procedure: @procedure) + .update_column :order, order + end + + def reset_sort! + @current_devise_profil.preference_list_dossiers + .where(procedure: @procedure) + .where.not(order: nil) + .update_all order: nil + end end \ No newline at end of file diff --git a/app/views/layouts/_navbar.html.haml b/app/views/layouts/_navbar.html.haml index 5880764bb..c05b8a17f 100644 --- a/app/views/layouts/_navbar.html.haml +++ b/app/views/layouts/_navbar.html.haml @@ -8,7 +8,7 @@ = image_tag('logo-tps.png', class: 'logo') - if gestionnaire_signed_in? - %a{href: (current_gestionnaire.procedure_filter.empty? ? '/' : backoffice_dossiers_procedure_path(current_gestionnaire.procedure_filter)), class: 'btn btn-md'} + %a{href: (current_gestionnaire.procedure_filter.blank? ? '/' : backoffice_dossiers_procedure_path(current_gestionnaire.procedure_filter)), class: 'btn btn-md'} Mes Dossiers - elsif user_signed_in? %a{href: '/', class: 'btn btn-md'} diff --git a/config/routes.rb b/config/routes.rb index 9f3d48777..3dc0daa97 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -163,7 +163,7 @@ Rails.application.routes.draw do end namespace :dossiers do - resources :procedure, only: [:show] + get 'procedure/:id' => 'procedure#index', as: 'procedure' end resources :commentaires, only: [:create] diff --git a/db/migrate/20160926160051_change_procedure_filter_attr_in_gestionnaire.rb b/db/migrate/20160926160051_change_procedure_filter_attr_in_gestionnaire.rb new file mode 100644 index 000000000..a52bd1b74 --- /dev/null +++ b/db/migrate/20160926160051_change_procedure_filter_attr_in_gestionnaire.rb @@ -0,0 +1,6 @@ +class ChangeProcedureFilterAttrInGestionnaire < ActiveRecord::Migration + def change + remove_column :gestionnaires, :procedure_filter + add_column :gestionnaires, :procedure_filter, :integer, default: nil + end +end diff --git a/db/schema.rb b/db/schema.rb index 837be6fb9..7490e1253 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160913093948) do +ActiveRecord::Schema.define(version: 20160926160051) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -207,7 +207,7 @@ ActiveRecord::Schema.define(version: 20160913093948) do t.inet "last_sign_in_ip" t.datetime "created_at" t.datetime "updated_at" - t.integer "procedure_filter", default: [], array: true + t.integer "procedure_filter" end add_index "gestionnaires", ["email"], name: "index_gestionnaires_on_email", unique: true, using: :btree diff --git a/spec/services/dossiers_list_gestionnaire_service_spec.rb b/spec/services/dossiers_list_gestionnaire_service_spec.rb new file mode 100644 index 000000000..a6c942f50 --- /dev/null +++ b/spec/services/dossiers_list_gestionnaire_service_spec.rb @@ -0,0 +1,93 @@ +require 'spec_helper' + +describe DossiersListGestionnaireService do + let(:gestionnaire) { create :gestionnaire } + let(:liste) { 'a_traiter' } + let(:dossier) { create :dossier } + + describe '#default_sort' do + let(:procedure) { dossier.procedure } + + before do + AccompagnateurService.change_assignement! gestionnaire, procedure, 'assign' + AccompagnateurService.build_default_column gestionnaire, procedure, 'assign' + + gestionnaire.reload + end + + subject { DossiersListGestionnaireService.new(gestionnaire, liste, procedure).default_sort } + + context 'when gestionnaire does not have default sort' do + it { is_expected.to eq({'nil' => 'nil'}) } + end + + context 'when gestionnaire have default sort' do + + before do + preference_attr.update_column(:order, 'asc') + end + + context 'when default sort is a dossier attr' do + let(:preference_attr) { gestionnaire.preference_list_dossiers.where(procedure: procedure, table: nil, attr: 'id').first } + + it { is_expected.to eq({"#{preference_attr.attr}" => "asc"}) } + end + + context 'when default sort is not a dossier attr' do + let(:preference_attr) { gestionnaire.preference_list_dossiers.where(procedure: procedure, table: 'entreprise', attr: 'raison_sociale').first } + + it { is_expected.to eq({"#{preference_attr.table}.#{preference_attr.attr}" => "asc"}) } + end + end + end + + describe '#change_sort!' do + let(:table) { 'entreprise' } + let(:attr) { 'raison_sociale' } + let(:order) { 'desc' } + + let(:select_preference_list_dossier) { gestionnaire.preference_list_dossiers + .find_by(table: table, attr: attr, procedure: nil) } + + subject { DossiersListGestionnaireService.new(gestionnaire, liste).change_sort! param_sort } + + describe 'with one or two params in sort' do + before do + subject + + gestionnaire.reload + end + + context 'when sort_params as table and attr' do + let(:param_sort) { ({"#{table}.#{attr}" => order}) } + + it { expect(select_preference_list_dossier.order).to eq 'desc' } + end + + context 'when sort_params as no table' do + let(:param_sort) { ({"#{attr}" => order}) } + let(:table) { nil } + let(:attr) { 'id' } + + it { expect(select_preference_list_dossier.order).to eq 'desc' } + end + + end + + context 'when procedure as already a preference order' do + let(:param_sort) { ({"#{attr}" => order}) } + let(:table) { nil } + let(:attr) { 'id' } + + before do + gestionnaire.preference_list_dossiers.find_by(procedure: nil, table: 'entreprise', attr: 'raison_sociale').update_column :order, :desc + end + + it 'keep one order by procedure id' do + expect(gestionnaire.preference_list_dossiers.where(procedure: nil).where.not(order: nil).size).to eq 1 + subject + expect(gestionnaire.preference_list_dossiers.where(procedure: nil).where.not(order: nil).size).to eq 1 + end + end + end +end