From 9bf80056585141f0e57e91dc24274133ada805a0 Mon Sep 17 00:00:00 2001 From: Xavier J Date: Tue, 2 Aug 2016 14:45:09 +0200 Subject: [PATCH 1/2] Fix get entreprise 404 not found --- app/models/entreprise.rb | 2 ++ app/services/dossier_service.rb | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/app/models/entreprise.rb b/app/models/entreprise.rb index 382a64c28..ad3a8ee9b 100644 --- a/app/models/entreprise.rb +++ b/app/models/entreprise.rb @@ -2,4 +2,6 @@ class Entreprise < ActiveRecord::Base belongs_to :dossier has_one :etablissement, dependent: :destroy has_one :rna_information, dependent: :destroy + + validates_presence_of :siren end diff --git a/app/services/dossier_service.rb b/app/services/dossier_service.rb index d6ee9b4ae..e086c3657 100644 --- a/app/services/dossier_service.rb +++ b/app/services/dossier_service.rb @@ -9,6 +9,10 @@ class DossierService def dossier_informations! @entreprise_adapter = SIADE::EntrepriseAdapter.new(DossierService.siren @siret) + if @entreprise_adapter.to_params.nil? + raise RestClient::ResourceNotFound + end + @dossier.create_entreprise(@entreprise_adapter.to_params) @etablissement_adapter = SIADE::EtablissementAdapter.new(@siret) From bce1b96fee17bf883063170f1d332b7bee9fba9a Mon Sep 17 00:00:00 2001 From: Xavier J Date: Tue, 2 Aug 2016 14:49:41 +0200 Subject: [PATCH 2/2] Build default pref list dossier for new gestionnaire --- app/models/gestionnaire.rb | 46 ++++++ app/models/preference_list_dossier.rb | 2 +- app/views/backoffice/dossiers/_list.html.haml | 2 +- ...2_build_default_preference_list_dossier.rb | 133 ++++++++++++++++++ db/schema.rb | 2 +- .../backoffice/navigate_to_dossier_spec.rb | 10 +- spec/models/gestionnaire_spec.rb | 35 +++++ 7 files changed, 218 insertions(+), 12 deletions(-) create mode 100644 db/migrate/20160802113112_build_default_preference_list_dossier.rb diff --git a/app/models/gestionnaire.rb b/app/models/gestionnaire.rb index 2faf97c47..5f9d2baff 100644 --- a/app/models/gestionnaire.rb +++ b/app/models/gestionnaire.rb @@ -10,6 +10,8 @@ class Gestionnaire < ActiveRecord::Base has_many :follows has_many :preference_list_dossiers + after_create :build_default_preferences_list_dossier + def dossiers_filter dossiers.where(procedure_id: procedure_filter_list) end @@ -38,4 +40,48 @@ class Gestionnaire < ActiveRecord::Base Follow.where(gestionnaire_id: id, dossier_id: dossier_id).any? end + + def build_default_preferences_list_dossier + + PreferenceListDossier.available_columns.each do |table| + table.second.each do |column| + + if valid_couple_table_attr? table.first, column.first + PreferenceListDossier.create( + libelle: column.second[:libelle], + table: column.second[:table], + attr: column.second[:attr], + attr_decorate: column.second[:attr_decorate], + bootstrap_lg: column.second[:bootstrap_lg], + order: nil, + filter: nil, + gestionnaire: self + ) + end + end + end + end + + private + + def valid_couple_table_attr? table, column + couples = [{ + table: :dossier, + column: :dossier_id + }, { + table: :procedure, + column: :libelle + }, { + table: :etablissement, + column: :siret + }, { + table: :entreprise, + column: :raison_sociale + }, { + table: :dossier, + column: :state + }] + + couples.include?({table: table, column: column}) + end end diff --git a/app/models/preference_list_dossier.rb b/app/models/preference_list_dossier.rb index 93befa9d0..a6a57f985 100644 --- a/app/models/preference_list_dossier.rb +++ b/app/models/preference_list_dossier.rb @@ -2,7 +2,7 @@ class PreferenceListDossier < ActiveRecord::Base belongs_to :gestionnaire def table_attr - return self.attr if table.empty? + return self.attr if table.nil? || table.empty? table + '.' + attr end diff --git a/app/views/backoffice/dossiers/_list.html.haml b/app/views/backoffice/dossiers/_list.html.haml index 3afa85d7a..8add886a6 100644 --- a/app/views/backoffice/dossiers/_list.html.haml +++ b/app/views/backoffice/dossiers/_list.html.haml @@ -10,7 +10,7 @@ %tr - current_gestionnaire.preference_list_dossiers.order(:id).each_with_index do |preference, index| %td - - if preference.table.empty? + - if preference.table.nil? || preference.table.empty? - value = dossier.decorate.public_send(preference.attr_decorate) - else - value = dossier.public_send(preference.table).decorate.public_send(preference.attr_decorate) diff --git a/db/migrate/20160802113112_build_default_preference_list_dossier.rb b/db/migrate/20160802113112_build_default_preference_list_dossier.rb new file mode 100644 index 000000000..d5fad79a4 --- /dev/null +++ b/db/migrate/20160802113112_build_default_preference_list_dossier.rb @@ -0,0 +1,133 @@ +class BuildDefaultPreferenceListDossier < ActiveRecord::Migration + class Gestionnaire < ActiveRecord::Base + def build_default_preferences_list_dossier + + PreferenceListDossier.available_columns.each do |table| + table.second.each do |column| + + if valid_couple_table_attr? table.first, column.first + PreferenceListDossier.create( + libelle: column.second[:libelle], + table: column.second[:table], + attr: column.second[:attr], + attr_decorate: column.second[:attr_decorate], + bootstrap_lg: column.second[:bootstrap_lg], + order: nil, + filter: nil, + gestionnaire_id: self.id + ) + end + end + end + end + + def valid_couple_table_attr? table, column + couples = [{ + table: :dossier, + column: :dossier_id + }, { + table: :procedure, + column: :libelle + }, { + table: :etablissement, + column: :siret + }, { + table: :entreprise, + column: :raison_sociale + }, { + table: :dossier, + column: :state + }] + + couples.include?({table: table, column: column}) + end + end + + class PreferenceListDossier < ActiveRecord::Base + def self.available_columns + { + dossier: columns_dossier, + procedure: columns_procedure, + entreprise: columns_entreprise, + etablissement: columns_etablissement, + user: columns_user + } + end + + def self.columns_dossier + table = nil + + { + dossier_id: create_column('ID', table, 'id', 'id', 1), + created_at: create_column('Créé le', table, 'created_at', 'first_creation', 2), + updated_at: create_column('Mise à jour le', table, 'updated_at', 'last_update', 2), + state: create_column('Statut', table, 'state', 'display_state', 1) + } + end + + def self.columns_procedure + table = 'procedure' + + { + libelle: create_column('Libellé procédure', table, 'libelle', 'libelle', 4), + organisation: create_column('Organisation', table, 'organisation', 'organisation', 3), + direction: create_column('Direction', table, 'direction', 'direction', 3) + } + end + + def self.columns_entreprise + table = 'entreprise' + + { + siren: create_column('SIREN', table, 'siren', 'siren', 2), + forme_juridique: create_column('Forme juridique', table, 'forme_juridique', 'forme_juridique', 3), + nom_commercial: create_column('Nom commercial', table, 'nom_commercial', 'nom_commercial', 3), + raison_sociale: create_column('Raison sociale', table, 'raison_sociale', 'raison_sociale', 3), + siret_siege_social: create_column('SIRET siège social', table, 'siret_siege_social', 'siret_siege_social', 2), + date_creation: create_column('Date de création', table, 'date_creation', 'date_creation', 2), + } + end + + def self.columns_etablissement + table = 'etablissement' + + { + siret: create_column('SIRET', table, 'siret', 'siret', 2), + libelle: create_column('Nom établissement', table, 'libelle_naf', 'libelle_naf', 3), + code_postal: create_column('Code postal', table, 'code_postal', 'code_postal', 1) + } + end + + def self.columns_user + table = 'user' + + { + email: create_column('Email', table, 'email', 'email', 2) + } + end + + def self.create_column libelle, table, attr, attr_decorate, bootstrap_lg + { + libelle: libelle, + table: table, + attr: attr, + attr_decorate: attr_decorate, + bootstrap_lg: bootstrap_lg, + order: nil, + filter: nil + } + end + end + + def up + Gestionnaire.all.each do |gestionnaire| + gestionnaire.build_default_preferences_list_dossier + end + end + + def down + Gestionnaire.all.each do |gestionnaire| + PreferenceListDossier.where(gestionnaire_id: gestionnaire.id).delete_all + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 0a0f042e0..5eed35e00 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: 20160722135927) do +ActiveRecord::Schema.define(version: 20160802113112) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" diff --git a/spec/features/backoffice/navigate_to_dossier_spec.rb b/spec/features/backoffice/navigate_to_dossier_spec.rb index 05114df6e..56d046347 100644 --- a/spec/features/backoffice/navigate_to_dossier_spec.rb +++ b/spec/features/backoffice/navigate_to_dossier_spec.rb @@ -10,12 +10,6 @@ feature 'on backoffice page' do before do create :assign_to, gestionnaire: gestionnaire, procedure: procedure - create :preference_list_dossier, - gestionnaire: gestionnaire, - table: 'entreprise', - attr: 'raison_sociale', - attr_decorate: 'raison_sociale' - visit backoffice_path end @@ -24,12 +18,10 @@ feature 'on backoffice page' do page.find_by_id(:gestionnaire_email).set gestionnaire.email page.find_by_id(:gestionnaire_password).set gestionnaire.password page.click_on 'Se connecter' - - end context 'when he click on first dossier' do before do - page.click_on dossier.entreprise.raison_sociale + page.click_on dossier.id end scenario 'it redirect to dossier page' do expect(page).to have_css('#backoffice_dossier_show') diff --git a/spec/models/gestionnaire_spec.rb b/spec/models/gestionnaire_spec.rb index e485b028f..ff8762702 100644 --- a/spec/models/gestionnaire_spec.rb +++ b/spec/models/gestionnaire_spec.rb @@ -156,4 +156,39 @@ describe Gestionnaire, type: :model do it { expect(Follow.all.size).to eq 1 } it { expect(subject.first).to eq dossier } end + + describe '#build_default_preferences_list_dossier' do + subject { gestionnaire.preference_list_dossiers } + + context 'when gestionnaire is created' do + it 'build default 5 pref list dossier object' do + expect(subject.size).to eq 5 + end + + it 'build dossier_id column' do + expect(subject.first.table).to be_nil + expect(subject.first.attr).to eq 'id' + end + + it 'build dossier state column' do + expect(subject[1].table).to be_nil + expect(subject[1].attr).to eq 'state' + end + + it 'build procedure libelle column' do + expect(subject[2].table).to eq 'procedure' + expect(subject[2].attr).to eq 'libelle' + end + + it 'build entreprise raison_sociale column' do + expect(subject[3].table).to eq 'entreprise' + expect(subject[3].attr).to eq 'raison_sociale' + end + + it 'build entreprise raison_sociale column' do + expect(subject.last.table).to eq 'etablissement' + expect(subject.last.attr).to eq 'siret' + end + end + end end