Add PreferenceSmartListingPage table and implement the usage

This commit is contained in:
Xavier J 2016-10-11 15:37:16 +02:00
parent 02000eeb41
commit 64d46a25df
8 changed files with 105 additions and 1 deletions

View file

@ -31,6 +31,14 @@ class Backoffice::DossiersListController < ApplicationController
dossiers_list_facade liste
dossiers_list = dossiers_list_facade.dossiers_to_display if dossiers_list.nil?
if params[:dossiers_smart_listing].nil? || params[:dossiers_smart_listing][:page].nil?
pref = current_gestionnaire.preference_smart_listing_page
if pref.liste == liste && pref.procedure_id == dossiers_list_facade.procedure_id
params[:dossiers_smart_listing] = {page: pref.page.to_s}
end
end
@dossiers = smart_listing_create :dossiers,
dossiers_list,
partial: "backoffice/dossiers/list",

View file

@ -4,6 +4,8 @@ class Gestionnaire < ActiveRecord::Base
has_and_belongs_to_many :administrateurs
has_one :preference_smart_listing_page, dependent: :destroy
has_many :assign_to, dependent: :destroy
has_many :procedures, through: :assign_to
has_many :dossiers, through: :procedures
@ -11,6 +13,7 @@ class Gestionnaire < ActiveRecord::Base
has_many :preference_list_dossiers
after_create :build_default_preferences_list_dossier
after_create :build_default_preferences_smart_listing_page
def dossiers_follow
dossiers.joins(:follows).where("follows.gestionnaire_id = #{id}")
@ -55,6 +58,10 @@ class Gestionnaire < ActiveRecord::Base
end
end
def build_default_preferences_smart_listing_page
PreferenceSmartListingPage.create(page: 1, procedure: nil, gestionnaire: self, liste: 'a_traiter')
end
private
def valid_couple_table_attr? table, column

View file

@ -0,0 +1,9 @@
class PreferenceSmartListingPage < ActiveRecord::Base
belongs_to :gestionnaire
belongs_to :procedure
validates :page, presence: true, allow_blank: false, allow_nil: false
validates :liste, presence: true, allow_blank: false, allow_nil: false
validates :procedure, presence: true, allow_blank: true, allow_nil: true
validates :gestionnaire, presence: true, allow_blank: false, allow_nil: false
end

View file

@ -0,0 +1,13 @@
class CreatePreferenceSmartListingPage < ActiveRecord::Migration
def change
create_table :preference_smart_listing_pages, id: false do |t|
t.string :liste
t.integer :page
end
add_belongs_to :preference_smart_listing_pages, :procedure
add_belongs_to :preference_smart_listing_pages, :gestionnaire
add_index :preference_smart_listing_pages, :gestionnaire_id, unique: true
end
end

View file

@ -0,0 +1,25 @@
class BuildDefaultPreferenceSmartListingPageForAllGestionnaire < ActiveRecord::Migration
class Gestionnaire < ActiveRecord::Base
has_one :preference_smart_listing_page, dependent: :destroy
def build_default_preferences_smart_listing_page
PreferenceSmartListingPage.create(page: 1, procedure: nil, gestionnaire: self, liste: 'a_traiter')
end
end
class PreferenceSmartListingPage < ActiveRecord::Base
belongs_to :gestionnaire
belongs_to :procedure
validates :page, presence: true, allow_blank: false, allow_nil: false
validates :liste, presence: true, allow_blank: false, allow_nil: false
validates :procedure, presence: true, allow_blank: true, allow_nil: true
validates :gestionnaire, presence: true, allow_blank: false, allow_nil: false
end
def change
Gestionnaire.all.each do |gestionnaire|
gestionnaire.build_default_preferences_smart_listing_page if gestionnaire.preference_smart_listing_page.nil?
end
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: 20161007095443) do
ActiveRecord::Schema.define(version: 20161011132750) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -269,6 +269,15 @@ ActiveRecord::Schema.define(version: 20161007095443) do
t.integer "procedure_id"
end
create_table "preference_smart_listing_pages", id: false, force: :cascade do |t|
t.string "liste"
t.integer "page"
t.integer "procedure_id"
t.integer "gestionnaire_id"
end
add_index "preference_smart_listing_pages", ["gestionnaire_id"], name: "index_preference_smart_listing_pages_on_gestionnaire_id", unique: true, using: :btree
create_table "procedure_paths", force: :cascade do |t|
t.string "path", limit: 30
t.integer "procedure_id"

View file

@ -28,6 +28,7 @@ describe Gestionnaire, type: :model do
end
describe 'association' do
it { is_expected.to have_one(:preference_smart_listing_page) }
it { is_expected.to have_and_belong_to_many(:administrateurs) }
it { is_expected.to have_many(:procedures) }
it { is_expected.to have_many(:dossiers) }
@ -161,4 +162,26 @@ describe Gestionnaire, type: :model do
end
end
end
describe '#build_default_preferences_smart_listing_page' do
subject { gestionnaire.preference_smart_listing_page }
context 'when gestionnaire is created' do
it 'build page column' do
expect(subject.page).to eq 1
end
it 'build liste column' do
expect(subject.liste).to eq 'a_traiter'
end
it 'build procedure_id column' do
expect(subject.procedure).to eq nil
end
it 'build gestionnaire column' do
expect(subject.gestionnaire).to eq gestionnaire
end
end
end
end

View file

@ -0,0 +1,10 @@
require 'spec_helper'
describe PreferenceSmartListingPage do
it { is_expected.to have_db_column(:page) }
it { is_expected.to have_db_column(:liste) }
it { is_expected.to have_db_column(:procedure_id) }
it { is_expected.to belong_to(:gestionnaire) }
it { is_expected.to belong_to(:procedure) }
end