Smart listing sorting is saved on dossier list

This commit is contained in:
Xavier J 2016-09-27 17:26:49 +02:00
parent 0f1236aa7a
commit b3d454a53b
9 changed files with 193 additions and 52 deletions

View file

@ -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

View file

@ -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]

View file

@ -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

View file

@ -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

View file

@ -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'}

View file

@ -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]

View file

@ -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

View file

@ -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

View file

@ -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