From 9ac95345c17528076f5af0669719f904ee8cd14b Mon Sep 17 00:00:00 2001 From: Xavier J Date: Mon, 6 Jun 2016 14:44:34 +0200 Subject: [PATCH 01/33] Add underline on calendar button date picker --- app/assets/stylesheets/description.scss | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/app/assets/stylesheets/description.scss b/app/assets/stylesheets/description.scss index 8f303ce15..0c89a0a05 100644 --- a/app/assets/stylesheets/description.scss +++ b/app/assets/stylesheets/description.scss @@ -1,8 +1,8 @@ @import "bootstrap"; @import "bootstrap-datepicker3"; -#description_page #liste_champs{ - h4{ +#description_page #liste_champs { + h4 { margin-top: 35px; } } @@ -16,12 +16,17 @@ } } +.datepicker-switch { + color: #0086b3; + text-decoration: underline; +} + .type_champ-textarea { @extend .col-md-8; @extend .col-lg-8; textarea.form-control { - width:100%; + width: 100%; height: 133px; } } @@ -30,7 +35,7 @@ @extend .col-md-3; @extend .col-lg-3; - input[type='number']{ + input[type='number'] { width: 100%; } } @@ -39,7 +44,7 @@ @extend .col-md-2; @extend .col-lg-2; - input[type='number']{ + input[type='number'] { width: 100%; } } From 3513b168880e0d2397a71b724b8e2b89e69a8003 Mon Sep 17 00:00:00 2001 From: Xavier J Date: Mon, 6 Jun 2016 16:17:19 +0200 Subject: [PATCH 02/33] Add type de champs email and phone --- app/assets/javascripts/description.js | 37 ++++++++++++++++++++++ app/assets/stylesheets/description.scss | 23 ++++++++++++++ app/models/type_de_champ.rb | 2 ++ app/views/users/description/show.html.haml | 2 +- 4 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/description.js diff --git a/app/assets/javascripts/description.js b/app/assets/javascripts/description.js new file mode 100644 index 000000000..7ca93b611 --- /dev/null +++ b/app/assets/javascripts/description.js @@ -0,0 +1,37 @@ +$(document).on('page:load', action_type_de_champs); +$(document).ready(action_type_de_champs); + + +function action_type_de_champs() { + $("input[type='email']").on('change', function () { + toggleErrorClass(this, validateEmail($(this).val())); + }); + + $("input[type='phone']").on('change', function () { + val = $(this).val(); + val = val.replace(/[ ]/g, ''); + + toggleErrorClass(this, validatePhone(val)); + }); +} + +function toggleErrorClass(node, boolean) { + if (boolean) + $(node).removeClass('input-error'); + else + $(node).addClass('input-error'); +} + +function validatePhone(phone) { + var re = /^(0|(\+[1-9]{2})|(00[1-9]{2}))[1-9][0-9]{8}$/; + return validateInput(phone, re) +} + +function validateEmail(email) { + var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; + return validateInput(email, re) +} + +function validateInput(input, regex) { + return regex.test(input); +} \ No newline at end of file diff --git a/app/assets/stylesheets/description.scss b/app/assets/stylesheets/description.scss index 0c89a0a05..32bfd4956 100644 --- a/app/assets/stylesheets/description.scss +++ b/app/assets/stylesheets/description.scss @@ -7,6 +7,11 @@ } } +.input-error { + color: darkred !important; + border-color: darkred !important +} + .type_champ-text { @extend .col-md-6; @extend .col-lg-6; @@ -16,6 +21,24 @@ } } +.type_champ-email { + @extend .col-md-4; + @extend .col-lg-4; + + input[type='email'] { + width: 100%; + } +} + +.type_champ-phone { + @extend .col-md-2; + @extend .col-lg-2; + + input[type='phone'] { + width: 100%; + } +} + .datepicker-switch { color: #0086b3; text-decoration: underline; diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 8bd7c8a95..4f1b0e88a 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -1,5 +1,7 @@ class TypeDeChamp < ActiveRecord::Base enum type_champs: {text: 'text', + email: 'email', + phone: 'phone', textarea: 'textarea', datetime: 'datetime', number: 'number', diff --git a/app/views/users/description/show.html.haml b/app/views/users/description/show.html.haml index a1d9b0300..1cf3428e7 100644 --- a/app/views/users/description/show.html.haml +++ b/app/views/users/description/show.html.haml @@ -46,7 +46,7 @@ placeholder: champ.libelle, id: "champs_#{champ.id}", value: champ.value, - type:"#{champ.type_champ}", + type: champ.type_champ, 'data-provide' => ('datepicker' if champ.type_champ == 'datetime'), 'data-date-format' => ('dd/mm/yyyy' if champ.type_champ == 'datetime')} From ddeaf5abf30d20c5f1185e3545db3eb06916d029 Mon Sep 17 00:00:00 2001 From: Xavier J Date: Mon, 6 Jun 2016 16:32:59 +0200 Subject: [PATCH 03/33] Add type de champs civilite --- app/assets/stylesheets/description.scss | 5 +++ app/models/type_de_champ.rb | 1 + app/views/users/description/_champs.html.haml | 38 +++++++++++++++++++ app/views/users/description/show.html.haml | 30 +-------------- 4 files changed, 45 insertions(+), 29 deletions(-) create mode 100644 app/views/users/description/_champs.html.haml diff --git a/app/assets/stylesheets/description.scss b/app/assets/stylesheets/description.scss index 32bfd4956..38947921c 100644 --- a/app/assets/stylesheets/description.scss +++ b/app/assets/stylesheets/description.scss @@ -30,6 +30,11 @@ } } +.type_champ-civilite { + @extend .col-md-3; + @extend .col-lg-3; +} + .type_champ-phone { @extend .col-md-2; @extend .col-lg-2; diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 4f1b0e88a..aa5d4b87e 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -2,6 +2,7 @@ class TypeDeChamp < ActiveRecord::Base enum type_champs: {text: 'text', email: 'email', phone: 'phone', + civilite: 'civilite', textarea: 'textarea', datetime: 'datetime', number: 'number', diff --git a/app/views/users/description/_champs.html.haml b/app/views/users/description/_champs.html.haml new file mode 100644 index 000000000..d1a724967 --- /dev/null +++ b/app/views/users/description/_champs.html.haml @@ -0,0 +1,38 @@ +-@champs.each do |champ| + .row + %div{class: "type_champ-#{champ.type_champ}"} + -if champ.type_champ == 'checkbox' + %h4{style:'margin-left:15px;'} + = champ.libelle + - if champ.mandatory? + = '*' + %input{type: 'hidden', name:"champs['#{champ.id}']", id: "champs_#{champ.id}", value: ''} + %input{type: 'checkbox', style:'margin-left: 15px;', name:"champs['#{champ.id}']", id: "champs_#{champ.id}", checked: ('checked' if champ.value == 'on')} + -else + %h4 + = champ.libelle + - if champ.mandatory? + = '*' + + -if champ.type_champ == 'textarea' + %textarea.form-control{name:"champs['#{champ.id}']", + placeholder: champ.libelle, + id: "champs_#{champ.id}"} + =champ.value + -elsif champ.type_champ == 'civilite' + %label.radio-inline + = radio_button_tag "champs['#{champ.id}']", "M.", champ.value == 'Mme' ? false : true + Monsieur + + %label.radio-inline + = radio_button_tag "champs['#{champ.id}']", "Mme", champ.value == 'Mme' + Madame + + -else + %input.form-control{name:"champs['#{champ.id}']", + placeholder: champ.libelle, + id: "champs_#{champ.id}", + value: champ.value, + type: champ.type_champ, + 'data-provide' => ('datepicker' if champ.type_champ == 'datetime'), + 'data-date-format' => ('dd/mm/yyyy' if champ.type_champ == 'datetime')} \ No newline at end of file diff --git a/app/views/users/description/show.html.haml b/app/views/users/description/show.html.haml index 1cf3428e7..17b06cbba 100644 --- a/app/views/users/description/show.html.haml +++ b/app/views/users/description/show.html.haml @@ -20,35 +20,7 @@ #liste_champs -unless @champs.nil? - -@champs.each do |champ| - .row - %div{class: "type_champ-#{champ.type_champ}"} - -if champ.type_champ == 'checkbox' - %h4{style:'margin-left:15px;'} - = champ.libelle - - if champ.mandatory? - = '*' - %input{type: 'hidden', name:"champs['#{champ.id}']", id: "champs_#{champ.id}", value: ''} - %input{type: 'checkbox', style:'margin-left: 15px;', name:"champs['#{champ.id}']", id: "champs_#{champ.id}", checked: ('checked' if champ.value == 'on')} - -else - %h4 - = champ.libelle - - if champ.mandatory? - = '*' - - -if champ.type_champ == 'textarea' - %textarea.form-control{name:"champs['#{champ.id}']", - placeholder: champ.libelle, - id: "champs_#{champ.id}"} - =champ.value - -else - %input.form-control{name:"champs['#{champ.id}']", - placeholder: champ.libelle, - id: "champs_#{champ.id}", - value: champ.value, - type: champ.type_champ, - 'data-provide' => ('datepicker' if champ.type_champ == 'datetime'), - 'data-date-format' => ('dd/mm/yyyy' if champ.type_champ == 'datetime')} + =render partial: 'champs' -if !@procedure.lien_demarche.blank? || @procedure.cerfa_flag || @dossier.pieces_justificatives.size > 1 %br From d26feb4c721b1c02fe311060da042fa90b139ffc Mon Sep 17 00:00:00 2001 From: Xavier J Date: Mon, 6 Jun 2016 16:38:14 +0200 Subject: [PATCH 04/33] Add JPG and PNG image on cerfa and piece justificative uploader --- app/models/piece_justificative.rb | 6 ++++-- app/uploaders/cerfa_uploader.rb | 2 +- app/uploaders/piece_justificative_uploader.rb | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/app/models/piece_justificative.rb b/app/models/piece_justificative.rb index 7bb2cdb34..4d80291b1 100644 --- a/app/models/piece_justificative.rb +++ b/app/models/piece_justificative.rb @@ -23,7 +23,7 @@ class PieceJustificative < ActiveRecord::Base (RemoteDownloader.new content.filename).url else (LocalDownloader.new content.path, - (type_de_piece_justificative.nil? ? content.original_filename : type_de_piece_justificative.libelle)).url + (type_de_piece_justificative.nil? ? content.original_filename : type_de_piece_justificative.libelle)).url end end end @@ -38,7 +38,9 @@ class PieceJustificative < ActiveRecord::Base application/vnd.openxmlformats-officedocument.presentationml.presentation, application/vnd.oasis.opendocument.text, application/vnd.oasis.opendocument.presentation, - application/vnd.oasis.opendocument.spreadsheet + application/vnd.oasis.opendocument.spreadsheet, + image/png, + image/jpeg " end end diff --git a/app/uploaders/cerfa_uploader.rb b/app/uploaders/cerfa_uploader.rb index c69dcbac8..1f8371a38 100644 --- a/app/uploaders/cerfa_uploader.rb +++ b/app/uploaders/cerfa_uploader.rb @@ -25,7 +25,7 @@ class CerfaUploader < CarrierWave::Uploader::Base # Add a white list of extensions which are allowed to be uploaded. # For images you might use something like this: def extension_white_list - %w(pdf doc docx xls xlsx ppt pptx odt ods odp) + %w(pdf doc docx xls xlsx ppt pptx odt ods odp jpg jpeg png) end def filename diff --git a/app/uploaders/piece_justificative_uploader.rb b/app/uploaders/piece_justificative_uploader.rb index ce446eb0a..c3cd6a556 100644 --- a/app/uploaders/piece_justificative_uploader.rb +++ b/app/uploaders/piece_justificative_uploader.rb @@ -25,7 +25,7 @@ class PieceJustificativeUploader < CarrierWave::Uploader::Base # Add a white list of extensions which are allowed to be uploaded. # For images you might use something like this: def extension_white_list - %w(pdf doc docx xls xlsx ppt pptx odt ods odp) + %w(pdf doc docx xls xlsx ppt pptx odt ods odp jpg jpeg png) end def filename From e6b5698008b4f4d27eb64c4986279defe2e3d22d Mon Sep 17 00:00:00 2001 From: Xavier J Date: Mon, 6 Jun 2016 17:40:32 +0200 Subject: [PATCH 05/33] Add previsualization formulaire page for administrateur --- .../admin/previsualisations_controller.rb | 13 ++ .../admin/previsualisations/show.html.haml | 9 + app/views/admin/procedures/_navbar.html.haml | 5 +- app/views/admin/procedures/new.html.haml | 3 + app/views/admin/procedures/show.html.haml | 29 ++- app/views/users/description/_show.html.haml | 50 +++++ app/views/users/description/show.html.haml | 49 +---- config/routes.rb | 1 + .../previsualisations_controller_spec.rb | 16 ++ .../previsualisations/show.html.haml_spec.rb | 172 ++++++++++++++++++ .../users/description/show.html.haml_spec.rb | 1 + 11 files changed, 284 insertions(+), 64 deletions(-) create mode 100644 app/controllers/admin/previsualisations_controller.rb create mode 100644 app/views/admin/previsualisations/show.html.haml create mode 100644 app/views/users/description/_show.html.haml create mode 100644 spec/controllers/admin/previsualisations_controller_spec.rb create mode 100644 spec/views/admin/previsualisations/show.html.haml_spec.rb diff --git a/app/controllers/admin/previsualisations_controller.rb b/app/controllers/admin/previsualisations_controller.rb new file mode 100644 index 000000000..22f39d343 --- /dev/null +++ b/app/controllers/admin/previsualisations_controller.rb @@ -0,0 +1,13 @@ +class Admin::PrevisualisationsController < AdminController + before_action :retrieve_procedure + + def show + @procedure + @dossier = Dossier.new(id: 0, procedure: @procedure) + + Champ.where(dossier_id: @dossier.id).destroy_all + @dossier.build_default_champs + + @champs = @dossier.ordered_champs + end +end \ No newline at end of file diff --git a/app/views/admin/previsualisations/show.html.haml b/app/views/admin/previsualisations/show.html.haml new file mode 100644 index 000000000..6e5ac9db8 --- /dev/null +++ b/app/views/admin/previsualisations/show.html.haml @@ -0,0 +1,9 @@ +%h2.text-info + =@procedure.libelle +%br + +%ul.nav.nav-tabs + = render partial: 'admin/procedures/navbar', locals: {active: 'Prévisualisation'} + +#previsualisation + =render 'users/description/show' \ No newline at end of file diff --git a/app/views/admin/procedures/_navbar.html.haml b/app/views/admin/procedures/_navbar.html.haml index 10bbe3d66..25198c9b0 100644 --- a/app/views/admin/procedures/_navbar.html.haml +++ b/app/views/admin/procedures/_navbar.html.haml @@ -14,4 +14,7 @@ %li{ class: ('disabled' if @procedure.locked?) || ('active' if active == 'Pieces') } = link_to_unless(@procedure.locked?, 'Pièces justificatives', admin_procedure_pieces_justificatives_path(@procedure)) do - = link_to('Pièces justificatives', '#') \ No newline at end of file + = link_to('Pièces justificatives', '#') + +%li{ class: ('active' if active == 'Prévisualisation'), style: 'float:right' } + = link_to('Prévisualisation', admin_procedure_previsualisation_path(@procedure), {style: 'font-style: italic;'}) \ No newline at end of file diff --git a/app/views/admin/procedures/new.html.haml b/app/views/admin/procedures/new.html.haml index 123a1b16c..d72bed14b 100644 --- a/app/views/admin/procedures/new.html.haml +++ b/app/views/admin/procedures/new.html.haml @@ -4,3 +4,6 @@ = form_for @procedure, url: {controller: 'admin/procedures', action: :create}, multipart: true do |f| =render partial: 'informations', locals: {f: f} =f.submit 'Valider', class: 'btn btn-info', style: 'float:right' + %br + %br + %br \ No newline at end of file diff --git a/app/views/admin/procedures/show.html.haml b/app/views/admin/procedures/show.html.haml index efb1d9e10..73ea9a435 100644 --- a/app/views/admin/procedures/show.html.haml +++ b/app/views/admin/procedures/show.html.haml @@ -6,21 +6,20 @@ %ul.nav.nav-tabs = render partial: 'navbar', locals: {active: 'Informations'} - %li{style:'float:right'} - = form_tag admin_procedure_archive_path(procedure_id: @facade.procedure.id, archive: !@facade.procedure.archived?), method: :put do - %button#archive.btn.btn-nav.text-info{type: :button} - %i.fa.fa-eraser - - if @facade.procedure.archived - = 'Réactiver' - - else - = 'Archiver' - #confirm - %button#valid.btn.btn-nav.text-success{type: :submit} - %i.fa.fa-check - Valider - %button#cancel.btn.btn-nav.text-danger{type: :button} - %i.fa.fa-remove - Annuler + = form_tag admin_procedure_archive_path(procedure_id: @facade.procedure.id, archive: !@facade.procedure.archived?), method: :put, style:'float: right; margin-top: 10px' do + %button#archive.btn.btn-small.btn-default.text-info{type: :button} + %i.fa.fa-eraser + - if @facade.procedure.archived + = 'Réactiver' + - else + = 'Archiver' + #confirm + %button#valid.btn.btn-small.btn-success{type: :submit} + %i.fa.fa-check + Valider + %button#cancel.btn.btn-small.btn-danger{type: :button} + %i.fa.fa-remove + Annuler - if @facade.procedure.locked? #procedure_locked.center diff --git a/app/views/users/description/_show.html.haml b/app/views/users/description/_show.html.haml new file mode 100644 index 000000000..a42e114b0 --- /dev/null +++ b/app/views/users/description/_show.html.haml @@ -0,0 +1,50 @@ +.container#description_page + %h2 + = @dossier.procedure.libelle + %h3 Votre dossier + + %br + + -#TODO use form_for + = form_tag(url_for({controller: 'users/description', action: :create, dossier_id: @dossier.id}), class: 'form-inline', method: 'POST', multipart: true) do + %div + .row + .col-md-12 + %h4 Libellé pour votre dossier * + = text_field_tag :nom_projet, @dossier.nom_projet, placeholder: 'Nom du projet', class: 'form-control' + %br + .row + .col-md-12 + %h4 Description * + = text_area_tag :description, @dossier.description, rows: '6', placeholder: 'Description du projet', class: 'form-control wysihtml5' + + #liste_champs + -unless @champs.nil? + =render partial: 'users/description/champs' + + -if !@procedure.lien_demarche.blank? || @procedure.cerfa_flag || @dossier.types_de_piece_justificative.size > 0 + %br + %h3 Documents administratifs + + -unless @procedure.lien_demarche.blank? + %p + Formulaire / documentation de la démarche : + %a{style:'font-size:0.9em; padding-left:3px', id: 'lien_cerfa' ,href: "#{@procedure.lien_demarche}", :target => '_blank'} Accéder + + + %br + //TODO a refactorer + = render partial: 'users/description/pieces_justificatives' + + - if user_signed_in? + + %div{style: 'text-align:right'} + %h6 Tous les champs portant un * sont obligatoires. + + -if !@dossier.draft? + =render partial: '/layouts/modifications_terminees' + -else + = submit_tag 'Soumettre mon dossier', id: 'suivant', class: %w(btn btn btn-success), style: 'float:right', data: { disable_with: 'Soumettre votre dossier', submit: true} + + %br + %br \ No newline at end of file diff --git a/app/views/users/description/show.html.haml b/app/views/users/description/show.html.haml index 17b06cbba..74930f900 100644 --- a/app/views/users/description/show.html.haml +++ b/app/views/users/description/show.html.haml @@ -1,48 +1 @@ -.container#description_page - %h2 - = @dossier.procedure.libelle - %h3 Votre dossier - - %br - - -#TODO use form_for - = form_tag(url_for({controller: :description, action: :create, dossier_id: @dossier.id}), class: 'form-inline', method: 'POST', multipart: true) do - %div - .row - .col-md-12 - %h4 Libellé pour votre dossier * - = text_field_tag :nom_projet, @dossier.nom_projet, placeholder: 'Nom du projet', class: 'form-control' - %br - .row - .col-md-12 - %h4 Description * - = text_area_tag :description, @dossier.description, rows: '6', placeholder: 'Description du projet', class: 'form-control wysihtml5' - - #liste_champs - -unless @champs.nil? - =render partial: 'champs' - - -if !@procedure.lien_demarche.blank? || @procedure.cerfa_flag || @dossier.pieces_justificatives.size > 1 - %br - %h3 Documents administratifs - - -unless @procedure.lien_demarche.blank? - %p - Formulaire / documentation de la démarche : - %a{style:'font-size:0.9em; padding-left:3px', id: 'lien_cerfa' ,href: "#{@procedure.lien_demarche}", :target => '_blank'} Accéder - - - %br - //TODO a refactorer - = render partial: 'pieces_justificatives' - - %div{style: 'text-align:right'} - %h6 Tous les champs portant un * sont obligatoires. - - -if !@dossier.draft? - =render partial: '/layouts/modifications_terminees' - -else - = submit_tag 'Soumettre mon dossier', id: 'suivant', class: %w(btn btn btn-success), style: 'float:right', data: { disable_with: 'Soumettre votre dossier', submit: true} - - %br - %br \ No newline at end of file += render partial: 'show' \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 81964b8d6..03c1c4a11 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -94,6 +94,7 @@ Rails.application.routes.draw do resource :accompagnateurs, only: [:show, :update] + resource :previsualisation, only: [:show] resources :types_de_champ, only: [:destroy] resource :pieces_justificatives, only: [:show, :update] diff --git a/spec/controllers/admin/previsualisations_controller_spec.rb b/spec/controllers/admin/previsualisations_controller_spec.rb new file mode 100644 index 000000000..8c9836bfb --- /dev/null +++ b/spec/controllers/admin/previsualisations_controller_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe Admin::PrevisualisationsController, type: :controller do + let(:admin) { create(:administrateur) } + let(:procedure) { create :procedure, administrateur: admin } + + before do + sign_in admin + end + + describe 'GET #show' do + subject { get :show, procedure_id: procedure.id } + it { expect(subject.status).to eq(200) } + end + +end \ No newline at end of file diff --git a/spec/views/admin/previsualisations/show.html.haml_spec.rb b/spec/views/admin/previsualisations/show.html.haml_spec.rb new file mode 100644 index 000000000..06fc2a884 --- /dev/null +++ b/spec/views/admin/previsualisations/show.html.haml_spec.rb @@ -0,0 +1,172 @@ +require 'spec_helper' + +describe 'admin/previsualisations/show.html.haml', type: :view do + let(:user) { create(:user) } + let(:cerfa_flag) { true } + let(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ, cerfa_flag: cerfa_flag) } + let(:dossier) { create(:dossier, procedure: procedure, user: user) } + let(:dossier_id) { dossier.id } + + before do + assign(:dossier, dossier) + assign(:procedure, dossier.procedure) + assign(:champs, dossier.ordered_champs) + end + + context 'tous les attributs sont présents sur la page' do + before do + render + end + it 'Le formulaire envoie vers /users/dossiers/:dossier_id/description en #POST' do + expect(rendered).to have_selector("form[action='/users/dossiers/#{dossier_id}/description'][method=post]") + end + + it 'Nom du projet' do + expect(rendered).to have_selector('input[id=nom_projet][name=nom_projet]') + end + + it 'Description du projet' do + expect(rendered).to have_selector('textarea[id=description][name=description]') + end + + it 'Charger votre CERFA (PDF)' do + expect(rendered).to have_selector('input[type=file][name=cerfa_pdf][id=cerfa_pdf]') + end + + it 'Lien CERFA' do + expect(rendered).to have_selector('#lien_cerfa') + end + end + + context 'si la page précédente n\'est pas recapitulatif' do + before do + render + end + it 'le bouton "Terminer" n\'est pas présent' do + expect(rendered).not_to have_selector('#suivant') + end + end + + context 'si la page précédente est recapitularif' do + before do + dossier.initiated! + dossier.reload + render + end + + it 'le bouton "Terminer" n\'est pas présent' do + expect(rendered).to_not have_selector('#suivant') + end + + it 'le bouton "Modification terminé" n\'est pas présent' do + expect(rendered).not_to have_selector('#modification_terminee') + end + + it 'le lien de retour au récapitulatif n\'est pas présent' do + expect(rendered).not_to have_selector("a[href='/users/dossiers/#{dossier_id}/recapitulatif']") + end + end + + context 'les valeurs sont réaffichées si elles sont présentes dans la BDD' do + let!(:dossier) do + create(:dossier, + nom_projet: 'Projet de test', + description: 'Description de test', + user: user) + end + + before do + render + end + + it 'Nom du projet' do + expect(rendered).to have_selector("input[id=nom_projet][value='#{dossier.nom_projet}']") + end + + it 'Description du projet' do + expect(rendered).to have_content("#{dossier.description}") + end + end + + context 'Champs' do + let(:champs) { dossier.champs } + + before do + render + end + + describe 'first champs' do + subject { dossier.champs.first } + it { expect(rendered).to have_css(".type_champ-#{subject.type_champ}") } + it { expect(rendered).to have_css("#champs_#{subject.id}") } + end + + describe 'last champs' do + subject { dossier.champs.last } + it { expect(rendered).to have_css(".type_champ-#{subject.type_champ}") } + it { expect(rendered).to have_css("#champs_#{subject.id}") } + end + end + + context 'Pièces justificatives' do + let(:all_type_pj_procedure_id) { dossier.procedure.type_de_piece_justificative_ids } + + before do + render + end + + context 'la liste des pièces justificatives a envoyé est affichée' do + it 'RIB' do + expect(rendered).to have_css("#piece_justificative_#{all_type_pj_procedure_id[0]}") + end + end + + context 'la liste des pièces récupérées automatiquement est signaliée' do + it 'Attestation MSA' do + expect(rendered).to have_selector("#piece_justificative_#{all_type_pj_procedure_id[1]}", "Nous l'avons récupéré pour vous.") + end + end + end + + context 'Envoi des CERFA désactivé' do + let!(:cerfa_flag) { false } + + before do + render + end + + it { expect(rendered).to_not have_css("#cerfa_flag") } + it { expect(rendered).to_not have_selector('input[type=file][name=cerfa_pdf][id=cerfa_pdf]') } + end + + describe 'display title Documents administratifs' do + before do + render + end + + let(:procedure) { create :procedure, lien_demarche: '' } + let(:dossier) { create(:dossier, procedure: procedure) } + + context 'when dossier not have cerfa, piece justificative and demarche link' do + it { expect(rendered).not_to have_content 'Documents administratifs' } + end + + context 'when dossier have pj' do + let(:dossier) { create(:dossier) } + + it { expect(rendered).to have_content 'Documents administratifs' } + end + + context 'when procedure have demarche link' do + let(:procedure) { create :procedure } + + it { expect(rendered).to have_content 'Documents administratifs' } + end + + context 'when procedure have cerfa flag true' do + let(:procedure) {create(:procedure, cerfa_flag: true)} + + it { expect(rendered).to have_content 'Documents administratifs' } + end + end +end diff --git a/spec/views/users/description/show.html.haml_spec.rb b/spec/views/users/description/show.html.haml_spec.rb index 22b0060a9..e6c123b9c 100644 --- a/spec/views/users/description/show.html.haml_spec.rb +++ b/spec/views/users/description/show.html.haml_spec.rb @@ -8,6 +8,7 @@ describe 'users/description/show.html.haml', type: :view do let(:dossier_id) { dossier.id } before do + sign_in user assign(:dossier, dossier) assign(:procedure, dossier.procedure) assign(:champs, dossier.ordered_champs) From 69f9c52b023a2185a3ab6aa2497d28b3b3e6a0f8 Mon Sep 17 00:00:00 2001 From: Xavier J Date: Mon, 6 Jun 2016 18:25:51 +0200 Subject: [PATCH 06/33] Preview procedure logo on edit procedure page for admin --- app/controllers/admin/procedures_controller.rb | 2 +- app/uploaders/procedure_logo_uploader.rb | 2 +- app/views/admin/accompagnateurs/show.html.haml | 7 +------ .../admin/pieces_justificatives/show.html.haml | 7 +------ .../admin/previsualisations/show.html.haml | 7 +------ app/views/admin/procedures/_head.html.haml | 8 ++++++++ .../admin/procedures/_informations.html.haml | 4 +++- app/views/admin/procedures/edit.html.haml | 7 +------ app/views/admin/procedures/show.html.haml | 7 +------ app/views/admin/types_de_champ/show.html.haml | 7 +------ .../admin/procedures_controller_spec.rb | 2 +- .../admin/procedures/edit.html.haml_spec.rb | 17 +++++++++++++++++ 12 files changed, 37 insertions(+), 40 deletions(-) create mode 100644 app/views/admin/procedures/_head.html.haml create mode 100644 spec/views/admin/procedures/edit.html.haml_spec.rb diff --git a/app/controllers/admin/procedures_controller.rb b/app/controllers/admin/procedures_controller.rb index 0ace5c1b5..8ee4befa2 100644 --- a/app/controllers/admin/procedures_controller.rb +++ b/app/controllers/admin/procedures_controller.rb @@ -60,7 +60,7 @@ class Admin::ProceduresController < AdminController end flash.notice = 'Préocédure modifiée' - redirect_to admin_procedures_path + redirect_to edit_admin_procedure_path(id: @procedure.id) end def archive diff --git a/app/uploaders/procedure_logo_uploader.rb b/app/uploaders/procedure_logo_uploader.rb index 1199b32fa..dab2a2f79 100644 --- a/app/uploaders/procedure_logo_uploader.rb +++ b/app/uploaders/procedure_logo_uploader.rb @@ -36,7 +36,7 @@ class ProcedureLogoUploader < CarrierWave::Uploader::Base if Features.remote_storage filename = "#{model.class.to_s.underscore}-#{secure_token}.#{file.extension.downcase}" else - filename = "logo.#{file.extension.downcase}" + filename = "logo-#{secure_token}.#{file.extension.downcase}" end end filename diff --git a/app/views/admin/accompagnateurs/show.html.haml b/app/views/admin/accompagnateurs/show.html.haml index d4bee085c..edcac3188 100644 --- a/app/views/admin/accompagnateurs/show.html.haml +++ b/app/views/admin/accompagnateurs/show.html.haml @@ -1,9 +1,4 @@ -%h2.text-info - =@procedure.libelle -%br - -%ul.nav.nav-tabs - = render partial: 'admin/procedures/navbar', locals: {active: 'Accompagnateurs'} +=render partial: 'admin/procedures/head', locals: {active: 'Accompagnateurs'} #accompagnateur_form .row diff --git a/app/views/admin/pieces_justificatives/show.html.haml b/app/views/admin/pieces_justificatives/show.html.haml index 6fdc68a99..5ca55f972 100644 --- a/app/views/admin/pieces_justificatives/show.html.haml +++ b/app/views/admin/pieces_justificatives/show.html.haml @@ -1,9 +1,4 @@ -%h2.text-info - =@procedure.libelle -%br - -%ul.nav.nav-tabs - = render partial: 'admin/procedures/navbar', locals: {active: 'Pieces'} +=render partial: 'admin/procedures/head', locals: {active: 'Pieces'} #piece_justificative_form = render 'form' \ No newline at end of file diff --git a/app/views/admin/previsualisations/show.html.haml b/app/views/admin/previsualisations/show.html.haml index 6e5ac9db8..01c815d6b 100644 --- a/app/views/admin/previsualisations/show.html.haml +++ b/app/views/admin/previsualisations/show.html.haml @@ -1,9 +1,4 @@ -%h2.text-info - =@procedure.libelle -%br - -%ul.nav.nav-tabs - = render partial: 'admin/procedures/navbar', locals: {active: 'Prévisualisation'} +=render partial: 'admin/procedures/head', locals: {active: 'Prévisualisation'} #previsualisation =render 'users/description/show' \ No newline at end of file diff --git a/app/views/admin/procedures/_head.html.haml b/app/views/admin/procedures/_head.html.haml new file mode 100644 index 000000000..185fd711e --- /dev/null +++ b/app/views/admin/procedures/_head.html.haml @@ -0,0 +1,8 @@ +%h2.text-info + -unless @procedure.logo.blank? + = image_tag @procedure.logo, style: 'width: 30px' + =@procedure.libelle +%br + +%ul.nav.nav-tabs + = render partial: 'admin/procedures/navbar', locals: {active: active} \ No newline at end of file diff --git a/app/views/admin/procedures/_informations.html.haml b/app/views/admin/procedures/_informations.html.haml index 199762600..c71686181 100644 --- a/app/views/admin/procedures/_informations.html.haml +++ b/app/views/admin/procedures/_informations.html.haml @@ -14,7 +14,9 @@ .row .col-md-6.col-lg-6 %h4 Logo de la procédure - = f.file_field(:logo, accept: 'image/png, image/jpg,image/jpeg') + - unless @procedure.logo.blank? + = image_tag @procedure.logo, {style: 'height: 40px; display: inline; margin-right: 6px', id: 'preview_procedure_logo'} + = f.file_field(:logo, accept: 'image/png, image/jpg, image/jpeg', style: 'display:inline') %div{style:'margin-top:5px'} %i diff --git a/app/views/admin/procedures/edit.html.haml b/app/views/admin/procedures/edit.html.haml index cc799d336..afbfaf50f 100644 --- a/app/views/admin/procedures/edit.html.haml +++ b/app/views/admin/procedures/edit.html.haml @@ -1,9 +1,4 @@ -%h2.text-info - =@procedure.libelle -%br - -%ul.nav.nav-tabs - = render partial: 'navbar', locals: {active: 'Description'} += render partial: 'head', locals: {active: 'Description'} #procedure_new.section.section-label = form_for @procedure, url: url_for({controller: 'admin/procedures', action: :update, id: @procedure.id}), multipart: true do |f| diff --git a/app/views/admin/procedures/show.html.haml b/app/views/admin/procedures/show.html.haml index 73ea9a435..6bb99288b 100644 --- a/app/views/admin/procedures/show.html.haml +++ b/app/views/admin/procedures/show.html.haml @@ -1,10 +1,5 @@ #procedure_show - %h2.text-info - =@facade.procedure.libelle - %br - - %ul.nav.nav-tabs - = render partial: 'navbar', locals: {active: 'Informations'} + =render partial: 'head', locals: {active: 'Informations'} = form_tag admin_procedure_archive_path(procedure_id: @facade.procedure.id, archive: !@facade.procedure.archived?), method: :put, style:'float: right; margin-top: 10px' do %button#archive.btn.btn-small.btn-default.text-info{type: :button} diff --git a/app/views/admin/types_de_champ/show.html.haml b/app/views/admin/types_de_champ/show.html.haml index a89627788..2dbac0970 100644 --- a/app/views/admin/types_de_champ/show.html.haml +++ b/app/views/admin/types_de_champ/show.html.haml @@ -1,9 +1,4 @@ -%h2.text-info - =@procedure.libelle -%br - -%ul.nav.nav-tabs - = render partial: 'admin/procedures/navbar', locals: {active: 'Champs'} +=render partial: 'admin/procedures/head', locals: {active: 'Champs'} #liste_champ = render partial: 'form' \ No newline at end of file diff --git a/spec/controllers/admin/procedures_controller_spec.rb b/spec/controllers/admin/procedures_controller_spec.rb index bcfc2ded8..c4d414b0a 100644 --- a/spec/controllers/admin/procedures_controller_spec.rb +++ b/spec/controllers/admin/procedures_controller_spec.rb @@ -192,7 +192,7 @@ describe Admin::ProceduresController, type: :controller do it { expect(subject.cadastre).to be_truthy } end - it { expect(subject).to redirect_to(admin_procedures_path) } + it { expect(subject).to redirect_to(edit_admin_procedure_path id: procedure.id) } it { expect(flash[:notice]).to be_present } end diff --git a/spec/views/admin/procedures/edit.html.haml_spec.rb b/spec/views/admin/procedures/edit.html.haml_spec.rb new file mode 100644 index 000000000..44db3875c --- /dev/null +++ b/spec/views/admin/procedures/edit.html.haml_spec.rb @@ -0,0 +1,17 @@ +require 'spec_helper' + +describe 'admin/procedures/edit.html.haml', type: :view do + let(:logo) { Rack::Test::UploadedFile.new("./spec/support/files/logo_test_procedure.png", 'image/png') } + let(:procedure) { create(:procedure, logo: logo) } + + before do + assign(:procedure, procedure) + render + end + + context 'when procedure logo is present' do + it 'display on the page' do + expect(rendered).to have_selector('#preview_procedure_logo') + end + end +end \ No newline at end of file From 32870e8128d0d3bd871b63183dc97492930d253b Mon Sep 17 00:00:00 2001 From: Guillaume Lazzara Date: Tue, 7 Jun 2016 10:31:54 +0200 Subject: [PATCH 07/33] Link fog credentials file during deployment --- config/deploy.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config/deploy.rb b/config/deploy.rb index 8f377a78a..6e25a2cf6 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -63,6 +63,7 @@ set :shared_paths, [ 'public/system', 'public/uploads', 'config/database.yml', + "config/fog_credentials.yml", 'config/initializers/secret_token.rb', "config/environments/#{ENV['to']}.rb", "config/initializers/token.rb", @@ -132,7 +133,7 @@ task :deploy => :environment do to :launch do queue "/etc/init.d/#{user} upgrade " - + queue "cd #{deploy_to}/#{current_path}/" queue "bundle exec rake db:seed RAILS_ENV=#{rails_env}" queue %[echo "-----> Rake Seeding Completed."] From 0c67cda7c54e5250c43ae59159b92799b946fbfc Mon Sep 17 00:00:00 2001 From: Guillaume Lazzara Date: Tue, 7 Jun 2016 10:32:21 +0200 Subject: [PATCH 08/33] Set specific cache path regarding environment --- app/uploaders/cerfa_uploader.rb | 6 +++++- app/uploaders/piece_justificative_uploader.rb | 6 +++++- app/uploaders/procedure_logo_uploader.rb | 6 +++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/app/uploaders/cerfa_uploader.rb b/app/uploaders/cerfa_uploader.rb index 1f8371a38..0b798ec81 100644 --- a/app/uploaders/cerfa_uploader.rb +++ b/app/uploaders/cerfa_uploader.rb @@ -19,7 +19,11 @@ class CerfaUploader < CarrierWave::Uploader::Base end def cache_dir - '/tmp/tps-cache' + if Rails.env.production? + '/tmp/tps-cache' + else + '/tmp/tps-dev-cache' + end end # Add a white list of extensions which are allowed to be uploaded. diff --git a/app/uploaders/piece_justificative_uploader.rb b/app/uploaders/piece_justificative_uploader.rb index c3cd6a556..5f26b3d5f 100644 --- a/app/uploaders/piece_justificative_uploader.rb +++ b/app/uploaders/piece_justificative_uploader.rb @@ -19,7 +19,11 @@ class PieceJustificativeUploader < CarrierWave::Uploader::Base end def cache_dir - '/tmp/tps-cache' + if Rails.env.production? + '/tmp/tps-cache' + else + '/tmp/tps-dev-cache' + end end # Add a white list of extensions which are allowed to be uploaded. diff --git a/app/uploaders/procedure_logo_uploader.rb b/app/uploaders/procedure_logo_uploader.rb index dab2a2f79..fd1366e0d 100644 --- a/app/uploaders/procedure_logo_uploader.rb +++ b/app/uploaders/procedure_logo_uploader.rb @@ -22,7 +22,11 @@ class ProcedureLogoUploader < CarrierWave::Uploader::Base end def cache_dir - '/tmp/tps-cache' + if Rails.env.production? + '/tmp/tps-cache' + else + '/tmp/tps-dev-cache' + end end # Add a white list of extensions which are allowed to be uploaded. From 64d27e0088ecc299e5e7b866b37938096e5551dc Mon Sep 17 00:00:00 2001 From: Guillaume Lazzara Date: Tue, 7 Jun 2016 15:16:35 +0200 Subject: [PATCH 09/33] Add 'mes dossiers' link in navbar --- app/views/layouts/_navbar.html.haml | 42 +++++++++++++++++++ app/views/layouts/application.html.haml | 39 +---------------- spec/views/layouts/_navbar_spec.rb | 56 +++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 38 deletions(-) create mode 100644 app/views/layouts/_navbar.html.haml create mode 100644 spec/views/layouts/_navbar_spec.rb diff --git a/app/views/layouts/_navbar.html.haml b/app/views/layouts/_navbar.html.haml new file mode 100644 index 000000000..29abbf4ae --- /dev/null +++ b/app/views/layouts/_navbar.html.haml @@ -0,0 +1,42 @@ +#beta + Beta += image_tag('marianne_small.png', class: 'logo') +%a{href: '/'} + = image_tag('logo-tps.png', class: 'logo') +%a{href: '/'} + -if gestionnaire_signed_in? + %span{ style: 'margin-left: 25px;' } + %b + Mes Dossiers + +#sign_out + -if gestionnaire_signed_in? + = render partial: 'gestionnaires/login_banner' + -elsif administrateur_signed_in? + = render partial: 'administrateurs/login_banner' + - elsif user_signed_in? + %div.user + -if current_user.loged_in_with_france_connect? + %div{ id: "fconnect-profile", "data-fc-logout-url" => '/users/sign_out" data-method="delete' } + %a.text-info{ href: "#" } + = "#{current_user.given_name} #{current_user.family_name}" + + = link_to "", '/users/sign_out', method: :delete, :class => 'btn fa fa-power-off off-fc-link' + + -else + %i.fa.fa-user + = current_user.email + + = link_to "Déconnexion", '/users/sign_out', method: :delete, :class => 'btn btn-md' + - else + = link_to "Utilisateur", '/users/sign_in', method: :get, :class => 'btn btn-md' + = link_to "Accompagnateur", '/gestionnaires/sign_in', method: :get, :class => 'btn btn-md' + = link_to "Administrateur", '/administrateurs/sign_in', method: :get, :class => 'btn btn-md' + +#flash_message.center +- if flash.notice + .alert.alert-success + = flash.notice +- if flash.alert + .alert.alert-danger + = flash.alert \ No newline at end of file diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 4618fd213..5382edc73 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -12,44 +12,7 @@ %body %div#wrap %div#header.navbar - - #beta - Beta - = image_tag('marianne_small.png', class: 'logo') - %a{href: '/'} - = image_tag('logo-tps.png', class: 'logo') - - #sign_out - -if gestionnaire_signed_in? - = render partial: 'gestionnaires/login_banner' - -elsif administrateur_signed_in? - = render partial: 'administrateurs/login_banner' - - elsif user_signed_in? - %div.user - -if current_user.loged_in_with_france_connect? - %div{ id: "fconnect-profile", "data-fc-logout-url" => '/users/sign_out" data-method="delete' } - %a.text-info{ href: "#" } - = "#{current_user.given_name} #{current_user.family_name}" - - = link_to "", '/users/sign_out', method: :delete, :class => 'btn fa fa-power-off off-fc-link' - - -else - %i.fa.fa-user - = current_user.email - - = link_to "Déconnexion", '/users/sign_out', method: :delete, :class => 'btn btn-md' - - else - = link_to "Utilisateur", '/users/sign_in', method: :get, :class => 'btn btn-md' - = link_to "Accompagnateur", '/gestionnaires/sign_in', method: :get, :class => 'btn btn-md' - = link_to "Administrateur", '/administrateurs/sign_in', method: :get, :class => 'btn btn-md' - - #flash_message.center - - if flash.notice - .alert.alert-success - = flash.notice - - if flash.alert - .alert.alert-danger - = flash.alert + =render partial: "layouts/navbar" #main_div.main_div = yield diff --git a/spec/views/layouts/_navbar_spec.rb b/spec/views/layouts/_navbar_spec.rb new file mode 100644 index 000000000..a0c0ec6d0 --- /dev/null +++ b/spec/views/layouts/_navbar_spec.rb @@ -0,0 +1,56 @@ +require 'spec_helper' + +describe 'layouts/_navbar.html.haml', type: :view do + let(:administrateur) { create(:administrateur) } + let(:gestionnaire) { create(:gestionnaire, administrateurs: [administrateur]) } + + let!(:procedure) { create(:procedure, administrateur: administrateur) } + + describe 'navbar entries' do + + context 'when disconnected' do + before do + render + end + subject { rendered } + it { is_expected.to match(/href="\/users\/sign_in">Utilisateur/) } + it { is_expected.to match(/href="\/gestionnaires\/sign_in">Accompagnateur/) } + it { is_expected.to match(/href="\/administrateurs\/sign_in">Administrateur/) } + it { is_expected.not_to match(/Mes Dossiers/) } + it { is_expected.not_to match(/Se déconnecter/) } + end + + context 'when administrateur is connected' do + before do + @request.env["devise.mapping"] = Devise.mappings[:administrateur] + @current_user = administrateur + sign_in @current_user + render + end + + subject { rendered } + it { is_expected.not_to match(/href="\/users\/sign_in">Utilisateur/) } + it { is_expected.not_to match(/href="\/gestionnaires\/sign_in">Accompagnateur/) } + it { is_expected.not_to match(/href="\/administrateurs\/sign_in">Administrateur/) } + it { is_expected.not_to match(/Mes Dossiers/) } + it { is_expected.to match(/Se déconnecter/) } + end + + context 'when gestionnaire is connected' do + before do + @request.env["devise.mapping"] = Devise.mappings[:gestionnaire] + @current_user = gestionnaire + sign_in @current_user + render + end + + subject { rendered } + it { is_expected.not_to match(/href="\/users\/sign_in">Utilisateur/) } + it { is_expected.not_to match(/href="\/gestionnaires\/sign_in">Accompagnateur/) } + it { is_expected.not_to match(/href="\/administrateurs\/sign_in">Administrateur/) } + it { is_expected.to match(/Mes Dossiers/) } + it { is_expected.to match(/Se déconnecter/) } + end + + end +end From 0314bc54dd41449aac06f55dd77181a6bba5ce37 Mon Sep 17 00:00:00 2001 From: Guillaume Lazzara Date: Tue, 7 Jun 2016 15:52:55 +0200 Subject: [PATCH 10/33] =?UTF-8?q?Add=20'mes=20proc=C3=A9dures'=20link=20in?= =?UTF-8?q?=20navbar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/views/layouts/_navbar.html.haml | 5 ++++- spec/views/layouts/_navbar_spec.rb | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/views/layouts/_navbar.html.haml b/app/views/layouts/_navbar.html.haml index 29abbf4ae..cdc617695 100644 --- a/app/views/layouts/_navbar.html.haml +++ b/app/views/layouts/_navbar.html.haml @@ -8,7 +8,10 @@ %span{ style: 'margin-left: 25px;' } %b Mes Dossiers - + -elsif administrateur_signed_in? + %span{ style: 'margin-left: 25px;' } + %b + Mes Procédures #sign_out -if gestionnaire_signed_in? = render partial: 'gestionnaires/login_banner' diff --git a/spec/views/layouts/_navbar_spec.rb b/spec/views/layouts/_navbar_spec.rb index a0c0ec6d0..3c3156d6d 100644 --- a/spec/views/layouts/_navbar_spec.rb +++ b/spec/views/layouts/_navbar_spec.rb @@ -17,6 +17,7 @@ describe 'layouts/_navbar.html.haml', type: :view do it { is_expected.to match(/href="\/gestionnaires\/sign_in">Accompagnateur/) } it { is_expected.to match(/href="\/administrateurs\/sign_in">Administrateur/) } it { is_expected.not_to match(/Mes Dossiers/) } + it { is_expected.not_to match(/Mes Procédures/) } it { is_expected.not_to match(/Se déconnecter/) } end @@ -33,6 +34,7 @@ describe 'layouts/_navbar.html.haml', type: :view do it { is_expected.not_to match(/href="\/gestionnaires\/sign_in">Accompagnateur/) } it { is_expected.not_to match(/href="\/administrateurs\/sign_in">Administrateur/) } it { is_expected.not_to match(/Mes Dossiers/) } + it { is_expected.to match(/Mes Procédures/) } it { is_expected.to match(/Se déconnecter/) } end @@ -48,6 +50,7 @@ describe 'layouts/_navbar.html.haml', type: :view do it { is_expected.not_to match(/href="\/users\/sign_in">Utilisateur/) } it { is_expected.not_to match(/href="\/gestionnaires\/sign_in">Accompagnateur/) } it { is_expected.not_to match(/href="\/administrateurs\/sign_in">Administrateur/) } + it { is_expected.not_to match(/Mes Procédures/) } it { is_expected.to match(/Mes Dossiers/) } it { is_expected.to match(/Se déconnecter/) } end From 35a07aec87ff8fb6e0ebed781d129dc49d1071c6 Mon Sep 17 00:00:00 2001 From: Xavier J Date: Wed, 8 Jun 2016 10:41:01 +0200 Subject: [PATCH 11/33] Ajustement graphique navbar --- app/assets/stylesheets/application.scss | 4 ++++ app/views/layouts/_navbar.html.haml | 20 ++++---------------- app/views/layouts/application.html.haml | 7 +++++++ 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index b5c193e7e..c28c00141 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -182,6 +182,10 @@ div.pagination { text-align: center; } +.alert{ + margin-bottom: 0px; +} + .alert.alert-success.move_up { position: absolute; top: 0px; diff --git a/app/views/layouts/_navbar.html.haml b/app/views/layouts/_navbar.html.haml index cdc617695..6ed7c1bc6 100644 --- a/app/views/layouts/_navbar.html.haml +++ b/app/views/layouts/_navbar.html.haml @@ -3,15 +3,11 @@ = image_tag('marianne_small.png', class: 'logo') %a{href: '/'} = image_tag('logo-tps.png', class: 'logo') -%a{href: '/'} - -if gestionnaire_signed_in? - %span{ style: 'margin-left: 25px;' } - %b - Mes Dossiers +%a{href: '/', class: 'btn btn-md'} + -if gestionnaire_signed_in? || user_signed_in? + Mes Dossiers -elsif administrateur_signed_in? - %span{ style: 'margin-left: 25px;' } - %b - Mes Procédures + Mes Procédures #sign_out -if gestionnaire_signed_in? = render partial: 'gestionnaires/login_banner' @@ -35,11 +31,3 @@ = link_to "Utilisateur", '/users/sign_in', method: :get, :class => 'btn btn-md' = link_to "Accompagnateur", '/gestionnaires/sign_in', method: :get, :class => 'btn btn-md' = link_to "Administrateur", '/administrateurs/sign_in', method: :get, :class => 'btn btn-md' - -#flash_message.center -- if flash.notice - .alert.alert-success - = flash.notice -- if flash.alert - .alert.alert-danger - = flash.alert \ No newline at end of file diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 5382edc73..440c59544 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -14,6 +14,13 @@ %div#header.navbar =render partial: "layouts/navbar" + #flash_message.center + - if flash.notice + .alert.alert-success + = flash.notice + - if flash.alert + .alert.alert-danger + = flash.alert #main_div.main_div = yield From 1551b7ce211cc1381fa8774d79c70905a91dd829 Mon Sep 17 00:00:00 2001 From: Guillaume Lazzara Date: Wed, 8 Jun 2016 16:45:18 +0200 Subject: [PATCH 12/33] Add ordering feature on piece justificative view --- .../admin/pieces_justificatives_controller.rb | 19 ++++- .../admin/types_de_champ_controller.rb | 6 +- .../type_de_piece_justificative_decorator.rb | 37 +++++++++ app/models/piece_justificative.rb | 2 +- app/models/procedure.rb | 22 ++++-- .../type_de_piece_justificative_serializer.rb | 4 +- .../pieces_justificatives/_fields.html.haml | 13 +++- .../pieces_justificatives/_form.html.haml | 5 +- .../admin/pieces_justificatives/show.js.erb | 2 +- config/routes.rb | 4 + ...er_place_in_type_de_piece_justificative.rb | 9 +++ db/schema.rb | 3 +- .../pieces_justificatives_controller_spec.rb | 76 ++++++++++++++++++- .../type_de_champ_decorator_spec.rb | 55 ++++++++++++++ ...e_de_piece_justificative_decorator_spec.rb | 54 +++++++++++++ .../type_de_piece_justificative_spec.rb | 7 ++ .../show.html.haml_spec.rb | 48 ++++++++++++ 17 files changed, 343 insertions(+), 23 deletions(-) create mode 100644 app/decorators/type_de_piece_justificative_decorator.rb create mode 100644 db/migrate/20160607150440_add_order_place_in_type_de_piece_justificative.rb create mode 100644 spec/decorators/type_de_champ_decorator_spec.rb create mode 100644 spec/decorators/type_de_piece_justificative_decorator_spec.rb create mode 100644 spec/views/admin/types_de_piece_justificative/show.html.haml_spec.rb diff --git a/app/controllers/admin/pieces_justificatives_controller.rb b/app/controllers/admin/pieces_justificatives_controller.rb index 638596511..7ed5c7ef5 100644 --- a/app/controllers/admin/pieces_justificatives_controller.rb +++ b/app/controllers/admin/pieces_justificatives_controller.rb @@ -22,6 +22,23 @@ class Admin::PiecesJustificativesController < AdminController def update_params params .require(:procedure) - .permit(types_de_piece_justificative_attributes: [:libelle, :description, :id]) + .permit(types_de_piece_justificative_attributes: [:libelle, :description, :id, :order_place]) + end + + def move_up + index = params[:index].to_i - 1 + if @procedure.switch_types_de_piece_justificative index + render 'show', format: :js + else + render json: {}, status: 400 + end + end + + def move_down + if @procedure.switch_types_de_piece_justificative params[:index].to_i + render 'show', format: :js + else + render json: {}, status: 400 + end end end \ No newline at end of file diff --git a/app/controllers/admin/types_de_champ_controller.rb b/app/controllers/admin/types_de_champ_controller.rb index b7af379ec..6aa974d74 100644 --- a/app/controllers/admin/types_de_champ_controller.rb +++ b/app/controllers/admin/types_de_champ_controller.rb @@ -1,7 +1,7 @@ class Admin::TypesDeChampController < AdminController before_action :retrieve_procedure before_action :procedure_locked? - + def destroy @procedure.types_de_champ.destroy(params[:id]) render 'show', format: :js @@ -19,7 +19,9 @@ class Admin::TypesDeChampController < AdminController end def update_params - params.require(:procedure).permit(types_de_champ_attributes: [:libelle, :description, :order_place, :type_champ, :id, :mandatory]) + params + .require(:procedure) + .permit(types_de_champ_attributes: [:libelle, :description, :order_place, :type_champ, :id, :mandatory]) end def move_up diff --git a/app/decorators/type_de_piece_justificative_decorator.rb b/app/decorators/type_de_piece_justificative_decorator.rb new file mode 100644 index 000000000..284c1b854 --- /dev/null +++ b/app/decorators/type_de_piece_justificative_decorator.rb @@ -0,0 +1,37 @@ + +class TypeDePieceJustificativeDecorator < Draper::Decorator + delegate_all + def button_up params + h.link_to '', params[:url], class: up_classes, id: "btn_up_#{params[:index]}", remote: true, method: :post if display_up_button?(params[:index]) + end + + def button_down params + h.link_to '', params[:url], class: down_classes, id: "btn_down_#{params[:index]}", remote: true, method: :post if display_down_button?(params[:index]) + end + + private + + def up_classes + base_classes << 'fa-chevron-up' + end + + def down_classes + base_classes << 'fa-chevron-down' + end + + def base_classes + %w(btn btn-default form-control fa) + end + + def display_up_button?(index) + !(index == 0 || count_type_de_piece_justificative < 2) + end + + def display_down_button?(index) + (index + 1) < count_type_de_piece_justificative + end + + def count_type_de_piece_justificative + @count_type_de_piece_justificative ||= procedure.types_de_piece_justificative.count + end +end \ No newline at end of file diff --git a/app/models/piece_justificative.rb b/app/models/piece_justificative.rb index 4d80291b1..1208069d2 100644 --- a/app/models/piece_justificative.rb +++ b/app/models/piece_justificative.rb @@ -5,7 +5,7 @@ class PieceJustificative < ActiveRecord::Base belongs_to :user - delegate :api_entreprise, :libelle, to: :type_de_piece_justificative + delegate :api_entreprise, :libelle, :order_place, to: :type_de_piece_justificative alias_attribute :type, :type_de_piece_justificative_id diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 41aee684c..1d85633a3 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -25,18 +25,28 @@ class Procedure < ActiveRecord::Base types_de_champ.order(:order_place) end + def types_de_piece_justificative_ordered + types_de_piece_justificative.order(:order_place) + end + def self.not_archived id Procedure.where(archived: false).find(id) end def switch_types_de_champ index_of_first_element + switch_list_order(types_de_champ_ordered, index_of_first_element) + end + + def switch_types_de_piece_justificative index_of_first_element + switch_list_order(types_de_piece_justificative_ordered, index_of_first_element) + end + + def switch_list_order(list, index_of_first_element) return false if index_of_first_element < 0 - types_de_champ_tmp = types_de_champ_ordered - nb_types_de_champ = types_de_champ_tmp.count - return false if index_of_first_element == nb_types_de_champ - 1 - return false if types_de_champ_ordered.count < 1 - types_de_champ_tmp[index_of_first_element].update_attributes(order_place: index_of_first_element + 1) - types_de_champ_tmp[index_of_first_element + 1].update_attributes(order_place: index_of_first_element) + return false if index_of_first_element == list.count - 1 + return false if list.count < 1 + list[index_of_first_element].update_attributes(order_place: index_of_first_element + 1) + list[index_of_first_element + 1].update_attributes(order_place: index_of_first_element) true end diff --git a/app/serializers/type_de_piece_justificative_serializer.rb b/app/serializers/type_de_piece_justificative_serializer.rb index 57aae2903..9dc1743b0 100644 --- a/app/serializers/type_de_piece_justificative_serializer.rb +++ b/app/serializers/type_de_piece_justificative_serializer.rb @@ -1,6 +1,6 @@ class TypeDePieceJustificativeSerializer < ActiveModel::Serializer attributes :id, :libelle, - :description - + :description, + :order_place end \ No newline at end of file diff --git a/app/views/admin/pieces_justificatives/_fields.html.haml b/app/views/admin/pieces_justificatives/_fields.html.haml index 7149f7687..aaa552edd 100644 --- a/app/views/admin/pieces_justificatives/_fields.html.haml +++ b/app/views/admin/pieces_justificatives/_fields.html.haml @@ -1,14 +1,21 @@ - -= f.fields_for :types_de_piece_justificative, types_de_piece_justificative do |ff| += f.fields_for :types_de_piece_justificative, types_de_piece_justificative, remote: true do |ff| .form-inline .form-group %h4 Libellé =ff.text_field :libelle, class: 'form-control libelle', placeholder: 'Libellé' - .form-group %h4 Description =ff.text_area :description, class: 'form-control description', placeholder: 'Description' + .form-group + = ff.hidden_field :order_place, value: ff.index + = ff.hidden_field :id + - unless ff.object.id.nil? + .form-group + %br   + = ff.object.button_up(index: ff.index, url: move_up_admin_procedure_pieces_justificatives_path(@procedure, ff.index)) + = ff.object.button_down(index: ff.index, url: move_down_admin_procedure_pieces_justificatives_path(@procedure, ff.index)) + .form-group %br   diff --git a/app/views/admin/pieces_justificatives/_form.html.haml b/app/views/admin/pieces_justificatives/_form.html.haml index 0a9ee481a..7891ae363 100644 --- a/app/views/admin/pieces_justificatives/_form.html.haml +++ b/app/views/admin/pieces_justificatives/_form.html.haml @@ -1,8 +1,7 @@ = form_for [:admin, @procedure], url: admin_procedure_pieces_justificatives_path(@procedure) , remote: true do |f| #liste_piece_justificative - = render partial: 'fields', locals:{ types_de_piece_justificative: @procedure.types_de_piece_justificative, f: f } + = render partial: 'fields', locals:{ types_de_piece_justificative: @procedure.types_de_piece_justificative_ordered.decorate, f: f } = f.submit "Enregistrer", class: 'btn btn-success', id: :save %hr - #new_type_de_piece_justificative - = render partial: 'fields', locals:{ types_de_piece_justificative: TypeDePieceJustificative.new, f: f } + = render partial: 'fields', locals:{ types_de_piece_justificative: TypeDePieceJustificative.new.decorate, f: f } diff --git a/app/views/admin/pieces_justificatives/show.js.erb b/app/views/admin/pieces_justificatives/show.js.erb index c41307172..094e982cb 100644 --- a/app/views/admin/pieces_justificatives/show.js.erb +++ b/app/views/admin/pieces_justificatives/show.js.erb @@ -1,4 +1,4 @@ <% flash.each do |type, message| %> $("#flash_message").html("
<%= message.html_safe %>
").children().fadeOut(5000) <% end %> -$('#piece_justificative_form').html("<%= escape_javascript(render partial: 'form', locals: { procedure: @procedure } ) %>"); \ No newline at end of file +$('#piece_justificative_form').html("<%= escape_javascript(render partial: 'form', locals: { procedure: @procedure } ) %>"); diff --git a/config/routes.rb b/config/routes.rb index 03c1c4a11..6a2841de3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -89,6 +89,10 @@ Rails.application.routes.draw do post '/:index/move_up' => 'types_de_champ#move_up', as: :move_up post '/:index/move_down' => 'types_de_champ#move_down', as: :move_down end + resource :pieces_justificatives, only: [:show, :update] do + post '/:index/move_up' => 'pieces_justificatives#move_up', as: :move_up + post '/:index/move_down' => 'pieces_justificatives#move_down', as: :move_down + end put 'archive' => 'procedures#archive', as: :archive diff --git a/db/migrate/20160607150440_add_order_place_in_type_de_piece_justificative.rb b/db/migrate/20160607150440_add_order_place_in_type_de_piece_justificative.rb new file mode 100644 index 000000000..7cf5fa801 --- /dev/null +++ b/db/migrate/20160607150440_add_order_place_in_type_de_piece_justificative.rb @@ -0,0 +1,9 @@ +class AddOrderPlaceInTypeDePieceJustificative < ActiveRecord::Migration + def up + add_column :types_de_piece_justificative, :order_place, :integer + end + + def down + remove_column :types_de_piece_justificative, :order_place + end +end diff --git a/db/schema.rb b/db/schema.rb index 1c61d1297..cb89f0cce 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: 20160524093540) do +ActiveRecord::Schema.define(version: 20160607150440) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -278,6 +278,7 @@ ActiveRecord::Schema.define(version: 20160524093540) do t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "procedure_id" + t.integer "order_place" end create_table "users", force: :cascade do |t| diff --git a/spec/controllers/admin/pieces_justificatives_controller_spec.rb b/spec/controllers/admin/pieces_justificatives_controller_spec.rb index 1c6ddca9c..09ca67188 100644 --- a/spec/controllers/admin/pieces_justificatives_controller_spec.rb +++ b/spec/controllers/admin/pieces_justificatives_controller_spec.rb @@ -2,12 +2,12 @@ require 'spec_helper' describe Admin::PiecesJustificativesController, type: :controller do let(:admin) { create(:administrateur) } + let(:procedure) { create(:procedure, administrateur: admin) } before do sign_in admin end describe 'GET #show' do - let(:procedure) { create(:procedure, administrateur: admin) } let(:procedure_id) { procedure.id } subject { get :show, procedure_id: procedure_id } @@ -30,7 +30,6 @@ describe Admin::PiecesJustificativesController, type: :controller do end describe 'PUT #update' do - let(:procedure) { create(:procedure, administrateur: admin) } let(:procedure_id) { procedure.id } let(:libelle) { 'RIB' } let(:description) { "relevé d'identité bancaire" } @@ -72,7 +71,6 @@ describe Admin::PiecesJustificativesController, type: :controller do end describe 'DELETE #destroy' do - let(:procedure) { create(:procedure, administrateur: admin) } let!(:pj) { create(:type_de_piece_justificative, procedure: procedure) } let(:procedure_id) { procedure.id } let(:pj_id) { pj.id } @@ -97,4 +95,76 @@ describe Admin::PiecesJustificativesController, type: :controller do it { expect{ subject }.to change(TypeDePieceJustificative, :count).by(-1) } end end + + describe 'POST #move_up' do + subject { post :move_up, procedure_id: procedure.id, index: index, format: :js } + + context 'when procedure have no type de champ' do + let(:index) { 0 } + it { expect(subject.status).to eq(400) } + end + context 'when procedure have only one type de champ' do + let(:index) { 1 } + let!(:type_de_piece_justificative) { create(:type_de_piece_justificative, procedure: procedure) } + it { expect(subject.status).to eq(400) } + end + context 'when procedure have tow type de champs' do + context 'when index == 0' do + let(:index) { 0 } + let!(:type_de_piece_justificative_1) { create(:type_de_piece_justificative, procedure: procedure) } + let!(:type_de_piece_justificative_2) { create(:type_de_piece_justificative, procedure: procedure) } + it { expect(subject.status).to eq(400) } + end + context 'when index > 0' do + let(:index) { 1 } + let!(:type_de_piece_justificative_0) { create(:type_de_piece_justificative, procedure: procedure, order_place: 0) } + let!(:type_de_piece_justificative_1) { create(:type_de_piece_justificative, procedure: procedure, order_place: 1) } + + it { expect(subject.status).to eq(200) } + it { expect(subject).to render_template('show') } + it 'changes order places' do + post :move_up, procedure_id: procedure.id, index: index, format: :js + type_de_piece_justificative_0.reload + type_de_piece_justificative_1.reload + expect(type_de_piece_justificative_0.order_place).to eq(1) + expect(type_de_piece_justificative_1.order_place).to eq(0) + end + end + end + end + + describe 'POST #move_down' do + let(:request) { post :move_down, procedure_id: procedure.id, index: index, format: :js } + let(:index) { 0 } + + subject { request } + + context 'when procedure have no type de champ' do + it { expect(subject.status).to eq(400) } + end + context 'when procedure have only one type de champ' do + let!(:type_de_piece_justificative_0) { create(:type_de_piece_justificative, procedure: procedure) } + it { expect(subject.status).to eq(400) } + end + context 'when procedure have 2 type de champ' do + let!(:type_de_piece_justificative_0) { create(:type_de_piece_justificative, procedure: procedure, order_place: 0) } + let!(:type_de_piece_justificative_1) { create(:type_de_piece_justificative, procedure: procedure, order_place: 1) } + context 'when index represent last type_de_piece_justificative' do + let(:index) { 1 } + it { expect(subject.status).to eq(400) } + end + context 'when index does not represent last type_de_piece_justificative' do + let(:index) { 0 } + it { expect(subject.status).to eq(200) } + it { expect(subject).to render_template('show') } + it 'changes order place' do + request + type_de_piece_justificative_0.reload + type_de_piece_justificative_1.reload + expect(type_de_piece_justificative_0.order_place).to eq(1) + expect(type_de_piece_justificative_1.order_place).to eq(0) + end + end + end + end end \ No newline at end of file diff --git a/spec/decorators/type_de_champ_decorator_spec.rb b/spec/decorators/type_de_champ_decorator_spec.rb new file mode 100644 index 000000000..61a2bc73f --- /dev/null +++ b/spec/decorators/type_de_champ_decorator_spec.rb @@ -0,0 +1,55 @@ + +require 'spec_helper' + +describe TypeDeChampDecorator do + let(:procedure) { create(:procedure) } + let(:url) { 'http://localhost' } + let(:params) { { url: url, index: index } } + let!(:type_de_champ_0) { create(:type_de_champ, procedure: procedure, order_place: 0) } + let!(:type_de_champ_1) { create(:type_de_champ, procedure: procedure, order_place: 1) } + let!(:type_de_champ_2) { create(:type_de_champ, procedure: procedure, order_place: 2) } + + describe '#button_up' do + + describe 'with first piece justificative' do + let(:index) { 0 } + subject { type_de_champ_0.decorate } + let(:button_up) { type_de_champ_.decorate } + + it 'returns a button up' do + expect(subject.button_up(params)).to be(nil) + end + it 'returns a button down' do + expect(subject.button_down(params)).to match(/fa-chevron-down/) + end + end + + describe 'with second out of three piece justificative' do + let(:index) { 1 } + subject { type_de_champ_1.decorate } + let(:button_up) { type_de_champ_1.decorate } + + it 'returns a button up' do + expect(subject.button_up(params)).to match(/fa-chevron-up/) + end + it 'returns a button down' do + expect(subject.button_down(params)).to match(/fa-chevron-down/) + end + end + + describe 'with last piece justificative' do + let(:index) { 2 } + subject { type_de_champ_2.decorate } + let(:button_up) { type_de_champ_1.decorate } + + it 'returns a button up' do + expect(subject.button_up(params)).to match(/fa-chevron-up/) + end + it 'returns a button down' do + expect(subject.button_down(params)).to be(nil) + end + end + end + + +end \ No newline at end of file diff --git a/spec/decorators/type_de_piece_justificative_decorator_spec.rb b/spec/decorators/type_de_piece_justificative_decorator_spec.rb new file mode 100644 index 000000000..45e0d401d --- /dev/null +++ b/spec/decorators/type_de_piece_justificative_decorator_spec.rb @@ -0,0 +1,54 @@ +require 'spec_helper' + +describe TypeDePieceJustificativeDecorator do + let(:procedure) { create(:procedure) } + let(:url) { 'http://localhost' } + let(:params) { { url: url, index: index } } + let!(:type_de_piece_justificative_0) { create(:type_de_piece_justificative, procedure: procedure, order_place: 0) } + let!(:type_de_piece_justificative_1) { create(:type_de_piece_justificative, procedure: procedure, order_place: 1) } + let!(:type_de_piece_justificative_2) { create(:type_de_piece_justificative, procedure: procedure, order_place: 2) } + + describe '#button_up' do + + describe 'with first piece justificative' do + let(:index) { 0 } + subject { type_de_piece_justificative_0.decorate } + let(:button_up) { type_de_piece_justificative_.decorate } + + it 'returns a button up' do + expect(subject.button_up(params)).to be(nil) + end + it 'returns a button down' do + expect(subject.button_down(params)).to match(/fa-chevron-down/) + end + end + + describe 'with second out of three piece justificative' do + let(:index) { 1 } + subject { type_de_piece_justificative_1.decorate } + let(:button_up) { type_de_piece_justificative_1.decorate } + + it 'returns a button up' do + expect(subject.button_up(params)).to match(/fa-chevron-up/) + end + it 'returns a button down' do + expect(subject.button_down(params)).to match(/fa-chevron-down/) + end + end + + describe 'with last piece justificative' do + let(:index) { 2 } + subject { type_de_piece_justificative_2.decorate } + let(:button_up) { type_de_piece_justificative_1.decorate } + + it 'returns a button up' do + expect(subject.button_up(params)).to match(/fa-chevron-up/) + end + it 'returns a button down' do + expect(subject.button_down(params)).to be(nil) + end + end + end + + +end \ No newline at end of file diff --git a/spec/models/type_de_piece_justificative_spec.rb b/spec/models/type_de_piece_justificative_spec.rb index 0fe11970e..a2b47c14e 100644 --- a/spec/models/type_de_piece_justificative_spec.rb +++ b/spec/models/type_de_piece_justificative_spec.rb @@ -9,6 +9,7 @@ describe TypeDePieceJustificative do it { is_expected.to have_db_column(:api_entreprise) } it { is_expected.to have_db_column(:created_at) } it { is_expected.to have_db_column(:updated_at) } + it { is_expected.to have_db_column(:order_place) } end describe 'associations' do @@ -22,5 +23,11 @@ describe TypeDePieceJustificative do it { is_expected.not_to allow_value('').for(:libelle) } it { is_expected.to allow_value('RIB').for(:libelle) } end + + context 'order_place' do + # it { is_expected.not_to allow_value(nil).for(:order_place) } + # it { is_expected.not_to allow_value('').for(:order_place) } + it { is_expected.to allow_value(1).for(:order_place) } + end end end diff --git a/spec/views/admin/types_de_piece_justificative/show.html.haml_spec.rb b/spec/views/admin/types_de_piece_justificative/show.html.haml_spec.rb new file mode 100644 index 000000000..6434a7ddd --- /dev/null +++ b/spec/views/admin/types_de_piece_justificative/show.html.haml_spec.rb @@ -0,0 +1,48 @@ +require 'spec_helper' + +describe 'admin/pieces_justificatives/show.html.haml', type: :view do + let(:procedure) { create(:procedure) } + + describe 'fields sorted' do + let(:first_libelle) { 'salut la compagnie' } + let(:last_libelle) { 'je suis bien sur la page' } + let!(:type_de_piece_justificative_1) { create(:type_de_piece_justificative, procedure: procedure, order_place: 1, libelle: last_libelle) } + let!(:type_de_piece_justificative_0) { create(:type_de_piece_justificative, procedure: procedure, order_place: 0, libelle: first_libelle) } + before do + procedure.reload + assign(:procedure, procedure) + render + end + it 'sorts by order place' do + expect(rendered).to match(/#{first_libelle}.*#{last_libelle}/m) + end + end + + describe 'arrow button' do + subject do + procedure.reload + assign(:procedure, procedure) + render + rendered + end + context 'when there is no field in database' do + it { expect(subject).not_to have_css('.fa-chevron-down') } + it { expect(subject).not_to have_css('.fa-chevron-up') } + end + context 'when there is only one field in database' do + let!(:type_de_piece_justificative_0) { create(:type_de_piece_justificative, procedure: procedure, order_place: 0) } + it { expect(subject).not_to have_css('#btn_down_0') } + it { expect(subject).not_to have_css('#btn_up_0') } + it { expect(subject).not_to have_css('#btn_up_1') } + it { expect(subject).not_to have_css('#btn_down_1') } + end + context 'when there are 2 fields in database' do + let!(:type_de_piece_justificative_0) { create(:type_de_piece_justificative, procedure: procedure, order_place: 0) } + let!(:type_de_piece_justificative_1) { create(:type_de_piece_justificative, procedure: procedure, order_place: 1) } + it { expect(subject).to have_css('#btn_down_0') } + it { expect(subject).not_to have_css('#btn_up_0') } + it { expect(subject).to have_css('#btn_up_1') } + it { expect(subject).not_to have_css('#btn_down_1') } + end + end +end \ No newline at end of file From 23ab25396f76366977f2678eba38411bc8cb87c3 Mon Sep 17 00:00:00 2001 From: Xavier J Date: Thu, 9 Jun 2016 12:08:18 +0200 Subject: [PATCH 13/33] Add type champs "Address" plug at the BAN --- app/assets/javascripts/application.js | 3 +- app/assets/javascripts/description.js | 26 + app/assets/stylesheets/description.scss | 9 + app/assets/stylesheets/typeahead.scss | 35 + app/controllers/ban/search_controller.rb | 9 + app/models/champ.rb | 5 + app/models/type_de_champ.rb | 18 +- app/views/users/description/_champs.html.haml | 2 +- config/routes.rb | 4 + lib/carto/bano/address_retriever.rb | 36 + lib/carto/bano/driver.rb | 7 +- .../controllers/ban/search_controller_spec.rb | 16 + spec/factories/champ.rb | 4 + spec/lib/carto/bano/address_retriever_spec.rb | 44 + spec/models/champ_spec.rb | 22 + spec/support/files/ban_address_search.json | 117 + .../files/ban_address_search_no_result.json | 9 + vendor/assets/javascripts/typeahead.bundle.js | 2451 +++++++++++++++++ 18 files changed, 2804 insertions(+), 13 deletions(-) create mode 100644 app/assets/stylesheets/typeahead.scss create mode 100644 app/controllers/ban/search_controller.rb create mode 100644 lib/carto/bano/address_retriever.rb create mode 100644 spec/controllers/ban/search_controller_spec.rb create mode 100644 spec/factories/champ.rb create mode 100644 spec/lib/carto/bano/address_retriever_spec.rb create mode 100644 spec/support/files/ban_address_search.json create mode 100644 spec/support/files/ban_address_search_no_result.json create mode 100644 vendor/assets/javascripts/typeahead.bundle.js diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 5ec3d6999..fa22bb903 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -29,8 +29,7 @@ //= require franceconnect //= require bootstrap-wysihtml5 //= require bootstrap-wysihtml5/locales/fr-FR - - +//= require typeahead.bundle $(document).on('page:load', scroll_to); $(document).ready(scroll_to); diff --git a/app/assets/javascripts/description.js b/app/assets/javascripts/description.js index 7ca93b611..bb1a793ad 100644 --- a/app/assets/javascripts/description.js +++ b/app/assets/javascripts/description.js @@ -13,6 +13,8 @@ function action_type_de_champs() { toggleErrorClass(this, validatePhone(val)); }); + + address_type_init(); } function toggleErrorClass(node, boolean) { @@ -34,4 +36,28 @@ function validateEmail(email) { function validateInput(input, regex) { return regex.test(input); +} + +function address_type_init() { + + display = 'label'; + + var bloodhound = new Bloodhound({ + datumTokenizer: Bloodhound.tokenizers.obj.whitespace(display), + queryTokenizer: Bloodhound.tokenizers.whitespace, + + remote: { + url: '/ban/search?request=%QUERY', + wildcard: '%QUERY' + } + }); + bloodhound.initialize(); + + $("input[type='address']").typeahead({ + minLength: 1 + }, { + display: display, + source: bloodhound, + limit: 5 + }); } \ No newline at end of file diff --git a/app/assets/stylesheets/description.scss b/app/assets/stylesheets/description.scss index 38947921c..1e1972bea 100644 --- a/app/assets/stylesheets/description.scss +++ b/app/assets/stylesheets/description.scss @@ -21,6 +21,15 @@ } } +.type_champ-address { + @extend .col-md-6; + @extend .col-lg-6; + + input[type='address'] { + width: 100%; + } +} + .type_champ-email { @extend .col-md-4; @extend .col-lg-4; diff --git a/app/assets/stylesheets/typeahead.scss b/app/assets/stylesheets/typeahead.scss new file mode 100644 index 000000000..e29aff51f --- /dev/null +++ b/app/assets/stylesheets/typeahead.scss @@ -0,0 +1,35 @@ +.tt-menu { + width: 555px; + padding: 8px 0; + background-color: #fff; + border: 1px solid #ccc; + border: 1px solid rgba(0, 0, 0, 0.2); + -webkit-border-radius: 8px; + -moz-border-radius: 8px; + border-radius: 8px; + -webkit-box-shadow: 0 5px 10px rgba(0, 0, 0, .2); + -moz-box-shadow: 0 5px 10px rgba(0, 0, 0, .2); + box-shadow: 0 5px 10px rgba(0, 0, 0, .2); +} + +.tt-suggestion { + padding: 3px 20px; + font-size: 18px; + line-height: 24px; +} + +.twitter-typeahead { + width: 555px; +} + +.tt-suggestion:hover { + cursor: pointer; + color: #fff; + background-color: #0097cf; +} + +.tt-suggestion.tt-cursor { + color: #fff; + background-color: #0097cf; + +} \ No newline at end of file diff --git a/app/controllers/ban/search_controller.rb b/app/controllers/ban/search_controller.rb new file mode 100644 index 000000000..63875a93a --- /dev/null +++ b/app/controllers/ban/search_controller.rb @@ -0,0 +1,9 @@ +class Ban::SearchController < ApplicationController + def get + request = params[:request] + + render json: Carto::Bano::AddressRetriever.new(request).list.inject([]) { + |acc, value| acc.push({label: value}) + }.to_json + end +end \ No newline at end of file diff --git a/app/models/champ.rb b/app/models/champ.rb index 2679d1923..b4c389c8c 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -7,4 +7,9 @@ class Champ < ActiveRecord::Base def mandatory? mandatory end + + def data_provide + return 'datepicker' if type_champ == 'datetime' + return 'typeahead' if type_champ == 'address' + end end diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index aa5d4b87e..98b148447 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -1,12 +1,14 @@ class TypeDeChamp < ActiveRecord::Base - enum type_champs: {text: 'text', - email: 'email', - phone: 'phone', - civilite: 'civilite', - textarea: 'textarea', - datetime: 'datetime', - number: 'number', - checkbox: 'checkbox' + enum type_champs: { + text: 'text', + textarea: 'textarea', + datetime: 'datetime', + number: 'number', + checkbox: 'checkbox', + civilite: 'civilite', + email: 'email', + phone: 'phone', + address: 'address' } belongs_to :procedure diff --git a/app/views/users/description/_champs.html.haml b/app/views/users/description/_champs.html.haml index d1a724967..c1738f35b 100644 --- a/app/views/users/description/_champs.html.haml +++ b/app/views/users/description/_champs.html.haml @@ -34,5 +34,5 @@ id: "champs_#{champ.id}", value: champ.value, type: champ.type_champ, - 'data-provide' => ('datepicker' if champ.type_champ == 'datetime'), + 'data-provide' => champ.data_provide, 'data-date-format' => ('dd/mm/yyyy' if champ.type_champ == 'datetime')} \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 03c1c4a11..8847d0832 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -108,6 +108,10 @@ Rails.application.routes.draw do resources :gestionnaires, only: [:index, :create, :destroy] end + namespace :ban do + get 'search' => 'search#get' + end + get 'backoffice' => 'backoffice#index' namespace :backoffice do diff --git a/lib/carto/bano/address_retriever.rb b/lib/carto/bano/address_retriever.rb new file mode 100644 index 000000000..83fb03de3 --- /dev/null +++ b/lib/carto/bano/address_retriever.rb @@ -0,0 +1,36 @@ +module Carto + module Bano + # input : address + # output : Array List label address + class AddressRetriever + def initialize(address) + @address = address + end + + def list + @list ||= convert_driver_result_to_full_address + end + + private + + def driver + @driver ||= Carto::Bano::Driver.new @address, 5 + end + + def convert_driver_result_to_full_address + result = JSON.parse(driver.call) + + if result['features'].size == 0 + Rails.logger.error "unable to find location for address #{@address}" + return [] + end + + result['features'].inject([]) do |acc, feature| + acc.push feature['properties']['label'] + end + rescue TypeError, JSON::ParserError + [] + end + end + end +end diff --git a/lib/carto/bano/driver.rb b/lib/carto/bano/driver.rb index 60ec57f27..5d18382fb 100644 --- a/lib/carto/bano/driver.rb +++ b/lib/carto/bano/driver.rb @@ -3,12 +3,15 @@ module Carto # input : string (address) # output : json class Driver - def initialize(address) + def initialize(address, limit = 1) @address = address + @limit = limit end def call - RestClient.get api_url, params: { q: @address, limit: 1 } + RestClient.get api_url, params: { q: @address, limit: @limit } + rescue RestClient::ServiceUnavailable + nil end def api_url diff --git a/spec/controllers/ban/search_controller_spec.rb b/spec/controllers/ban/search_controller_spec.rb new file mode 100644 index 000000000..5d799817e --- /dev/null +++ b/spec/controllers/ban/search_controller_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe Ban::SearchController, type: :controller do + describe '#GET' do + + let (:request) { '' } + + before do + stub_request(:get, "http://api-adresse.data.gouv.fr/search?limit=5&q="). + to_return(:status => 200, :body => 'Missing query', :headers => {}) + get :get, request: request + end + + it { expect(response.status).to eq 200 } + end +end diff --git a/spec/factories/champ.rb b/spec/factories/champ.rb new file mode 100644 index 000000000..96620cb80 --- /dev/null +++ b/spec/factories/champ.rb @@ -0,0 +1,4 @@ +FactoryGirl.define do + factory :champ do + end +end diff --git a/spec/lib/carto/bano/address_retriever_spec.rb b/spec/lib/carto/bano/address_retriever_spec.rb new file mode 100644 index 000000000..0f7797a0e --- /dev/null +++ b/spec/lib/carto/bano/address_retriever_spec.rb @@ -0,0 +1,44 @@ +require 'spec_helper' + +describe Carto::Bano::AddressRetriever do + describe '#list' do + let(:request) { 'Paris' } + let(:response) { File.open('spec/support/files/ban_address_search.json') } + let(:status) { 200 } + + subject { described_class.new(request).list } + + before do + stub_request(:get, "http://api-adresse.data.gouv.fr/search?&q=#{request}&limit=5") + .to_return(status: status, body: response, headers: {}) + end + + context 'when address return a list of address' do + it { expect(subject.size).to eq 5 } + it { is_expected.to be_an_instance_of Array } + end + + context 'when address return an empty list' do + let(:response) { File.open('spec/support/files/ban_address_search_no_result.json') } + + it { expect(subject.size).to eq 0 } + it { is_expected.to be_an_instance_of Array } + end + + context 'when BAN is unavailable' do + let(:status) { 503 } + let(:response) { '' } + + it { expect(subject.size).to eq 0 } + it { is_expected.to be_an_instance_of Array } + end + + context 'when request is empty' do + let(:response) { 'Missing query' } + let(:request) { '' } + + it { expect(subject.size).to eq 0 } + it { is_expected.to be_an_instance_of Array } + end + end +end diff --git a/spec/models/champ_spec.rb b/spec/models/champ_spec.rb index 4216eb359..0e2cd61a8 100644 --- a/spec/models/champ_spec.rb +++ b/spec/models/champ_spec.rb @@ -15,4 +15,26 @@ describe Champ do it { is_expected.to delegate_method(:type_champ).to(:type_de_champ) } it { is_expected.to delegate_method(:order_place).to(:type_de_champ) } end + + describe 'data_provide' do + let(:champ) { create :champ } + + subject { champ.data_provide } + + context 'when type_champ is datetime' do + before do + champ.type_de_champ = create :type_de_champ, type_champ: 'datetime' + end + + it { is_expected.to eq 'datepicker' } + end + + context 'when type_champ is address' do + before do + champ.type_de_champ = create :type_de_champ, type_champ: 'address' + end + + it { is_expected.to eq 'typeahead' } + end + end end \ No newline at end of file diff --git a/spec/support/files/ban_address_search.json b/spec/support/files/ban_address_search.json new file mode 100644 index 000000000..ac00ec114 --- /dev/null +++ b/spec/support/files/ban_address_search.json @@ -0,0 +1,117 @@ +{ + "limit": 5, + "attribution": "BAN", + "version": "draft", + "licence": "ODbL 1.0", + "query": "Paris", + "type": "FeatureCollection", + "features": [ + { + "geometry": { + "type": "Point", + "coordinates": [ + 2.3469, + 48.8589 + ] + }, + "properties": { + "adm_weight": "6", + "citycode": "75056", + "name": "Paris", + "city": "Paris", + "postcode": "75000", + "context": "75, \u00cele-de-France", + "score": 1.0, + "label": "Paris", + "id": "75056", + "type": "city", + "population": "2244" + }, + "type": "Feature" + }, + { + "geometry": { + "type": "Point", + "coordinates": [ + 4.366801, + 44.425528 + ] + }, + "properties": { + "citycode": "07330", + "postcode": "07150", + "name": "Paris", + "id": "07330_B095_bd3524", + "context": "07, Ard\u00e8che, Rh\u00f4ne-Alpes", + "score": 0.8291454545454544, + "label": "Paris 07150 Vallon-Pont-d'Arc", + "city": "Vallon-Pont-d'Arc", + "type": "locality" + }, + "type": "Feature" + }, + { + "geometry": { + "type": "Point", + "coordinates": [ + 3.564293, + 45.766413 + ] + }, + "properties": { + "citycode": "63125", + "postcode": "63120", + "name": "Paris", + "city": "Courpi\u00e8re", + "context": "63, Puy-de-D\u00f4me, Auvergne", + "score": 0.8255363636363636, + "label": "Paris 63120 Courpi\u00e8re", + "id": "63125_B221_03549b", + "type": "locality" + }, + "type": "Feature" + }, + { + "geometry": { + "type": "Point", + "coordinates": [ + 1.550208, + 44.673592 + ] + }, + "properties": { + "citycode": "46138", + "postcode": "46240", + "name": "PARIS (Vaillac)", + "city": "C\u0153ur de Causse", + "context": "46, Lot, Midi-Pyr\u00e9n\u00e9es", + "score": 0.824090909090909, + "label": "PARIS (Vaillac) 46240 C\u0153ur de Causse", + "id": "46138_XXXX_6ee4ec", + "type": "street" + }, + "type": "Feature" + }, + { + "geometry": { + "type": "Point", + "coordinates": [ + -0.526884, + 43.762253 + ] + }, + "properties": { + "citycode": "40282", + "postcode": "40500", + "name": "Paris", + "city": "Saint-Sever", + "context": "40, Landes, Aquitaine", + "score": 0.8236181818181818, + "label": "Paris 40500 Saint-Sever", + "id": "40282_B237_2364e3", + "type": "locality" + }, + "type": "Feature" + } + ] +} \ No newline at end of file diff --git a/spec/support/files/ban_address_search_no_result.json b/spec/support/files/ban_address_search_no_result.json new file mode 100644 index 000000000..1c2078f75 --- /dev/null +++ b/spec/support/files/ban_address_search_no_result.json @@ -0,0 +1,9 @@ +{ + "limit": 5, + "attribution": "BAN", + "version": "draft", + "licence": "ODbL 1.0", + "query": "Paris", + "type": "FeatureCollection", + "features": [] +} \ No newline at end of file diff --git a/vendor/assets/javascripts/typeahead.bundle.js b/vendor/assets/javascripts/typeahead.bundle.js new file mode 100644 index 000000000..64b508ac5 --- /dev/null +++ b/vendor/assets/javascripts/typeahead.bundle.js @@ -0,0 +1,2451 @@ +/*! + * typeahead.js 0.11.1 + * https://github.com/twitter/typeahead.js + * Copyright 2013-2015 Twitter, Inc. and other contributors; Licensed MIT + */ + +(function(root, factory) { + if (typeof define === "function" && define.amd) { + define("bloodhound", [ "jquery" ], function(a0) { + return root["Bloodhound"] = factory(a0); + }); + } else if (typeof exports === "object") { + module.exports = factory(require("jquery")); + } else { + root["Bloodhound"] = factory(jQuery); + } +})(this, function($) { + var _ = function() { + "use strict"; + return { + isMsie: function() { + return /(msie|trident)/i.test(navigator.userAgent) ? navigator.userAgent.match(/(msie |rv:)(\d+(.\d+)?)/i)[2] : false; + }, + isBlankString: function(str) { + return !str || /^\s*$/.test(str); + }, + escapeRegExChars: function(str) { + return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); + }, + isString: function(obj) { + return typeof obj === "string"; + }, + isNumber: function(obj) { + return typeof obj === "number"; + }, + isArray: $.isArray, + isFunction: $.isFunction, + isObject: $.isPlainObject, + isUndefined: function(obj) { + return typeof obj === "undefined"; + }, + isElement: function(obj) { + return !!(obj && obj.nodeType === 1); + }, + isJQuery: function(obj) { + return obj instanceof $; + }, + toStr: function toStr(s) { + return _.isUndefined(s) || s === null ? "" : s + ""; + }, + bind: $.proxy, + each: function(collection, cb) { + $.each(collection, reverseArgs); + function reverseArgs(index, value) { + return cb(value, index); + } + }, + map: $.map, + filter: $.grep, + every: function(obj, test) { + var result = true; + if (!obj) { + return result; + } + $.each(obj, function(key, val) { + if (!(result = test.call(null, val, key, obj))) { + return false; + } + }); + return !!result; + }, + some: function(obj, test) { + var result = false; + if (!obj) { + return result; + } + $.each(obj, function(key, val) { + if (result = test.call(null, val, key, obj)) { + return false; + } + }); + return !!result; + }, + mixin: $.extend, + identity: function(x) { + return x; + }, + clone: function(obj) { + return $.extend(true, {}, obj); + }, + getIdGenerator: function() { + var counter = 0; + return function() { + return counter++; + }; + }, + templatify: function templatify(obj) { + return $.isFunction(obj) ? obj : template; + function template() { + return String(obj); + } + }, + defer: function(fn) { + setTimeout(fn, 0); + }, + debounce: function(func, wait, immediate) { + var timeout, result; + return function() { + var context = this, args = arguments, later, callNow; + later = function() { + timeout = null; + if (!immediate) { + result = func.apply(context, args); + } + }; + callNow = immediate && !timeout; + clearTimeout(timeout); + timeout = setTimeout(later, wait); + if (callNow) { + result = func.apply(context, args); + } + return result; + }; + }, + throttle: function(func, wait) { + var context, args, timeout, result, previous, later; + previous = 0; + later = function() { + previous = new Date(); + timeout = null; + result = func.apply(context, args); + }; + return function() { + var now = new Date(), remaining = wait - (now - previous); + context = this; + args = arguments; + if (remaining <= 0) { + clearTimeout(timeout); + timeout = null; + previous = now; + result = func.apply(context, args); + } else if (!timeout) { + timeout = setTimeout(later, remaining); + } + return result; + }; + }, + stringify: function(val) { + return _.isString(val) ? val : JSON.stringify(val); + }, + noop: function() {} + }; + }(); + var VERSION = "0.11.1"; + var tokenizers = function() { + "use strict"; + return { + nonword: nonword, + whitespace: whitespace, + obj: { + nonword: getObjTokenizer(nonword), + whitespace: getObjTokenizer(whitespace) + } + }; + function whitespace(str) { + str = _.toStr(str); + return str ? str.split(/\s+/) : []; + } + function nonword(str) { + str = _.toStr(str); + return str ? str.split(/\W+/) : []; + } + function getObjTokenizer(tokenizer) { + return function setKey(keys) { + keys = _.isArray(keys) ? keys : [].slice.call(arguments, 0); + return function tokenize(o) { + var tokens = []; + _.each(keys, function(k) { + tokens = tokens.concat(tokenizer(_.toStr(o[k]))); + }); + return tokens; + }; + }; + } + }(); + var LruCache = function() { + "use strict"; + function LruCache(maxSize) { + this.maxSize = _.isNumber(maxSize) ? maxSize : 100; + this.reset(); + if (this.maxSize <= 0) { + this.set = this.get = $.noop; + } + } + _.mixin(LruCache.prototype, { + set: function set(key, val) { + var tailItem = this.list.tail, node; + if (this.size >= this.maxSize) { + this.list.remove(tailItem); + delete this.hash[tailItem.key]; + this.size--; + } + if (node = this.hash[key]) { + node.val = val; + this.list.moveToFront(node); + } else { + node = new Node(key, val); + this.list.add(node); + this.hash[key] = node; + this.size++; + } + }, + get: function get(key) { + var node = this.hash[key]; + if (node) { + this.list.moveToFront(node); + return node.val; + } + }, + reset: function reset() { + this.size = 0; + this.hash = {}; + this.list = new List(); + } + }); + function List() { + this.head = this.tail = null; + } + _.mixin(List.prototype, { + add: function add(node) { + if (this.head) { + node.next = this.head; + this.head.prev = node; + } + this.head = node; + this.tail = this.tail || node; + }, + remove: function remove(node) { + node.prev ? node.prev.next = node.next : this.head = node.next; + node.next ? node.next.prev = node.prev : this.tail = node.prev; + }, + moveToFront: function(node) { + this.remove(node); + this.add(node); + } + }); + function Node(key, val) { + this.key = key; + this.val = val; + this.prev = this.next = null; + } + return LruCache; + }(); + var PersistentStorage = function() { + "use strict"; + var LOCAL_STORAGE; + try { + LOCAL_STORAGE = window.localStorage; + LOCAL_STORAGE.setItem("~~~", "!"); + LOCAL_STORAGE.removeItem("~~~"); + } catch (err) { + LOCAL_STORAGE = null; + } + function PersistentStorage(namespace, override) { + this.prefix = [ "__", namespace, "__" ].join(""); + this.ttlKey = "__ttl__"; + this.keyMatcher = new RegExp("^" + _.escapeRegExChars(this.prefix)); + this.ls = override || LOCAL_STORAGE; + !this.ls && this._noop(); + } + _.mixin(PersistentStorage.prototype, { + _prefix: function(key) { + return this.prefix + key; + }, + _ttlKey: function(key) { + return this._prefix(key) + this.ttlKey; + }, + _noop: function() { + this.get = this.set = this.remove = this.clear = this.isExpired = _.noop; + }, + _safeSet: function(key, val) { + try { + this.ls.setItem(key, val); + } catch (err) { + if (err.name === "QuotaExceededError") { + this.clear(); + this._noop(); + } + } + }, + get: function(key) { + if (this.isExpired(key)) { + this.remove(key); + } + return decode(this.ls.getItem(this._prefix(key))); + }, + set: function(key, val, ttl) { + if (_.isNumber(ttl)) { + this._safeSet(this._ttlKey(key), encode(now() + ttl)); + } else { + this.ls.removeItem(this._ttlKey(key)); + } + return this._safeSet(this._prefix(key), encode(val)); + }, + remove: function(key) { + this.ls.removeItem(this._ttlKey(key)); + this.ls.removeItem(this._prefix(key)); + return this; + }, + clear: function() { + var i, keys = gatherMatchingKeys(this.keyMatcher); + for (i = keys.length; i--; ) { + this.remove(keys[i]); + } + return this; + }, + isExpired: function(key) { + var ttl = decode(this.ls.getItem(this._ttlKey(key))); + return _.isNumber(ttl) && now() > ttl ? true : false; + } + }); + return PersistentStorage; + function now() { + return new Date().getTime(); + } + function encode(val) { + return JSON.stringify(_.isUndefined(val) ? null : val); + } + function decode(val) { + return $.parseJSON(val); + } + function gatherMatchingKeys(keyMatcher) { + var i, key, keys = [], len = LOCAL_STORAGE.length; + for (i = 0; i < len; i++) { + if ((key = LOCAL_STORAGE.key(i)).match(keyMatcher)) { + keys.push(key.replace(keyMatcher, "")); + } + } + return keys; + } + }(); + var Transport = function() { + "use strict"; + var pendingRequestsCount = 0, pendingRequests = {}, maxPendingRequests = 6, sharedCache = new LruCache(10); + function Transport(o) { + o = o || {}; + this.cancelled = false; + this.lastReq = null; + this._send = o.transport; + this._get = o.limiter ? o.limiter(this._get) : this._get; + this._cache = o.cache === false ? new LruCache(0) : sharedCache; + } + Transport.setMaxPendingRequests = function setMaxPendingRequests(num) { + maxPendingRequests = num; + }; + Transport.resetCache = function resetCache() { + sharedCache.reset(); + }; + _.mixin(Transport.prototype, { + _fingerprint: function fingerprint(o) { + o = o || {}; + return o.url + o.type + $.param(o.data || {}); + }, + _get: function(o, cb) { + var that = this, fingerprint, jqXhr; + fingerprint = this._fingerprint(o); + if (this.cancelled || fingerprint !== this.lastReq) { + return; + } + if (jqXhr = pendingRequests[fingerprint]) { + jqXhr.done(done).fail(fail); + } else if (pendingRequestsCount < maxPendingRequests) { + pendingRequestsCount++; + pendingRequests[fingerprint] = this._send(o).done(done).fail(fail).always(always); + } else { + this.onDeckRequestArgs = [].slice.call(arguments, 0); + } + function done(resp) { + cb(null, resp); + that._cache.set(fingerprint, resp); + } + function fail() { + cb(true); + } + function always() { + pendingRequestsCount--; + delete pendingRequests[fingerprint]; + if (that.onDeckRequestArgs) { + that._get.apply(that, that.onDeckRequestArgs); + that.onDeckRequestArgs = null; + } + } + }, + get: function(o, cb) { + var resp, fingerprint; + cb = cb || $.noop; + o = _.isString(o) ? { + url: o + } : o || {}; + fingerprint = this._fingerprint(o); + this.cancelled = false; + this.lastReq = fingerprint; + if (resp = this._cache.get(fingerprint)) { + cb(null, resp); + } else { + this._get(o, cb); + } + }, + cancel: function() { + this.cancelled = true; + } + }); + return Transport; + }(); + var SearchIndex = window.SearchIndex = function() { + "use strict"; + var CHILDREN = "c", IDS = "i"; + function SearchIndex(o) { + o = o || {}; + if (!o.datumTokenizer || !o.queryTokenizer) { + $.error("datumTokenizer and queryTokenizer are both required"); + } + this.identify = o.identify || _.stringify; + this.datumTokenizer = o.datumTokenizer; + this.queryTokenizer = o.queryTokenizer; + this.reset(); + } + _.mixin(SearchIndex.prototype, { + bootstrap: function bootstrap(o) { + this.datums = o.datums; + this.trie = o.trie; + }, + add: function(data) { + var that = this; + data = _.isArray(data) ? data : [ data ]; + _.each(data, function(datum) { + var id, tokens; + that.datums[id = that.identify(datum)] = datum; + tokens = normalizeTokens(that.datumTokenizer(datum)); + _.each(tokens, function(token) { + var node, chars, ch; + node = that.trie; + chars = token.split(""); + while (ch = chars.shift()) { + node = node[CHILDREN][ch] || (node[CHILDREN][ch] = newNode()); + node[IDS].push(id); + } + }); + }); + }, + get: function get(ids) { + var that = this; + return _.map(ids, function(id) { + return that.datums[id]; + }); + }, + search: function search(query) { + var that = this, tokens, matches; + tokens = normalizeTokens(this.queryTokenizer(query)); + _.each(tokens, function(token) { + var node, chars, ch, ids; + if (matches && matches.length === 0) { + return false; + } + node = that.trie; + chars = token.split(""); + while (node && (ch = chars.shift())) { + node = node[CHILDREN][ch]; + } + if (node && chars.length === 0) { + ids = node[IDS].slice(0); + matches = matches ? getIntersection(matches, ids) : ids; + } else { + matches = []; + return false; + } + }); + return matches ? _.map(unique(matches), function(id) { + return that.datums[id]; + }) : []; + }, + all: function all() { + var values = []; + for (var key in this.datums) { + values.push(this.datums[key]); + } + return values; + }, + reset: function reset() { + this.datums = {}; + this.trie = newNode(); + }, + serialize: function serialize() { + return { + datums: this.datums, + trie: this.trie + }; + } + }); + return SearchIndex; + function normalizeTokens(tokens) { + tokens = _.filter(tokens, function(token) { + return !!token; + }); + tokens = _.map(tokens, function(token) { + return token.toLowerCase(); + }); + return tokens; + } + function newNode() { + var node = {}; + node[IDS] = []; + node[CHILDREN] = {}; + return node; + } + function unique(array) { + var seen = {}, uniques = []; + for (var i = 0, len = array.length; i < len; i++) { + if (!seen[array[i]]) { + seen[array[i]] = true; + uniques.push(array[i]); + } + } + return uniques; + } + function getIntersection(arrayA, arrayB) { + var ai = 0, bi = 0, intersection = []; + arrayA = arrayA.sort(); + arrayB = arrayB.sort(); + var lenArrayA = arrayA.length, lenArrayB = arrayB.length; + while (ai < lenArrayA && bi < lenArrayB) { + if (arrayA[ai] < arrayB[bi]) { + ai++; + } else if (arrayA[ai] > arrayB[bi]) { + bi++; + } else { + intersection.push(arrayA[ai]); + ai++; + bi++; + } + } + return intersection; + } + }(); + var Prefetch = function() { + "use strict"; + var keys; + keys = { + data: "data", + protocol: "protocol", + thumbprint: "thumbprint" + }; + function Prefetch(o) { + this.url = o.url; + this.ttl = o.ttl; + this.cache = o.cache; + this.prepare = o.prepare; + this.transform = o.transform; + this.transport = o.transport; + this.thumbprint = o.thumbprint; + this.storage = new PersistentStorage(o.cacheKey); + } + _.mixin(Prefetch.prototype, { + _settings: function settings() { + return { + url: this.url, + type: "GET", + dataType: "json" + }; + }, + store: function store(data) { + if (!this.cache) { + return; + } + this.storage.set(keys.data, data, this.ttl); + this.storage.set(keys.protocol, location.protocol, this.ttl); + this.storage.set(keys.thumbprint, this.thumbprint, this.ttl); + }, + fromCache: function fromCache() { + var stored = {}, isExpired; + if (!this.cache) { + return null; + } + stored.data = this.storage.get(keys.data); + stored.protocol = this.storage.get(keys.protocol); + stored.thumbprint = this.storage.get(keys.thumbprint); + isExpired = stored.thumbprint !== this.thumbprint || stored.protocol !== location.protocol; + return stored.data && !isExpired ? stored.data : null; + }, + fromNetwork: function(cb) { + var that = this, settings; + if (!cb) { + return; + } + settings = this.prepare(this._settings()); + this.transport(settings).fail(onError).done(onResponse); + function onError() { + cb(true); + } + function onResponse(resp) { + cb(null, that.transform(resp)); + } + }, + clear: function clear() { + this.storage.clear(); + return this; + } + }); + return Prefetch; + }(); + var Remote = function() { + "use strict"; + function Remote(o) { + this.url = o.url; + this.prepare = o.prepare; + this.transform = o.transform; + this.transport = new Transport({ + cache: o.cache, + limiter: o.limiter, + transport: o.transport + }); + } + _.mixin(Remote.prototype, { + _settings: function settings() { + return { + url: this.url, + type: "GET", + dataType: "json" + }; + }, + get: function get(query, cb) { + var that = this, settings; + if (!cb) { + return; + } + query = query || ""; + settings = this.prepare(query, this._settings()); + return this.transport.get(settings, onResponse); + function onResponse(err, resp) { + err ? cb([]) : cb(that.transform(resp)); + } + }, + cancelLastRequest: function cancelLastRequest() { + this.transport.cancel(); + } + }); + return Remote; + }(); + var oParser = function() { + "use strict"; + return function parse(o) { + var defaults, sorter; + defaults = { + initialize: true, + identify: _.stringify, + datumTokenizer: null, + queryTokenizer: null, + sufficient: 5, + sorter: null, + local: [], + prefetch: null, + remote: null + }; + o = _.mixin(defaults, o || {}); + !o.datumTokenizer && $.error("datumTokenizer is required"); + !o.queryTokenizer && $.error("queryTokenizer is required"); + sorter = o.sorter; + o.sorter = sorter ? function(x) { + return x.sort(sorter); + } : _.identity; + o.local = _.isFunction(o.local) ? o.local() : o.local; + o.prefetch = parsePrefetch(o.prefetch); + o.remote = parseRemote(o.remote); + return o; + }; + function parsePrefetch(o) { + var defaults; + if (!o) { + return null; + } + defaults = { + url: null, + ttl: 24 * 60 * 60 * 1e3, + cache: true, + cacheKey: null, + thumbprint: "", + prepare: _.identity, + transform: _.identity, + transport: null + }; + o = _.isString(o) ? { + url: o + } : o; + o = _.mixin(defaults, o); + !o.url && $.error("prefetch requires url to be set"); + o.transform = o.filter || o.transform; + o.cacheKey = o.cacheKey || o.url; + o.thumbprint = VERSION + o.thumbprint; + o.transport = o.transport ? callbackToDeferred(o.transport) : $.ajax; + return o; + } + function parseRemote(o) { + var defaults; + if (!o) { + return; + } + defaults = { + url: null, + cache: true, + prepare: null, + replace: null, + wildcard: null, + limiter: null, + rateLimitBy: "debounce", + rateLimitWait: 300, + transform: _.identity, + transport: null + }; + o = _.isString(o) ? { + url: o + } : o; + o = _.mixin(defaults, o); + !o.url && $.error("remote requires url to be set"); + o.transform = o.filter || o.transform; + o.prepare = toRemotePrepare(o); + o.limiter = toLimiter(o); + o.transport = o.transport ? callbackToDeferred(o.transport) : $.ajax; + delete o.replace; + delete o.wildcard; + delete o.rateLimitBy; + delete o.rateLimitWait; + return o; + } + function toRemotePrepare(o) { + var prepare, replace, wildcard; + prepare = o.prepare; + replace = o.replace; + wildcard = o.wildcard; + if (prepare) { + return prepare; + } + if (replace) { + prepare = prepareByReplace; + } else if (o.wildcard) { + prepare = prepareByWildcard; + } else { + prepare = idenityPrepare; + } + return prepare; + function prepareByReplace(query, settings) { + settings.url = replace(settings.url, query); + return settings; + } + function prepareByWildcard(query, settings) { + settings.url = settings.url.replace(wildcard, encodeURIComponent(query)); + return settings; + } + function idenityPrepare(query, settings) { + return settings; + } + } + function toLimiter(o) { + var limiter, method, wait; + limiter = o.limiter; + method = o.rateLimitBy; + wait = o.rateLimitWait; + if (!limiter) { + limiter = /^throttle$/i.test(method) ? throttle(wait) : debounce(wait); + } + return limiter; + function debounce(wait) { + return function debounce(fn) { + return _.debounce(fn, wait); + }; + } + function throttle(wait) { + return function throttle(fn) { + return _.throttle(fn, wait); + }; + } + } + function callbackToDeferred(fn) { + return function wrapper(o) { + var deferred = $.Deferred(); + fn(o, onSuccess, onError); + return deferred; + function onSuccess(resp) { + _.defer(function() { + deferred.resolve(resp); + }); + } + function onError(err) { + _.defer(function() { + deferred.reject(err); + }); + } + }; + } + }(); + var Bloodhound = function() { + "use strict"; + var old; + old = window && window.Bloodhound; + function Bloodhound(o) { + o = oParser(o); + this.sorter = o.sorter; + this.identify = o.identify; + this.sufficient = o.sufficient; + this.local = o.local; + this.remote = o.remote ? new Remote(o.remote) : null; + this.prefetch = o.prefetch ? new Prefetch(o.prefetch) : null; + this.index = new SearchIndex({ + identify: this.identify, + datumTokenizer: o.datumTokenizer, + queryTokenizer: o.queryTokenizer + }); + o.initialize !== false && this.initialize(); + } + Bloodhound.noConflict = function noConflict() { + window && (window.Bloodhound = old); + return Bloodhound; + }; + Bloodhound.tokenizers = tokenizers; + _.mixin(Bloodhound.prototype, { + __ttAdapter: function ttAdapter() { + var that = this; + return this.remote ? withAsync : withoutAsync; + function withAsync(query, sync, async) { + return that.search(query, sync, async); + } + function withoutAsync(query, sync) { + return that.search(query, sync); + } + }, + _loadPrefetch: function loadPrefetch() { + var that = this, deferred, serialized; + deferred = $.Deferred(); + if (!this.prefetch) { + deferred.resolve(); + } else if (serialized = this.prefetch.fromCache()) { + this.index.bootstrap(serialized); + deferred.resolve(); + } else { + this.prefetch.fromNetwork(done); + } + return deferred.promise(); + function done(err, data) { + if (err) { + return deferred.reject(); + } + that.add(data); + that.prefetch.store(that.index.serialize()); + deferred.resolve(); + } + }, + _initialize: function initialize() { + var that = this, deferred; + this.clear(); + (this.initPromise = this._loadPrefetch()).done(addLocalToIndex); + return this.initPromise; + function addLocalToIndex() { + that.add(that.local); + } + }, + initialize: function initialize(force) { + return !this.initPromise || force ? this._initialize() : this.initPromise; + }, + add: function add(data) { + this.index.add(data); + return this; + }, + get: function get(ids) { + ids = _.isArray(ids) ? ids : [].slice.call(arguments); + return this.index.get(ids); + }, + search: function search(query, sync, async) { + var that = this, local; + local = this.sorter(this.index.search(query)); + sync(this.remote ? local.slice() : local); + if (this.remote && local.length < this.sufficient) { + this.remote.get(query, processRemote); + } else if (this.remote) { + this.remote.cancelLastRequest(); + } + return this; + function processRemote(remote) { + var nonDuplicates = []; + _.each(remote, function(r) { + !_.some(local, function(l) { + return that.identify(r) === that.identify(l); + }) && nonDuplicates.push(r); + }); + async && async(nonDuplicates); + } + }, + all: function all() { + return this.index.all(); + }, + clear: function clear() { + this.index.reset(); + return this; + }, + clearPrefetchCache: function clearPrefetchCache() { + this.prefetch && this.prefetch.clear(); + return this; + }, + clearRemoteCache: function clearRemoteCache() { + Transport.resetCache(); + return this; + }, + ttAdapter: function ttAdapter() { + return this.__ttAdapter(); + } + }); + return Bloodhound; + }(); + return Bloodhound; +}); + +(function(root, factory) { + if (typeof define === "function" && define.amd) { + define("typeahead.js", [ "jquery" ], function(a0) { + return factory(a0); + }); + } else if (typeof exports === "object") { + module.exports = factory(require("jquery")); + } else { + factory(jQuery); + } +})(this, function($) { + var _ = function() { + "use strict"; + return { + isMsie: function() { + return /(msie|trident)/i.test(navigator.userAgent) ? navigator.userAgent.match(/(msie |rv:)(\d+(.\d+)?)/i)[2] : false; + }, + isBlankString: function(str) { + return !str || /^\s*$/.test(str); + }, + escapeRegExChars: function(str) { + return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); + }, + isString: function(obj) { + return typeof obj === "string"; + }, + isNumber: function(obj) { + return typeof obj === "number"; + }, + isArray: $.isArray, + isFunction: $.isFunction, + isObject: $.isPlainObject, + isUndefined: function(obj) { + return typeof obj === "undefined"; + }, + isElement: function(obj) { + return !!(obj && obj.nodeType === 1); + }, + isJQuery: function(obj) { + return obj instanceof $; + }, + toStr: function toStr(s) { + return _.isUndefined(s) || s === null ? "" : s + ""; + }, + bind: $.proxy, + each: function(collection, cb) { + $.each(collection, reverseArgs); + function reverseArgs(index, value) { + return cb(value, index); + } + }, + map: $.map, + filter: $.grep, + every: function(obj, test) { + var result = true; + if (!obj) { + return result; + } + $.each(obj, function(key, val) { + if (!(result = test.call(null, val, key, obj))) { + return false; + } + }); + return !!result; + }, + some: function(obj, test) { + var result = false; + if (!obj) { + return result; + } + $.each(obj, function(key, val) { + if (result = test.call(null, val, key, obj)) { + return false; + } + }); + return !!result; + }, + mixin: $.extend, + identity: function(x) { + return x; + }, + clone: function(obj) { + return $.extend(true, {}, obj); + }, + getIdGenerator: function() { + var counter = 0; + return function() { + return counter++; + }; + }, + templatify: function templatify(obj) { + return $.isFunction(obj) ? obj : template; + function template() { + return String(obj); + } + }, + defer: function(fn) { + setTimeout(fn, 0); + }, + debounce: function(func, wait, immediate) { + var timeout, result; + return function() { + var context = this, args = arguments, later, callNow; + later = function() { + timeout = null; + if (!immediate) { + result = func.apply(context, args); + } + }; + callNow = immediate && !timeout; + clearTimeout(timeout); + timeout = setTimeout(later, wait); + if (callNow) { + result = func.apply(context, args); + } + return result; + }; + }, + throttle: function(func, wait) { + var context, args, timeout, result, previous, later; + previous = 0; + later = function() { + previous = new Date(); + timeout = null; + result = func.apply(context, args); + }; + return function() { + var now = new Date(), remaining = wait - (now - previous); + context = this; + args = arguments; + if (remaining <= 0) { + clearTimeout(timeout); + timeout = null; + previous = now; + result = func.apply(context, args); + } else if (!timeout) { + timeout = setTimeout(later, remaining); + } + return result; + }; + }, + stringify: function(val) { + return _.isString(val) ? val : JSON.stringify(val); + }, + noop: function() {} + }; + }(); + var WWW = function() { + "use strict"; + var defaultClassNames = { + wrapper: "twitter-typeahead", + input: "tt-input", + hint: "tt-hint", + menu: "tt-menu", + dataset: "tt-dataset", + suggestion: "tt-suggestion", + selectable: "tt-selectable", + empty: "tt-empty", + open: "tt-open", + cursor: "tt-cursor", + highlight: "tt-highlight" + }; + return build; + function build(o) { + var www, classes; + classes = _.mixin({}, defaultClassNames, o); + www = { + css: buildCss(), + classes: classes, + html: buildHtml(classes), + selectors: buildSelectors(classes) + }; + return { + css: www.css, + html: www.html, + classes: www.classes, + selectors: www.selectors, + mixin: function(o) { + _.mixin(o, www); + } + }; + } + function buildHtml(c) { + return { + wrapper: '', + menu: '
' + }; + } + function buildSelectors(classes) { + var selectors = {}; + _.each(classes, function(v, k) { + selectors[k] = "." + v; + }); + return selectors; + } + function buildCss() { + var css = { + wrapper: { + position: "relative", + display: "inline-block" + }, + hint: { + position: "absolute", + top: "0", + left: "0", + borderColor: "transparent", + boxShadow: "none", + opacity: "1" + }, + input: { + position: "relative", + verticalAlign: "top", + backgroundColor: "transparent" + }, + inputWithNoHint: { + position: "relative", + verticalAlign: "top" + }, + menu: { + position: "absolute", + top: "100%", + left: "0", + zIndex: "100", + display: "none" + }, + ltr: { + left: "0", + right: "auto" + }, + rtl: { + left: "auto", + right: " 0" + } + }; + if (_.isMsie()) { + _.mixin(css.input, { + backgroundImage: "url(data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)" + }); + } + return css; + } + }(); + var EventBus = function() { + "use strict"; + var namespace, deprecationMap; + namespace = "typeahead:"; + deprecationMap = { + render: "rendered", + cursorchange: "cursorchanged", + select: "selected", + autocomplete: "autocompleted" + }; + function EventBus(o) { + if (!o || !o.el) { + $.error("EventBus initialized without el"); + } + this.$el = $(o.el); + } + _.mixin(EventBus.prototype, { + _trigger: function(type, args) { + var $e; + $e = $.Event(namespace + type); + (args = args || []).unshift($e); + this.$el.trigger.apply(this.$el, args); + return $e; + }, + before: function(type) { + var args, $e; + args = [].slice.call(arguments, 1); + $e = this._trigger("before" + type, args); + return $e.isDefaultPrevented(); + }, + trigger: function(type) { + var deprecatedType; + this._trigger(type, [].slice.call(arguments, 1)); + if (deprecatedType = deprecationMap[type]) { + this._trigger(deprecatedType, [].slice.call(arguments, 1)); + } + } + }); + return EventBus; + }(); + var EventEmitter = function() { + "use strict"; + var splitter = /\s+/, nextTick = getNextTick(); + return { + onSync: onSync, + onAsync: onAsync, + off: off, + trigger: trigger + }; + function on(method, types, cb, context) { + var type; + if (!cb) { + return this; + } + types = types.split(splitter); + cb = context ? bindContext(cb, context) : cb; + this._callbacks = this._callbacks || {}; + while (type = types.shift()) { + this._callbacks[type] = this._callbacks[type] || { + sync: [], + async: [] + }; + this._callbacks[type][method].push(cb); + } + return this; + } + function onAsync(types, cb, context) { + return on.call(this, "async", types, cb, context); + } + function onSync(types, cb, context) { + return on.call(this, "sync", types, cb, context); + } + function off(types) { + var type; + if (!this._callbacks) { + return this; + } + types = types.split(splitter); + while (type = types.shift()) { + delete this._callbacks[type]; + } + return this; + } + function trigger(types) { + var type, callbacks, args, syncFlush, asyncFlush; + if (!this._callbacks) { + return this; + } + types = types.split(splitter); + args = [].slice.call(arguments, 1); + while ((type = types.shift()) && (callbacks = this._callbacks[type])) { + syncFlush = getFlush(callbacks.sync, this, [ type ].concat(args)); + asyncFlush = getFlush(callbacks.async, this, [ type ].concat(args)); + syncFlush() && nextTick(asyncFlush); + } + return this; + } + function getFlush(callbacks, context, args) { + return flush; + function flush() { + var cancelled; + for (var i = 0, len = callbacks.length; !cancelled && i < len; i += 1) { + cancelled = callbacks[i].apply(context, args) === false; + } + return !cancelled; + } + } + function getNextTick() { + var nextTickFn; + if (window.setImmediate) { + nextTickFn = function nextTickSetImmediate(fn) { + setImmediate(function() { + fn(); + }); + }; + } else { + nextTickFn = function nextTickSetTimeout(fn) { + setTimeout(function() { + fn(); + }, 0); + }; + } + return nextTickFn; + } + function bindContext(fn, context) { + return fn.bind ? fn.bind(context) : function() { + fn.apply(context, [].slice.call(arguments, 0)); + }; + } + }(); + var highlight = function(doc) { + "use strict"; + var defaults = { + node: null, + pattern: null, + tagName: "strong", + className: null, + wordsOnly: false, + caseSensitive: false + }; + return function hightlight(o) { + var regex; + o = _.mixin({}, defaults, o); + if (!o.node || !o.pattern) { + return; + } + o.pattern = _.isArray(o.pattern) ? o.pattern : [ o.pattern ]; + regex = getRegex(o.pattern, o.caseSensitive, o.wordsOnly); + traverse(o.node, hightlightTextNode); + function hightlightTextNode(textNode) { + var match, patternNode, wrapperNode; + if (match = regex.exec(textNode.data)) { + wrapperNode = doc.createElement(o.tagName); + o.className && (wrapperNode.className = o.className); + patternNode = textNode.splitText(match.index); + patternNode.splitText(match[0].length); + wrapperNode.appendChild(patternNode.cloneNode(true)); + textNode.parentNode.replaceChild(wrapperNode, patternNode); + } + return !!match; + } + function traverse(el, hightlightTextNode) { + var childNode, TEXT_NODE_TYPE = 3; + for (var i = 0; i < el.childNodes.length; i++) { + childNode = el.childNodes[i]; + if (childNode.nodeType === TEXT_NODE_TYPE) { + i += hightlightTextNode(childNode) ? 1 : 0; + } else { + traverse(childNode, hightlightTextNode); + } + } + } + }; + function getRegex(patterns, caseSensitive, wordsOnly) { + var escapedPatterns = [], regexStr; + for (var i = 0, len = patterns.length; i < len; i++) { + escapedPatterns.push(_.escapeRegExChars(patterns[i])); + } + regexStr = wordsOnly ? "\\b(" + escapedPatterns.join("|") + ")\\b" : "(" + escapedPatterns.join("|") + ")"; + return caseSensitive ? new RegExp(regexStr) : new RegExp(regexStr, "i"); + } + }(window.document); + var Input = function() { + "use strict"; + var specialKeyCodeMap; + specialKeyCodeMap = { + 9: "tab", + 27: "esc", + 37: "left", + 39: "right", + 13: "enter", + 38: "up", + 40: "down" + }; + function Input(o, www) { + o = o || {}; + if (!o.input) { + $.error("input is missing"); + } + www.mixin(this); + this.$hint = $(o.hint); + this.$input = $(o.input); + this.query = this.$input.val(); + this.queryWhenFocused = this.hasFocus() ? this.query : null; + this.$overflowHelper = buildOverflowHelper(this.$input); + this._checkLanguageDirection(); + if (this.$hint.length === 0) { + this.setHint = this.getHint = this.clearHint = this.clearHintIfInvalid = _.noop; + } + } + Input.normalizeQuery = function(str) { + return _.toStr(str).replace(/^\s*/g, "").replace(/\s{2,}/g, " "); + }; + _.mixin(Input.prototype, EventEmitter, { + _onBlur: function onBlur() { + this.resetInputValue(); + this.trigger("blurred"); + }, + _onFocus: function onFocus() { + this.queryWhenFocused = this.query; + this.trigger("focused"); + }, + _onKeydown: function onKeydown($e) { + var keyName = specialKeyCodeMap[$e.which || $e.keyCode]; + this._managePreventDefault(keyName, $e); + if (keyName && this._shouldTrigger(keyName, $e)) { + this.trigger(keyName + "Keyed", $e); + } + }, + _onInput: function onInput() { + this._setQuery(this.getInputValue()); + this.clearHintIfInvalid(); + this._checkLanguageDirection(); + }, + _managePreventDefault: function managePreventDefault(keyName, $e) { + var preventDefault; + switch (keyName) { + case "up": + case "down": + preventDefault = !withModifier($e); + break; + + default: + preventDefault = false; + } + preventDefault && $e.preventDefault(); + }, + _shouldTrigger: function shouldTrigger(keyName, $e) { + var trigger; + switch (keyName) { + case "tab": + trigger = !withModifier($e); + break; + + default: + trigger = true; + } + return trigger; + }, + _checkLanguageDirection: function checkLanguageDirection() { + var dir = (this.$input.css("direction") || "ltr").toLowerCase(); + if (this.dir !== dir) { + this.dir = dir; + this.$hint.attr("dir", dir); + this.trigger("langDirChanged", dir); + } + }, + _setQuery: function setQuery(val, silent) { + var areEquivalent, hasDifferentWhitespace; + areEquivalent = areQueriesEquivalent(val, this.query); + hasDifferentWhitespace = areEquivalent ? this.query.length !== val.length : false; + this.query = val; + if (!silent && !areEquivalent) { + this.trigger("queryChanged", this.query); + } else if (!silent && hasDifferentWhitespace) { + this.trigger("whitespaceChanged", this.query); + } + }, + bind: function() { + var that = this, onBlur, onFocus, onKeydown, onInput; + onBlur = _.bind(this._onBlur, this); + onFocus = _.bind(this._onFocus, this); + onKeydown = _.bind(this._onKeydown, this); + onInput = _.bind(this._onInput, this); + this.$input.on("blur.tt", onBlur).on("focus.tt", onFocus).on("keydown.tt", onKeydown); + if (!_.isMsie() || _.isMsie() > 9) { + this.$input.on("input.tt", onInput); + } else { + this.$input.on("keydown.tt keypress.tt cut.tt paste.tt", function($e) { + if (specialKeyCodeMap[$e.which || $e.keyCode]) { + return; + } + _.defer(_.bind(that._onInput, that, $e)); + }); + } + return this; + }, + focus: function focus() { + this.$input.focus(); + }, + blur: function blur() { + this.$input.blur(); + }, + getLangDir: function getLangDir() { + return this.dir; + }, + getQuery: function getQuery() { + return this.query || ""; + }, + setQuery: function setQuery(val, silent) { + this.setInputValue(val); + this._setQuery(val, silent); + }, + hasQueryChangedSinceLastFocus: function hasQueryChangedSinceLastFocus() { + return this.query !== this.queryWhenFocused; + }, + getInputValue: function getInputValue() { + return this.$input.val(); + }, + setInputValue: function setInputValue(value) { + this.$input.val(value); + this.clearHintIfInvalid(); + this._checkLanguageDirection(); + }, + resetInputValue: function resetInputValue() { + this.setInputValue(this.query); + }, + getHint: function getHint() { + return this.$hint.val(); + }, + setHint: function setHint(value) { + this.$hint.val(value); + }, + clearHint: function clearHint() { + this.setHint(""); + }, + clearHintIfInvalid: function clearHintIfInvalid() { + var val, hint, valIsPrefixOfHint, isValid; + val = this.getInputValue(); + hint = this.getHint(); + valIsPrefixOfHint = val !== hint && hint.indexOf(val) === 0; + isValid = val !== "" && valIsPrefixOfHint && !this.hasOverflow(); + !isValid && this.clearHint(); + }, + hasFocus: function hasFocus() { + return this.$input.is(":focus"); + }, + hasOverflow: function hasOverflow() { + var constraint = this.$input.width() - 2; + this.$overflowHelper.text(this.getInputValue()); + return this.$overflowHelper.width() >= constraint; + }, + isCursorAtEnd: function() { + var valueLength, selectionStart, range; + valueLength = this.$input.val().length; + selectionStart = this.$input[0].selectionStart; + if (_.isNumber(selectionStart)) { + return selectionStart === valueLength; + } else if (document.selection) { + range = document.selection.createRange(); + range.moveStart("character", -valueLength); + return valueLength === range.text.length; + } + return true; + }, + destroy: function destroy() { + this.$hint.off(".tt"); + this.$input.off(".tt"); + this.$overflowHelper.remove(); + this.$hint = this.$input = this.$overflowHelper = $("
"); + } + }); + return Input; + function buildOverflowHelper($input) { + return $('').css({ + position: "absolute", + visibility: "hidden", + whiteSpace: "pre", + fontFamily: $input.css("font-family"), + fontSize: $input.css("font-size"), + fontStyle: $input.css("font-style"), + fontVariant: $input.css("font-variant"), + fontWeight: $input.css("font-weight"), + wordSpacing: $input.css("word-spacing"), + letterSpacing: $input.css("letter-spacing"), + textIndent: $input.css("text-indent"), + textRendering: $input.css("text-rendering"), + textTransform: $input.css("text-transform") + }).insertAfter($input); + } + function areQueriesEquivalent(a, b) { + return Input.normalizeQuery(a) === Input.normalizeQuery(b); + } + function withModifier($e) { + return $e.altKey || $e.ctrlKey || $e.metaKey || $e.shiftKey; + } + }(); + var Dataset = function() { + "use strict"; + var keys, nameGenerator; + keys = { + val: "tt-selectable-display", + obj: "tt-selectable-object" + }; + nameGenerator = _.getIdGenerator(); + function Dataset(o, www) { + o = o || {}; + o.templates = o.templates || {}; + o.templates.notFound = o.templates.notFound || o.templates.empty; + if (!o.source) { + $.error("missing source"); + } + if (!o.node) { + $.error("missing node"); + } + if (o.name && !isValidName(o.name)) { + $.error("invalid dataset name: " + o.name); + } + www.mixin(this); + this.highlight = !!o.highlight; + this.name = o.name || nameGenerator(); + this.limit = o.limit || 5; + this.displayFn = getDisplayFn(o.display || o.displayKey); + this.templates = getTemplates(o.templates, this.displayFn); + this.source = o.source.__ttAdapter ? o.source.__ttAdapter() : o.source; + this.async = _.isUndefined(o.async) ? this.source.length > 2 : !!o.async; + this._resetLastSuggestion(); + this.$el = $(o.node).addClass(this.classes.dataset).addClass(this.classes.dataset + "-" + this.name); + } + Dataset.extractData = function extractData(el) { + var $el = $(el); + if ($el.data(keys.obj)) { + return { + val: $el.data(keys.val) || "", + obj: $el.data(keys.obj) || null + }; + } + return null; + }; + _.mixin(Dataset.prototype, EventEmitter, { + _overwrite: function overwrite(query, suggestions) { + suggestions = suggestions || []; + if (suggestions.length) { + this._renderSuggestions(query, suggestions); + } else if (this.async && this.templates.pending) { + this._renderPending(query); + } else if (!this.async && this.templates.notFound) { + this._renderNotFound(query); + } else { + this._empty(); + } + this.trigger("rendered", this.name, suggestions, false); + }, + _append: function append(query, suggestions) { + suggestions = suggestions || []; + if (suggestions.length && this.$lastSuggestion.length) { + this._appendSuggestions(query, suggestions); + } else if (suggestions.length) { + this._renderSuggestions(query, suggestions); + } else if (!this.$lastSuggestion.length && this.templates.notFound) { + this._renderNotFound(query); + } + this.trigger("rendered", this.name, suggestions, true); + }, + _renderSuggestions: function renderSuggestions(query, suggestions) { + var $fragment; + $fragment = this._getSuggestionsFragment(query, suggestions); + this.$lastSuggestion = $fragment.children().last(); + this.$el.html($fragment).prepend(this._getHeader(query, suggestions)).append(this._getFooter(query, suggestions)); + }, + _appendSuggestions: function appendSuggestions(query, suggestions) { + var $fragment, $lastSuggestion; + $fragment = this._getSuggestionsFragment(query, suggestions); + $lastSuggestion = $fragment.children().last(); + this.$lastSuggestion.after($fragment); + this.$lastSuggestion = $lastSuggestion; + }, + _renderPending: function renderPending(query) { + var template = this.templates.pending; + this._resetLastSuggestion(); + template && this.$el.html(template({ + query: query, + dataset: this.name + })); + }, + _renderNotFound: function renderNotFound(query) { + var template = this.templates.notFound; + this._resetLastSuggestion(); + template && this.$el.html(template({ + query: query, + dataset: this.name + })); + }, + _empty: function empty() { + this.$el.empty(); + this._resetLastSuggestion(); + }, + _getSuggestionsFragment: function getSuggestionsFragment(query, suggestions) { + var that = this, fragment; + fragment = document.createDocumentFragment(); + _.each(suggestions, function getSuggestionNode(suggestion) { + var $el, context; + context = that._injectQuery(query, suggestion); + $el = $(that.templates.suggestion(context)).data(keys.obj, suggestion).data(keys.val, that.displayFn(suggestion)).addClass(that.classes.suggestion + " " + that.classes.selectable); + fragment.appendChild($el[0]); + }); + this.highlight && highlight({ + className: this.classes.highlight, + node: fragment, + pattern: query + }); + return $(fragment); + }, + _getFooter: function getFooter(query, suggestions) { + return this.templates.footer ? this.templates.footer({ + query: query, + suggestions: suggestions, + dataset: this.name + }) : null; + }, + _getHeader: function getHeader(query, suggestions) { + return this.templates.header ? this.templates.header({ + query: query, + suggestions: suggestions, + dataset: this.name + }) : null; + }, + _resetLastSuggestion: function resetLastSuggestion() { + this.$lastSuggestion = $(); + }, + _injectQuery: function injectQuery(query, obj) { + return _.isObject(obj) ? _.mixin({ + _query: query + }, obj) : obj; + }, + update: function update(query) { + var that = this, canceled = false, syncCalled = false, rendered = 0; + this.cancel(); + this.cancel = function cancel() { + canceled = true; + that.cancel = $.noop; + that.async && that.trigger("asyncCanceled", query); + }; + this.source(query, sync, async); + !syncCalled && sync([]); + function sync(suggestions) { + if (syncCalled) { + return; + } + syncCalled = true; + suggestions = (suggestions || []).slice(0, that.limit); + rendered = suggestions.length; + that._overwrite(query, suggestions); + if (rendered < that.limit && that.async) { + that.trigger("asyncRequested", query); + } + } + function async(suggestions) { + suggestions = suggestions || []; + if (!canceled && rendered < that.limit) { + that.cancel = $.noop; + rendered += suggestions.length; + that._append(query, suggestions.slice(0, that.limit)); + that.async && that.trigger("asyncReceived", query); + } + } + }, + cancel: $.noop, + clear: function clear() { + this._empty(); + this.cancel(); + this.trigger("cleared"); + }, + isEmpty: function isEmpty() { + return this.$el.is(":empty"); + }, + destroy: function destroy() { + this.$el = $("
"); + } + }); + return Dataset; + function getDisplayFn(display) { + display = display || _.stringify; + return _.isFunction(display) ? display : displayFn; + function displayFn(obj) { + return obj[display]; + } + } + function getTemplates(templates, displayFn) { + return { + notFound: templates.notFound && _.templatify(templates.notFound), + pending: templates.pending && _.templatify(templates.pending), + header: templates.header && _.templatify(templates.header), + footer: templates.footer && _.templatify(templates.footer), + suggestion: templates.suggestion || suggestionTemplate + }; + function suggestionTemplate(context) { + return $("
").text(displayFn(context)); + } + } + function isValidName(str) { + return /^[_a-zA-Z0-9-]+$/.test(str); + } + }(); + var Menu = function() { + "use strict"; + function Menu(o, www) { + var that = this; + o = o || {}; + if (!o.node) { + $.error("node is required"); + } + www.mixin(this); + this.$node = $(o.node); + this.query = null; + this.datasets = _.map(o.datasets, initializeDataset); + function initializeDataset(oDataset) { + var node = that.$node.find(oDataset.node).first(); + oDataset.node = node.length ? node : $("
").appendTo(that.$node); + return new Dataset(oDataset, www); + } + } + _.mixin(Menu.prototype, EventEmitter, { + _onSelectableClick: function onSelectableClick($e) { + this.trigger("selectableClicked", $($e.currentTarget)); + }, + _onRendered: function onRendered(type, dataset, suggestions, async) { + this.$node.toggleClass(this.classes.empty, this._allDatasetsEmpty()); + this.trigger("datasetRendered", dataset, suggestions, async); + }, + _onCleared: function onCleared() { + this.$node.toggleClass(this.classes.empty, this._allDatasetsEmpty()); + this.trigger("datasetCleared"); + }, + _propagate: function propagate() { + this.trigger.apply(this, arguments); + }, + _allDatasetsEmpty: function allDatasetsEmpty() { + return _.every(this.datasets, isDatasetEmpty); + function isDatasetEmpty(dataset) { + return dataset.isEmpty(); + } + }, + _getSelectables: function getSelectables() { + return this.$node.find(this.selectors.selectable); + }, + _removeCursor: function _removeCursor() { + var $selectable = this.getActiveSelectable(); + $selectable && $selectable.removeClass(this.classes.cursor); + }, + _ensureVisible: function ensureVisible($el) { + var elTop, elBottom, nodeScrollTop, nodeHeight; + elTop = $el.position().top; + elBottom = elTop + $el.outerHeight(true); + nodeScrollTop = this.$node.scrollTop(); + nodeHeight = this.$node.height() + parseInt(this.$node.css("paddingTop"), 10) + parseInt(this.$node.css("paddingBottom"), 10); + if (elTop < 0) { + this.$node.scrollTop(nodeScrollTop + elTop); + } else if (nodeHeight < elBottom) { + this.$node.scrollTop(nodeScrollTop + (elBottom - nodeHeight)); + } + }, + bind: function() { + var that = this, onSelectableClick; + onSelectableClick = _.bind(this._onSelectableClick, this); + this.$node.on("click.tt", this.selectors.selectable, onSelectableClick); + _.each(this.datasets, function(dataset) { + dataset.onSync("asyncRequested", that._propagate, that).onSync("asyncCanceled", that._propagate, that).onSync("asyncReceived", that._propagate, that).onSync("rendered", that._onRendered, that).onSync("cleared", that._onCleared, that); + }); + return this; + }, + isOpen: function isOpen() { + return this.$node.hasClass(this.classes.open); + }, + open: function open() { + this.$node.addClass(this.classes.open); + }, + close: function close() { + this.$node.removeClass(this.classes.open); + this._removeCursor(); + }, + setLanguageDirection: function setLanguageDirection(dir) { + this.$node.attr("dir", dir); + }, + selectableRelativeToCursor: function selectableRelativeToCursor(delta) { + var $selectables, $oldCursor, oldIndex, newIndex; + $oldCursor = this.getActiveSelectable(); + $selectables = this._getSelectables(); + oldIndex = $oldCursor ? $selectables.index($oldCursor) : -1; + newIndex = oldIndex + delta; + newIndex = (newIndex + 1) % ($selectables.length + 1) - 1; + newIndex = newIndex < -1 ? $selectables.length - 1 : newIndex; + return newIndex === -1 ? null : $selectables.eq(newIndex); + }, + setCursor: function setCursor($selectable) { + this._removeCursor(); + if ($selectable = $selectable && $selectable.first()) { + $selectable.addClass(this.classes.cursor); + this._ensureVisible($selectable); + } + }, + getSelectableData: function getSelectableData($el) { + return $el && $el.length ? Dataset.extractData($el) : null; + }, + getActiveSelectable: function getActiveSelectable() { + var $selectable = this._getSelectables().filter(this.selectors.cursor).first(); + return $selectable.length ? $selectable : null; + }, + getTopSelectable: function getTopSelectable() { + var $selectable = this._getSelectables().first(); + return $selectable.length ? $selectable : null; + }, + update: function update(query) { + var isValidUpdate = query !== this.query; + if (isValidUpdate) { + this.query = query; + _.each(this.datasets, updateDataset); + } + return isValidUpdate; + function updateDataset(dataset) { + dataset.update(query); + } + }, + empty: function empty() { + _.each(this.datasets, clearDataset); + this.query = null; + this.$node.addClass(this.classes.empty); + function clearDataset(dataset) { + dataset.clear(); + } + }, + destroy: function destroy() { + this.$node.off(".tt"); + this.$node = $("
"); + _.each(this.datasets, destroyDataset); + function destroyDataset(dataset) { + dataset.destroy(); + } + } + }); + return Menu; + }(); + var DefaultMenu = function() { + "use strict"; + var s = Menu.prototype; + function DefaultMenu() { + Menu.apply(this, [].slice.call(arguments, 0)); + } + _.mixin(DefaultMenu.prototype, Menu.prototype, { + open: function open() { + !this._allDatasetsEmpty() && this._show(); + return s.open.apply(this, [].slice.call(arguments, 0)); + }, + close: function close() { + this._hide(); + return s.close.apply(this, [].slice.call(arguments, 0)); + }, + _onRendered: function onRendered() { + if (this._allDatasetsEmpty()) { + this._hide(); + } else { + this.isOpen() && this._show(); + } + return s._onRendered.apply(this, [].slice.call(arguments, 0)); + }, + _onCleared: function onCleared() { + if (this._allDatasetsEmpty()) { + this._hide(); + } else { + this.isOpen() && this._show(); + } + return s._onCleared.apply(this, [].slice.call(arguments, 0)); + }, + setLanguageDirection: function setLanguageDirection(dir) { + this.$node.css(dir === "ltr" ? this.css.ltr : this.css.rtl); + return s.setLanguageDirection.apply(this, [].slice.call(arguments, 0)); + }, + _hide: function hide() { + this.$node.hide(); + }, + _show: function show() { + this.$node.css("display", "block"); + } + }); + return DefaultMenu; + }(); + var Typeahead = function() { + "use strict"; + function Typeahead(o, www) { + var onFocused, onBlurred, onEnterKeyed, onTabKeyed, onEscKeyed, onUpKeyed, onDownKeyed, onLeftKeyed, onRightKeyed, onQueryChanged, onWhitespaceChanged; + o = o || {}; + if (!o.input) { + $.error("missing input"); + } + if (!o.menu) { + $.error("missing menu"); + } + if (!o.eventBus) { + $.error("missing event bus"); + } + www.mixin(this); + this.eventBus = o.eventBus; + this.minLength = _.isNumber(o.minLength) ? o.minLength : 1; + this.input = o.input; + this.menu = o.menu; + this.enabled = true; + this.active = false; + this.input.hasFocus() && this.activate(); + this.dir = this.input.getLangDir(); + this._hacks(); + this.menu.bind().onSync("selectableClicked", this._onSelectableClicked, this).onSync("asyncRequested", this._onAsyncRequested, this).onSync("asyncCanceled", this._onAsyncCanceled, this).onSync("asyncReceived", this._onAsyncReceived, this).onSync("datasetRendered", this._onDatasetRendered, this).onSync("datasetCleared", this._onDatasetCleared, this); + onFocused = c(this, "activate", "open", "_onFocused"); + onBlurred = c(this, "deactivate", "_onBlurred"); + onEnterKeyed = c(this, "isActive", "isOpen", "_onEnterKeyed"); + onTabKeyed = c(this, "isActive", "isOpen", "_onTabKeyed"); + onEscKeyed = c(this, "isActive", "_onEscKeyed"); + onUpKeyed = c(this, "isActive", "open", "_onUpKeyed"); + onDownKeyed = c(this, "isActive", "open", "_onDownKeyed"); + onLeftKeyed = c(this, "isActive", "isOpen", "_onLeftKeyed"); + onRightKeyed = c(this, "isActive", "isOpen", "_onRightKeyed"); + onQueryChanged = c(this, "_openIfActive", "_onQueryChanged"); + onWhitespaceChanged = c(this, "_openIfActive", "_onWhitespaceChanged"); + this.input.bind().onSync("focused", onFocused, this).onSync("blurred", onBlurred, this).onSync("enterKeyed", onEnterKeyed, this).onSync("tabKeyed", onTabKeyed, this).onSync("escKeyed", onEscKeyed, this).onSync("upKeyed", onUpKeyed, this).onSync("downKeyed", onDownKeyed, this).onSync("leftKeyed", onLeftKeyed, this).onSync("rightKeyed", onRightKeyed, this).onSync("queryChanged", onQueryChanged, this).onSync("whitespaceChanged", onWhitespaceChanged, this).onSync("langDirChanged", this._onLangDirChanged, this); + } + _.mixin(Typeahead.prototype, { + _hacks: function hacks() { + var $input, $menu; + $input = this.input.$input || $("
"); + $menu = this.menu.$node || $("
"); + $input.on("blur.tt", function($e) { + var active, isActive, hasActive; + active = document.activeElement; + isActive = $menu.is(active); + hasActive = $menu.has(active).length > 0; + if (_.isMsie() && (isActive || hasActive)) { + $e.preventDefault(); + $e.stopImmediatePropagation(); + _.defer(function() { + $input.focus(); + }); + } + }); + $menu.on("mousedown.tt", function($e) { + $e.preventDefault(); + }); + }, + _onSelectableClicked: function onSelectableClicked(type, $el) { + this.select($el); + }, + _onDatasetCleared: function onDatasetCleared() { + this._updateHint(); + }, + _onDatasetRendered: function onDatasetRendered(type, dataset, suggestions, async) { + this._updateHint(); + this.eventBus.trigger("render", suggestions, async, dataset); + }, + _onAsyncRequested: function onAsyncRequested(type, dataset, query) { + this.eventBus.trigger("asyncrequest", query, dataset); + }, + _onAsyncCanceled: function onAsyncCanceled(type, dataset, query) { + this.eventBus.trigger("asynccancel", query, dataset); + }, + _onAsyncReceived: function onAsyncReceived(type, dataset, query) { + this.eventBus.trigger("asyncreceive", query, dataset); + }, + _onFocused: function onFocused() { + this._minLengthMet() && this.menu.update(this.input.getQuery()); + }, + _onBlurred: function onBlurred() { + if (this.input.hasQueryChangedSinceLastFocus()) { + this.eventBus.trigger("change", this.input.getQuery()); + } + }, + _onEnterKeyed: function onEnterKeyed(type, $e) { + var $selectable; + if ($selectable = this.menu.getActiveSelectable()) { + this.select($selectable) && $e.preventDefault(); + } + }, + _onTabKeyed: function onTabKeyed(type, $e) { + var $selectable; + if ($selectable = this.menu.getActiveSelectable()) { + this.select($selectable) && $e.preventDefault(); + } else if ($selectable = this.menu.getTopSelectable()) { + this.autocomplete($selectable) && $e.preventDefault(); + } + }, + _onEscKeyed: function onEscKeyed() { + this.close(); + }, + _onUpKeyed: function onUpKeyed() { + this.moveCursor(-1); + }, + _onDownKeyed: function onDownKeyed() { + this.moveCursor(+1); + }, + _onLeftKeyed: function onLeftKeyed() { + if (this.dir === "rtl" && this.input.isCursorAtEnd()) { + this.autocomplete(this.menu.getTopSelectable()); + } + }, + _onRightKeyed: function onRightKeyed() { + if (this.dir === "ltr" && this.input.isCursorAtEnd()) { + this.autocomplete(this.menu.getTopSelectable()); + } + }, + _onQueryChanged: function onQueryChanged(e, query) { + this._minLengthMet(query) ? this.menu.update(query) : this.menu.empty(); + }, + _onWhitespaceChanged: function onWhitespaceChanged() { + this._updateHint(); + }, + _onLangDirChanged: function onLangDirChanged(e, dir) { + if (this.dir !== dir) { + this.dir = dir; + this.menu.setLanguageDirection(dir); + } + }, + _openIfActive: function openIfActive() { + this.isActive() && this.open(); + }, + _minLengthMet: function minLengthMet(query) { + query = _.isString(query) ? query : this.input.getQuery() || ""; + return query.length >= this.minLength; + }, + _updateHint: function updateHint() { + var $selectable, data, val, query, escapedQuery, frontMatchRegEx, match; + $selectable = this.menu.getTopSelectable(); + data = this.menu.getSelectableData($selectable); + val = this.input.getInputValue(); + if (data && !_.isBlankString(val) && !this.input.hasOverflow()) { + query = Input.normalizeQuery(val); + escapedQuery = _.escapeRegExChars(query); + frontMatchRegEx = new RegExp("^(?:" + escapedQuery + ")(.+$)", "i"); + match = frontMatchRegEx.exec(data.val); + match && this.input.setHint(val + match[1]); + } else { + this.input.clearHint(); + } + }, + isEnabled: function isEnabled() { + return this.enabled; + }, + enable: function enable() { + this.enabled = true; + }, + disable: function disable() { + this.enabled = false; + }, + isActive: function isActive() { + return this.active; + }, + activate: function activate() { + if (this.isActive()) { + return true; + } else if (!this.isEnabled() || this.eventBus.before("active")) { + return false; + } else { + this.active = true; + this.eventBus.trigger("active"); + return true; + } + }, + deactivate: function deactivate() { + if (!this.isActive()) { + return true; + } else if (this.eventBus.before("idle")) { + return false; + } else { + this.active = false; + this.close(); + this.eventBus.trigger("idle"); + return true; + } + }, + isOpen: function isOpen() { + return this.menu.isOpen(); + }, + open: function open() { + if (!this.isOpen() && !this.eventBus.before("open")) { + this.menu.open(); + this._updateHint(); + this.eventBus.trigger("open"); + } + return this.isOpen(); + }, + close: function close() { + if (this.isOpen() && !this.eventBus.before("close")) { + this.menu.close(); + this.input.clearHint(); + this.input.resetInputValue(); + this.eventBus.trigger("close"); + } + return !this.isOpen(); + }, + setVal: function setVal(val) { + this.input.setQuery(_.toStr(val)); + }, + getVal: function getVal() { + return this.input.getQuery(); + }, + select: function select($selectable) { + var data = this.menu.getSelectableData($selectable); + if (data && !this.eventBus.before("select", data.obj)) { + this.input.setQuery(data.val, true); + this.eventBus.trigger("select", data.obj); + this.close(); + return true; + } + return false; + }, + autocomplete: function autocomplete($selectable) { + var query, data, isValid; + query = this.input.getQuery(); + data = this.menu.getSelectableData($selectable); + isValid = data && query !== data.val; + if (isValid && !this.eventBus.before("autocomplete", data.obj)) { + this.input.setQuery(data.val); + this.eventBus.trigger("autocomplete", data.obj); + return true; + } + return false; + }, + moveCursor: function moveCursor(delta) { + var query, $candidate, data, payload, cancelMove; + query = this.input.getQuery(); + $candidate = this.menu.selectableRelativeToCursor(delta); + data = this.menu.getSelectableData($candidate); + payload = data ? data.obj : null; + cancelMove = this._minLengthMet() && this.menu.update(query); + if (!cancelMove && !this.eventBus.before("cursorchange", payload)) { + this.menu.setCursor($candidate); + if (data) { + this.input.setInputValue(data.val); + } else { + this.input.resetInputValue(); + this._updateHint(); + } + this.eventBus.trigger("cursorchange", payload); + return true; + } + return false; + }, + destroy: function destroy() { + this.input.destroy(); + this.menu.destroy(); + } + }); + return Typeahead; + function c(ctx) { + var methods = [].slice.call(arguments, 1); + return function() { + var args = [].slice.call(arguments); + _.each(methods, function(method) { + return ctx[method].apply(ctx, args); + }); + }; + } + }(); + (function() { + "use strict"; + var old, keys, methods; + old = $.fn.typeahead; + keys = { + www: "tt-www", + attrs: "tt-attrs", + typeahead: "tt-typeahead" + }; + methods = { + initialize: function initialize(o, datasets) { + var www; + datasets = _.isArray(datasets) ? datasets : [].slice.call(arguments, 1); + o = o || {}; + www = WWW(o.classNames); + return this.each(attach); + function attach() { + var $input, $wrapper, $hint, $menu, defaultHint, defaultMenu, eventBus, input, menu, typeahead, MenuConstructor; + _.each(datasets, function(d) { + d.highlight = !!o.highlight; + }); + $input = $(this); + $wrapper = $(www.html.wrapper); + $hint = $elOrNull(o.hint); + $menu = $elOrNull(o.menu); + defaultHint = o.hint !== false && !$hint; + defaultMenu = o.menu !== false && !$menu; + defaultHint && ($hint = buildHintFromInput($input, www)); + defaultMenu && ($menu = $(www.html.menu).css(www.css.menu)); + $hint && $hint.val(""); + $input = prepInput($input, www); + if (defaultHint || defaultMenu) { + $wrapper.css(www.css.wrapper); + $input.css(defaultHint ? www.css.input : www.css.inputWithNoHint); + $input.wrap($wrapper).parent().prepend(defaultHint ? $hint : null).append(defaultMenu ? $menu : null); + } + MenuConstructor = defaultMenu ? DefaultMenu : Menu; + eventBus = new EventBus({ + el: $input + }); + input = new Input({ + hint: $hint, + input: $input + }, www); + menu = new MenuConstructor({ + node: $menu, + datasets: datasets + }, www); + typeahead = new Typeahead({ + input: input, + menu: menu, + eventBus: eventBus, + minLength: o.minLength + }, www); + $input.data(keys.www, www); + $input.data(keys.typeahead, typeahead); + } + }, + isEnabled: function isEnabled() { + var enabled; + ttEach(this.first(), function(t) { + enabled = t.isEnabled(); + }); + return enabled; + }, + enable: function enable() { + ttEach(this, function(t) { + t.enable(); + }); + return this; + }, + disable: function disable() { + ttEach(this, function(t) { + t.disable(); + }); + return this; + }, + isActive: function isActive() { + var active; + ttEach(this.first(), function(t) { + active = t.isActive(); + }); + return active; + }, + activate: function activate() { + ttEach(this, function(t) { + t.activate(); + }); + return this; + }, + deactivate: function deactivate() { + ttEach(this, function(t) { + t.deactivate(); + }); + return this; + }, + isOpen: function isOpen() { + var open; + ttEach(this.first(), function(t) { + open = t.isOpen(); + }); + return open; + }, + open: function open() { + ttEach(this, function(t) { + t.open(); + }); + return this; + }, + close: function close() { + ttEach(this, function(t) { + t.close(); + }); + return this; + }, + select: function select(el) { + var success = false, $el = $(el); + ttEach(this.first(), function(t) { + success = t.select($el); + }); + return success; + }, + autocomplete: function autocomplete(el) { + var success = false, $el = $(el); + ttEach(this.first(), function(t) { + success = t.autocomplete($el); + }); + return success; + }, + moveCursor: function moveCursoe(delta) { + var success = false; + ttEach(this.first(), function(t) { + success = t.moveCursor(delta); + }); + return success; + }, + val: function val(newVal) { + var query; + if (!arguments.length) { + ttEach(this.first(), function(t) { + query = t.getVal(); + }); + return query; + } else { + ttEach(this, function(t) { + t.setVal(newVal); + }); + return this; + } + }, + destroy: function destroy() { + ttEach(this, function(typeahead, $input) { + revert($input); + typeahead.destroy(); + }); + return this; + } + }; + $.fn.typeahead = function(method) { + if (methods[method]) { + return methods[method].apply(this, [].slice.call(arguments, 1)); + } else { + return methods.initialize.apply(this, arguments); + } + }; + $.fn.typeahead.noConflict = function noConflict() { + $.fn.typeahead = old; + return this; + }; + function ttEach($els, fn) { + $els.each(function() { + var $input = $(this), typeahead; + (typeahead = $input.data(keys.typeahead)) && fn(typeahead, $input); + }); + } + function buildHintFromInput($input, www) { + return $input.clone().addClass(www.classes.hint).removeData().css(www.css.hint).css(getBackgroundStyles($input)).prop("readonly", true).removeAttr("id name placeholder required").attr({ + autocomplete: "off", + spellcheck: "false", + tabindex: -1 + }); + } + function prepInput($input, www) { + $input.data(keys.attrs, { + dir: $input.attr("dir"), + autocomplete: $input.attr("autocomplete"), + spellcheck: $input.attr("spellcheck"), + style: $input.attr("style") + }); + $input.addClass(www.classes.input).attr({ + autocomplete: "off", + spellcheck: false + }); + try { + !$input.attr("dir") && $input.attr("dir", "auto"); + } catch (e) {} + return $input; + } + function getBackgroundStyles($el) { + return { + backgroundAttachment: $el.css("background-attachment"), + backgroundClip: $el.css("background-clip"), + backgroundColor: $el.css("background-color"), + backgroundImage: $el.css("background-image"), + backgroundOrigin: $el.css("background-origin"), + backgroundPosition: $el.css("background-position"), + backgroundRepeat: $el.css("background-repeat"), + backgroundSize: $el.css("background-size") + }; + } + function revert($input) { + var www, $wrapper; + www = $input.data(keys.www); + $wrapper = $input.parent().filter(www.selectors.wrapper); + _.each($input.data(keys.attrs), function(val, key) { + _.isUndefined(val) ? $input.removeAttr(key) : $input.attr(key, val); + }); + $input.removeData(keys.typeahead).removeData(keys.www).removeData(keys.attr).removeClass(www.classes.input); + if ($wrapper.length) { + $input.detach().insertAfter($wrapper); + $wrapper.remove(); + } + } + function $elOrNull(obj) { + var isValid, $el; + isValid = _.isJQuery(obj) || _.isElement(obj); + $el = isValid ? $(obj).first() : []; + return $el.length ? $el : null; + } + })(); +}); \ No newline at end of file From 3c75fb27c49c13b27c99a3d829fb396bfbaf7464 Mon Sep 17 00:00:00 2001 From: Xavier J Date: Thu, 9 Jun 2016 12:23:50 +0200 Subject: [PATCH 14/33] Fix bug get Forbidden last version github --- app/views/root/landing.html.haml | 12 ++++++++---- lib/github/api.rb | 4 +++- lib/github/releases.rb | 4 +++- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/app/views/root/landing.html.haml b/app/views/root/landing.html.haml index fdcbfbdba..700c56814 100644 --- a/app/views/root/landing.html.haml +++ b/app/views/root/landing.html.haml @@ -58,10 +58,14 @@ .row.word.news .latest_release.col-md-7.col-lg-7 - %h3.text-info - = "Dernière version (#{@latest_release.tag_name} - #{@latest_release.published_at})" - .body - =@latest_release.body.html_safe + - if @latest_release.nil? + %p + Erreur dans la récupération des données + -else + %h3.text-info + = "Dernière version (#{@latest_release.tag_name} - #{@latest_release.published_at})" + .body + =@latest_release.body.html_safe .center \- diff --git a/lib/github/api.rb b/lib/github/api.rb index 57c815fb7..8aab0ce86 100644 --- a/lib/github/api.rb +++ b/lib/github/api.rb @@ -12,7 +12,9 @@ class Github::API def self.call(end_point, params = {}) RestClient::Resource.new( - base_uri+end_point + base_uri+end_point, timeout: 5 ).get(params: params) + rescue RestClient::Forbidden + nil end end diff --git a/lib/github/releases.rb b/lib/github/releases.rb index f58f66b20..f71449b66 100644 --- a/lib/github/releases.rb +++ b/lib/github/releases.rb @@ -2,8 +2,10 @@ class Github::Releases def self.latest release = Hashie::Mash.new JSON.parse(Github::API.latest_release) - release.published_at = release.published_at.to_date.strftime('%d/%m/%Y') + return nil if release.nil? + + release.published_at = release.published_at.to_date.strftime('%d/%m/%Y') release end end \ No newline at end of file From 54256b1e55d7b25be74ccabe4c6337d12573645a Mon Sep 17 00:00:00 2001 From: Xavier J Date: Thu, 9 Jun 2016 14:18:28 +0200 Subject: [PATCH 15/33] Add description champs on description page. --- app/assets/javascripts/description.js | 8 ++++++++ app/assets/stylesheets/description.scss | 7 +++++++ app/models/champ.rb | 2 +- app/views/users/description/_champs.html.haml | 6 +++++- 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/description.js b/app/assets/javascripts/description.js index bb1a793ad..f0b10d5af 100644 --- a/app/assets/javascripts/description.js +++ b/app/assets/javascripts/description.js @@ -14,6 +14,14 @@ function action_type_de_champs() { toggleErrorClass(this, validatePhone(val)); }); + $("#liste_champs input").on('focus', function (){ + $("#description_"+this.id).slideDown(); + }); + + $("#liste_champs input").on('blur', function (){ + $("#description_"+this.id).slideUp(); + }); + address_type_init(); } diff --git a/app/assets/stylesheets/description.scss b/app/assets/stylesheets/description.scss index 1e1972bea..e40c2d551 100644 --- a/app/assets/stylesheets/description.scss +++ b/app/assets/stylesheets/description.scss @@ -85,3 +85,10 @@ width: 100%; } } + +.description_div { + margin-top: 5px; + margin-left: 5px; + color: dimgrey; + display: none; +} \ No newline at end of file diff --git a/app/models/champ.rb b/app/models/champ.rb index b4c389c8c..593acfe97 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -2,7 +2,7 @@ class Champ < ActiveRecord::Base belongs_to :dossier belongs_to :type_de_champ - delegate :libelle, :type_champ, :order_place, :mandatory, to: :type_de_champ + delegate :libelle, :type_champ, :order_place, :mandatory, :description, to: :type_de_champ def mandatory? mandatory diff --git a/app/views/users/description/_champs.html.haml b/app/views/users/description/_champs.html.haml index c1738f35b..464572686 100644 --- a/app/views/users/description/_champs.html.haml +++ b/app/views/users/description/_champs.html.haml @@ -35,4 +35,8 @@ value: champ.value, type: champ.type_champ, 'data-provide' => champ.data_provide, - 'data-date-format' => ('dd/mm/yyyy' if champ.type_champ == 'datetime')} \ No newline at end of file + 'data-date-format' => ('dd/mm/yyyy' if champ.type_champ == 'datetime')} + - unless champ.description.empty? + .row + .col-lg-8.col-md-8{class: 'description_div', id:"description_champs_#{champ.id}"} + = champ.description From b987b86bd81ac3cf5069607c63875388a302414b Mon Sep 17 00:00:00 2001 From: Xavier J Date: Thu, 9 Jun 2016 16:28:44 +0200 Subject: [PATCH 16/33] Add address input on carte view --- app/assets/images/marker-icon.png | Bin 0 -> 8607 bytes app/assets/javascripts/address_typeahead.js | 22 ++++++++++++ app/assets/javascripts/carte/carte.js | 36 ++++++++++++++++++++ app/assets/javascripts/description.js | 24 ------------- app/controllers/ban/search_controller.rb | 11 ++++++ app/views/users/carte/show.html.haml | 3 +- config/routes.rb | 1 + 7 files changed, 72 insertions(+), 25 deletions(-) create mode 100644 app/assets/images/marker-icon.png create mode 100644 app/assets/javascripts/address_typeahead.js diff --git a/app/assets/images/marker-icon.png b/app/assets/images/marker-icon.png new file mode 100644 index 0000000000000000000000000000000000000000..de1f1568d1e3422288d4c4c334c0f248d5bc7d94 GIT binary patch literal 8607 zcma)?cQ{<@+W&P1BMi~YjNTb7qXwgQB7zhKqxW7SQO4-enP|~N^coVKkmy7UqW2aF z(a9@&?{m(6uYG=Hu4~O&>sj~rbC+j5_aD!-BK34sfyDI0SXfv1=8*3~q8DFWJODCi!0_^ML=9&CR?{>4#3dRV&Kxq8~UID>z45f(09p0cd0za9Pi_g9=w zuG-rFcIE8xS6(+63i%>jg`k4MLQYOX|4r%PX@LAskpETbVd&?I6f!`1xOll+B5%yu z{u{{C&ienr`TgXN5dNF&rr!Vexc^;~o0Mg4thKeJl#uRr-bgDIcNeEW3ge1!_dxy* zQkL~!QT|=^TlhzsQg+TBo(N}4q`H#a4N1_>&PvJxhD31WdvjhJYbOC1r&EA^%@<@E6_6#nQ_O>FoIz-Rg!86&Dk?mV}B4SWAf9(2)ph z0fdFHq=2QUr3F+(R0M&%BmOV?-{k+H%Lx4=i?|PUs%Qe7xr(`e_(ap?QHCv5spg# zas7KT{{PGMXLSDs^EX$&-#Muplbh*4{9ZtEP#K|rjQ!32hA9PiM# z_XV(?t&69Nhpme%7!C)E2t&od+}a3BJLlhnJb#q%e~JF$$q4;kt$$m+f3}7{MsIq9 z9Py3vUwuZ7xU`R_5etj4LS0G0&=-5+F(97lgWrC(pG$poUDbQV9u0(XQ+`5# zofn-jprb>X^?nn$G0||>kdd;FtbjK;S~aDVQ96=|KhH8DjIoL|!j)aV5%uCRfR%i# zf~=c@%SaMEHw*PZ>F1>adc3d-`+u6#Wwl(OS_D-o&`I$v`dXz7VWJ(K3pR;mkA1{(duDVzXOvgA77@ zrXW}xAud>sJu7Mve@1?8kSpe{nP@KYlOGnyj)FHq>fy;Go&8QZmzJq}vh$u*S6=t9 z=qRbl4x0@hus(GE7%V>(^|Hb4G}J-Q= z0D_^!zTBmmK-(0*{FITt5B=FtkFQQah?ljDod8R6k0h9f?#PZ7(f=^R-}{;&n&>1L z8WodSXX{BMnqTOAu1(7&9+x%=JG$*T0E;T2Du>;!hB*#(y;7KBmE65nzjre9+-liE zq>!5wAZqCVU-6MCG|Ky|l;4c4X4Z5Obq~5|IDF`;QoGVlMfBOg^Y{);5>JVy9a&-< z?7;isw)G_)raELm{*{8YVJ_5;1bfiz6z*RLFq!5LpO@2`K6$upbD13GvFUu7BF3kg z0?CQW%_Ml66o=>b^^5bpae=NkK&FH3V5hh$C} zDX-UNS!ZrD@M@`?KlTM1uTBcOm`_%?p}ro`BNo3&r#5co8D%Go`=Z%(+7}Z?I!M^; z6bq9iCxA1#133)>E*d#3r(NuBF-{e<%W*^#h5RTm6=^HILf8XnEm5p?O`&H7mJtY* zS(DGbC%_0U2c$K2^TMb|C~z%;s~^Fg@bikgo~7B3rtdp;UK?RbVsCvOw~{jd1X#vT zqHNVKBIzkMe3y4U^vWhEfnGT~77fL9wJyyOV9A?mdskBGq&vN&{3SockXG@NvVy5G z!t&Rar`<1HB2@@-C270^LmAS-iW{(}q31DGt`Rlr@o>`l6g zWl7tB@kytbzFR3{huPK#FPI>pv8`wl(V0m(`hiRmeux*c7iReDRQ~P7E6r}^bU9#hx2c;kGj;tNxRyb{ z2dR;t_b|kY+8`2+&j%%u+!<66{6d~#J^`^vv#lVG&0AW(Kq=H^adwBaRaVgR?e@3I zVvp@?)eYIT0(2U2MAtWp`y9$0nhSdp+k%`5;mTY2O;LE*%1plIbo21{M*e;~sw!;8 z)ST73?38MftB-SRSh=u;?(Qmde)%bTw+iKB6I)jr?D41w_S;>B32E5`0?BM|AoEUoUT zMtSYxWZO)f-*GwQ0fCUIL@m`3TfVPDQU+T(V#*BJ*XT9s>>_ zwE^-sa(rb&MuDHHC)4P32&?YWQN)}sX?P9my^i1bMk+c&YisNVpHm|D@)rxU>YLj? z{(Ab1@@Wb@vlvGsorhqGf&sOqbL1ze;w(=7h5;inlBiff+c2#}`l*8--#`VwExVex zs_T@g%gBnEz_?~5EptkxXifMY-`TT#W3|e(wPd`Y&>40Kf5|#RSY6)OtZ+>})9WHv zHAeiL(U>K33N&b8T4L+uZogLMNK~SqZMxFSM)_cTr}7Lpo=qtRP{{MkO4Y~k9oqf( zxEVmM!LJ-Y3J9g=?CytZF$=0DWUtAe;?ssz`w|m$?%vANU7Y$DqOfFvAwZ-Y5@ufB z9*Chc_ysz2VMUJ+n#&UJ6y-1hmC$O+B@A?SZ)#MJ#>+3a=30++Y5j>V==J;5MS=Li zuqv&>vRB|$k9awP*0EPnIOCwWRGb~W3Tt$MHJ=1ruNuitXZG+=WRYVlKg@-_IX;LqIHJRV8|rX~(&U25 zUUVh=0GqZ)(T!Sd9==kUA*NL1Wp)F-dCOmaI)bd%DN{xDCokaT?P& z;`oEPqwfMrb&Y4**+9L!sNJ=7mY;Jd+8~{^h6%p)Enm?Q-7N58>-_Q7qnPdCQ8gt? z0)}#0Db>~nOV$gjgfhH6;cip@W7}m10sUnY`@2EUi@&hpq>1RmVH%<1xh~oS)pRw+ zS`%-a{I)F&ymA-p>!pfC8nQSIax)qVXr5UvH`9M3Uifltk zxMWj5Vd0LKJAHqFohk##_c6oDN_+sF&R}P3Tu}URT4NO{K{P3^G0zWPF&≫T|XR zCWg>a;5R;CflV8V8F5!!io7U88*gh z0~Sq!M$eiP(J=>`y_iXKLJ8{P?Fa92pojn4He5tN>R{-{gF;FwL3=dEx39iLyHf+#m*^o^Q5I4GmRE)i)y+`P!1dRaXyqsNz|x@JrWQ zwQDQ9so#~KTR^fz+V~qkEC!8rPLaFO*P$39QMo3$&Fg{uXpELgFB73UEOwz@2}AR` zu_nPcOvNp;Le5%527G-R5UP~CoFvnj_q8PgyLVnIE85`n13Q0IY^S%R1M=JTX8%3 zRyA#nEO(pKH^vU7@+XrN6W1uMiPaoTvB}C?`(#Ni7-1$R^TV`o#FVlH5xUQ69*WWp zNXnszhR_foP5|M1U1RqiW1|zu*Ra60(`V&2Pr4DJ5Q8Uo?d+}%C<(eGBUHfe=WUU1DWPimyL>Ek$uFP1uFVulkLZoUw}b{Iq!a zdfNPHJkTrlsJ7_b{t=8GaX?JuU$r1N_Q}RApQ4&Jyk_At1*_48JWD%bEyk0~&ZWby>mL|C8v1Kxx$zY^7YWv8nN8AlpE3pyQZX~!w<#tr~3M#eL z&W)5S7fk95lo^oC=_XnocRsB1gf!ewOJk$lrk&@FER0|mTS*TB-Q%TkyTq_B?~?1T zU9UqpJ$E*QDuYqa!%12Z&l)MCXQ_#Uk8w)2)!tv^SCE`QyNb-o4Y@r|10B&=3djLc zskm4S%LC8sAT=s<)jsJt^DUD8AvEkN=vL!0YRA{lj0|gkEC$?i&2|UD=?SqR`Ti*5 zUR@J8xTI3O%84Q5HrFH*^x~EgC<8KJ)ic@qRf|N}56H$P_kvg}ras?)YsAdC#G&~G z-Rp>#k*<-ZTWVXA3h?0Mw_8U&B=P&+tRdcR06oLZI=#+rrVmfgKbJ-KtLt;p-6qL1 z@>R^$bGk!lMZ+}zjQq`at%F1mvx%ngaC|SChTqlb?uwoKTyHg(@lQ8Jr1JH0rGKzl z*w1ddhgXczt3$yANpz14=M}Dsmwwv;+F`CBS$;mD>I#><9Zn+4sdim3CN2#j#D&M zdPm^rEF%gOqh(cL{J;QBt{>0|umm>64Ngk@5dWBNIN{moo+WwZo*-v=y2l{=W zn3e)Wf3ri15}VEiGiq@6s&@qy7~PgW#$`LM82q|7?7479K-@;$-Jz$(QyCv0L0v4+ zdLjdT#9FKTsvAR;x0W#i?mAPtC1v#@&(rs0uY!T~qQmU>8gn|6b(KEjOsWr>Gx68f zKRX|OQy3`SHRapuWnwB#JP}@p_k>?IKO*e;1LpZ&*T~go88gn{Zc+r&+W5}vw}G;q zzv6Z{^5m$oTiNod*jdSl)AKk#4tUYc`nii9o46KqBDf>NJ@Y%0j8}0~z!0HA1PP2c&v*Gzd^5$z?|m*#1}r! zVLjxS1vV%umRL0G%t7EngugSuUb8EPrIn{u>_s;lMTx+!uRd3cwKdQ-AH5IQUwwpi5Ijwp(LD)tun*CXf#t3RDnM=N z-4jM4RGiVMz|U9*H^YUu^2Yh+Zv9-^j1@8)z+HE~bYGvqw8LU3)K`mhS-$!}(b{-V zA*ch(H93joVC>dG??Q0tH8t`YAmz%;^$`w;u(ax6e=-nsi;TRgRIkU`QSXZWPI2P* zar)oT(xH&Tn0mEMV2{z_E?@@qs%}`^h zHPieg9kXJ_k$T0=){y)Cx^It8W@m&0B?`{)PdUVKM?i-0hMFFe6t70>9y$<);k_@9@y5J(YB5m>>xs$5+=nSv~DO%LzY4Gfio#&dR+=8ae}X4FX>@U^qwKCIu;!ZTn-K?r zRroWdJYz0^uzkk!U6UBjT<|uus`D-|f%R*oK*mAd&ivC2tGu4OEmifG`?J2SwE0*B zaA~VleS58O36Cy{S*aPszMgwQA33$NgznIm<1E4Vr|}v7SeHo+MOAZ&SQM(bRBRNm zal41ljbz%R2G7gI64bIF@=sk4+1I~jTqI9LLbxtc#m*DOW|^D#>mp|oa(^uoH(hSW z&0c)*8DUitWVh%Xr|a}TNgqsVd0)k(E=#JK5NEg3o0qy-EpkOWkk$sP=4wlgg^I>B z>rzH_#xvG+A92wBC{sEprwjz_&_|ngey`bMc>zv?qTqWu9BN4vMf?Kq)-}@~6o&{h zJf|8$G&5M9eCrT0mwy3zK&7BY@+)W9+^u_}D}<1tRaeu?XPubLE%NS+J{6iOR+zon zgfw{ZSOaE14k9y&9*eKTbhrn-4_4S~?|T1_nR90(uk8|K9yFoXh=#|--0W5~3Asyy~j zmrJosCYsyY+ex^slhE8RjH?{~Kshu|1ZC{8pOxOpG9S|NF0MmCjOy1F`gXk+;3IWs zcz9yRw}Wh-fbDE^ef6~CZiW0!QtY*qms*`$MEw`CCziI6WYi#i_o75_d$_bjt&AsK zRq{NHHjIUi_N$|LvNv@WM5$BEYAE-?0uJGR;R&;t412u(h=6lCxrkI=bjQbgEI%I| z6_`2Hgt@pu1p)A44(t8L{`NshFZ>p_=T<4L59AnY zH9Ew>8^rygyJf z!iNf)Mrn+Km_|Raf2SPw`8FP1M3Z{jotG%WPBRA5;+!Xu9_)&y^W=XR!^aLFL$8`F ziUr+lheYzNxUD}GUNi=A^T4NB^EtG?&YoEAzEAHL#zsct5i&Kcld!!bVv~`4U8-HV zdB?_vqQgMp6I~(+dmOO)*7Bw5tBuxRe2Q-PFfW=tRXvJ0_uV5~Re~$}XQc9iHihG4 zwRI)MYNR~PtE7l4%zaPy&DjzT7AY!wXoxQ0rhp@g2?c&MEKw(s+Xs=4vsUOuBKB-TQ zCI?XVJsGLL2=rtt-9iUb-1oh&AprSSZtl`=>2b3Q3w9nif6|YQv?OAXR&Fle8Mgi^ zj;WmQ&8iGCIOmX=OtBeZ>3Y#ySSEu3@>eI;ShMu?k=p|R!WT(jVaK`^>-I_*fn$zz z9_6Aa!E81@;}O+$pKo)DiNaEY4ox@vcz`?S7!ODBNtwMR0|qRvDlIILO#YOMpwV%% zV?wK20MlAybcxG5}5^>j(HfQ*H)54GB)MbRf zOBC_YBxgY!8%;v&C5I|YIA#<`B8=EO^`u1(`r^PNO)*}2((XulldxH13Q{O!Uo!mI zGndAs=R0RXqwT!(I2_>uo>;wZ%98%s>OSx#dBTvc$Pf?y)anul>_uHM*nXi;Z03BcqZL!{KDiv2%}INtTY3+wuwBpzR43y-I0{7U8&!o}98{b<;iIDPSA;WfzFA#ep~Oy~BT z9LR}n2}poQadimO9eZ4M@7#)nU{X%IvkgssqZBQ~_oK=pP{) zN3ZUr`Lv$Xi|1z^GKFExs!0KC&y0K)y0Q(0T(L8t3gBHH$k7WO@N>HTeC*-L+TcML z7cCN%m|26VX)d-!>uK6P5hQn6ilOX#qv4 zrwrEP#P$IA#z@FLvd~fPq5hrQzEy6eG)E0>NnJj+k4$WEfupMLjAY;u&5|*TVHL-! zRq;-;X}j{cwl9lGxv(8mCdo3XPlEO6WNXs5Rk6_8&keAVPlY*Yar$r|c2(-=zPhc5d3l5cMqgHR)zY1fkk5G~IGLu!R`-_lNkKujCHn+vh_t$DS3 z@FEtz8HGo^r0|H{B6I1k%>4!~`7^F!j^JsAEt`S9ZJPZI*z^4->i}!q!2r28wzT(1 zKPfOY(gst70WHiF+qwr@LO17T+gsuDqQ4f8akYGG!Uj?()3~wrSRP`%xy9@d%qGpO z%zm0-EZgZC6^>;=9k6tAfA(c%;BC^}$b1jUR8v@y7z)L)6q{;Jf1Gxpc|byOuKHkv znm5vxQ`Gmt{{0AbJj4LB+ohn5JGbPu6Rz(8_XTLk2W(ycw$J-$G(g4%e*o94hdM?b z?V8)I{$@MLE14&q7;*C)#V|hpbsk6AH}$>0+stg>(_YJm zy?NfKS#i#99vxLJg@|*O#Wb>32m0Iq&>=GB@h=v!_w|~APevGje>I`5tfN$+Xdd!E Dq_0E^ literal 0 HcmV?d00001 diff --git a/app/assets/javascripts/address_typeahead.js b/app/assets/javascripts/address_typeahead.js new file mode 100644 index 000000000..d0f8a434f --- /dev/null +++ b/app/assets/javascripts/address_typeahead.js @@ -0,0 +1,22 @@ +function address_type_init() { + display = 'label'; + + var bloodhound = new Bloodhound({ + datumTokenizer: Bloodhound.tokenizers.obj.whitespace(display), + queryTokenizer: Bloodhound.tokenizers.whitespace, + + remote: { + url: '/ban/search?request=%QUERY', + wildcard: '%QUERY' + } + }); + bloodhound.initialize(); + + $("input[type='address']").typeahead({ + minLength: 1 + }, { + display: display, + source: bloodhound, + limit: 5 + }); +} \ No newline at end of file diff --git a/app/assets/javascripts/carte/carte.js b/app/assets/javascripts/carte/carte.js index 172d7a345..90ebef7b4 100644 --- a/app/assets/javascripts/carte/carte.js +++ b/app/assets/javascripts/carte/carte.js @@ -14,6 +14,17 @@ function initCarto() { layers: [OSM] }); + icon = L.icon({ + iconUrl: '/assets/marker-icon.png', + //shadowUrl: 'leaf-shadow.png', + + iconSize: [34.48, 40], // size of the icon + //shadowSize: [50, 64], // size of the shadow + iconAnchor: [20, 20] // point of the icon which will correspond to marker's location + //shadowAnchor: [4, 62], // the same for the shadow + //popupAnchor: [-3, -76] // point from which the popup should open relative to the iconAnchor + }); + if (qp_active()) display_qp(JSON.parse($("#quartier_prioritaires").val())); @@ -39,6 +50,7 @@ function initCarto() { map.setView(new L.LatLng(position.lat, position.lon), position.zoom); add_event_freeDraw(); + add_event_search_address(); } function default_gestionnaire_position() { @@ -111,8 +123,32 @@ function get_position() { return position; } +function get_address_point(request) { + $.ajax({ + url: '/ban/address_point?request=' + request, + dataType: 'json', + async: true + }).done(function (data) { + if (data.lat != null) { + map.setView(new L.LatLng(data.lat, data.lon), data.zoom); + L.marker([data.lat, data.lon], {icon: icon}).addTo(map); + } + }); +} + function jsObject_to_array(qp_list) { return Object.keys(qp_list).map(function (v) { return qp_list[v]; }); } + +function add_event_search_address() { + $("#search_by_address input[type='address']").bind('typeahead:select', function (ev, seggestion) { + get_address_point(seggestion['label']); + }); + + $("#search_by_address input[type='address']").keypress(function (e) { + if (e.which == 13) + get_address_point($(this).val()); + }); +} \ No newline at end of file diff --git a/app/assets/javascripts/description.js b/app/assets/javascripts/description.js index f0b10d5af..98d18a906 100644 --- a/app/assets/javascripts/description.js +++ b/app/assets/javascripts/description.js @@ -45,27 +45,3 @@ function validateEmail(email) { function validateInput(input, regex) { return regex.test(input); } - -function address_type_init() { - - display = 'label'; - - var bloodhound = new Bloodhound({ - datumTokenizer: Bloodhound.tokenizers.obj.whitespace(display), - queryTokenizer: Bloodhound.tokenizers.whitespace, - - remote: { - url: '/ban/search?request=%QUERY', - wildcard: '%QUERY' - } - }); - bloodhound.initialize(); - - $("input[type='address']").typeahead({ - minLength: 1 - }, { - display: display, - source: bloodhound, - limit: 5 - }); -} \ No newline at end of file diff --git a/app/controllers/ban/search_controller.rb b/app/controllers/ban/search_controller.rb index 63875a93a..55780de7b 100644 --- a/app/controllers/ban/search_controller.rb +++ b/app/controllers/ban/search_controller.rb @@ -6,4 +6,15 @@ class Ban::SearchController < ApplicationController |acc, value| acc.push({label: value}) }.to_json end + + def get_address_point + point = Carto::Geocodeur.convert_adresse_to_point(params[:request]) + + unless point.nil? + lon = point.x.to_s + lat = point.y.to_s + end + + render json: {lon: lon, lat: lat, zoom: '14', dossier_id: params[:dossier_id]} + end end \ No newline at end of file diff --git a/app/views/users/carte/show.html.haml b/app/views/users/carte/show.html.haml index 6e310a059..75e0f6311 100644 --- a/app/views/users/carte/show.html.haml +++ b/app/views/users/carte/show.html.haml @@ -9,6 +9,8 @@ \- %button#delete.btn.btn-sm.btn-danger{type:'button'} Supprimer + %span#search_by_address{style: 'margin-left: 20px'} + %input.form-control{type: :address, placeholder: 'Rechercher une adresse'} %br %br #carte_page.row @@ -24,7 +26,6 @@ %h3.text-warning Cadastres %ul - = form_tag(url_for({controller: :carte, action: :save, dossier_id: @dossier.id}), class: 'form-inline', method: 'POST') do %br %input{type: 'hidden', value: "#{@dossier.json_latlngs}", name: 'json_latlngs', id: 'json_latlngs'} diff --git a/config/routes.rb b/config/routes.rb index d4c0ee938..9df3fd5df 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -114,6 +114,7 @@ Rails.application.routes.draw do namespace :ban do get 'search' => 'search#get' + get 'address_point' => 'search#get_address_point' end get 'backoffice' => 'backoffice#index' From 2791988da2923ae8eccdf2edcbd82a3671459d44 Mon Sep 17 00:00:00 2001 From: Guillaume Lazzara Date: Thu, 9 Jun 2016 17:49:38 +0200 Subject: [PATCH 17/33] Add draft/publish status for procedure. --- .../admin/procedures_controller.rb | 44 ++++++-- app/controllers/users/dossiers_controller.rb | 2 +- app/controllers/users/sessions_controller.rb | 2 +- app/models/procedure.rb | 7 +- .../admin/procedures/_draft_list.html.haml | 19 ++++ app/views/admin/procedures/_onglets.html.haml | 5 + app/views/admin/procedures/show.html.haml | 50 ++++++--- config/routes.rb | 2 + .../20160609125949_add_procedure_status.rb | 12 +++ db/schema.rb | 3 +- .../pieces_justificatives_controller_spec.rb | 7 +- .../admin/procedures_controller_spec.rb | 13 ++- .../admin/types_de_champ_controller_spec.rb | 7 +- .../users/dossiers_controller_spec.rb | 12 ++- .../users/sessions_controller_spec.rb | 12 ++- spec/factories/procedure.rb | 7 ++ spec/features/admin/procedure_locked_spec.rb | 15 +-- spec/features/users/complete_demande_spec.rb | 14 ++- spec/features/users/start_demande_spec.rb | 2 +- .../cassettes/complete_demande_spec.yml | 101 ++++++++++++++++++ spec/models/procedure_spec.rb | 49 ++++++--- .../admin/procedures/show.html.haml_spec.rb | 27 +++-- 22 files changed, 341 insertions(+), 71 deletions(-) create mode 100644 app/views/admin/procedures/_draft_list.html.haml create mode 100644 db/migrate/20160609125949_add_procedure_status.rb create mode 100644 spec/fixtures/cassettes/complete_demande_spec.yml diff --git a/app/controllers/admin/procedures_controller.rb b/app/controllers/admin/procedures_controller.rb index 8ee4befa2..bf5a137e3 100644 --- a/app/controllers/admin/procedures_controller.rb +++ b/app/controllers/admin/procedures_controller.rb @@ -7,7 +7,7 @@ class Admin::ProceduresController < AdminController def index @procedures = smart_listing_create :procedures, - current_administrateur.procedures.where(archived: false), + current_administrateur.procedures.where(published: true, archived: false), partial: "admin/procedures/list", array: true @@ -25,6 +25,18 @@ class Admin::ProceduresController < AdminController render 'index' end + def draft + @procedures = smart_listing_create :procedures, + current_administrateur.procedures.where(published: false, archived: false), + partial: "admin/procedures/draft_list", + array: true + + draft_class + + render 'index' + end + + def show @facade = AdminProceduresShowFacades.new @procedure.decorate end @@ -63,16 +75,12 @@ class Admin::ProceduresController < AdminController redirect_to edit_admin_procedure_path(id: @procedure.id) end + def publish + change_status({published: params[:published]}) + end + def archive - @procedure = current_administrateur.procedures.find(params[:procedure_id]) - @procedure.update_attributes({archived: params[:archive]}) - - flash.notice = 'Procédure éditée' - redirect_to admin_procedures_path - - rescue ActiveRecord::RecordNotFound - flash.alert = 'Procédure inéxistante' - redirect_to admin_procedures_path + change_status({archived: params[:archive]}) end def active_class @@ -83,6 +91,10 @@ class Admin::ProceduresController < AdminController @archived_class = 'active' end + def draft_class + @draft_class = 'active' + end + private def create_procedure_params @@ -92,4 +104,16 @@ class Admin::ProceduresController < AdminController def create_module_api_carto_params params.require(:procedure).require(:module_api_carto_attributes).permit(:id, :use_api_carto, :quartiers_prioritaires, :cadastre) end + + def change_status(status_options) + @procedure = current_administrateur.procedures.find(params[:procedure_id]) + @procedure.update_attributes(status_options) + + flash.notice = 'Procédure éditée' + redirect_to admin_procedures_path + + rescue ActiveRecord::RecordNotFound + flash.alert = 'Procédure inéxistante' + redirect_to admin_procedures_path + end end diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index 3faaad661..a6aa36f39 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -23,7 +23,7 @@ class Users::DossiersController < UsersController end def new - procedure = Procedure.where(archived: false).find(params[:procedure_id]) + procedure = Procedure.where(archived: false, published: true).find(params[:procedure_id]) @dossier = Dossier.new(procedure: procedure) @siret = params[:siret] || current_user.siret diff --git a/app/controllers/users/sessions_controller.rb b/app/controllers/users/sessions_controller.rb index 40c598220..c69384199 100644 --- a/app/controllers/users/sessions_controller.rb +++ b/app/controllers/users/sessions_controller.rb @@ -12,7 +12,7 @@ class Users::SessionsController < Sessions::SessionsController # GET /resource/sign_in def new unless user_return_to_procedure_id.nil? - @dossier = Dossier.new(procedure: Procedure.not_archived(user_return_to_procedure_id)) + @dossier = Dossier.new(procedure: Procedure.active(user_return_to_procedure_id)) end @user = User.new diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 1d85633a3..00aa073b6 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -33,6 +33,10 @@ class Procedure < ActiveRecord::Base Procedure.where(archived: false).find(id) end + def self.active id + Procedure.where(archived: false, published: true).find(id) + end + def switch_types_de_champ index_of_first_element switch_list_order(types_de_champ_ordered, index_of_first_element) end @@ -51,6 +55,7 @@ class Procedure < ActiveRecord::Base end def locked? - dossiers.where.not(state: :draft).count > 0 + published? end + end diff --git a/app/views/admin/procedures/_draft_list.html.haml b/app/views/admin/procedures/_draft_list.html.haml new file mode 100644 index 000000000..7d3505614 --- /dev/null +++ b/app/views/admin/procedures/_draft_list.html.haml @@ -0,0 +1,19 @@ +- unless smart_listing.empty? + %table.table + %thead + %th#ID= smart_listing.sortable 'ID', 'id' + %th#libelle= smart_listing.sortable 'Libellé', 'libelle' + + - @procedures.each do |procedure| + - procedure = procedure.decorate + %tr + %td.col-md-1.col-lg-1= procedure.id + %td.col-md-6.col-lg-6 + = link_to(procedure.libelle, "/admin/procedures/#{procedure.id}") + + = smart_listing.paginate + = smart_listing.pagination_per_page_links + +- else + %h4.center + Aucune procédure diff --git a/app/views/admin/procedures/_onglets.html.haml b/app/views/admin/procedures/_onglets.html.haml index 80fd9d40a..5c8e0a8c0 100644 --- a/app/views/admin/procedures/_onglets.html.haml +++ b/app/views/admin/procedures/_onglets.html.haml @@ -1,5 +1,10 @@ #onglets %ul.nav.nav-tabs + %li{class: @draft_class} + %a{:href => "#{url_for :admin_procedures_draft}"} + %h5{style: 'color: black'} + ="Brouillons" + %li{class: @active_class} %a{:href => "#{url_for :admin_procedures}"} %h5.text-success diff --git a/app/views/admin/procedures/show.html.haml b/app/views/admin/procedures/show.html.haml index 6bb99288b..8626a87e2 100644 --- a/app/views/admin/procedures/show.html.haml +++ b/app/views/admin/procedures/show.html.haml @@ -1,30 +1,48 @@ #procedure_show =render partial: 'head', locals: {active: 'Informations'} - = form_tag admin_procedure_archive_path(procedure_id: @facade.procedure.id, archive: !@facade.procedure.archived?), method: :put, style:'float: right; margin-top: 10px' do - %button#archive.btn.btn-small.btn-default.text-info{type: :button} - %i.fa.fa-eraser - - if @facade.procedure.archived - = 'Réactiver' - - else - = 'Archiver' - #confirm - %button#valid.btn.btn-small.btn-success{type: :submit} - %i.fa.fa-check - Valider - %button#cancel.btn.btn-small.btn-danger{type: :button} - %i.fa.fa-remove - Annuler + -if ! @facade.procedure.published? + = form_tag admin_procedure_publish_path(procedure_id: @facade.procedure.id, publish: true), method: :put, style:'float: right; margin-top: 10px' do + %button#archive.btn.btn-small.btn-success.text-info{type: :button} + %i.fa.fa-eraser + Publier + #confirm + %button#valid.btn.btn-small.btn-success{type: :submit} + %i.fa.fa-check + Valider + %button#cancel.btn.btn-small.btn-danger{type: :button} + %i.fa.fa-remove + Annuler + + -else + = form_tag admin_procedure_archive_path(procedure_id: @facade.procedure.id, archive: !@facade.procedure.archived?), method: :put, style:'float: right; margin-top: 10px' do + %button#archive.btn.btn-small.btn-default.text-info{type: :button} + %i.fa.fa-eraser + - if @facade.procedure.archived + = 'Réactiver' + - else + = 'Archiver' + #confirm + %button#valid.btn.btn-small.btn-success{type: :submit} + %i.fa.fa-check + Valider + %button#cancel.btn.btn-small.btn-danger{type: :button} + %i.fa.fa-remove + Annuler - if @facade.procedure.locked? #procedure_locked.center %h5 - .label.label-info La procédure ne peut plus être modifiée car un usagé a déjà déposé un dossier + .label.label-info La procédure ne peut plus être modifiée car elle a été publiée %div %h3 Lien procédure %div{style:'margin-left:3%'} - = @facade.procedure.lien + -if @facade.procedure.published? + = @facade.procedure.lien + -else + %b + Cette procédure n'a pas encore été publiée et n'est donc pas accessible par le public. %br %h3 Détails diff --git a/config/routes.rb b/config/routes.rb index 9df3fd5df..3b5f6ebb2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -82,6 +82,7 @@ Rails.application.routes.draw do namespace :admin do get 'sign_in' => '/administrateurs/sessions#new' get 'procedures/archived' => 'procedures#archived' + get 'procedures/draft' => 'procedures#draft' get 'profile' => 'profile#show', as: :profile resources :procedures do @@ -95,6 +96,7 @@ Rails.application.routes.draw do end put 'archive' => 'procedures#archive', as: :archive + put 'publish' => 'procedures#publish', as: :publish resource :accompagnateurs, only: [:show, :update] diff --git a/db/migrate/20160609125949_add_procedure_status.rb b/db/migrate/20160609125949_add_procedure_status.rb new file mode 100644 index 000000000..6d4ea7e2e --- /dev/null +++ b/db/migrate/20160609125949_add_procedure_status.rb @@ -0,0 +1,12 @@ +class AddProcedureStatus < ActiveRecord::Migration + class Procedure < ActiveRecord::Base + end + + def change + add_column :procedures, :published, :boolean, default: false, null: false + Procedure.all.each do |procedure| + procedure.published = true + procedure.save! + end + end +end diff --git a/db/schema.rb b/db/schema.rb index cb89f0cce..4839009ec 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: 20160607150440) do +ActiveRecord::Schema.define(version: 20160609125949) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -242,6 +242,7 @@ ActiveRecord::Schema.define(version: 20160607150440) do t.string "logo" t.boolean "cerfa_flag", default: false t.string "logo_secure_token" + t.boolean "published", default: false, null: false end create_table "quartier_prioritaires", force: :cascade do |t| diff --git a/spec/controllers/admin/pieces_justificatives_controller_spec.rb b/spec/controllers/admin/pieces_justificatives_controller_spec.rb index 09ca67188..5c33da67e 100644 --- a/spec/controllers/admin/pieces_justificatives_controller_spec.rb +++ b/spec/controllers/admin/pieces_justificatives_controller_spec.rb @@ -2,7 +2,8 @@ require 'spec_helper' describe Admin::PiecesJustificativesController, type: :controller do let(:admin) { create(:administrateur) } - let(:procedure) { create(:procedure, administrateur: admin) } + let(:published) { false } + let(:procedure) { create(:procedure, administrateur: admin, published: published) } before do sign_in admin end @@ -17,8 +18,8 @@ describe Admin::PiecesJustificativesController, type: :controller do it { expect(subject.status).to eq(404) } end - context 'when procedure have at least a file' do - let!(:dossier) { create(:dossier, procedure: procedure, state: :initiated) } + context 'when procedure is published' do + let(:published) { true } it { is_expected.to redirect_to admin_procedure_path id: procedure_id } end diff --git a/spec/controllers/admin/procedures_controller_spec.rb b/spec/controllers/admin/procedures_controller_spec.rb index c4d414b0a..11587b94d 100644 --- a/spec/controllers/admin/procedures_controller_spec.rb +++ b/spec/controllers/admin/procedures_controller_spec.rb @@ -46,8 +46,15 @@ describe Admin::ProceduresController, type: :controller do it { expect(response.status).to eq(200) } end + describe 'GET #published' do + subject { get :published } + + it { expect(response.status).to eq(200) } + end + describe 'GET #edit' do - let(:procedure) { create(:procedure, administrateur: admin) } + let(:published) { false } + let(:procedure) { create(:procedure, administrateur: admin, published: published) } let(:procedure_id) { procedure.id } subject { get :edit, id: procedure_id } @@ -66,8 +73,8 @@ describe Admin::ProceduresController, type: :controller do it { expect(subject).to have_http_status(:success) } end - context 'when procedure have at least a file' do - let!(:dossier) { create(:dossier, procedure: procedure, state: :initiated) } + context 'when procedure is published' do + let(:published) { true } it { is_expected.to redirect_to admin_procedure_path id: procedure_id } end diff --git a/spec/controllers/admin/types_de_champ_controller_spec.rb b/spec/controllers/admin/types_de_champ_controller_spec.rb index 5227c56ae..c334ccb0c 100644 --- a/spec/controllers/admin/types_de_champ_controller_spec.rb +++ b/spec/controllers/admin/types_de_champ_controller_spec.rb @@ -9,7 +9,8 @@ describe Admin::TypesDeChampController, type: :controller do end describe 'GET #show' do - let(:procedure) { create(:procedure, administrateur: admin) } + let(:published) { false } + let(:procedure) { create(:procedure, administrateur: admin, published: published) } let(:procedure_id) { procedure.id } subject { get :show, procedure_id: procedure_id } @@ -19,8 +20,8 @@ describe Admin::TypesDeChampController, type: :controller do it { expect(subject.status).to eq(404) } end - context 'when procedure have at least a file' do - let!(:dossier) { create(:dossier, procedure: procedure, state: :initiated) } + context 'when procedure is published' do + let(:published) { true } it { is_expected.to redirect_to admin_procedure_path id: procedure_id } end diff --git a/spec/controllers/users/dossiers_controller_spec.rb b/spec/controllers/users/dossiers_controller_spec.rb index f996114da..637f39710 100644 --- a/spec/controllers/users/dossiers_controller_spec.rb +++ b/spec/controllers/users/dossiers_controller_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe Users::DossiersController, type: :controller do let(:user) { create(:user) } - let(:procedure) { create(:procedure) } + let(:procedure) { create(:procedure, :published) } let(:procedure_id) { procedure.id } let(:dossier) { create(:dossier, :with_entreprise, user: user, procedure: procedure) } let(:dossier_id) { dossier.id } @@ -80,6 +80,16 @@ describe Users::DossiersController, type: :controller do it { is_expected.to redirect_to users_dossiers_path } end + + context 'when procedure is not published' do + let(:procedure) { create(:procedure, published: false) } + + before do + sign_in create(:user) + end + + it { is_expected.to redirect_to users_dossiers_path } + end end end diff --git a/spec/controllers/users/sessions_controller_spec.rb b/spec/controllers/users/sessions_controller_spec.rb index 256913b1f..9728aba35 100644 --- a/spec/controllers/users/sessions_controller_spec.rb +++ b/spec/controllers/users/sessions_controller_spec.rb @@ -85,8 +85,18 @@ describe Users::SessionsController, type: :controller do it { expect(subject).to redirect_to root_path } end + context 'when procedure is not published' do + let(:procedure) { create :procedure, published: false } + before do + session["user_return_to"] = "?procedure_id=#{procedure.id}" + end + + it { expect(subject.status).to eq 302} + it { expect(subject).to redirect_to root_path } + end + context 'when procedure_id exist' do - let(:procedure) { create :procedure } + let(:procedure) { create :procedure, published: true } before do session["user_return_to"] = "?procedure_id=#{procedure.id}" diff --git a/spec/factories/procedure.rb b/spec/factories/procedure.rb index c8dea5a80..2614d06e6 100644 --- a/spec/factories/procedure.rb +++ b/spec/factories/procedure.rb @@ -5,6 +5,7 @@ FactoryGirl.define do description "Demande de subvention à l'intention des associations" organisation "Orga SGMAP" direction "direction SGMAP" + published false after(:build) do |procedure, _evaluator| if procedure.module_api_carto.nil? @@ -44,5 +45,11 @@ FactoryGirl.define do procedure.types_de_piece_justificative << msa end end + + trait :published do + after(:build) do |procedure, _evaluator| + procedure.published = true + end + end end end diff --git a/spec/features/admin/procedure_locked_spec.rb b/spec/features/admin/procedure_locked_spec.rb index 3dea1b3b0..4676e9043 100644 --- a/spec/features/admin/procedure_locked_spec.rb +++ b/spec/features/admin/procedure_locked_spec.rb @@ -3,26 +3,27 @@ require 'spec_helper' feature 'procedure locked' do let(:administrateur) { create(:administrateur) } - let(:procedure) { create(:procedure, administrateur: administrateur) } + let(:published) { false } + let(:procedure) { create(:procedure, administrateur: administrateur, published: published) } before do login_as administrateur, scope: :administrateur visit admin_procedure_path(procedure) end - context 'when procedure have no file' do + context 'when procedure is not published' do scenario 'info label is not present' do - expect(page).not_to have_content('La procédure ne peut plus être modifiée car un usagé a déjà déposé un dossier') + expect(page).not_to have_content('La procédure ne peut plus être modifiée car elle a été publiée') end end - context 'when procedure have at least a file' do + context 'when procedure is published' do + let(:published) { true } before do - create(:dossier, procedure: procedure, state: :initiated) visit admin_procedure_path(procedure) end scenario 'info label is present' do - expect(page).to have_content('La procédure ne peut plus être modifiée car un usagé a déjà déposé un dossier') + expect(page).to have_content('La procédure ne peut plus être modifiée car elle a été publiée') end context 'when user click on Description tab' do @@ -45,7 +46,7 @@ feature 'procedure locked' do end end - context 'when user click on Pieces Justificatiives tab' do + context 'when user click on Pieces Justificatives tab' do before do page.click_on 'Pièces justificatives' end diff --git a/spec/features/users/complete_demande_spec.rb b/spec/features/users/complete_demande_spec.rb index 46b766c06..a1f8dc0f8 100644 --- a/spec/features/users/complete_demande_spec.rb +++ b/spec/features/users/complete_demande_spec.rb @@ -2,9 +2,10 @@ require 'spec_helper' feature 'user path for dossier creation' do let(:user) { create(:user) } - let(:procedure) { create(:procedure) } + let(:procedure) { create(:procedure, :published) } let(:siret) { '53272417600013' } let(:siren) { siret[0...9] } + context 'user arrives on siret page' do before do visit new_users_dossiers_path(procedure_id: procedure.id) @@ -65,4 +66,15 @@ feature 'user path for dossier creation' do end end end + + context 'user cannot access non-published procedures' do + let(:procedure) { create(:procedure) } + before do + visit new_users_dossiers_path(procedure_id: procedure.id) + end + + scenario 'user is on home page', vcr: { cassette_name: 'complete_demande_spec' } do + expect(page).to have_content('La procédure n\'existe pas') + end + end end \ No newline at end of file diff --git a/spec/features/users/start_demande_spec.rb b/spec/features/users/start_demande_spec.rb index 135b7f499..480550df1 100644 --- a/spec/features/users/start_demande_spec.rb +++ b/spec/features/users/start_demande_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' feature 'user arrive on siret page' do - let(:procedure) { create(:procedure) } + let(:procedure) { create(:procedure, :published) } let(:user) { create(:user) } let(:siret) { '42149333900020' } let(:siren) { siret[0...9] } diff --git a/spec/fixtures/cassettes/complete_demande_spec.yml b/spec/fixtures/cassettes/complete_demande_spec.yml new file mode 100644 index 000000000..95841bbea --- /dev/null +++ b/spec/fixtures/cassettes/complete_demande_spec.yml @@ -0,0 +1,101 @@ +--- +http_interactions: +- request: + method: get + uri: https://api.github.com/repos/sgmap/tps/releases/latest + body: + encoding: US-ASCII + string: '' + headers: + Accept: + - "*/*; q=0.5, application/xml" + Accept-Encoding: + - gzip, deflate + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Server: + - GitHub.com + Date: + - Thu, 09 Jun 2016 14:42:08 GMT + Content-Type: + - application/json; charset=utf-8 + Transfer-Encoding: + - chunked + Status: + - 200 OK + X-Ratelimit-Limit: + - '60' + X-Ratelimit-Remaining: + - '43' + X-Ratelimit-Reset: + - '1465485629' + Cache-Control: + - public, max-age=60, s-maxage=60 + Vary: + - Accept + - Accept-Encoding + Etag: + - W/"0962b5ade3f87b4e0092d56f4719512e" + Last-Modified: + - Fri, 03 Jun 2016 10:05:19 GMT + X-Github-Media-Type: + - github.v3 + Access-Control-Expose-Headers: + - ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, + X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval + Access-Control-Allow-Origin: + - "*" + Content-Security-Policy: + - default-src 'none' + Strict-Transport-Security: + - max-age=31536000; includeSubdomains; preload + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - deny + X-Xss-Protection: + - 1; mode=block + X-Served-By: + - c6c65e5196703428e7641f7d1e9bc353 + Content-Encoding: + - gzip + X-Github-Request-Id: + - B918B84A:B215:2AB0D00D:5759803F + body: + encoding: ASCII-8BIT + string: !binary |- + H4sIAAAAAAAAA6VW7WobRxR9lWH/+I/jlSrjEuE2mEAhYEogTiltihjtXq0m + zM5s5mMjR/hdQn9Fz7Ev1nNHK1lSZAdRsJFXe++Ze88994yXWXQ6G2fzEBo/ + znPZqItKhXmcXhS2zh011ue+qmWTIwDPmqQnn49GV1ejwc/ZeSa9p+An/wsm + X4MALTbayvIAbf2lP6WwHnH5ysiazrWckn4A/DzU+gB8p9sjfQZZ5e3wYoRc + VWbjvu3zDN9PGBrU9a+DdBWFCVirVVB+jje19IEcUncixVjczGZUBBmUNaIk + L2SBpEZWRgaKzgsKolHdtwKvPkYf1EwVCG7xWN4DSX2K5AFaOjkL2XgmtSeM + IYa5ddl4mWlbKYPT//wY9ZthX/hwNBhevrwaILCVKPVwXunLDcPRkyusCWRC + UkHMN+mv2l+Yisr1KMxJxmN7TkQM5/NtOc/PYBs2s1rbz8g8LHVfobvg+TYH + Ja3/VqY6OR85y9yGOYEllM66qZT/kcb3Cknxy5w/JqpkBA9+HR0q+2Dd9jD6 + DJTy2aCKZVrGBBWnvnCqYQGdws5eHnCsq6RRX5IQT8FBHusvecMJ5KZ45FEL + XZ2SuE5Y5o1TrSzumQJHBWEjysnJYAeZwAr3Da/xe4yayVWBJrKseYXSaj2c + Z42j3vi261Y4wraWE4kNzH4aDK9eDPAzuhu8HA/xM/gLUE2cahjBkajhYDy4 + HF+OOKq3vvHf/7CruKnUhw71A0/ukzY29UU1p2P0SRuMqS3v0df1fPTr7za2 + BHPpVv46x/MH98FcR73+1Cp9vjdCGjiUchFuZqS+9yRqqdhB8EKQEYHqxgvX + rUiLYBGm2c5sEWsWg/gUFaKCw6/w0QktRaNB8My6mi7SmXl/2PXm0KDA7qNl + HrpoQygccHt1tPaeXDLdx7NLBU2gPg4uqYmKixMzHRd4FOzneCMRhCQ8zyX8 + F+neK3Ln3Fuj0Q9jPuXaVEtTonVEWDTHbq+7Fa4GPqhHOt7kLXiQFYK4EGNo + wXdGAolGxEcGBPm9TpN7L0KUoAgHi+5rotTZoluV0ZHwShSko3pR9MQn2u/e + vhOV6/4tUgaO0Ar94XCzVsHzxbIMeHFgek7yVSYaintlyXTzoe+ETrsFHbsI + fdOtClx/fOEx0TNriv7WFPgnREzJW2X8hWCB3J7tK0AAvyXnWEmYDw9trbk0 + uH5gW0Kg27hAjOYYpROf62q71cFo8o36sQ03dbfSyqJdDGa7H8d25Ba4OwNj + KXjffSsPJMqstCjVK2gErfPwtqIEo71a1hogs9HzekCp+OMiPC6vOxsDUp5O + Ex5C2l2hOYZrXRIVcwpdp0EmfeB4ZtyzRpm/VB43gH9osPObk/oQuAGC0tIp + 31ijplwGpqxVZZ7Y+T26MUJcAQ5C2PeHzXzSt5jRa4uLN+nm0cGOTegxjhWv + z2SJApORaZaCKuZpGNKAsDNaNNYF8frdH6I8w6Js5tIqibc3b9/wLh0nHWuy + u7m8BrwmSaXSFXPVdiuu4KgxpMJ7B/xNLcQ0Vj0N3/vm97RkD/8BJlYZoPAL + AAA= + http_version: + recorded_at: Thu, 09 Jun 2016 14:42:08 GMT +recorded_with: VCR 3.0.1 diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index 6b6118104..b36e64837 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -79,28 +79,47 @@ describe Procedure do end describe 'locked?' do - let(:procedure) { create(:procedure) } + let(:procedure) { create(:procedure, published: published) } subject { procedure.locked? } - context 'when procedure does not have dossier' do + context 'when procedure is in draft status' do + let(:published) { false } it { is_expected.to be_falsey } end - context 'when procedure have dossier with state draft' do - before do - create(:dossier, procedure: procedure, state: :draft) - end - - it { is_expected.to be_falsey } - end - - context 'when procedure have dossier with state initiated' do - before do - create(:dossier, procedure: procedure, state: :initiated) - end - + context 'when procedure is in draft status' do + let(:published) { true } it { is_expected.to be_truthy } end end + + describe 'active' do + let(:procedure) { create(:procedure, published: published, archived: archived) } + subject { Procedure.active(procedure.id) } + + context 'when procedure is in draft status and not archived' do + let(:published) { false } + let(:archived) { false } + it { expect { subject }.to raise_error(ActiveRecord::RecordNotFound) } + end + + context 'when procedure is published and not archived' do + let(:published) { true } + let(:archived) { false } + it { is_expected.to be_truthy } + end + + context 'when procedure is published and archived' do + let(:published) { true } + let(:archived) { true } + it { expect { subject }.to raise_error(ActiveRecord::RecordNotFound) } + end + + context 'when procedure is in draft status and archived' do + let(:published) { false } + let(:archived) { true } + it { expect { subject }.to raise_error(ActiveRecord::RecordNotFound) } + end + end end diff --git a/spec/views/admin/procedures/show.html.haml_spec.rb b/spec/views/admin/procedures/show.html.haml_spec.rb index 3f3474aa8..7e5c272a1 100644 --- a/spec/views/admin/procedures/show.html.haml_spec.rb +++ b/spec/views/admin/procedures/show.html.haml_spec.rb @@ -1,7 +1,9 @@ require 'spec_helper' describe 'admin/procedures/show.html.haml', type: :view do - let(:procedure) { create(:procedure) } + let(:archived) { false } + let(:published) { false } + let(:procedure) { create(:procedure, published: published, archived: archived) } before do assign(:facade, AdminProceduresShowFacades.new(procedure.decorate)) @@ -9,19 +11,32 @@ describe 'admin/procedures/show.html.haml', type: :view do render end + describe 'publish button' do + it { expect(rendered).to have_content('Publier') } + end + describe 'archive and unarchive button' do - context 'when procedure is active' do + let(:published) { true } + + context 'when procedure is published' do it { expect(rendered).to have_content('Archiver') } end context 'when procedure is archived' do - let(:procedure) { create(:procedure, archived: true) } - + let(:archived) { true } it { expect(rendered).to have_content('Réactiver') } end end - describe 'procedure link is present' do - it { expect(rendered).to have_content(new_users_dossiers_url(procedure_id: procedure.id)) } + describe 'procedure link' do + + context 'is not present when not published' do + it { expect(rendered).to have_content('Cette procédure n\'a pas encore été publiée et n\'est donc pas accessible par le public.') } + end + + context 'is present when already published' do + let(:published) { true } + it { expect(rendered).to have_content(new_users_dossiers_url(procedure_id: procedure.id)) } + end end end \ No newline at end of file From d7eee773da8f4e44d0aa53dc194d97a677338458 Mon Sep 17 00:00:00 2001 From: Xavier J Date: Mon, 13 Jun 2016 10:13:37 +0200 Subject: [PATCH 18/33] Migrate API Entreprise INSEE V1 to V2 --- lib/siade/api.rb | 12 +- lib/siade/etablissement_adapter.rb | 2 +- .../users/dossiers_controller_spec.rb | 10 +- spec/features/users/complete_demande_spec.rb | 8 +- spec/features/users/start_demande_spec.rb | 8 +- spec/lib/siade/api_spec.rb | 8 +- spec/lib/siade/entreprise_adapter_spec.rb | 8 +- spec/lib/siade/etablissement_adapter_spec.rb | 6 +- spec/lib/siade/exercices_adapter_spec.rb | 2 +- spec/lib/siade/rna_adapter_spec.rb | 2 +- spec/support/files/entreprise.json | 105 +++++++++++++++--- spec/support/files/etablissement.json | 40 +++++-- 12 files changed, 152 insertions(+), 59 deletions(-) diff --git a/lib/siade/api.rb b/lib/siade/api.rb index 777e0d2e3..620c5f627 100644 --- a/lib/siade/api.rb +++ b/lib/siade/api.rb @@ -7,22 +7,22 @@ class SIADE::API end def self.entreprise(siren) - endpoint = "/api/v1/entreprises/#{siren}" + endpoint = "/v2/entreprises/#{siren}" call(base_url + endpoint) end def self.etablissement(siret) - endpoint = "/api/v1/etablissements/#{siret}" + endpoint = "/v2/etablissements/#{siret}" call(base_url + endpoint) end def self.exercices(siret) - endpoint = "/api/v1/etablissements/exercices/#{siret}" + endpoint = "/v1/etablissements/exercices/#{siret}" call(base_url + endpoint) end def self.rna(siret) - endpoint = "/api/v1/associations/#{siret}" + endpoint = "/v1/associations/#{siret}" call(base_url + endpoint) end @@ -39,11 +39,9 @@ class SIADE::API def self.base_url if Rails.env.production? - 'https://apientreprise.fr' + 'https://api.apientreprise.fr' else 'https://api-dev.apientreprise.fr' end - - 'https://apientreprise.fr' end end diff --git a/lib/siade/etablissement_adapter.rb b/lib/siade/etablissement_adapter.rb index a042f878b..b03c3d821 100644 --- a/lib/siade/etablissement_adapter.rb +++ b/lib/siade/etablissement_adapter.rb @@ -30,7 +30,7 @@ class SIADE::EtablissementAdapter def adresse adresse = '' - [:l1, :l2, :l3, :l4, :l5].each do |line| + [:l1, :l2, :l3, :l4, :l5, :l6, :l7].each do |line| adresse = adresse + data_source[:etablissement][:adresse][line] + "\r\n" unless data_source[:etablissement][:adresse][line].nil? end adresse diff --git a/spec/controllers/users/dossiers_controller_spec.rb b/spec/controllers/users/dossiers_controller_spec.rb index 637f39710..55b311b8b 100644 --- a/spec/controllers/users/dossiers_controller_spec.rb +++ b/spec/controllers/users/dossiers_controller_spec.rb @@ -95,19 +95,19 @@ describe Users::DossiersController, type: :controller do describe 'POST #create' do before do - stub_request(:get, "https://apientreprise.fr/api/v1/etablissements/#{siret_not_found}?token=#{SIADETOKEN}") + stub_request(:get, "https://api-dev.apientreprise.fr/v2/etablissements/#{siret_not_found}?token=#{SIADETOKEN}") .to_return(status: 404, body: 'fake body') - stub_request(:get, "https://apientreprise.fr/api/v1/etablissements/#{siret}?token=#{SIADETOKEN}") + stub_request(:get, "https://api-dev.apientreprise.fr/v2/etablissements/#{siret}?token=#{SIADETOKEN}") .to_return(status: 200, body: File.read('spec/support/files/etablissement.json')) - stub_request(:get, "https://apientreprise.fr/api/v1/entreprises/#{siren}?token=#{SIADETOKEN}") + stub_request(:get, "https://api-dev.apientreprise.fr/v2/entreprises/#{siren}?token=#{SIADETOKEN}") .to_return(status: 200, body: File.read('spec/support/files/entreprise.json')) - stub_request(:get, "https://apientreprise.fr/api/v1/etablissements/exercices/#{siret}?token=#{SIADETOKEN}") + stub_request(:get, "https://api-dev.apientreprise.fr/v1/etablissements/exercices/#{siret}?token=#{SIADETOKEN}") .to_return(status: exercices_status, body: exercices_body) - stub_request(:get, "https://apientreprise.fr/api/v1/associations/#{siret}?token=#{SIADETOKEN}") + stub_request(:get, "https://api-dev.apientreprise.fr/v1/associations/#{siret}?token=#{SIADETOKEN}") .to_return(status: rna_status, body: rna_body) end diff --git a/spec/features/users/complete_demande_spec.rb b/spec/features/users/complete_demande_spec.rb index a1f8dc0f8..fb7a86e9f 100644 --- a/spec/features/users/complete_demande_spec.rb +++ b/spec/features/users/complete_demande_spec.rb @@ -28,14 +28,14 @@ feature 'user path for dossier creation' do end context 'sets siret' do before do - stub_request(:get, "https://apientreprise.fr/api/v1/etablissements/#{siret}?token=#{SIADETOKEN}") + stub_request(:get, "https://api-dev.apientreprise.fr/v2/etablissements/#{siret}?token=#{SIADETOKEN}") .to_return(body: File.read('spec/support/files/etablissement.json', status: 200)) - stub_request(:get, "https://apientreprise.fr/api/v1/entreprises/#{siren}?token=#{SIADETOKEN}") + stub_request(:get, "https://api-dev.apientreprise.fr/v2/entreprises/#{siren}?token=#{SIADETOKEN}") .to_return(status: 200, body: File.read('spec/support/files/entreprise.json')) - stub_request(:get, "https://apientreprise.fr/api/v1/etablissements/exercices/#{siret}?token=#{SIADETOKEN}") + stub_request(:get, "https://api-dev.apientreprise.fr/v1/etablissements/exercices/#{siret}?token=#{SIADETOKEN}") .to_return(status: 200, body: File.read('spec/support/files/exercices.json')) - stub_request(:get, "https://apientreprise.fr/api/v1/associations/#{siret}?token=#{SIADETOKEN}") + stub_request(:get, "https://api-dev.apientreprise.fr/v1/associations/#{siret}?token=#{SIADETOKEN}") .to_return(status: 404, body: '') page.find_by_id('dossier_siret').set siret diff --git a/spec/features/users/start_demande_spec.rb b/spec/features/users/start_demande_spec.rb index 480550df1..56c52d81b 100644 --- a/spec/features/users/start_demande_spec.rb +++ b/spec/features/users/start_demande_spec.rb @@ -26,13 +26,13 @@ feature 'user arrive on siret page' do end context 'when enter a siret' do before do - stub_request(:get, "https://apientreprise.fr/api/v1/etablissements/#{siret}?token=#{SIADETOKEN}") + stub_request(:get, "https://api-dev.apientreprise.fr/v2/etablissements/#{siret}?token=#{SIADETOKEN}") .to_return(status: 200, body: File.read('spec/support/files/etablissement.json')) - stub_request(:get, "https://apientreprise.fr/api/v1/entreprises/#{siren}?token=#{SIADETOKEN}") + stub_request(:get, "https://api-dev.apientreprise.fr/v2/entreprises/#{siren}?token=#{SIADETOKEN}") .to_return(status: 200, body: File.read('spec/support/files/entreprise.json')) - stub_request(:get, "https://apientreprise.fr/api/v1/etablissements/exercices/#{siret}?token=#{SIADETOKEN}") + stub_request(:get, "https://api-dev.apientreprise.fr/v1/etablissements/exercices/#{siret}?token=#{SIADETOKEN}") .to_return(status: 200, body: File.read('spec/support/files/exercices.json')) - stub_request(:get, "https://apientreprise.fr/api/v1/associations/#{siret}?token=#{SIADETOKEN}") + stub_request(:get, "https://api-dev.apientreprise.fr/v1/associations/#{siret}?token=#{SIADETOKEN}") .to_return(status: 404, body: '') page.find_by_id('dossier_siret').set siret diff --git a/spec/lib/siade/api_spec.rb b/spec/lib/siade/api_spec.rb index c66b676f2..a82a3e4d7 100644 --- a/spec/lib/siade/api_spec.rb +++ b/spec/lib/siade/api_spec.rb @@ -4,7 +4,7 @@ describe SIADE::API do describe '.entreprise' do subject { described_class.entreprise(siren) } before do - stub_request(:get, "https://apientreprise.fr/api/v1/entreprises/#{siren}?token=#{SIADETOKEN}") + stub_request(:get, "https://api-dev.apientreprise.fr/v2/entreprises/#{siren}?token=#{SIADETOKEN}") .to_return(status: status, body: body) end context 'when siren does not exist' do @@ -30,7 +30,7 @@ describe SIADE::API do describe '.etablissement' do subject { described_class.etablissement(siret) } before do - stub_request(:get, "https://apientreprise.fr/api/v1/etablissements/#{siret}?token=#{SIADETOKEN}") + stub_request(:get, "https://api-dev.apientreprise.fr/v2/etablissements/#{siret}?token=#{SIADETOKEN}") .to_return(status: status, body: body) end @@ -57,7 +57,7 @@ describe SIADE::API do describe '.exercices' do before do - stub_request(:get, /https:\/\/apientreprise.fr\/api\/v1\/etablissements\/exercices\/.*token=/) + stub_request(:get, /https:\/\/api-dev.apientreprise.fr\/v1\/etablissements\/exercices\/.*token=/) .to_return(status: status, body: body) end @@ -88,7 +88,7 @@ describe SIADE::API do describe '.rna' do before do - stub_request(:get, /https:\/\/apientreprise.fr\/api\/v1\/associations\/.*token=/) + stub_request(:get, /https:\/\/api-dev.apientreprise.fr\/v1\/associations\/.*token=/) .to_return(status: status, body: body) end diff --git a/spec/lib/siade/entreprise_adapter_spec.rb b/spec/lib/siade/entreprise_adapter_spec.rb index 66b96fa4b..3cfd96673 100644 --- a/spec/lib/siade/entreprise_adapter_spec.rb +++ b/spec/lib/siade/entreprise_adapter_spec.rb @@ -4,7 +4,7 @@ describe SIADE::EntrepriseAdapter do subject { described_class.new('418166096').to_params } before do - stub_request(:get, "https://apientreprise.fr/api/v1/entreprises/418166096?token=#{SIADETOKEN}") + stub_request(:get, "https://api-dev.apientreprise.fr/v2/entreprises/418166096?token=#{SIADETOKEN}") .to_return(body: File.read('spec/support/files/entreprise.json', status: 200)) end @@ -18,7 +18,7 @@ describe SIADE::EntrepriseAdapter do end it 'L\'entreprise contient bien un capital_social' do - expect(subject[:capital_social]).to eq(372_795) + expect(subject[:capital_social]).to eq(462308) end it 'L\'entreprise contient bien un numero_tva_intracommunautaire' do @@ -46,11 +46,11 @@ describe SIADE::EntrepriseAdapter do end it 'L\'entreprise contient bien un code_effectif_entreprise' do - expect(subject[:code_effectif_entreprise]).to eq('22') + expect(subject[:code_effectif_entreprise]).to eq('31') end it 'L\'entreprise contient bien une date_creation' do - expect(subject[:date_creation]).to eq('Mon, 01 Jan 1900 00:00:00.000000000 +0100') + expect(subject[:date_creation]).to eq('Wed, 01 Apr 1998 00:00:00.000000000 +0200') end it 'L\'entreprise contient bien un nom' do diff --git a/spec/lib/siade/etablissement_adapter_spec.rb b/spec/lib/siade/etablissement_adapter_spec.rb index 14a2b6f6a..82d0191ad 100644 --- a/spec/lib/siade/etablissement_adapter_spec.rb +++ b/spec/lib/siade/etablissement_adapter_spec.rb @@ -6,7 +6,7 @@ describe SIADE::EtablissementAdapter do subject { described_class.new(siret).to_params } before do - stub_request(:get, "https://apientreprise.fr/api/v1/etablissements/#{siret}?token=#{SIADETOKEN}") + stub_request(:get, "https://api-dev.apientreprise.fr/v2/etablissements/#{siret}?token=#{SIADETOKEN}") .to_return(body: File.read('spec/support/files/etablissement.json', status: 200)) end @@ -33,7 +33,7 @@ describe SIADE::EtablissementAdapter do context 'Concaténation lignes adresse' do it 'L\'entreprise contient bien une adresse sur plusieurs lignes' do - expect(subject[:adresse]).to eq("OCTO-TECHNOLOGY\r\n50 AV DES CHAMPS ELYSEES\r\n75008 PARIS 8\r\nligne 4\r\nligne 5\r\n") + expect(subject[:adresse]).to eq("OCTO TECHNOLOGY\r\n50 AVENUE DES CHAMPS ELYSEES\r\n75008 PARIS\r\nFRANCE\r\n") end end @@ -73,7 +73,7 @@ describe SIADE::EtablissementAdapter do subject { described_class.new(bad_siret).to_params } before do - stub_request(:get, "https://apientreprise.fr/api/v1/etablissements/#{bad_siret}?token=#{SIADETOKEN}") + stub_request(:get, "https://api-dev.apientreprise.fr/v2/etablissements/#{bad_siret}?token=#{SIADETOKEN}") .to_return(body: 'Fake body', status: 404) end diff --git a/spec/lib/siade/exercices_adapter_spec.rb b/spec/lib/siade/exercices_adapter_spec.rb index 9b8a7bc96..99cbb858d 100644 --- a/spec/lib/siade/exercices_adapter_spec.rb +++ b/spec/lib/siade/exercices_adapter_spec.rb @@ -5,7 +5,7 @@ describe SIADE::ExercicesAdapter do subject { described_class.new(siret).to_params } before do - stub_request(:get, /https:\/\/apientreprise.fr\/api\/v1\/etablissements\/exercices\/.*token=/) + stub_request(:get, /https:\/\/api-dev.apientreprise.fr\/v1\/etablissements\/exercices\/.*token=/) .to_return(body: File.read('spec/support/files/exercices.json', status: 200)) end diff --git a/spec/lib/siade/rna_adapter_spec.rb b/spec/lib/siade/rna_adapter_spec.rb index 1462ea76f..719049aa0 100644 --- a/spec/lib/siade/rna_adapter_spec.rb +++ b/spec/lib/siade/rna_adapter_spec.rb @@ -8,7 +8,7 @@ describe SIADE::RNAAdapter do subject { described_class.new(siret).to_params } before do - stub_request(:get, /https:\/\/apientreprise.fr\/api\/v1\/associations\/.*token=/) + stub_request(:get, /https:\/\/api-dev.apientreprise.fr\/v1\/associations\/.*token=/) .to_return(body: body, status: status) end diff --git a/spec/support/files/entreprise.json b/spec/support/files/entreprise.json index 841069450..0c4cbb294 100644 --- a/spec/support/files/entreprise.json +++ b/spec/support/files/entreprise.json @@ -1,24 +1,27 @@ { "entreprise": { "siren": "418166096", - "capital_social": 372795, + "capital_social": 462308, "numero_tva_intracommunautaire": "FR16418166096", "forme_juridique": "SA à directoire (s.a.i.)", "forme_juridique_code": "5699", - "nombre_etablissements_actifs": 1, "nom_commercial": "OCTO-TECHNOLOGY", "procedure_collective": false, "raison_sociale": "OCTO-TECHNOLOGY", "siret_siege_social": "41816609600051", - "code_effectif_entreprise": "22", - "date_creation": -2208992400, + "code_effectif_entreprise": "31", + "date_creation": 891381600, "nom": "test_nom", "prenom": "test_prenom", - "etat_administratif": { - "value": "Actif", - "date_mise_a_jour": 891381600 - }, "date_radiation": null, + "categorie_entreprise": "PME", + "tranche_effectif_salarie_entreprise": { + "de": 200, + "a": 249, + "code": "31", + "date_reference": "2014", + "intitule": "200 à 249 salariés" + }, "exercices": [ { "chiffre_affaires": null, @@ -46,7 +49,10 @@ "fonction": "PRESIDENT DU DIRECTOIRE", "dirigeant": true, "date_naissance": "1965-01-27", - "date_naissance_timestamp": -155523600 + "date_naissance_timestamp": -155523600, + "raison_sociale": "", + "identifiant": "", + "type": "PP" }, { "nom": "BONTE", @@ -54,7 +60,10 @@ "fonction": "PRESIDENT DU CONSEIL DE SURVEILLANCE", "dirigeant": true, "date_naissance": "1965-01-21", - "date_naissance_timestamp": -156042000 + "date_naissance_timestamp": -156042000, + "raison_sociale": "", + "identifiant": "", + "type": "PP" }, { "nom": "BOSQUE", @@ -62,7 +71,10 @@ "fonction": "VICE PRESIDENT ET MEMBRE DU CONSEIL DE SURVEILLANCE", "dirigeant": true, "date_naissance": "1970-12-31", - "date_naissance_timestamp": 31446000 + "date_naissance_timestamp": 31446000, + "raison_sociale": "", + "identifiant": "", + "type": "PP" }, { "nom": "CINQUIN", @@ -70,7 +82,10 @@ "fonction": "MEMBRE DU DIRECTOIRE", "dirigeant": true, "date_naissance": "1972-01-25", - "date_naissance_timestamp": 65142000 + "date_naissance_timestamp": 65142000, + "raison_sociale": "", + "identifiant": "", + "type": "PP" }, { "nom": "LUCAS", @@ -78,7 +93,10 @@ "fonction": "MEMBRE DU CONSEIL DE SURVEILLANCE", "dirigeant": true, "date_naissance": "1964-12-02", - "date_naissance_timestamp": -160362000 + "date_naissance_timestamp": -160362000, + "raison_sociale": "", + "identifiant": "", + "type": "PP" }, { "nom": "DEGONSE", @@ -86,7 +104,10 @@ "fonction": "MEMBRE DU CONSEIL DE SURVEILLANCE", "dirigeant": true, "date_naissance": "1947-07-03", - "date_naissance_timestamp": -710038800 + "date_naissance_timestamp": -710038800, + "raison_sociale": "", + "identifiant": "", + "type": "PP" }, { "nom": "PLANTIN", @@ -94,7 +115,10 @@ "fonction": "COMMISSAIRE AUX COMPTES TITULAIRE", "dirigeant": true, "date_naissance": "1959-01-27", - "date_naissance_timestamp": -344912400 + "date_naissance_timestamp": -344912400, + "raison_sociale": "", + "identifiant": "", + "type": "PP" }, { "nom": "", @@ -102,9 +126,58 @@ "fonction": "COMMISSAIRE AUX COMPTES SUPPLEANT", "dirigeant": true, "date_naissance": "", - "date_naissance_timestamp": 0 + "date_naissance_timestamp": 0, + "raison_sociale": "BCRH \u0026 ASSOCIES - SOCIETE A RESPONSABILITE LIMITEE A ASSOCIE UNIQUE", + "identifiant": "490092574", + "type": "PM" } ] }, + "etablissement_siege": { + "siege_social": true, + "siret": "41816609600051", + "naf": "6202A", + "libelle_naf": "Conseil en systèmes et logiciels informatiques", + "date_mise_a_jour": 1449183600, + "tranche_effectif_salarie_etablissement": { + "de": 200, + "a": 249, + "code": "31", + "date_reference": "2014", + "intitule": "200 à 249 salariés" + }, + "date_creation_etablissement": 1108594800, + "enseigne": null, + "region_implantation": { + "code": "11", + "value": "Île-de-France" + }, + "commune_implantation": { + "code": "75108", + "value": "PARIS 8" + }, + "pays_implantation": { + "code": null, + "value": null + }, + "diffusable_commercialement": null, + "adresse": { + "l1": "OCTO TECHNOLOGY", + "l2": null, + "l3": null, + "l4": "50 AVENUE DES CHAMPS ELYSEES", + "l5": null, + "l6": "75008 PARIS", + "l7": "FRANCE", + "numero_voie": "50", + "type_voie": "AV", + "nom_voie": "DES CHAMPS ELYSEES", + "complement_adresse": null, + "code_postal": "75008", + "localite": "PARIS 8", + "code_insee_localite": "75108", + "cedex": null + } + }, "gateway_error": false } \ No newline at end of file diff --git a/spec/support/files/etablissement.json b/spec/support/files/etablissement.json index 2bcffdb96..6dc828828 100644 --- a/spec/support/files/etablissement.json +++ b/spec/support/files/etablissement.json @@ -4,23 +4,45 @@ "siret": "41816609600051", "naf": "6202A", "libelle_naf": "Conseil en systèmes et logiciels informatiques", - "etat_administratif_etablissement": { - "value": "Actif", - "date_mise_a_jour": 1108594800 + "date_mise_a_jour": 1449183600, + "tranche_effectif_salarie_etablissement": { + "de": 200, + "a": 249, + "code": "31", + "date_reference": "2014", + "intitule": "200 à 249 salariés" }, + "date_creation_etablissement": 1108594800, + "enseigne": null, + "region_implantation": { + "code": "11", + "value": "Île-de-France" + }, + "commune_implantation": { + "code": "75108", + "value": "PARIS 8" + }, + "pays_implantation": { + "code": null, + "value": null + }, + "diffusable_commercialement": null, "adresse": { - "l1": "OCTO-TECHNOLOGY", - "l2": "50 AV DES CHAMPS ELYSEES", - "l3": "75008 PARIS 8", - "l4": "ligne 4", - "l5": "ligne 5", + "l1": "OCTO TECHNOLOGY", + "l2": null, + "l3": null, + "l4": "50 AVENUE DES CHAMPS ELYSEES", + "l5": null, + "l6": "75008 PARIS", + "l7": "FRANCE", "numero_voie": "50", "type_voie": "AV", "nom_voie": "DES CHAMPS ELYSEES", "complement_adresse": "complement_adresse", "code_postal": "75008", "localite": "PARIS 8", - "code_insee_localite": "75108" + "code_insee_localite": "75108", + "cedex": null } }, "gateway_error": false From 14142d9433e635abba90f2e2e010cf00e337952d Mon Sep 17 00:00:00 2001 From: Xavier J Date: Mon, 13 Jun 2016 11:04:50 +0200 Subject: [PATCH 19/33] Dix published and archive method in procedure controller --- app/controllers/admin/procedures_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/admin/procedures_controller.rb b/app/controllers/admin/procedures_controller.rb index bf5a137e3..461cf9ce0 100644 --- a/app/controllers/admin/procedures_controller.rb +++ b/app/controllers/admin/procedures_controller.rb @@ -76,11 +76,11 @@ class Admin::ProceduresController < AdminController end def publish - change_status({published: params[:published]}) + change_status({published: true}) end def archive - change_status({archived: params[:archive]}) + change_status({archived: true}) end def active_class From fdee7154cf07b85529e802aefe2a4bf901bfd1c6 Mon Sep 17 00:00:00 2001 From: Xavier J Date: Mon, 13 Jun 2016 11:15:25 +0200 Subject: [PATCH 20/33] Fix error Github recuperation --- lib/github/releases.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/github/releases.rb b/lib/github/releases.rb index f71449b66..ec8ef101b 100644 --- a/lib/github/releases.rb +++ b/lib/github/releases.rb @@ -1,10 +1,10 @@ class Github::Releases def self.latest - release = Hashie::Mash.new JSON.parse(Github::API.latest_release) - - return nil if release.nil? + latest_release = Github::API.latest_release + return nil if latest_release.nil? + release = Hashie::Mash.new JSON.parse(latest_release) release.published_at = release.published_at.to_date.strftime('%d/%m/%Y') release end From 866565495d135ccaf212cca6a42c143a82f8d81f Mon Sep 17 00:00:00 2001 From: Xavier J Date: Mon, 13 Jun 2016 12:03:05 +0200 Subject: [PATCH 21/33] Delete default description in dossier --- .../admin/procedures_controller.rb | 2 +- .../users/description_controller.rb | 2 +- app/models/dossier.rb | 1 - app/serializers/dossier_serializer.rb | 1 - app/views/admin/procedures/_onglets.html.haml | 2 +- app/views/dossiers/_infos_dossier.html.haml | 3 - app/views/users/description/_champs.html.haml | 8 ++- app/views/users/description/_show.html.haml | 5 -- ...7_delete_default_description_to_dossier.rb | 62 +++++++++++++++++++ db/schema.rb | 3 +- .../api/v1/dossiers_controller_spec.rb | 3 +- .../users/description_controller_spec.rb | 6 -- spec/factories/dossier.rb | 1 - .../upload_piece_justificative_spec.rb | 1 - spec/features/users/complete_demande_spec.rb | 1 - spec/models/dossier_spec.rb | 12 +--- .../previsualisations/show.html.haml_spec.rb | 9 --- .../users/description/show.html.haml_spec.rb | 9 --- 18 files changed, 75 insertions(+), 56 deletions(-) create mode 100644 db/migrate/20160609145737_delete_default_description_to_dossier.rb diff --git a/app/controllers/admin/procedures_controller.rb b/app/controllers/admin/procedures_controller.rb index 461cf9ce0..4539cc02f 100644 --- a/app/controllers/admin/procedures_controller.rb +++ b/app/controllers/admin/procedures_controller.rb @@ -80,7 +80,7 @@ class Admin::ProceduresController < AdminController end def archive - change_status({archived: true}) + change_status({archived: params[:archive]}) end def active_class diff --git a/app/controllers/users/description_controller.rb b/app/controllers/users/description_controller.rb index fb6ecb930..ea9a9954f 100644 --- a/app/controllers/users/description_controller.rb +++ b/app/controllers/users/description_controller.rb @@ -94,6 +94,6 @@ class Users::DescriptionController < UsersController private def create_params - params.permit(:nom_projet, :description) + params.permit(:nom_projet) end end diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 900b71fcb..7c00a303f 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -30,7 +30,6 @@ class Dossier < ActiveRecord::Base after_save :build_default_champs, if: Proc.new { procedure_id_changed? } validates :nom_projet, presence: true, allow_blank: false, allow_nil: true - validates :description, presence: true, allow_blank: false, allow_nil: true validates :user, presence: true WAITING_FOR_GESTIONNAIRE = %w(initiated updated submitted) diff --git a/app/serializers/dossier_serializer.rb b/app/serializers/dossier_serializer.rb index ad772388d..90b391f66 100644 --- a/app/serializers/dossier_serializer.rb +++ b/app/serializers/dossier_serializer.rb @@ -1,7 +1,6 @@ class DossierSerializer < ActiveModel::Serializer attributes :id, :nom_projet, - :description, :created_at, :updated_at, :archived, diff --git a/app/views/admin/procedures/_onglets.html.haml b/app/views/admin/procedures/_onglets.html.haml index 5c8e0a8c0..99835a39b 100644 --- a/app/views/admin/procedures/_onglets.html.haml +++ b/app/views/admin/procedures/_onglets.html.haml @@ -2,7 +2,7 @@ %ul.nav.nav-tabs %li{class: @draft_class} %a{:href => "#{url_for :admin_procedures_draft}"} - %h5{style: 'color: black'} + %h5.text-primary ="Brouillons" %li{class: @active_class} diff --git a/app/views/dossiers/_infos_dossier.html.haml b/app/views/dossiers/_infos_dossier.html.haml index a477395b5..f916f5dcb 100644 --- a/app/views/dossiers/_infos_dossier.html.haml +++ b/app/views/dossiers/_infos_dossier.html.haml @@ -6,9 +6,6 @@ %h4 = @facade.dossier.procedure.libelle - .description - = h @facade.dossier.description.html_safe - - if @facade.dossier.mandataire_social && gestionnaire_signed_in? .mandataire_social.text-success.center %br diff --git a/app/views/users/description/_champs.html.haml b/app/views/users/description/_champs.html.haml index 464572686..db6decbf3 100644 --- a/app/views/users/description/_champs.html.haml +++ b/app/views/users/description/_champs.html.haml @@ -15,9 +15,10 @@ = '*' -if champ.type_champ == 'textarea' - %textarea.form-control{name:"champs['#{champ.id}']", - placeholder: champ.libelle, - id: "champs_#{champ.id}"} + %textarea.form-control.wysihtml5{name:"champs['#{champ.id}']", + placeholder: champ.description, + id: "champs_#{champ.id}", + row: '6'} =champ.value -elsif champ.type_champ == 'civilite' %label.radio-inline @@ -36,6 +37,7 @@ type: champ.type_champ, 'data-provide' => champ.data_provide, 'data-date-format' => ('dd/mm/yyyy' if champ.type_champ == 'datetime')} + - unless champ.description.empty? .row .col-lg-8.col-md-8{class: 'description_div', id:"description_champs_#{champ.id}"} diff --git a/app/views/users/description/_show.html.haml b/app/views/users/description/_show.html.haml index a42e114b0..c0db91dc6 100644 --- a/app/views/users/description/_show.html.haml +++ b/app/views/users/description/_show.html.haml @@ -12,11 +12,6 @@ .col-md-12 %h4 Libellé pour votre dossier * = text_field_tag :nom_projet, @dossier.nom_projet, placeholder: 'Nom du projet', class: 'form-control' - %br - .row - .col-md-12 - %h4 Description * - = text_area_tag :description, @dossier.description, rows: '6', placeholder: 'Description du projet', class: 'form-control wysihtml5' #liste_champs -unless @champs.nil? diff --git a/db/migrate/20160609145737_delete_default_description_to_dossier.rb b/db/migrate/20160609145737_delete_default_description_to_dossier.rb new file mode 100644 index 000000000..185e5ff4d --- /dev/null +++ b/db/migrate/20160609145737_delete_default_description_to_dossier.rb @@ -0,0 +1,62 @@ +class DeleteDefaultDescriptionToDossier < ActiveRecord::Migration + class Dossier < ActiveRecord::Base + + end + + class Champ < ActiveRecord::Base + + end + + class Procedure < ActiveRecord::Base + + end + + class TypeDeChamp < ActiveRecord::Base + + end + + def up + Procedure.all.each do |procedure| + #change all type_de_champ place_order by +1 to insert new type_de_champ description on first place + TypeDeChamp.where(procedure_id: procedure.id).each do |type_de_champ| + type_de_champ.order_place += 1 + type_de_champ.save + end + + #insert type_de_champ description on first place + TypeDeChamp.create(libelle: 'Description', description: 'Description de votre demande', type_champ: 'textarea', order_place: 0, procedure_id: procedure.id, mandatory: true) + end + + Dossier.all.each do |dossier| + #get the new type de champ + new_type_de_champ = TypeDeChamp.where(libelle: 'Description', type_champ: 'textarea', order_place: 0, procedure_id: dossier.procedure_id, mandatory: true) + + #create a new champ with the actual description value + Champ.create(value: dossier.description, type_de_champ_id: new_type_de_champ.first.id, dossier_id: dossier.id) + end + + remove_column :dossiers, :description + end + + def down + add_column :dossiers, :description, :text + + TypeDeChamp.where(libelle: 'Description', type_champ: 'textarea', order_place: 0, mandatory: true).each do |type_de_champ| + Champ.where(type_de_champ_id: type_de_champ.id).each do |champ| + dossier = Dossier.find(champ.dossier_id) + dossier.description = champ.value + dossier.save + + champ.delete + end + + procedure_id = type_de_champ.procedure_id + type_de_champ.delete + + TypeDeChamp.where(procedure_id: procedure_id).each do |type_de_champ_2| + type_de_champ_2.order_place -= 1 + type_de_champ_2.save + end + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 4839009ec..c990bd982 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: 20160609125949) do +ActiveRecord::Schema.define(version: 20160609145737) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -113,7 +113,6 @@ ActiveRecord::Schema.define(version: 20160609125949) do add_index "commentaires", ["dossier_id"], name: "index_commentaires_on_dossier_id", using: :btree create_table "dossiers", force: :cascade do |t| - t.string "description" t.boolean "autorisation_donnees" t.string "nom_projet" t.integer "procedure_id" diff --git a/spec/controllers/api/v1/dossiers_controller_spec.rb b/spec/controllers/api/v1/dossiers_controller_spec.rb index ad06ed0ac..83518f772 100644 --- a/spec/controllers/api/v1/dossiers_controller_spec.rb +++ b/spec/controllers/api/v1/dossiers_controller_spec.rb @@ -116,7 +116,7 @@ describe API::V1::DossiersController do let!(:dossier) { Timecop.freeze(date_creation) { create(:dossier, :with_entreprise, procedure: procedure) } } let(:dossier_id) { dossier.id } let(:body) { JSON.parse(retour.body, symbolize_names: true) } - let(:field_list) { [:id, :nom_projet, :created_at, :updated_at, :description, :archived, :mandataire_social, :entreprise, :etablissement, :cerfa, :types_de_piece_justificative, :pieces_justificatives, :champs, :commentaires] } + let(:field_list) { [:id, :nom_projet, :created_at, :updated_at, :archived, :mandataire_social, :entreprise, :etablissement, :cerfa, :types_de_piece_justificative, :pieces_justificatives, :champs, :commentaires] } subject { body[:dossier] } it 'return REST code 200', :show_in_doc do @@ -126,7 +126,6 @@ describe API::V1::DossiersController do it { expect(subject[:nom_projet]).to eq(dossier.nom_projet) } it { expect(subject[:created_at]).to eq('2008-09-01T08:05:00.000Z') } it { expect(subject[:updated_at]).to eq('2008-09-01T08:05:00.000Z') } - it { expect(subject[:description]).to eq(dossier.description) } it { expect(subject[:archived]).to eq(dossier.archived) } it { expect(subject[:mandataire_social]).to eq(dossier.mandataire_social) } diff --git a/spec/controllers/users/description_controller_spec.rb b/spec/controllers/users/description_controller_spec.rb index 2e6c17e65..29c8028d8 100644 --- a/spec/controllers/users/description_controller_spec.rb +++ b/spec/controllers/users/description_controller_spec.rb @@ -116,12 +116,6 @@ describe Users::DescriptionController, type: :controller, vcr: { cassette_name: it { is_expected.to render_template(:show) } it { expect(flash[:alert]).to be_present } end - - context 'description empty' do - let(:description) { '' } - it { is_expected.to render_template(:show) } - it { expect(flash[:alert]).to be_present } - end end context 'Quand la procédure accepte les CERFA' do diff --git a/spec/factories/dossier.rb b/spec/factories/dossier.rb index a36447b1a..3919ecccd 100644 --- a/spec/factories/dossier.rb +++ b/spec/factories/dossier.rb @@ -1,7 +1,6 @@ FactoryGirl.define do factory :dossier do nom_projet "Demande de subvention dans le cadre d'accompagnement d'enfant à l'étranger" - description "Ma super description" state 'draft' association :user, factory: [:user] diff --git a/spec/features/description_page/upload_piece_justificative_spec.rb b/spec/features/description_page/upload_piece_justificative_spec.rb index 3ec0e57c7..89b14e15a 100644 --- a/spec/features/description_page/upload_piece_justificative_spec.rb +++ b/spec/features/description_page/upload_piece_justificative_spec.rb @@ -22,7 +22,6 @@ feature 'user is on description page' do context 'he fill description fields' do before do find_by_id('nom_projet').set 'mon nom' - find_by_id('description').set 'ma description' end context 'before submit' do it 'dossier cerfa is empty' do diff --git a/spec/features/users/complete_demande_spec.rb b/spec/features/users/complete_demande_spec.rb index fb7a86e9f..0f219c031 100644 --- a/spec/features/users/complete_demande_spec.rb +++ b/spec/features/users/complete_demande_spec.rb @@ -55,7 +55,6 @@ feature 'user path for dossier creation' do context 'user fill and validate description page' do before do page.find_by_id('nom_projet').set 'Mon super projet' - page.find_by_id('description').set 'Ma super description' page.click_on 'Soumettre mon dossier' end scenario 'user is on recap page' do diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index e9e8d0a4a..0952b3592 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -3,7 +3,6 @@ require 'spec_helper' describe Dossier do let(:user) { create(:user) } describe 'database columns' do - it { is_expected.to have_db_column(:description) } it { is_expected.to have_db_column(:autorisation_donnees) } it { is_expected.to have_db_column(:nom_projet) } it { is_expected.to have_db_column(:created_at) } @@ -40,11 +39,6 @@ describe Dossier do it { is_expected.not_to allow_value('').for(:nom_projet) } it { is_expected.to allow_value('mon super projet').for(:nom_projet) } end - context 'description' do - it { is_expected.to allow_value(nil).for(:description) } - it { is_expected.not_to allow_value('').for(:description) } - it { is_expected.to allow_value('ma superbe description').for(:description) } - end end describe 'methods' do @@ -115,6 +109,7 @@ describe Dossier do describe '#save' do subject { build(:dossier, procedure: procedure, user: user) } let!(:procedure) { create(:procedure) } + context 'when is linked to a procedure' do it 'creates default champs' do expect(subject).to receive(:build_default_champs) @@ -122,11 +117,11 @@ describe Dossier do end end context 'when is not linked to a procedure' do - subject { create(:dossier, procedure: procedure, user: user) } + subject { create(:dossier, procedure: nil, user: user) } it 'does not create default champs' do expect(subject).not_to receive(:build_default_champs) - subject.update_attributes(description: 'plop') + subject.update_attributes(nom_projet: 'plop') end end end @@ -541,7 +536,6 @@ describe Dossier do subject { dossier.as_csv } it { expect(subject[:nom_projet]).to eq("Demande de subvention dans le cadre d'accompagnement d'enfant à l'étranger") } - it { expect(subject[:description]).to eq("Ma super description") } it { expect(subject[:archived]).to be_falsey } it { expect(subject['etablissement.siret']).to eq('44011762001530') } it { expect(subject['etablissement.siege_social']).to be_truthy } diff --git a/spec/views/admin/previsualisations/show.html.haml_spec.rb b/spec/views/admin/previsualisations/show.html.haml_spec.rb index 06fc2a884..4d97de3a8 100644 --- a/spec/views/admin/previsualisations/show.html.haml_spec.rb +++ b/spec/views/admin/previsualisations/show.html.haml_spec.rb @@ -25,10 +25,6 @@ describe 'admin/previsualisations/show.html.haml', type: :view do expect(rendered).to have_selector('input[id=nom_projet][name=nom_projet]') end - it 'Description du projet' do - expect(rendered).to have_selector('textarea[id=description][name=description]') - end - it 'Charger votre CERFA (PDF)' do expect(rendered).to have_selector('input[type=file][name=cerfa_pdf][id=cerfa_pdf]') end @@ -71,7 +67,6 @@ describe 'admin/previsualisations/show.html.haml', type: :view do let!(:dossier) do create(:dossier, nom_projet: 'Projet de test', - description: 'Description de test', user: user) end @@ -82,10 +77,6 @@ describe 'admin/previsualisations/show.html.haml', type: :view do it 'Nom du projet' do expect(rendered).to have_selector("input[id=nom_projet][value='#{dossier.nom_projet}']") end - - it 'Description du projet' do - expect(rendered).to have_content("#{dossier.description}") - end end context 'Champs' do diff --git a/spec/views/users/description/show.html.haml_spec.rb b/spec/views/users/description/show.html.haml_spec.rb index e6c123b9c..04e256317 100644 --- a/spec/views/users/description/show.html.haml_spec.rb +++ b/spec/views/users/description/show.html.haml_spec.rb @@ -26,10 +26,6 @@ describe 'users/description/show.html.haml', type: :view do expect(rendered).to have_selector('input[id=nom_projet][name=nom_projet]') end - it 'Description du projet' do - expect(rendered).to have_selector('textarea[id=description][name=description]') - end - it 'Charger votre CERFA (PDF)' do expect(rendered).to have_selector('input[type=file][name=cerfa_pdf][id=cerfa_pdf]') end @@ -72,7 +68,6 @@ describe 'users/description/show.html.haml', type: :view do let!(:dossier) do create(:dossier, nom_projet: 'Projet de test', - description: 'Description de test', user: user) end @@ -83,10 +78,6 @@ describe 'users/description/show.html.haml', type: :view do it 'Nom du projet' do expect(rendered).to have_selector("input[id=nom_projet][value='#{dossier.nom_projet}']") end - - it 'Description du projet' do - expect(rendered).to have_content("#{dossier.description}") - end end context 'Champs' do From bbae7214c99ca85b0929bba285e3bc621274047d Mon Sep 17 00:00:00 2001 From: Xavier J Date: Tue, 14 Jun 2016 11:00:57 +0200 Subject: [PATCH 22/33] New UI for the map --- app/assets/images/edit.png | Bin 0 -> 1078 bytes app/assets/images/pencil.png | Bin 0 -> 1226 bytes app/assets/javascripts/carte/carte.js | 18 +++++++++++++----- app/assets/stylesheets/carte.scss | 12 +++++++++--- app/views/users/carte/show.html.haml | 16 ++++++++++------ 5 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 app/assets/images/edit.png create mode 100644 app/assets/images/pencil.png diff --git a/app/assets/images/edit.png b/app/assets/images/edit.png new file mode 100644 index 0000000000000000000000000000000000000000..3f82d738be7c384e3a678d3aa112ec64bb350e08 GIT binary patch literal 1078 zcmeAS@N?(olHy`uVBq!ia0vp^{2!lvI6-E$sR$z z3=CCj3=9n|3=F@3LJcn%7)lKo7+xhXFj&oCU=S~uvn$XBD8X6a5n0T@z%2~Ij105p zNH8!kMrMXYltlRYSS9D@>LsS+C#C9DVstT4fPE4;bsH1+JHo@{EISEfi{E8 zw==W>t3(ll+GC>+vK+}V5TAlYfnK%aveAbJn;nV>NV%>S(Dyzq)etxFhO@PI*<^Of3g%<=Hi+0{Qn_=>6 z-nTh3&ZappzIYcZALVTi7VB<3nqu_#RoU*t1r{~cJ7b=uMDEMl z7}2-oV8VwrSF`rII0(2-+^ynSv-FMG?6>J!Q&06Cf6VT-I52K)nB!bObB3L2RW@?l zZssiWTmJcT)!u6#%3LNMG%2v0>o@(?*I%87AMRjQayFL_thJHhvlpCp^zq01Ej!Qr zsrxUiVsZZY=lp<~H{X5}J~WX{D0kcKwe!Lx=5AfZowxlr>z_8a#gV2`y}_%mR&gEN z7t{Q;YA?4R+l+%JBX-7wZOE&&yKg-GbZL)z?(MhVVoM72C!egzHJfd(uxj?=PKKBJ zSF_LKIU=-`>jS&Sd8Wn#%DdbTme1huRuMX0vP|scx2d8NWd7Fux7?(?`R192U#q+= z_CHcMCeopC!Q$l}b#9KNAf?%U%eOCBRckk2-(e5`qwEJI1(M(A@bp#~`l*Tau%Ai$ zERRtPXT0ZVp4u-iLH_n z$Rap^xU(cP4PjGWG1OZ?59)(t^bPe4^x)0=OOi#$y1&vX?y1|^y6G_}lfkr(-LdeMtlI{B3+tuvh` ztt@^SV)k*lWU>X#V;o3tQp-G zXQ}7Uo9Fi7RsqY}IIelx{0|BiTw1a4&8tJZ7O=D&iGOl~%VN_ryHolsg`eBZ=W{PU z$ujxo5izxlV40-gc*px|UGg(GUVgU5bLShei~lrST?!=bRM_aa@$UICwcw`Z(d=4{ zW0O8;thrTwC$U)XGV7%cYd=azNFCWx$29rxdPB<)&b@py*35h45_9TcLG;?-XJO5U ztlc~O*ULE8DHn({bkE4_{A~C<>X*s45VY}!rSC+onHtWBz&%=e){Y6UWz1J4F_M6Lv$RBF_yU{@6%Z0418M(jN=K8V6%gaAg z)k=L>_SP-2nD=S8W#H-Jg7w#gFJ3eh=xh<-YBi8JyP`_JUBz6w#^1k0O!VN?PQK<$ zqtJMH&$9x_=htcM*uB#+-@q~OcEjx1=1wp6i+Ok?%(YWff4qO`hOO~0pC3OtE#~<1 zRaSpwE-ZK&%(`xEBxCtx!8Fm!RjR56y}bu*?B31UsOovVoVBvhp8t75Tf=$w^M{+~ zw)`~OrZttr+k3YB3^j?oETykWhGsv0A3l7#yYl(ng%>h5*cl5q9dLLSzOg~k+f(t@ zG_fB>?Ju;16t%2pw+20FIv{dEaq`5#tx|T1iX0uC8$Z=Czq?!PGhsn{9jMgtboFyt I=akR{0J4JpZ2$lO literal 0 HcmV?d00001 diff --git a/app/assets/javascripts/carte/carte.js b/app/assets/javascripts/carte/carte.js index 90ebef7b4..1e9ba0514 100644 --- a/app/assets/javascripts/carte/carte.js +++ b/app/assets/javascripts/carte/carte.js @@ -93,22 +93,30 @@ function add_event_freeDraw() { freeDraw.on('markers', function (e) { $("#json_latlngs").val(JSON.stringify(e.latLngs)); + add_event_edit(); + get_external_data(e.latLngs); }); + $("#map").on('click', function(){ + freeDraw.setMode(L.FreeDraw.MODES.VIEW); + }); + $("#new").on('click', function (e) { freeDraw.setMode(L.FreeDraw.MODES.CREATE); }); - $("#edit").on('click', function (e) { - freeDraw.setMode(L.FreeDraw.MODES.EDIT); - }); - $("#delete").on('click', function (e) { freeDraw.setMode(L.FreeDraw.MODES.DELETE); }); } +function add_event_edit (){ + $(".leaflet-container g path").on('click', function (e) { + setTimeout(function(){freeDraw.setMode(L.FreeDraw.MODES.EDIT | L.FreeDraw.MODES.DELETE)}, 50); + }); +} + function get_position() { var position; @@ -131,7 +139,7 @@ function get_address_point(request) { }).done(function (data) { if (data.lat != null) { map.setView(new L.LatLng(data.lat, data.lon), data.zoom); - L.marker([data.lat, data.lon], {icon: icon}).addTo(map); + //L.marker([data.lat, data.lon], {icon: icon}).addTo(map); } }); } diff --git a/app/assets/stylesheets/carte.scss b/app/assets/stylesheets/carte.scss index ebbb98dc7..274d502ea 100644 --- a/app/assets/stylesheets/carte.scss +++ b/app/assets/stylesheets/carte.scss @@ -12,12 +12,16 @@ table { @extend .col-md-12; @extend .col-lg-12; + margin-left: 15px; + width: 90%; height: 600px; } #map.qp, #map.cadastre { @extend .col-md-9; @extend .col-lg-9; + + width: 70%; } .list { @@ -35,6 +39,10 @@ table { } } +.leaflet-container path { + cursor: url('/assets/edit.png'), default !important; +} + #infos_dossier { #map.mini { height: 300px; @@ -42,8 +50,6 @@ table { } } - - #map section.overlay { position: absolute; top: 0; @@ -56,7 +62,7 @@ table { } #map.mode-create { - cursor: crosshair !important; + cursor: url('/assets/pencil.png'), crosshair !important; } #map g path.tracer { diff --git a/app/views/users/carte/show.html.haml b/app/views/users/carte/show.html.haml index 75e0f6311..877279e05 100644 --- a/app/views/users/carte/show.html.haml +++ b/app/views/users/carte/show.html.haml @@ -1,13 +1,17 @@ %h2 - ='Localisation de votre demande' + ='Localisation' +%h4.text-primary + Positionnez-vous et dessinez sur la carte la zone d'action de votre demande. %br .content{style:'margin-bottom:60px'} - %button#new.btn.btn-sm.btn-success{type:'button'} Nouveau - \- - %button#edit.btn.btn-sm.btn-info{type:'button'} Editer - \- - %button#delete.btn.btn-sm.btn-danger{type:'button'} Supprimer + %button#new.btn.btn-md.btn-success{type:'button'} + %i.fa.fa-pencil + Ajouter une zone + -#\- + -#%button#edit.btn.btn-sm.btn-info{type:'button'} Editer + -#\- + -#%button#delete.btn.btn-sm.btn-danger{type:'button'} Supprimer %span#search_by_address{style: 'margin-left: 20px'} %input.form-control{type: :address, placeholder: 'Rechercher une adresse'} From 954ac2bc8695bb878e479f1e5ef2ec7284768845 Mon Sep 17 00:00:00 2001 From: Xavier J Date: Tue, 14 Jun 2016 11:59:50 +0200 Subject: [PATCH 23/33] Fix bug revisualization controller --- .../admin/previsualisations_controller.rb | 2 +- app/services/previsualisation_service.rb | 5 +++ .../services/previsualisation_service_spec.rb | 31 +++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 app/services/previsualisation_service.rb create mode 100644 spec/services/previsualisation_service_spec.rb diff --git a/app/controllers/admin/previsualisations_controller.rb b/app/controllers/admin/previsualisations_controller.rb index 22f39d343..a5c873e7f 100644 --- a/app/controllers/admin/previsualisations_controller.rb +++ b/app/controllers/admin/previsualisations_controller.rb @@ -5,7 +5,7 @@ class Admin::PrevisualisationsController < AdminController @procedure @dossier = Dossier.new(id: 0, procedure: @procedure) - Champ.where(dossier_id: @dossier.id).destroy_all + PrevisualisationService.destroy_all_champs @dossier @dossier.build_default_champs @champs = @dossier.ordered_champs diff --git a/app/services/previsualisation_service.rb b/app/services/previsualisation_service.rb new file mode 100644 index 000000000..316c0a4cd --- /dev/null +++ b/app/services/previsualisation_service.rb @@ -0,0 +1,5 @@ +class PrevisualisationService + def self.destroy_all_champs dossier + Champ.where(dossier_id: dossier.id, type_de_champ_id: dossier.procedure.types_de_champ.ids).destroy_all + end +end \ No newline at end of file diff --git a/spec/services/previsualisation_service_spec.rb b/spec/services/previsualisation_service_spec.rb new file mode 100644 index 000000000..2fcc4ee00 --- /dev/null +++ b/spec/services/previsualisation_service_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper' + +describe PrevisualisationService do + + describe '.destroy_all_champs' do + subject { described_class.destroy_all_champs dossier } + + let(:procedure_1) { create :procedure, :with_type_de_champ } + let(:procedure_2) { create :procedure, :with_type_de_champ } + + let!(:dossier_1) { create :dossier, procedure: procedure_1 } + let!(:dossier_2) { create :dossier, procedure: procedure_2 } + + it { expect(Dossier.all.size).to eq 2 } + it { expect(TypeDeChamp.all.size).to eq 2 } + it { expect(Champ.all.size).to eq 2 } + + context 'when function destroy_all_champs is call' do + let(:dossier) { dossier_1 } + + before do + subject + end + + it { expect(Dossier.all.size).to eq 2 } + it { expect(TypeDeChamp.all.size).to eq 2 } + it { expect(Champ.all.size).to eq 1 } + it { expect(Champ.first.type_de_champ).to eq procedure_2.types_de_champ.first } + end + end +end \ No newline at end of file From eb1e4fe7ce09cc696851b9ff0fe8695b33e19da5 Mon Sep 17 00:00:00 2001 From: Xavier J Date: Tue, 14 Jun 2016 12:32:27 +0200 Subject: [PATCH 24/33] Update migration DeleteDefaultDescriptionToDossier --- .../20160609145737_delete_default_description_to_dossier.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/db/migrate/20160609145737_delete_default_description_to_dossier.rb b/db/migrate/20160609145737_delete_default_description_to_dossier.rb index 185e5ff4d..ded5122e7 100644 --- a/db/migrate/20160609145737_delete_default_description_to_dossier.rb +++ b/db/migrate/20160609145737_delete_default_description_to_dossier.rb @@ -41,6 +41,8 @@ class DeleteDefaultDescriptionToDossier < ActiveRecord::Migration def down add_column :dossiers, :description, :text + Champ.destroy_all(dossier_id: 0) + TypeDeChamp.where(libelle: 'Description', type_champ: 'textarea', order_place: 0, mandatory: true).each do |type_de_champ| Champ.where(type_de_champ_id: type_de_champ.id).each do |champ| dossier = Dossier.find(champ.dossier_id) From 5c2902bda5cdb03b86a45e1ce97e804dad5742c4 Mon Sep 17 00:00:00 2001 From: Xavier J Date: Tue, 14 Jun 2016 15:21:32 +0200 Subject: [PATCH 25/33] Filter procedure on /demo page for staging --- app/controllers/demo_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/demo_controller.rb b/app/controllers/demo_controller.rb index 4007676c9..89f1da027 100644 --- a/app/controllers/demo_controller.rb +++ b/app/controllers/demo_controller.rb @@ -6,7 +6,7 @@ class DemoController < ApplicationController return redirect_to root_path if Rails.env.production? smart_listing_create :procedures, - Procedure.where(archived: false), + Procedure.where(archived: false, published: true), partial: "demo/list", array: true end From f58f9d27d97e6db627d18757916c2dd2ae4951a1 Mon Sep 17 00:00:00 2001 From: Guillaume Lazzara Date: Tue, 14 Jun 2016 15:30:23 +0200 Subject: [PATCH 26/33] Enable password reset for Gestionnaires --- .../gestionnaires/passwords_controller.rb | 32 +++++++++++++++++++ .../gestionnaires/passwords/edit.html.haml | 31 ++++++++++++++++++ .../gestionnaires/passwords/new.html.haml | 22 +++++++++++++ .../gestionnaires/sessions/new.html.haml | 3 +- .../gestionnaires/shared/_links.html.erb | 2 ++ config/routes.rb | 7 ++-- 6 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 app/controllers/gestionnaires/passwords_controller.rb create mode 100644 app/views/gestionnaires/passwords/edit.html.haml create mode 100644 app/views/gestionnaires/passwords/new.html.haml create mode 100644 app/views/gestionnaires/shared/_links.html.erb diff --git a/app/controllers/gestionnaires/passwords_controller.rb b/app/controllers/gestionnaires/passwords_controller.rb new file mode 100644 index 000000000..a7a1d1734 --- /dev/null +++ b/app/controllers/gestionnaires/passwords_controller.rb @@ -0,0 +1,32 @@ +class Gestionnaires::PasswordsController < Devise::PasswordsController + # GET /resource/password/new + # def new + # super + # end + + # POST /resource/password + # def create + # super + # end + + # GET /resource/password/edit?reset_password_token=abcdef + # def edit + # super + # end + + # PUT /resource/password + # def update + # super + # end + + # protected + + # def after_resetting_password_path_for(resource) + # super(resource) + # end + + # The path used after sending reset password instructions + # def after_sending_reset_password_instructions_path_for(resource_name) + # super(resource_name) + # end +end diff --git a/app/views/gestionnaires/passwords/edit.html.haml b/app/views/gestionnaires/passwords/edit.html.haml new file mode 100644 index 000000000..ff4027fe4 --- /dev/null +++ b/app/views/gestionnaires/passwords/edit.html.haml @@ -0,0 +1,31 @@ += devise_error_messages! + +#form_login + = image_tag('logo-tps.png') + %br + %h2#gestionnaire_login Changement de mot de passe + + %br + %br + #new_user + = form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :put }) do |f| + = f.hidden_field :reset_password_token + %h4 + = f.label 'Nouveau mot de passe' + + .input-group + .input-group-addon + %span.glyphicon.glyphicon-asterisk + = f.password_field :password, autofocus: true, autocomplete: "off", class: 'form-control' + %br + %h4 + = f.label 'Retaper le mot de passe' + .input-group + .input-group-addon + %span.glyphicon.glyphicon-asterisk + = f.password_field :password_confirmation, autocomplete: "off", class: 'form-control' + %br + %br + .actions + = f.submit "Changer", class:'btn btn-primary' + %br \ No newline at end of file diff --git a/app/views/gestionnaires/passwords/new.html.haml b/app/views/gestionnaires/passwords/new.html.haml new file mode 100644 index 000000000..9c15a8ca9 --- /dev/null +++ b/app/views/gestionnaires/passwords/new.html.haml @@ -0,0 +1,22 @@ += devise_error_messages! + +#form_login + = image_tag('logo-tps.png') + %br + %h2#gestionnaire_login Mot de passe oublié + + %br + %br + #new_user + = form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :post }) do |f| + %h4 + = f.label :email + .input-group + .input-group-addon + %span.glyphicon.glyphicon-user + = f.email_field :email, class: 'form-control', placeholder: 'Email' + %br + %br + .actions + = f.submit "Renvoyer", class:'btn btn-primary' + %br \ No newline at end of file diff --git a/app/views/gestionnaires/sessions/new.html.haml b/app/views/gestionnaires/sessions/new.html.haml index 3a96664d7..a3de3cd1e 100644 --- a/app/views/gestionnaires/sessions/new.html.haml +++ b/app/views/gestionnaires/sessions/new.html.haml @@ -25,4 +25,5 @@ %br .actions = f.submit "Se connecter", class:'btn btn-primary' - %br \ No newline at end of file + %br + = render "gestionnaires/shared/links" diff --git a/app/views/gestionnaires/shared/_links.html.erb b/app/views/gestionnaires/shared/_links.html.erb new file mode 100644 index 000000000..51021059c --- /dev/null +++ b/app/views/gestionnaires/shared/_links.html.erb @@ -0,0 +1,2 @@ +<%= link_to "Mot de passe oublié ?", new_password_path(resource_name) %>
+ diff --git a/config/routes.rb b/config/routes.rb index 3b5f6ebb2..db74fc87f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,8 +9,9 @@ Rails.application.routes.draw do }, skip: [:password, :registrations] devise_for :gestionnaires, controllers: { - sessions: 'gestionnaires/sessions' - }, skip: [:password, :registrations] + sessions: 'gestionnaires/sessions', + passwords: 'gestionnaires/passwords' + }, skip: [:registrations] devise_for :users, controllers: { sessions: 'users/sessions', @@ -25,6 +26,8 @@ Rails.application.routes.draw do devise_scope :gestionnaire do get '/gestionnaires/sign_in/demo' => 'gestionnaires/sessions#demo' + get '/gestionnaires/edit' => 'gestionnaires/registrations#edit', :as => 'edit_gestionnaires_registration' + put '/gestionnaires' => 'gestionnaires/registrations#update', :as => 'gestionnaires_registration' end devise_scope :administrateur do From 7fd2aa40ca4f57cd5214df0b01903570237d4fa8 Mon Sep 17 00:00:00 2001 From: Guillaume Lazzara Date: Tue, 14 Jun 2016 16:00:37 +0200 Subject: [PATCH 27/33] Do not send notification email when comments cannot be saved. --- app/controllers/commentaires_controller.rb | 5 +++-- .../backoffice/commentaires_controller_spec.rb | 12 ++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/app/controllers/commentaires_controller.rb b/app/controllers/commentaires_controller.rb index f711a4ab5..4a0b75662 100644 --- a/app/controllers/commentaires_controller.rb +++ b/app/controllers/commentaires_controller.rb @@ -22,10 +22,11 @@ class CommentairesController < ApplicationController end @commentaire.body = params['texte_commentaire'] - @commentaire.save unless flash.alert + saved = false + saved = @commentaire.save unless flash.alert if is_gestionnaire? - NotificationMailer.new_answer(@commentaire.dossier).deliver_now! + NotificationMailer.new_answer(@commentaire.dossier).deliver_now! if saved redirect_to url_for(controller: 'backoffice/dossiers', action: :show, id: params['dossier_id']) elsif current_user.email != @commentaire.dossier.user.email invite = Invite.where(dossier: @commentaire.dossier, user: current_user).first diff --git a/spec/controllers/backoffice/commentaires_controller_spec.rb b/spec/controllers/backoffice/commentaires_controller_spec.rb index a66229f20..a85f0cc99 100644 --- a/spec/controllers/backoffice/commentaires_controller_spec.rb +++ b/spec/controllers/backoffice/commentaires_controller_spec.rb @@ -91,5 +91,17 @@ describe Backoffice::CommentairesController, type: :controller do end end end + + describe 'comment cannot be saved' do + before do + allow_any_instance_of(Commentaire).to receive(:save).and_return(false) + end + it 'Notification email is not sent' do + expect(NotificationMailer).not_to receive(:new_answer) + expect(NotificationMailer).not_to receive(:deliver_now!) + + post :create, dossier_id: dossier_id, texte_commentaire: texte_commentaire + end + end end end From 7af785933471ea10f172d1a02b286cadb39d56a6 Mon Sep 17 00:00:00 2001 From: Guillaume Lazzara Date: Wed, 15 Jun 2016 11:34:05 +0200 Subject: [PATCH 28/33] Implement procedure cloning --- Gemfile | 2 + Gemfile.lock | 5 ++- .../admin/procedures_controller.rb | 17 ++++++++ app/models/procedure.rb | 7 ++++ .../admin/procedures/_draft_list.html.haml | 2 + app/views/admin/procedures/_list.html.haml | 2 + config/routes.rb | 1 + .../admin/procedures_controller_spec.rb | 31 ++++++++++++++ spec/models/procedure_spec.rb | 40 +++++++++++++++++++ spec/spec_helper.rb | 7 ++++ 10 files changed, 113 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 5bd2a237d..12229eea2 100644 --- a/Gemfile +++ b/Gemfile @@ -22,6 +22,8 @@ gem 'jbuilder', '~> 2.0' # bundle exec rake doc:rails generates the API under doc/api. gem 'sdoc', '~> 0.4.0', group: :doc +# Enable deep clone of active record models +gem 'deep_cloneable', '~> 2.2.1' # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' diff --git a/Gemfile.lock b/Gemfile.lock index 21f55c50e..31a9b9b55 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -119,6 +119,8 @@ GEM sprockets (>= 2.0.0) database_cleaner (1.4.1) debug_inspector (0.0.2) + deep_cloneable (2.2.1) + activerecord (>= 3.1.0, < 5.2.0) devise (3.4.1) bcrypt (~> 3.0) orm_adapter (~> 0.1) @@ -634,6 +636,7 @@ DEPENDENCIES coffee-rails (~> 4.1.0) css_splitter database_cleaner + deep_cloneable (~> 2.2.1) devise draper factory_girl @@ -690,4 +693,4 @@ DEPENDENCIES will_paginate-bootstrap BUNDLED WITH - 1.11.2 + 1.12.5 diff --git a/app/controllers/admin/procedures_controller.rb b/app/controllers/admin/procedures_controller.rb index 4539cc02f..19e003a70 100644 --- a/app/controllers/admin/procedures_controller.rb +++ b/app/controllers/admin/procedures_controller.rb @@ -83,6 +83,23 @@ class Admin::ProceduresController < AdminController change_status({archived: params[:archive]}) end + def clone + @procedure = current_administrateur.procedures.find(params[:procedure_id]) + + new_procedure = @procedure.clone + if new_procedure + flash.notice = 'Procédure clonée' + redirect_to edit_admin_procedure_path(id: new_procedure.id) + else + flash.now.alert = @procedure.errors.full_messages.join('
').html_safe + render 'index' + end + + rescue ActiveRecord::RecordNotFound + flash.alert = 'Procédure inéxistante' + redirect_to admin_procedures_path + end + def active_class @active_class = 'active' end diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 00aa073b6..5f4de31f6 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -58,4 +58,11 @@ class Procedure < ActiveRecord::Base published? end + def clone + procedure = self.deep_clone(include: [ :types_de_piece_justificative, :types_de_champ, :module_api_carto ]) + procedure.archived = false + procedure.published = false + return procedure if procedure.save + end + end diff --git a/app/views/admin/procedures/_draft_list.html.haml b/app/views/admin/procedures/_draft_list.html.haml index 7d3505614..b169f2fc5 100644 --- a/app/views/admin/procedures/_draft_list.html.haml +++ b/app/views/admin/procedures/_draft_list.html.haml @@ -3,6 +3,7 @@ %thead %th#ID= smart_listing.sortable 'ID', 'id' %th#libelle= smart_listing.sortable 'Libellé', 'libelle' + %th#lien Actions - @procedures.each do |procedure| - procedure = procedure.decorate @@ -10,6 +11,7 @@ %td.col-md-1.col-lg-1= procedure.id %td.col-md-6.col-lg-6 = link_to(procedure.libelle, "/admin/procedures/#{procedure.id}") + %td= link_to('cloner', admin_procedure_clone_path(procedure.id), 'data-method' => :put, class: 'btn-xs btn-primary') = smart_listing.paginate = smart_listing.pagination_per_page_links diff --git a/app/views/admin/procedures/_list.html.haml b/app/views/admin/procedures/_list.html.haml index 651583f12..4735d527f 100644 --- a/app/views/admin/procedures/_list.html.haml +++ b/app/views/admin/procedures/_list.html.haml @@ -4,6 +4,7 @@ %th#ID= smart_listing.sortable 'ID', 'id' %th#libelle= smart_listing.sortable 'Libellé', 'libelle' %th#lien Lien + %th#lien Actions - @procedures.each do |procedure| - procedure = procedure.decorate @@ -12,6 +13,7 @@ %td.col-md-6.col-lg-6 = link_to(procedure.libelle, "/admin/procedures/#{procedure.id}") %td= link_to procedure.lien, procedure.lien + %td= link_to('cloner', admin_procedure_clone_path(procedure.id), 'data-method' => :put, class: 'btn-xs btn-primary') = smart_listing.paginate = smart_listing.pagination_per_page_links diff --git a/config/routes.rb b/config/routes.rb index db74fc87f..764b56da0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -100,6 +100,7 @@ Rails.application.routes.draw do put 'archive' => 'procedures#archive', as: :archive put 'publish' => 'procedures#publish', as: :publish + put 'clone' => 'procedures#clone', as: :clone resource :accompagnateurs, only: [:show, :update] diff --git a/spec/controllers/admin/procedures_controller_spec.rb b/spec/controllers/admin/procedures_controller_spec.rb index 11587b94d..8d8779dc3 100644 --- a/spec/controllers/admin/procedures_controller_spec.rb +++ b/spec/controllers/admin/procedures_controller_spec.rb @@ -265,4 +265,35 @@ describe Admin::ProceduresController, type: :controller do it { expect(flash[:alert]).to have_content 'Procédure inéxistante' } end end + + describe 'PUT #clone' do + let!(:procedure) { create(:procedure, administrateur: admin) } + subject { put :clone, procedure_id: procedure.id } + + it { expect{ subject }.to change(Procedure, :count).by(1) } + + context 'when admin is the owner of the procedure' do + before do + subject + end + + it 'creates a new procedure and redirect to it' do + expect(response).to redirect_to edit_admin_procedure_path(id: Procedure.last.id) + expect(flash[:notice]).to have_content 'Procédure clonée' + end + end + + context 'when admin is not the owner of the procedure' do + let(:admin_2) { create(:administrateur) } + + before do + sign_out admin + sign_in admin_2 + subject + end + + it { expect(response).to redirect_to :admin_procedures } + it { expect(flash[:alert]).to have_content 'Procédure inéxistante' } + end + end end diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index b36e64837..f805f0292 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -122,4 +122,44 @@ describe Procedure do it { expect { subject }.to raise_error(ActiveRecord::RecordNotFound) } end end + + describe 'clone' do + let(:archived) { false } + let(:published) { false } + let(:procedure) { create(:procedure, archived: archived, published: published) } + let!(:type_de_champ_0) { create(:type_de_champ, procedure: procedure, order_place: 0) } + let!(:type_de_champ_1) { create(:type_de_champ, procedure: procedure, order_place: 1) } + let!(:piece_justificative_0) { create(:type_de_piece_justificative, procedure: procedure, order_place: 0) } + let!(:piece_justificative_1) { create(:type_de_piece_justificative, procedure: procedure, order_place: 1) } + subject { procedure.clone } + + it 'should duplicate specific objects with different id' do + expect(subject.id).not_to eq(procedure.id) + expect(subject).to have_same_attributes_as(procedure) + expect(subject.module_api_carto).to have_same_attributes_as(procedure.module_api_carto) + + subject.types_de_champ.zip(procedure.types_de_champ).each do |stc, ptc| + expect(stc).to have_same_attributes_as(ptc) + end + + subject.types_de_piece_justificative.zip(procedure.types_de_piece_justificative).each do |stc, ptc| + expect(stc).to have_same_attributes_as(ptc) + end + end + + it 'should not duplicate specific related objects' do + expect(subject.dossiers).to eq([]) + expect(subject.gestionnaires).to eq([]) + expect(subject.assign_to).to eq([]) + end + + describe 'procedure status is reset' do + let(:archived) { true } + let(:published) { true } + it 'sets published and archived to false' do + expect(subject.archived).to be_falsey + expect(subject.published).to be_falsey + end + end + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 7411d7377..bca602fe6 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -113,4 +113,11 @@ RSpec.configure do |config| end end } + + RSpec::Matchers.define :have_same_attributes_as do |expected| + match do |actual| + ignored = [:id, :procedure_id, :updated_at, :created_at] + actual.attributes.with_indifferent_access.except(*ignored) == expected.attributes.with_indifferent_access.except(*ignored) + end + end end From cc8d2ef4ee113af8f3ef0fda1b49012d0eda43d7 Mon Sep 17 00:00:00 2001 From: Xavier J Date: Mon, 20 Jun 2016 13:57:57 +0200 Subject: [PATCH 29/33] New UI and UX for the dossier initialization --- app/assets/javascripts/dossiers.js | 34 +++++ app/assets/stylesheets/application.scss | 3 +- app/assets/stylesheets/etapes.scss | 29 +++++ app/assets/stylesheets/siret.scss | 7 -- app/controllers/users/dossiers_controller.rb | 74 ++++++----- app/facades/dossier_facades.rb | 2 +- app/models/dossier.rb | 6 + app/models/entreprise.rb | 4 +- app/models/etablissement.rb | 2 +- app/services/dossier_service.rb | 68 ++++++++++ app/views/dossiers/_show.html.haml | 32 +++-- app/views/dossiers/etapes/_etape1.html.haml | 19 +++ app/views/dossiers/etapes/_etape2.html.haml | 39 ++++++ app/views/dossiers/new_siret.js.erb | 7 ++ .../users/dossiers/_title_procedure.html.haml | 11 -- app/views/users/dossiers/new.html.haml | 11 -- app/views/users/sessions/new.html.haml | 15 ++- config/routes.rb | 3 + .../users/dossiers_controller_spec.rb | 119 +++++++++++++++--- spec/factories/exercice.rb | 8 ++ spec/factories/rna_information.rb | 11 ++ spec/features/users/complete_demande_spec.rb | 16 ++- spec/features/users/start_demande_spec.rb | 6 +- spec/models/dossier_spec.rb | 31 +++++ spec/views/dossiers/show.html.haml_spec.rb | 1 + .../users/dossiers/new_html.haml_spec.rb | 49 -------- 26 files changed, 451 insertions(+), 156 deletions(-) create mode 100644 app/assets/javascripts/dossiers.js create mode 100644 app/assets/stylesheets/etapes.scss create mode 100644 app/services/dossier_service.rb create mode 100644 app/views/dossiers/etapes/_etape1.html.haml create mode 100644 app/views/dossiers/etapes/_etape2.html.haml create mode 100644 app/views/dossiers/new_siret.js.erb delete mode 100644 app/views/users/dossiers/_title_procedure.html.haml delete mode 100644 app/views/users/dossiers/new.html.haml create mode 100644 spec/factories/exercice.rb create mode 100644 spec/factories/rna_information.rb delete mode 100644 spec/views/users/dossiers/new_html.haml_spec.rb diff --git a/app/assets/javascripts/dossiers.js b/app/assets/javascripts/dossiers.js new file mode 100644 index 000000000..86764fb01 --- /dev/null +++ b/app/assets/javascripts/dossiers.js @@ -0,0 +1,34 @@ +$(document).on('page:load', the_terms); +$(document).ready(the_terms); + +function the_terms() { + var the_terms = $("#dossier_autorisation_donnees"); + + if(the_terms.size() == 0) + return; + + check_value (the_terms); + + the_terms.click(function () { + check_value (the_terms); + }); + + function check_value (the_terms){ + if (the_terms.is(":checked")) { + $("#etape_suivante").removeAttr("disabled"); + } else { + $("#etape_suivante").attr("disabled", "disabled"); + } + } +} + +function error_form_siret(invalid_siret){ + $("input[type='submit']").removeClass('btn-success').addClass('btn-danger').val('Erreur SIRET'); + $("#dossier_siret").addClass('input-error').val(invalid_siret).on('input', reset_form_siret); + +} + +function reset_form_siret(){ + $("input[type='submit']").removeClass('btn-danger').addClass('btn-success').val('Valider'); + $("#dossier_siret").removeClass('input-error'); +} \ No newline at end of file diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index c28c00141..92fa141f8 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -186,7 +186,8 @@ div.pagination { margin-bottom: 0px; } -.alert.alert-success.move_up { +.alert.alert-success.move_up, +.alert.alert-danger.siret{ position: absolute; top: 0px; left: 0; diff --git a/app/assets/stylesheets/etapes.scss b/app/assets/stylesheets/etapes.scss new file mode 100644 index 000000000..b9f307024 --- /dev/null +++ b/app/assets/stylesheets/etapes.scss @@ -0,0 +1,29 @@ +.etapes_menu{ + h3 { + margin-left: 3%; + } + padding-right: 0 !important; +} + +.etapes_informations { + padding-left: 0 !important; +} + +.etape{ + margin-bottom: 0px; +} + +.etape_2{ + .etapes_menu{ + #dossier_siret{ + width: 200px; + } + + button{ + margin-top: 8px; + } + } + .etapes_informations{ + padding-top: 15px; + } +} \ No newline at end of file diff --git a/app/assets/stylesheets/siret.scss b/app/assets/stylesheets/siret.scss index cfbf10c3c..0bf56056a 100644 --- a/app/assets/stylesheets/siret.scss +++ b/app/assets/stylesheets/siret.scss @@ -6,13 +6,6 @@ display: none; } -#dossier_siret { - margin-bottom: 10px; - margin-left: auto; - margin-right: auto; - width: 200px; -} - #titre_procedure { margin-top: 3%; margin-bottom: 2%; diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index a6aa36f39..50a5d9c9d 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -3,7 +3,7 @@ class Users::DossiersController < UsersController helper SmartListing::Helper before_action :authenticate_user! - before_action :check_siret, only: :create + before_action :check_siret, only: :siret_informations before_action only: [:show] do authorized_routes? self.class @@ -25,50 +25,53 @@ class Users::DossiersController < UsersController def new procedure = Procedure.where(archived: false, published: true).find(params[:procedure_id]) - @dossier = Dossier.new(procedure: procedure) - @siret = params[:siret] || current_user.siret + dossier = Dossier.create(procedure: procedure, user: current_user, state: 'draft') + siret = params[:siret] || current_user.siret + update_current_user_siret! siret unless siret.nil? + + redirect_to users_dossier_path(id: dossier.id) rescue ActiveRecord::RecordNotFound error_procedure end def show - @facade = DossierFacades.new params[:id], current_user.email + @facade = facade + @siret = current_user.siret unless current_user.siret.nil? rescue ActiveRecord::RecordNotFound flash.alert = t('errors.messages.dossier_not_found') redirect_to url_for users_dossiers_path end - def create - entreprise_adapter = SIADE::EntrepriseAdapter.new(siren) + def siret_informations + @facade = facade params[:dossier_id] - dossier = Dossier.create(user: current_user, - state: 'draft', - procedure_id: create_params[:procedure_id], - mandataire_social: mandataire_social?(entreprise_adapter.mandataires_sociaux)) + update_current_user_siret! siret - entreprise = dossier.create_entreprise(entreprise_adapter.to_params) + DossierService.new(@facade.dossier, siret, current_user.france_connect_information).dossier_informations! - entreprise.create_rna_information(SIADE::RNAAdapter.new(siret).to_params) - - etablissement = dossier.create_etablissement(SIADE::EtablissementAdapter.new(siret).to_params - .merge({entreprise_id: entreprise.id})) - - etablissement.exercices.create(SIADE::ExercicesAdapter.new(siret).to_params) - - redirect_to url_for(controller: :dossiers, action: :show, id: dossier.id) + @facade = facade params[:dossier_id] + render '/dossiers/new_siret', formats: 'js' rescue RestClient::ResourceNotFound errors_valid_siret rescue ActiveRecord::RecordNotFound flash.alert = t('errors.messages.dossier_not_found') - redirect_to url_for(controller: :siret) + redirect_to url_for users_dossiers_path + end + + def change_siret + Dossier.find(params[:dossier_id]).reset! + + @facade = facade params[:dossier_id] + + render '/dossiers/new_siret', formats: :js end def update - @facade = DossierFacades.new params[:id], current_user.email + @facade = facade params[:dossier][:id] if checked_autorisation_donnees? @facade.dossier.update_attributes(update_params) @@ -79,8 +82,8 @@ class Users::DossiersController < UsersController redirect_to url_for(controller: :description, action: :show, dossier_id: @facade.dossier.id) end else - flash.now.alert = 'Les conditions sont obligatoires.' - render 'show' + flash.alert = 'Les conditions sont obligatoires.' + redirect_to users_dossier_path(id: @facade.dossier.id) end end @@ -132,7 +135,9 @@ class Users::DossiersController < UsersController def errors_valid_siret flash.alert = t('errors.messages.invalid_siret') - redirect_to url_for new_users_dossiers_path(procedure_id: create_params[:procedure_id]) + @facade = facade params[:dossier_id] + + render '/dossiers/new_siret', formats: :js, locals: {invalid_siret: siret} end def update_params @@ -147,28 +152,21 @@ class Users::DossiersController < UsersController create_params[:siret] end - def siren - siret[0..8] - end - def create_params - params.require(:dossier).permit(:siret, :procedure_id) + params.require(:dossier).permit(:siret) end def error_procedure flash.alert = t('errors.messages.procedure_not_found') + redirect_to url_for users_dossiers_path end - def mandataire_social? mandataires_list - unless current_user.france_connect_information.nil? - mandataires_list.each do |mandataire| - return true if mandataire[:nom].upcase == current_user.family_name.upcase && - mandataire[:prenom].upcase == current_user.given_name.upcase && - mandataire[:date_naissance_timestamp] == current_user.birthdate.to_time.to_i - end - end + def update_current_user_siret! siret + current_user.update_attributes(siret: siret) + end - false + def facade id = params[:id] + DossierFacades.new id, current_user.email end end diff --git a/app/facades/dossier_facades.rb b/app/facades/dossier_facades.rb index 56b63d39c..55d4728da 100644 --- a/app/facades/dossier_facades.rb +++ b/app/facades/dossier_facades.rb @@ -15,7 +15,7 @@ class DossierFacades end def entreprise - @dossier.entreprise.decorate + @dossier.entreprise.decorate unless @dossier.entreprise.nil? end def etablissement diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 7c00a303f..354cdb661 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -193,4 +193,10 @@ class Dossier < ActiveRecord::Base entreprise_attr = EntrepriseSerializer.new(self.entreprise).attributes.map {|k, v| ["entreprise.#{k}", v] }.to_h dossier_attr.merge(etablissement_attr).merge(entreprise_attr) end + + def reset! + entreprise.destroy unless entreprise.nil? + + update_attributes(autorisation_donnees: false) + end end diff --git a/app/models/entreprise.rb b/app/models/entreprise.rb index 0e0ab79eb..382a64c28 100644 --- a/app/models/entreprise.rb +++ b/app/models/entreprise.rb @@ -1,5 +1,5 @@ class Entreprise < ActiveRecord::Base belongs_to :dossier - has_one :etablissement - has_one :rna_information + has_one :etablissement, dependent: :destroy + has_one :rna_information, dependent: :destroy end diff --git a/app/models/etablissement.rb b/app/models/etablissement.rb index 9fee925cd..9d09b9787 100644 --- a/app/models/etablissement.rb +++ b/app/models/etablissement.rb @@ -2,7 +2,7 @@ class Etablissement < ActiveRecord::Base belongs_to :dossier belongs_to :entreprise - has_many :exercices + has_many :exercices, dependent: :destroy def geo_adresse [numero_voie, type_voie, nom_voie, complement_adresse, code_postal, localite].join(' ') diff --git a/app/services/dossier_service.rb b/app/services/dossier_service.rb new file mode 100644 index 000000000..1c33fd769 --- /dev/null +++ b/app/services/dossier_service.rb @@ -0,0 +1,68 @@ +class DossierService + + def initialize dossier, siret, france_connect_information + @dossier = dossier + @siret = siret + @france_connect_information = france_connect_information + end + + def dossier_informations! + entreprise_thread = Thread.new { + @entreprise_adapter = SIADE::EntrepriseAdapter.new(DossierService.siren @siret) + + @dossier.create_entreprise(@entreprise_adapter.to_params) + } + + etablissement_thread = Thread.new { + @etablissement_adapter = SIADE::EtablissementAdapter.new(@siret) + + @dossier.create_etablissement(@etablissement_adapter.to_params) + } + + rna_thread = Thread.new { + @rna_adapter = SIADE::RNAAdapter.new(@siret) + + sleep(0.1) while entreprise_thread.alive? + + @dossier.entreprise.create_rna_information(@rna_adapter.to_params) + } + + exercices_thread = Thread.new { + @exercices_adapter = SIADE::ExercicesAdapter.new(@siret) + + sleep(0.1) while etablissement_thread.alive? + + @dossier.etablissement.exercices.create(@exercices_adapter.to_params) + } + + sleep(0.1) while entreprise_thread.alive? || + etablissement_thread.alive? || + rna_thread.alive? || + exercices_thread.alive? + + @dossier.update_attributes(mandataire_social: mandataire_social?(@entreprise_adapter.mandataires_sociaux)) + @dossier.etablissement.update_attributes(entreprise: @dossier.entreprise) + + @dossier + end + + + def self.siren siret + siret[0..8] + end + + private + + def mandataire_social? mandataires_list + unless @france_connect_information.nil? + + mandataires_list.each do |mandataire| + return true if mandataire[:nom].upcase == @france_connect_information.family_name.upcase && + mandataire[:prenom].upcase == @france_connect_information.given_name.upcase && + mandataire[:date_naissance_timestamp] == @france_connect_information.birthdate.to_time.to_i + end + end + + false + end +end \ No newline at end of file diff --git a/app/views/dossiers/_show.html.haml b/app/views/dossiers/_show.html.haml index 1d1fdf7c8..3b273f41d 100644 --- a/app/views/dossiers/_show.html.haml +++ b/app/views/dossiers/_show.html.haml @@ -1,13 +1,21 @@ -.container#recap_info_entreprise - %h2 Récapitulatif de vos informations (récupérées auprès de l'INSEE et d'INFOGREFFE) - %br +#users_siret_index + + .row.etape.etape_1 + = render partial: '/dossiers/etapes/etape1' + + .row.etape.etape_2 + = render partial: '/dossiers/etapes/etape2' + + -#- if @facade.procedure.module_api_carto.use_api_carto? + -# .row.etape.etape_3 + -# .etape.etapes_menu.col-md-3.col-lg-3 + -# %h3 + -# 3 - Ma zone d'intervention + -# .etape.etapes_informations.col-md-9.col-lg-9 + -# + -#.row.etape.etape_4 + -# .etape.etapes_menu.col-md-3.col-lg-3 + -# %h3 + -# = "#{@facade.procedure.module_api_carto.use_api_carto? ? '4' : '3'} - Mon dossier" + -# .etape.etapes_informations.col-md-9.col-lg-9 - %div.row - = render partial: '/dossiers/infos_entreprise' - %br - = form_for @facade.dossier, url: { controller: '/users/dossiers', action: :update } do |f| - %label{ style:'font-weight:normal' } - = f.check_box :autorisation_donnees - J'autorise les décideurs publics à vérifier les informations de mon organisation auprès des administrations concernées. Ces informations resteront strictement confidentielles. - %br - = f.submit 'Etape suivante', class: "btn btn btn-info", style: 'float:right', id: 'etape_suivante' \ No newline at end of file diff --git a/app/views/dossiers/etapes/_etape1.html.haml b/app/views/dossiers/etapes/_etape1.html.haml new file mode 100644 index 000000000..917e08b0f --- /dev/null +++ b/app/views/dossiers/etapes/_etape1.html.haml @@ -0,0 +1,19 @@ +.etape.etapes_menu.col-md-3.col-lg-3 + %h3 + Ma procédure + %br + .center + - if @facade.procedure.euro_flag + #euro_flag.flag + =image_tag('drapeau_europe.png') + + #logo_procedure.flag + =image_tag( @facade.procedure.decorate.logo_img ) + +.etape.etapes_informations.col-md-9.col-lg-9 + .row + %h2#titre_procedure.text-info + = @facade.procedure.libelle + + %p{style:'width: 95%;'} + = h @facade.procedure.description.html_safe \ No newline at end of file diff --git a/app/views/dossiers/etapes/_etape2.html.haml b/app/views/dossiers/etapes/_etape2.html.haml new file mode 100644 index 000000000..cb14a694c --- /dev/null +++ b/app/views/dossiers/etapes/_etape2.html.haml @@ -0,0 +1,39 @@ +.etape.etapes_menu.col-md-3.col-lg-3 + %h3 + Mes informations + %br + - unless @facade.entreprise.nil? + .center{style:'margin-left: -5%'} + Vous êtes authentifié avec le SIRET + + %h3.text-success + = @facade.etablissement.siret + = form_for @facade.dossier, url: users_dossier_change_siret_path(dossier_id: @facade.dossier.id), method: :put, remote: true do |f| + = f.submit 'Changer de SIRET', class: %w(btn btn-xs btn-primary) + + +.etape.etapes_informations.col-md-9.col-lg-9 + .row + - if @facade.entreprise.nil? + #new_siret{style:'margin-left: 20%; margin-top: 5%'} + = form_for @facade.dossier, html: {class: 'form-inline'}, url: users_dossier_siret_informations_path(dossier_id: @facade.dossier.id), method: :post, remote: true do |f| + .form-group.form-group-lg + = f.text_field :siret, class: "form-control", placeholder: "Entrez votre Siret", value: @siret + = f.hidden_field :dossier_id, value: @facade.dossier.id + = f.submit 'Valider', class: %w(btn btn-lg btn-success), data: { disable_with: "Recherche en cours ..." } + - else + #recap_info_entreprise + = render partial: '/dossiers/infos_entreprise' + + %p#insee_infogreffe{style:'color:grey; float:right'} + %i + Informations récupérées auprès de l'INSEE et d'INFOGREFFE + + %br + = form_for @facade.dossier, url: { controller: '/users/dossiers', action: :update } do |f| + = f.hidden_field :id + %label{ style:'font-weight:normal' } + = f.check_box :autorisation_donnees + J'autorise les décideurs publics à vérifier les informations de mon organisation auprès des administrations concernées. Ces informations resteront strictement confidentielles. + %br + = f.submit 'Etape suivante', class: "btn btn btn-info", style: 'float:right', id: 'etape_suivante', disabled: :disabled diff --git a/app/views/dossiers/new_siret.js.erb b/app/views/dossiers/new_siret.js.erb new file mode 100644 index 000000000..803581a3c --- /dev/null +++ b/app/views/dossiers/new_siret.js.erb @@ -0,0 +1,7 @@ +$('.row.etape.etape_2').html("<%= escape_javascript(render partial: '/dossiers/etapes/etape2', locals: { facade: @facade } ) %>"); +the_terms(); + +<% unless flash.empty? %> +error_form_siret('<%= invalid_siret %>'); +<% end %> +<% flash.clear %> \ No newline at end of file diff --git a/app/views/users/dossiers/_title_procedure.html.haml b/app/views/users/dossiers/_title_procedure.html.haml deleted file mode 100644 index 160a05412..000000000 --- a/app/views/users/dossiers/_title_procedure.html.haml +++ /dev/null @@ -1,11 +0,0 @@ -- if @dossier.procedure.euro_flag - #euro_flag.flag - =image_tag('drapeau_europe.png') - -#logo_procedure.flag - =image_tag( @dossier.procedure.decorate.logo_img ) - -%h2#titre_procedure.text-info - = @dossier.procedure.libelle -%p - = h @dossier.procedure.description.html_safe diff --git a/app/views/users/dossiers/new.html.haml b/app/views/users/dossiers/new.html.haml deleted file mode 100644 index 992b2f842..000000000 --- a/app/views/users/dossiers/new.html.haml +++ /dev/null @@ -1,11 +0,0 @@ -.container.center#users_siret_index - .row - %p.lead{id: 'pro_section', style:'padding-top: 120px'} - = render partial: 'title_procedure' - %br - - = form_for @dossier, url: {controller: 'users/dossiers', action: :create}, method: :post do |f| - .form-group.form-group-lg - = f.text_field :siret, class: "form-control", placeholder: "Entrez votre Siret", value: @siret - = f.hidden_field :procedure_id - = f.submit 'Commencer', class: %w(btn btn-lg btn-success), style: 'margin-top:20px;' \ No newline at end of file diff --git a/app/views/users/sessions/new.html.haml b/app/views/users/sessions/new.html.haml index 028fd6d2a..0cade0fe2 100644 --- a/app/views/users/sessions/new.html.haml +++ b/app/views/users/sessions/new.html.haml @@ -2,7 +2,20 @@ %br - if @dossier = link_to 'X', users_no_procedure_url, class: 'btn btn-xs', style:'float: right' - = render partial: 'users/dossiers/title_procedure' + + - if @dossier.procedure.euro_flag + #euro_flag.flag + =image_tag('drapeau_europe.png') + + #logo_procedure.flag + =image_tag( @dossier.procedure.decorate.logo_img ) + + %h2#titre_procedure.text-info + = @dossier.procedure.libelle + %p + = h @dossier.procedure.description.html_safe + + - else = image_tag('logo-tps.png', {id: 'logo_tps'}) %br diff --git a/config/routes.rb b/config/routes.rb index 3b5f6ebb2..d142cadda 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -73,6 +73,9 @@ Rails.application.routes.draw do post '/carte' => 'carte#save' put '/archive' => 'dossiers#archive' + + post '/siret_informations' => 'dossiers#siret_informations' + put '/change_siret' => 'dossiers#change_siret' end resource :dossiers end diff --git a/spec/controllers/users/dossiers_controller_spec.rb b/spec/controllers/users/dossiers_controller_spec.rb index 55b311b8b..4514cf689 100644 --- a/spec/controllers/users/dossiers_controller_spec.rb +++ b/spec/controllers/users/dossiers_controller_spec.rb @@ -5,18 +5,20 @@ describe Users::DossiersController, type: :controller do let(:procedure) { create(:procedure, :published) } let(:procedure_id) { procedure.id } - let(:dossier) { create(:dossier, :with_entreprise, user: user, procedure: procedure) } + let(:dossier) { create(:dossier, user: user, procedure: procedure) } let(:dossier_id) { dossier.id } let(:siret_not_found) { 999_999_999_999 } let(:rna_status) { 404 } let(:rna_body) { '' } + let(:user) { create :user } + let(:exercices_status) { 200 } let(:exercices_body) { File.read('spec/support/files/exercices.json') } - let(:siren) { dossier.siren } - let(:siret) { dossier.siret } + let(:siren) { '440117620' } + let(:siret) { '44011762001530' } let(:siret_with_whitespaces) { '440 1176 2001 530' } let(:bad_siret) { 1 } @@ -55,10 +57,66 @@ describe Users::DossiersController, type: :controller do context 'when procedure_id is valid' do context 'when user is logged in' do before do - sign_in create(:user) + sign_in user end - it { is_expected.to have_http_status(:success) } + it { is_expected.to have_http_status(302) } + it { is_expected.to redirect_to users_dossier_path(id: Dossier.last) } + + it { expect { subject }.to change(Dossier, :count).by 1 } + + describe 'save user siret' do + + context 'when user have not a saved siret' do + context 'when siret is present on request' do + subject { get :new, procedure_id: procedure_id, siret: siret } + + before do + subject + user.reload + end + + it { expect(user.siret).to eq siret } + end + + context 'when siret is not present on the request' do + before do + subject + user.reload + end + + it { expect(user.siret).to eq nil } + end + end + + context 'when user have a saved siret' do + before do + user.siret = '53029478400026' + user.save + user.reload + end + + context 'when siret is present on request' do + subject { get :new, procedure_id: procedure_id, siret: siret } + + before do + subject + user.reload + end + + it { expect(user.siret).to eq siret } + end + + context 'when siret is not present on the request' do + before do + subject + user.reload + end + + it { expect(user.siret).to eq '53029478400026' } + end + end + end context 'when procedure is archived' do let(:procedure) { create(:procedure, archived: 'true') } @@ -68,6 +126,7 @@ describe Users::DossiersController, type: :controller do end context 'when user is not logged' do it { is_expected.to have_http_status(302) } + it { is_expected.to redirect_to new_user_session_path } end end @@ -75,7 +134,7 @@ describe Users::DossiersController, type: :controller do let(:procedure_id) { 0 } before do - sign_in create(:user) + sign_in user end it { is_expected.to redirect_to users_dossiers_path } @@ -85,7 +144,7 @@ describe Users::DossiersController, type: :controller do let(:procedure) { create(:procedure, published: false) } before do - sign_in create(:user) + sign_in user end it { is_expected.to redirect_to users_dossiers_path } @@ -93,7 +152,7 @@ describe Users::DossiersController, type: :controller do end end - describe 'POST #create' do + describe 'POST #siret_informations' do before do stub_request(:get, "https://api-dev.apientreprise.fr/v2/etablissements/#{siret_not_found}?token=#{SIADETOKEN}") .to_return(status: 404, body: 'fake body') @@ -109,6 +168,8 @@ describe Users::DossiersController, type: :controller do stub_request(:get, "https://api-dev.apientreprise.fr/v1/associations/#{siret}?token=#{SIADETOKEN}") .to_return(status: rna_status, body: rna_body) + + dossier end describe 'dossier attributs' do @@ -119,10 +180,10 @@ describe Users::DossiersController, type: :controller do sign_in user end - subject { post :create, dossier: {siret: example_siret, procedure_id: Procedure.last} } + subject { post :siret_informations, dossier_id: dossier.id, dossier: {siret: example_siret} } it 'create a dossier' do - expect { subject }.to change { Dossier.count }.by(1) + expect { subject }.to change { Dossier.count }.by(0) end it 'creates entreprise' do @@ -238,27 +299,32 @@ describe Users::DossiersController, type: :controller do context 'with non existant siret' do before do - sign_in create(:user) + sign_in user + subject end let(:siret_not_found) { '11111111111111' } - subject { post :create, dossier: {siret: siret_not_found, procedure_id: procedure.id} } + subject { post :siret_informations, dossier_id: dossier.id, dossier: {siret: siret_not_found} } + it 'does not create new dossier' do expect { subject }.not_to change { Dossier.count } end - it 'redirects to show' do - expect(subject).to redirect_to new_users_dossiers_path(procedure_id: procedure_id) - end + it { expect(response.status).to eq 200 } + it { expect(flash.alert).to eq 'Le siret est incorrect' } + it { expect(response.to_a[2]).to be_an_instance_of ActionDispatch::Response::RackBody } end end end describe 'PUT #update' do + subject { put :update, id: dossier_id, dossier: {id: dossier_id, autorisation_donnees: autorisation_donnees} } + before do sign_in dossier.user - put :update, id: dossier_id, dossier: {autorisation_donnees: autorisation_donnees} + subject end + context 'when Checkbox is checked' do let(:autorisation_donnees) { '1' } @@ -272,7 +338,7 @@ describe Users::DossiersController, type: :controller do let(:procedure) { create(:procedure, :with_api_carto) } before do - put :update, id: dossier_id, dossier: {autorisation_donnees: autorisation_donnees} + subject end it 'redirects to carte' do expect(response).to redirect_to(controller: :carte, action: :show, dossier_id: dossier.id) @@ -295,6 +361,25 @@ describe Users::DossiersController, type: :controller do dossier.reload expect(dossier.autorisation_donnees).to be_falsy end + + it { is_expected.to redirect_to users_dossier_path(id: dossier.id) } + end + end + + describe 'PUT #change_siret' do + let(:dossier) { create(:dossier, :with_entreprise, user: user, procedure: procedure) } + + subject { put :change_siret, dossier_id: dossier.id } + + before do + sign_in user + end + + it { expect(subject.status).to eq 200 } + + it 'function dossier.reset! is call' do + expect_any_instance_of(Dossier).to receive(:reset!) + subject end end diff --git a/spec/factories/exercice.rb b/spec/factories/exercice.rb new file mode 100644 index 000000000..bf804d551 --- /dev/null +++ b/spec/factories/exercice.rb @@ -0,0 +1,8 @@ +FactoryGirl.define do + factory :exercice do + ca '12345678' + dateFinExercice "2014-12-30 23:00:00" + date_fin_exercice_timestamp 1419980400 + association :etablissement, factory: [:etablissement] + end +end diff --git a/spec/factories/rna_information.rb b/spec/factories/rna_information.rb new file mode 100644 index 000000000..70aef163d --- /dev/null +++ b/spec/factories/rna_information.rb @@ -0,0 +1,11 @@ +FactoryGirl.define do + factory :rna_information do + association_id "W072000535" + titre "ASSOCIATION POUR LA PROMOTION DE SPECTACLES AU CHATEAU DE ROCHEMAURE" + objet "mise en oeuvre et réalisation de spectacles au chateau de rochemaure" + date_creation "1990-04-24" + date_declaration "2014-11-28" + date_publication "1990-05-16" + association :entreprise, factory: [:entreprise] + end +end diff --git a/spec/features/users/complete_demande_spec.rb b/spec/features/users/complete_demande_spec.rb index 0f219c031..c028bd2a2 100644 --- a/spec/features/users/complete_demande_spec.rb +++ b/spec/features/users/complete_demande_spec.rb @@ -6,7 +6,7 @@ feature 'user path for dossier creation' do let(:siret) { '53272417600013' } let(:siren) { siret[0...9] } - context 'user arrives on siret page' do + context 'user arrives on siret page', js: true do before do visit new_users_dossiers_path(procedure_id: procedure.id) end @@ -39,11 +39,23 @@ feature 'user path for dossier creation' do .to_return(status: 404, body: '') page.find_by_id('dossier_siret').set siret - page.click_on 'Commencer' + page.click_on 'Valider' end + scenario 'user is on page recap info entreprise' do expect(page).to have_css('#recap_info_entreprise') end + + context 'when user would like change siret' do + before do + page.click_on('Changer de SIRET') + end + + scenario 'redirects to siret page' do + expect(page).to have_css('#dossier_siret') + end + end + context 'when validating info entreprise recap page' do before do page.check('dossier_autorisation_donnees') diff --git a/spec/features/users/start_demande_spec.rb b/spec/features/users/start_demande_spec.rb index 56c52d81b..664fc8729 100644 --- a/spec/features/users/start_demande_spec.rb +++ b/spec/features/users/start_demande_spec.rb @@ -6,7 +6,7 @@ feature 'user arrive on siret page' do let(:siret) { '42149333900020' } let(:siren) { siret[0...9] } - context 'when user is not logged in' do + context 'when user is not logged in', js: true do before do visit new_users_dossiers_path(procedure_id: procedure.id) end @@ -22,7 +22,7 @@ feature 'user arrive on siret page' do end end scenario 'he is redirected to siret page to enter a siret' do - expect(page).to have_css('#pro_section') + expect(page).to have_css('#new_siret') end context 'when enter a siret' do before do @@ -36,7 +36,7 @@ feature 'user arrive on siret page' do .to_return(status: 404, body: '') page.find_by_id('dossier_siret').set siret - page.click_on 'Commencer' + page.click_on 'Valider' end scenario 'he is redirected to recap info entreprise page' do expect(page).to have_css('#recap_info_entreprise') diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 0952b3592..ff1ba94a7 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -563,4 +563,35 @@ describe Dossier do it { expect(subject['entreprise.prenom']).to be_nil } end + describe '#reset!' do + let!(:dossier) { create :dossier, :with_entreprise, autorisation_donnees: true } + let!(:rna_information) { create :rna_information, entreprise: dossier.entreprise } + let!(:exercice) { create :exercice, etablissement: dossier.etablissement } + + subject { dossier.reset! } + + it { expect(dossier.entreprise).not_to be_nil } + it { expect(dossier.etablissement).not_to be_nil } + it { expect(dossier.etablissement.exercices).not_to be_empty } + it { expect(dossier.etablissement.exercices.size).to eq 1 } + it { expect(dossier.entreprise.rna_information).not_to be_nil } + it { expect(dossier.autorisation_donnees).to be_truthy } + + it { expect{subject}.to change(RNAInformation, :count).by(-1) } + it { expect{subject}.to change(Exercice, :count).by(-1) } + + it { expect{subject}.to change(Entreprise, :count).by(-1) } + it { expect{subject}.to change(Etablissement, :count).by(-1) } + + context 'when method reset! is call' do + before do + subject + dossier.reload + end + + it { expect(dossier.entreprise).to be_nil } + it { expect(dossier.etablissement).to be_nil } + it { expect(dossier.autorisation_donnees).to be_falsey } + end + end end diff --git a/spec/views/dossiers/show.html.haml_spec.rb b/spec/views/dossiers/show.html.haml_spec.rb index dd79af9bb..7c5bfba17 100644 --- a/spec/views/dossiers/show.html.haml_spec.rb +++ b/spec/views/dossiers/show.html.haml_spec.rb @@ -9,6 +9,7 @@ describe 'dossiers/show.html.haml', type: :view do render end + it 'have autorisation_donnees check box' do expect(rendered).to have_css('#dossier_autorisation_donnees') end diff --git a/spec/views/users/dossiers/new_html.haml_spec.rb b/spec/views/users/dossiers/new_html.haml_spec.rb deleted file mode 100644 index f2fa59422..000000000 --- a/spec/views/users/dossiers/new_html.haml_spec.rb +++ /dev/null @@ -1,49 +0,0 @@ -require 'spec_helper' - -describe 'users/dossiers/new.html.haml', type: :view, vcr: { cassette_name: 'user_dossier_new_html' } do - let(:user) { create(:user) } - let(:euro_flag) { false } - let(:cerfa_flag) { false } - let(:logo) { '' } - let(:procedure) { create(:procedure, euro_flag: euro_flag, cerfa_flag: cerfa_flag, logo: logo) } - let!(:dossier) { create(:dossier, procedure: procedure, user: user).decorate } - - before do - sign_in user - - assign(:dossier, dossier.decorate) - render - end - - subject { rendered } - - it { is_expected.to have_css('#users_siret_index') } - - describe 'euro flag' do - context 'euro flag is not present' do - it { is_expected.not_to have_css('#euro_flag.flag') } - end - - context 'euro flag is present' do - let(:euro_flag) { true } - it { is_expected.to have_css('#euro_flag.flag') } - end - end - - describe 'logo procedure' do - context 'procedure have no logo' do - it 'TPS logo is present' do - is_expected.to match(/src="\/assets\/logo-tps-.*\.png"/) - end - end - - context 'procedure have logo' do - # let(:logo) { fixture_file_upload('spec/support/files/logo_test_procedure.png', 'image/png') } - let(:logo) { File.new(File.join(::Rails.root.to_s, "/spec/support/files", "logo_test_procedure.png")) } - - it 'Procedure logo is present' do - is_expected.to have_css("img[src='#{procedure.logo}']") - end - end - end -end \ No newline at end of file From 1dcb29ec659a95b42fa6b021a859abbdf139e1a7 Mon Sep 17 00:00:00 2001 From: Xavier J Date: Mon, 20 Jun 2016 17:37:04 +0200 Subject: [PATCH 30/33] Add feature delete for draft procedure --- .../admin/procedures_controller.rb | 19 ++++++-- app/decorators/commentaire_decorator.rb | 2 +- app/decorators/dossier_decorator.rb | 2 +- app/decorators/procedure_decorator.rb | 4 ++ app/models/gestionnaire.rb | 2 +- app/models/procedure.rb | 4 +- .../admin/procedures/_draft_list.html.haml | 21 --------- app/views/admin/procedures/_list.html.haml | 14 ++++-- .../admin/procedures_controller_spec.rb | 43 ++++++++++++++++++- 9 files changed, 77 insertions(+), 34 deletions(-) delete mode 100644 app/views/admin/procedures/_draft_list.html.haml diff --git a/app/controllers/admin/procedures_controller.rb b/app/controllers/admin/procedures_controller.rb index 19e003a70..2698c8dca 100644 --- a/app/controllers/admin/procedures_controller.rb +++ b/app/controllers/admin/procedures_controller.rb @@ -7,7 +7,7 @@ class Admin::ProceduresController < AdminController def index @procedures = smart_listing_create :procedures, - current_administrateur.procedures.where(published: true, archived: false), + current_administrateur.procedures.where(published: true, archived: false).order(created_at: :desc), partial: "admin/procedures/list", array: true @@ -16,7 +16,7 @@ class Admin::ProceduresController < AdminController def archived @procedures = smart_listing_create :procedures, - current_administrateur.procedures.where(archived: true), + current_administrateur.procedures.where(archived: true).order(created_at: :desc), partial: "admin/procedures/list", array: true @@ -27,8 +27,8 @@ class Admin::ProceduresController < AdminController def draft @procedures = smart_listing_create :procedures, - current_administrateur.procedures.where(published: false, archived: false), - partial: "admin/procedures/draft_list", + current_administrateur.procedures.where(published: false, archived: false).order(created_at: :desc), + partial: "admin/procedures/list", array: true draft_class @@ -45,6 +45,17 @@ class Admin::ProceduresController < AdminController end + def destroy + procedure = Procedure.find(params[:id]) + + return render json: {}, status: 401 if procedure.published? || procedure.archived? + + procedure.destroy + + flash.notice = 'Procédure supprimée' + redirect_to admin_procedures_draft_path + end + def new @procedure ||= Procedure.new @procedure.module_api_carto ||= ModuleAPICarto.new diff --git a/app/decorators/commentaire_decorator.rb b/app/decorators/commentaire_decorator.rb index 9cf845b95..06b752c3b 100644 --- a/app/decorators/commentaire_decorator.rb +++ b/app/decorators/commentaire_decorator.rb @@ -2,7 +2,7 @@ class CommentaireDecorator < Draper::Decorator delegate_all def created_at_fr - created_at.to_datetime.strftime('%d/%m/%Y - %H:%M') + created_at.localtime.strftime('%d/%m/%Y - %H:%M') rescue 'dd/mm/YYYY - HH:MM' end diff --git a/app/decorators/dossier_decorator.rb b/app/decorators/dossier_decorator.rb index e39477d51..a1b76c80d 100644 --- a/app/decorators/dossier_decorator.rb +++ b/app/decorators/dossier_decorator.rb @@ -3,7 +3,7 @@ class DossierDecorator < Draper::Decorator delegate_all def display_date - date_previsionnelle.to_date.strftime('%d/%m/%Y') + date_previsionnelle.localtime.strftime('%d/%m/%Y') rescue 'dd/mm/YYYY' end diff --git a/app/decorators/procedure_decorator.rb b/app/decorators/procedure_decorator.rb index 445753b64..b0b38bea2 100644 --- a/app/decorators/procedure_decorator.rb +++ b/app/decorators/procedure_decorator.rb @@ -5,6 +5,10 @@ class ProcedureDecorator < Draper::Decorator h.new_users_dossiers_url(procedure_id: id) end + def created_at_fr + created_at.localtime.strftime('%d/%m/%Y %H:%M') + end + def logo_img return 'logo-tps.png' if logo.blank? logo diff --git a/app/models/gestionnaire.rb b/app/models/gestionnaire.rb index 94ee10e21..e01fe47a2 100644 --- a/app/models/gestionnaire.rb +++ b/app/models/gestionnaire.rb @@ -4,7 +4,7 @@ class Gestionnaire < ActiveRecord::Base has_and_belongs_to_many :administrateurs - has_many :assign_to + has_many :assign_to, dependent: :destroy has_many :procedures, through: :assign_to has_many :dossiers, through: :procedures diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 5f4de31f6..de66b000e 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -1,13 +1,13 @@ class Procedure < ActiveRecord::Base has_many :types_de_piece_justificative, dependent: :destroy has_many :types_de_champ, dependent: :destroy - has_many :dossiers, dependent: :destroy + has_many :dossiers has_one :module_api_carto, dependent: :destroy belongs_to :administrateur - has_many :assign_to + has_many :assign_to, dependent: :destroy has_many :gestionnaires, through: :assign_to delegate :use_api_carto, to: :module_api_carto diff --git a/app/views/admin/procedures/_draft_list.html.haml b/app/views/admin/procedures/_draft_list.html.haml deleted file mode 100644 index b169f2fc5..000000000 --- a/app/views/admin/procedures/_draft_list.html.haml +++ /dev/null @@ -1,21 +0,0 @@ -- unless smart_listing.empty? - %table.table - %thead - %th#ID= smart_listing.sortable 'ID', 'id' - %th#libelle= smart_listing.sortable 'Libellé', 'libelle' - %th#lien Actions - - - @procedures.each do |procedure| - - procedure = procedure.decorate - %tr - %td.col-md-1.col-lg-1= procedure.id - %td.col-md-6.col-lg-6 - = link_to(procedure.libelle, "/admin/procedures/#{procedure.id}") - %td= link_to('cloner', admin_procedure_clone_path(procedure.id), 'data-method' => :put, class: 'btn-xs btn-primary') - - = smart_listing.paginate - = smart_listing.pagination_per_page_links - -- else - %h4.center - Aucune procédure diff --git a/app/views/admin/procedures/_list.html.haml b/app/views/admin/procedures/_list.html.haml index 4735d527f..ed729fc6d 100644 --- a/app/views/admin/procedures/_list.html.haml +++ b/app/views/admin/procedures/_list.html.haml @@ -3,7 +3,9 @@ %thead %th#ID= smart_listing.sortable 'ID', 'id' %th#libelle= smart_listing.sortable 'Libellé', 'libelle' - %th#lien Lien + - unless @draft_class + %th#lien Lien + %th#created_at= smart_listing.sortable 'Date création', 'created_at' %th#lien Actions - @procedures.each do |procedure| @@ -12,8 +14,14 @@ %td.col-md-1.col-lg-1= procedure.id %td.col-md-6.col-lg-6 = link_to(procedure.libelle, "/admin/procedures/#{procedure.id}") - %td= link_to procedure.lien, procedure.lien - %td= link_to('cloner', admin_procedure_clone_path(procedure.id), 'data-method' => :put, class: 'btn-xs btn-primary') + - unless @draft_class + %td= link_to procedure.lien, procedure.lien + %td + = procedure.created_at_fr + %td + = link_to('Cloner', admin_procedure_clone_path(procedure.id), 'data-method' => :put, class: 'btn-sm btn-primary') + - unless procedure.published? || procedure.archived? + = link_to('X', url_for(controller: 'admin/procedures', action: :destroy, id: procedure.id), 'data-method' => :delete, class: 'btn-sm btn-danger') = smart_listing.paginate = smart_listing.pagination_per_page_links diff --git a/spec/controllers/admin/procedures_controller_spec.rb b/spec/controllers/admin/procedures_controller_spec.rb index 8d8779dc3..6bd380b79 100644 --- a/spec/controllers/admin/procedures_controller_spec.rb +++ b/spec/controllers/admin/procedures_controller_spec.rb @@ -52,6 +52,47 @@ describe Admin::ProceduresController, type: :controller do it { expect(response.status).to eq(200) } end + describe 'DELETE #destroy' do + + let(:procedure_draft) { create :procedure, published: false, archived: false } + let(:procedure_published) { create :procedure, published: true, archived: false } + let(:procedure_archived) { create :procedure, published: false, archived: true } + + subject { delete :destroy, id: procedure.id } + + context 'when procedure is draft' do + let!(:procedure) { procedure_draft } + + describe 'tech params' do + before do + subject + end + + it { expect(subject.status).to eq 302 } + it { expect(flash[:notice]).to be_present } + end + + it 'destroy procedure is call' do + expect_any_instance_of(Procedure).to receive(:destroy) + subject + end + + it { expect { subject }.to change{Procedure.count}.by(-1) } + end + + context 'when procedure is published' do + let(:procedure) { procedure_published } + + it { expect(subject.status).to eq 401 } + end + + context 'when procedure is archived' do + let(:procedure) { procedure_published } + + it { expect(subject.status).to eq 401 } + end + end + describe 'GET #edit' do let(:published) { false } let(:procedure) { create(:procedure, administrateur: admin, published: published) } @@ -270,7 +311,7 @@ describe Admin::ProceduresController, type: :controller do let!(:procedure) { create(:procedure, administrateur: admin) } subject { put :clone, procedure_id: procedure.id } - it { expect{ subject }.to change(Procedure, :count).by(1) } + it { expect { subject }.to change(Procedure, :count).by(1) } context 'when admin is the owner of the procedure' do before do From 2fa10a43e5383e8333a8a66d22af91291d870c6e Mon Sep 17 00:00:00 2001 From: Xavier J Date: Tue, 21 Jun 2016 12:41:28 +0200 Subject: [PATCH 31/33] =?UTF-8?q?Fix=20bug=20pr=C3=A9visualisation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/dossier.rb | 2 +- spec/models/dossier_spec.rb | 43 +++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 354cdb661..80e212454 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -51,7 +51,7 @@ class Dossier < ActiveRecord::Base end def ordered_champs - champs.joins(', types_de_champ').where('champs.type_de_champ_id = types_de_champ.id').order('order_place') + champs.joins(', types_de_champ').where("champs.type_de_champ_id = types_de_champ.id AND types_de_champ.procedure_id = #{procedure.id}").order('order_place') end def ordered_commentaires diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index ff1ba94a7..c9ae004e5 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -594,4 +594,47 @@ describe Dossier do it { expect(dossier.autorisation_donnees).to be_falsey } end end + + describe '#ordered_champs' do + let!(:procedure_1) { create :procedure } + let!(:procedure_2) { create :procedure } + + let(:dossier_1) { Dossier.new(id: 0, procedure: procedure_1) } + let(:dossier_2) { Dossier.new(id: 0, procedure: procedure_2) } + + before do + create :type_de_champ, libelle: 'type_1_1', order_place: 1, procedure: dossier_1.procedure + create :type_de_champ, libelle: 'type_1_2', order_place: 2, procedure: dossier_1.procedure + + create :type_de_champ, libelle: 'type_2_1', order_place: 1, procedure: dossier_2.procedure + create :type_de_champ, libelle: 'type_2_2', order_place: 2, procedure: dossier_2.procedure + create :type_de_champ, libelle: 'type_2_3', order_place: 3, procedure: dossier_2.procedure + + dossier_1.build_default_champs + dossier_2.build_default_champs + end + + subject { dossier.ordered_champs } + + it { expect(Champ.where(dossier_id: 0).size).to eq 5 } + + describe 'for dossier 1' do + let(:dossier) { dossier_1 } + + it { expect(subject.size).to eq 2 } + it { expect(subject.first.type_de_champ.libelle).to eq 'type_1_1' } + it { expect(subject.last.type_de_champ.libelle).to eq 'type_1_2' } + end + + describe 'for dossier 2' do + let(:dossier) { dossier_2 } + + it { expect(subject.size).to eq 3 } + + it { expect(subject.first.type_de_champ.libelle).to eq 'type_2_1' } + it { expect(subject.second.type_de_champ.libelle).to eq 'type_2_2' } + it { expect(subject.last.type_de_champ.libelle).to eq 'type_2_3' } + end + + end end From 4355d4775a38914abc57350c0e2ce759d64450ae Mon Sep 17 00:00:00 2001 From: Xavier J Date: Wed, 22 Jun 2016 11:25:41 +0200 Subject: [PATCH 32/33] Add type de champ date Add hour and minute on UI for datetime --- app/assets/stylesheets/description.scss | 15 ++++-- .../users/description_controller.rb | 8 ++++ app/models/champ.rb | 23 +++++++++- app/models/type_de_champ.rb | 1 + app/views/users/description/_champs.html.haml | 20 +++----- .../description/champs/_civilite.html.haml | 7 +++ .../description/champs/_datetime.html.haml | 21 +++++++++ .../description/champs/_textarea.html.haml | 5 ++ ...onvert_all_datetime_to_date_on_database.rb | 15 ++++++ db/schema.rb | 2 +- .../users/description_controller_spec.rb | 46 ++++++++++++------- spec/factories/procedure.rb | 8 ++++ .../users/description/show.html.haml_spec.rb | 14 +++++- 13 files changed, 148 insertions(+), 37 deletions(-) create mode 100644 app/views/users/description/champs/_civilite.html.haml create mode 100644 app/views/users/description/champs/_datetime.html.haml create mode 100644 app/views/users/description/champs/_textarea.html.haml create mode 100644 db/migrate/20160622081321_convert_all_datetime_to_date_on_database.rb diff --git a/app/assets/stylesheets/description.scss b/app/assets/stylesheets/description.scss index e40c2d551..6bef57d7f 100644 --- a/app/assets/stylesheets/description.scss +++ b/app/assets/stylesheets/description.scss @@ -77,12 +77,21 @@ } } -.type_champ-datetime { +.type_champ-date { @extend .col-md-2; @extend .col-lg-2; - input[type='number'] { - width: 100%; + input[type='date'] { + width: 120px; + } +} + +.type_champ-datetime { + @extend .col-md-4; + @extend .col-lg-4; + + input[type='datetime'] { + width: 120px; } } diff --git a/app/controllers/users/description_controller.rb b/app/controllers/users/description_controller.rb index ea9a9954f..dfe9672d3 100644 --- a/app/controllers/users/description_controller.rb +++ b/app/controllers/users/description_controller.rb @@ -46,6 +46,14 @@ class Users::DescriptionController < UsersController @dossier.champs.each do |champ| champ.value = params[:champs]["'#{champ.id}'"] + if champ.type_champ == 'datetime' + champ.value = params[:champs]["'#{champ.id}'"]+ + ' ' + + params[:time_hour]["'#{champ.id}'"] + + ':' + + params[:time_minute]["'#{champ.id}'"] + end + if champ.mandatory? && (champ.value.nil? || champ.value.blank?) flash.now.alert = "Le champ #{champ.libelle} doit être rempli." return render 'show' diff --git a/app/models/champ.rb b/app/models/champ.rb index 593acfe97..7b6c357b9 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -9,7 +9,28 @@ class Champ < ActiveRecord::Base end def data_provide - return 'datepicker' if type_champ == 'datetime' + return 'datepicker' if type_champ == 'datetime' || type_champ == 'date' return 'typeahead' if type_champ == 'address' end + + def data_date_format + ('dd/mm/yyyy' if type_champ == 'datetime' || type_champ == 'date') + end + + def same_hour? num + same_date? num, '%H' + end + + def same_minute? num + same_date? num, '%M' + end + + def same_date? num, compare + if type_champ == 'datetime' && !value.nil? + if value.to_datetime.strftime(compare) == num + return true + end + end + false + end end diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 98b148447..3f3bdf1f4 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -2,6 +2,7 @@ class TypeDeChamp < ActiveRecord::Base enum type_champs: { text: 'text', textarea: 'textarea', + date: 'date', datetime: 'datetime', number: 'number', checkbox: 'checkbox', diff --git a/app/views/users/description/_champs.html.haml b/app/views/users/description/_champs.html.haml index db6decbf3..df175b482 100644 --- a/app/views/users/description/_champs.html.haml +++ b/app/views/users/description/_champs.html.haml @@ -15,19 +15,13 @@ = '*' -if champ.type_champ == 'textarea' - %textarea.form-control.wysihtml5{name:"champs['#{champ.id}']", - placeholder: champ.description, - id: "champs_#{champ.id}", - row: '6'} - =champ.value - -elsif champ.type_champ == 'civilite' - %label.radio-inline - = radio_button_tag "champs['#{champ.id}']", "M.", champ.value == 'Mme' ? false : true - Monsieur + =render partial: 'users/description/champs/textarea', locals: {champ: champ} - %label.radio-inline - = radio_button_tag "champs['#{champ.id}']", "Mme", champ.value == 'Mme' - Madame + -elsif champ.type_champ == 'civilite' + =render partial: 'users/description/champs/civilite', locals: {champ: champ} + + - elsif champ.type_champ == 'datetime' + =render partial: 'users/description/champs/datetime', locals: {champ: champ} -else %input.form-control{name:"champs['#{champ.id}']", @@ -36,7 +30,7 @@ value: champ.value, type: champ.type_champ, 'data-provide' => champ.data_provide, - 'data-date-format' => ('dd/mm/yyyy' if champ.type_champ == 'datetime')} + 'data-date-format' => champ.data_date_format} - unless champ.description.empty? .row diff --git a/app/views/users/description/champs/_civilite.html.haml b/app/views/users/description/champs/_civilite.html.haml new file mode 100644 index 000000000..1a9ad45f3 --- /dev/null +++ b/app/views/users/description/champs/_civilite.html.haml @@ -0,0 +1,7 @@ +%label.radio-inline + = radio_button_tag "champs['#{champ.id}']", "M.", champ.value == 'Mme' ? false : true + Monsieur + +%label.radio-inline + = radio_button_tag "champs['#{champ.id}']", "Mme", champ.value == 'Mme' + Madame \ No newline at end of file diff --git a/app/views/users/description/champs/_datetime.html.haml b/app/views/users/description/champs/_datetime.html.haml new file mode 100644 index 000000000..557704485 --- /dev/null +++ b/app/views/users/description/champs/_datetime.html.haml @@ -0,0 +1,21 @@ +%input.form-control{name:"champs['#{champ.id}']", + placeholder: champ.libelle, + id: "champs_#{champ.id}", + value: (champ.value.split(/[ ][0-9]*:[0-9]*/).first unless champ.value.nil?), + type: champ.type_champ, + 'data-provide' => champ.data_provide, + 'data-date-format' => champ.data_date_format} + +%select.form-control{name:"time_hour['#{champ.id}']", style: 'margin-left: 5px', id:"time_hour_#{champ.id}"} + - (0..23).each do |num| + - num = "%.2i" %num + %option{value: num, selected: (:selected if champ.same_hour?(num))} + = num +h +%select.form-control{name:"time_minute['#{champ.id}']", id:"time_minute_#{champ.id}"} + - (0..59).each do |num| + - num = "%.2i" %num + - if num.to_i%5 == 0 + %option{value: num, selected: (:selected if champ.same_minute?(num))} + = num +min \ No newline at end of file diff --git a/app/views/users/description/champs/_textarea.html.haml b/app/views/users/description/champs/_textarea.html.haml new file mode 100644 index 000000000..4c062a300 --- /dev/null +++ b/app/views/users/description/champs/_textarea.html.haml @@ -0,0 +1,5 @@ +%textarea.form-control.wysihtml5{name:"champs['#{champ.id}']", + placeholder: champ.description, + id: "champs_#{champ.id}", + row: '6'} + =champ.value \ No newline at end of file diff --git a/db/migrate/20160622081321_convert_all_datetime_to_date_on_database.rb b/db/migrate/20160622081321_convert_all_datetime_to_date_on_database.rb new file mode 100644 index 000000000..7d6d09bd0 --- /dev/null +++ b/db/migrate/20160622081321_convert_all_datetime_to_date_on_database.rb @@ -0,0 +1,15 @@ +class ConvertAllDatetimeToDateOnDatabase < ActiveRecord::Migration + + class TypeDeChamp < ActiveRecord::Base + + end + + def change + TypeDeChamp.all.each do |type_de_champ| + if type_de_champ.type_champ == 'datetime' + type_de_champ.type_champ = 'date' + type_de_champ.save + end + end + end +end diff --git a/db/schema.rb b/db/schema.rb index c990bd982..630778df7 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: 20160609145737) do +ActiveRecord::Schema.define(version: 20160622081321) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" diff --git a/spec/controllers/users/description_controller_spec.rb b/spec/controllers/users/description_controller_spec.rb index 29c8028d8..bcf08bcbb 100644 --- a/spec/controllers/users/description_controller_spec.rb +++ b/spec/controllers/users/description_controller_spec.rb @@ -1,9 +1,9 @@ require 'spec_helper' -describe Users::DescriptionController, type: :controller, vcr: { cassette_name: 'controllers_users_description_controller' } do +describe Users::DescriptionController, type: :controller, vcr: {cassette_name: 'controllers_users_description_controller'} do let(:user) { create(:user) } - let(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ, cerfa_flag: true) } + let(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ, :with_datetime, cerfa_flag: true) } let(:dossier) { create(:dossier, procedure: procedure, user: user) } let(:dossier_id) { dossier.id } @@ -67,11 +67,10 @@ describe Users::DescriptionController, type: :controller, vcr: { cassette_name: let(:description) { 'Description de test Coucou, je suis un saut à la ligne Je suis un double saut la ligne.' } context 'Tous les attributs sont bons' do - # TODO separer en deux tests : check donnees et check redirect describe 'Premier enregistrement des données' do before do dossier.draft! - post :create, dossier_id: dossier_id, nom_projet: nom_projet, description: description + post :create, dossier_id: dossier_id, nom_projet: nom_projet dossier.reload end @@ -79,12 +78,13 @@ describe Users::DescriptionController, type: :controller, vcr: { cassette_name: expect(response).to redirect_to("/users/dossiers/#{dossier_id}/recapitulatif") end + it { expect(dossier.nom_projet).to eq nom_projet } + it 'etat du dossier est soumis' do expect(dossier.state).to eq('initiated') end end - # TODO changer les valeurs des champs et check in bdd context 'En train de manipuler un dossier non brouillon' do before do dossier.initiated! @@ -119,7 +119,7 @@ describe Users::DescriptionController, type: :controller, vcr: { cassette_name: end context 'Quand la procédure accepte les CERFA' do - context 'Sauvegarde du CERFA PDF', vcr: { cassette_name: 'controllers_users_description_controller_save_cerfa' } do + context 'Sauvegarde du CERFA PDF', vcr: {cassette_name: 'controllers_users_description_controller_save_cerfa'} do before do post :create, dossier_id: dossier_id, nom_projet: nom_projet, @@ -128,7 +128,7 @@ describe Users::DescriptionController, type: :controller, vcr: { cassette_name: dossier.reload end - context 'when a CERFA PDF is sent', vcr: { cassette_name: 'controllers_users_description_controller_cerfa_is_sent' } do + context 'when a CERFA PDF is sent', vcr: {cassette_name: 'controllers_users_description_controller_cerfa_is_sent'} do subject { dossier.cerfa.first } it 'content' do @@ -180,13 +180,23 @@ describe Users::DescriptionController, type: :controller, vcr: { cassette_name: describe 'Sauvegarde des champs' do let(:champs_dossier) { dossier.champs } let(:dossier_champs_first) { 'test value' } + let(:dossier_date_value) { '23/06/2016' } + let(:dossier_hour_value) { '17' } + let(:dossier_minute_value) { '00' } before do post :create, {dossier_id: dossier_id, nom_projet: nom_projet, description: description, champs: { - "'#{dossier.champs.first.id}'" => dossier_champs_first + "'#{dossier.champs.first.id}'" => dossier_champs_first, + "'#{dossier.champs.second.id}'" => dossier_date_value + }, + time_hour: { + "'#{dossier.champs.second.id}'" => dossier_hour_value, + }, + time_minute: { + "'#{dossier.champs.second.id}'" => dossier_minute_value, } } dossier.reload @@ -195,6 +205,10 @@ describe Users::DescriptionController, type: :controller, vcr: { cassette_name: it { expect(dossier.champs.first.value).to eq(dossier_champs_first) } it { expect(response).to redirect_to users_dossier_recapitulatif_path } + context 'when champs is type_de_champ datetime' do + it { expect(dossier.champs.second.value).to eq(dossier_date_value+' '+dossier_hour_value+':'+dossier_minute_value) } + end + context 'when champs value is empty' do let(:dossier_champs_first) { '' } @@ -202,7 +216,7 @@ describe Users::DescriptionController, type: :controller, vcr: { cassette_name: it { expect(response).to redirect_to users_dossier_recapitulatif_path } context 'when champs is mandatory' do - let(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ_mandatory, cerfa_flag: true) } + let(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ_mandatory, :with_datetime, cerfa_flag: true) } it { expect(response).not_to redirect_to users_dossier_recapitulatif_path } it { expect(flash[:alert]).to be_present } @@ -210,7 +224,7 @@ describe Users::DescriptionController, type: :controller, vcr: { cassette_name: end end - context 'Sauvegarde des pièces justificatives', vcr: { cassette_name: 'controllers_users_description_controller_sauvegarde_pj' } do + context 'Sauvegarde des pièces justificatives', vcr: {cassette_name: 'controllers_users_description_controller_sauvegarde_pj'} do let(:all_pj_type) { dossier.procedure.type_de_piece_justificative_ids } before do post :create, {dossier_id: dossier_id, @@ -221,7 +235,7 @@ describe Users::DescriptionController, type: :controller, vcr: { cassette_name: dossier.reload end - describe 'clamav anti-virus presence', vcr: { cassette_name: 'controllers_users_description_controller_clamav_presence' } do + describe 'clamav anti-virus presence', vcr: {cassette_name: 'controllers_users_description_controller_clamav_presence'} do it 'ClamavService safe_file? is call' do expect(ClamavService).to receive(:safe_file?).twice @@ -230,8 +244,6 @@ describe Users::DescriptionController, type: :controller, vcr: { cassette_name: description: description, 'piece_justificative_'+all_pj_type[0].to_s => piece_justificative_0, 'piece_justificative_'+all_pj_type[1].to_s => piece_justificative_1} - - end end @@ -248,7 +260,7 @@ describe Users::DescriptionController, type: :controller, vcr: { cassette_name: end end - describe 'POST #pieces_justificatives', vcr: { cassette_name: 'controllers_users_description_controller_pieces_justificatives' } do + describe 'POST #pieces_justificatives', vcr: {cassette_name: 'controllers_users_description_controller_pieces_justificatives'} do let(:all_pj_type) { dossier.procedure.type_de_piece_justificative_ids } subject { patch :pieces_justificatives, {dossier_id: dossier.id, @@ -275,7 +287,7 @@ describe Users::DescriptionController, type: :controller, vcr: { cassette_name: end end - context 'when PJ have already a document', vcr: { cassette_name: 'controllers_users_description_controller_pj_already_exist' } do + context 'when PJ have already a document', vcr: {cassette_name: 'controllers_users_description_controller_pj_already_exist'} do before do create :piece_justificative, :rib, dossier: dossier, type_de_piece_justificative_id: all_pj_type[0] create :piece_justificative, :contrat, dossier: dossier, type_de_piece_justificative_id: all_pj_type[1] @@ -283,7 +295,7 @@ describe Users::DescriptionController, type: :controller, vcr: { cassette_name: it { expect(dossier.pieces_justificatives.size).to eq 2 } - context 'when upload two PJ', vcr: { cassette_name: 'controllers_users_description_controller_pj_already_exist_upload_2pj' } do + context 'when upload two PJ', vcr: {cassette_name: 'controllers_users_description_controller_pj_already_exist_upload_2pj'} do before do subject dossier.reload @@ -345,7 +357,7 @@ describe Users::DescriptionController, type: :controller, vcr: { cassette_name: it { expect(dossier.pieces_justificatives.size).to eq 2 } - context 'when upload two PJ', vcr: { cassette_name: 'controllers_users_description_controller_upload_2pj' } do + context 'when upload two PJ', vcr: {cassette_name: 'controllers_users_description_controller_upload_2pj'} do before do subject dossier.reload diff --git a/spec/factories/procedure.rb b/spec/factories/procedure.rb index 2614d06e6..376180656 100644 --- a/spec/factories/procedure.rb +++ b/spec/factories/procedure.rb @@ -36,6 +36,14 @@ FactoryGirl.define do end end + trait :with_datetime do + after(:build) do |procedure, _evaluator| + type_de_champ = create(:type_de_champ, mandatory: true, type_champ: :datetime) + + procedure.types_de_champ << type_de_champ + end + end + trait :with_two_type_de_piece_justificative do after(:build) do |procedure, _evaluator| rib = create(:type_de_piece_justificative, :rib) diff --git a/spec/views/users/description/show.html.haml_spec.rb b/spec/views/users/description/show.html.haml_spec.rb index 04e256317..a6a283dc4 100644 --- a/spec/views/users/description/show.html.haml_spec.rb +++ b/spec/views/users/description/show.html.haml_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe 'users/description/show.html.haml', type: :view do let(:user) { create(:user) } let(:cerfa_flag) { true } - let(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ, cerfa_flag: cerfa_flag) } + let(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ, :with_datetime, cerfa_flag: cerfa_flag) } let(:dossier) { create(:dossier, procedure: procedure, user: user) } let(:dossier_id) { dossier.id } @@ -82,8 +82,12 @@ describe 'users/description/show.html.haml', type: :view do context 'Champs' do let(:champs) { dossier.champs } + let(:types_de_champ) { procedure.types_de_champ.where(type_champ: 'datetime').first } + let(:champ_datetime) { champs.where(type_de_champ_id: types_de_champ.id).first } before do + champ_datetime.value = "22/06/2016 12:05" + champ_datetime.save render end @@ -98,6 +102,12 @@ describe 'users/description/show.html.haml', type: :view do it { expect(rendered).to have_css(".type_champ-#{subject.type_champ}") } it { expect(rendered).to have_css("#champs_#{subject.id}") } end + + describe 'datetime value is correctly setup when is not nil' do + it { expect(rendered).to have_css("input[type='datetime'][id='champs_#{champ_datetime.id}'][value='22/06/2016']") } + it { expect(rendered).to have_css("option[value='12'][selected='selected']")} + it { expect(rendered).to have_css("option[value='05'][selected='selected']")} + end end context 'Pièces justificatives' do @@ -156,7 +166,7 @@ describe 'users/description/show.html.haml', type: :view do end context 'when procedure have cerfa flag true' do - let(:procedure) {create(:procedure, cerfa_flag: true)} + let(:procedure) { create(:procedure, cerfa_flag: true) } it { expect(rendered).to have_content 'Documents administratifs' } end From b68b62b8f086558eeeda58be9a62da018c58c75c Mon Sep 17 00:00:00 2001 From: Xavier J Date: Wed, 22 Jun 2016 11:29:59 +0200 Subject: [PATCH 33/33] Disable Thread for API requests on new dossier --- app/services/dossier_service.rb | 36 ++++++++------------------------- 1 file changed, 8 insertions(+), 28 deletions(-) diff --git a/app/services/dossier_service.rb b/app/services/dossier_service.rb index 1c33fd769..d6ee9b4ae 100644 --- a/app/services/dossier_service.rb +++ b/app/services/dossier_service.rb @@ -7,38 +7,18 @@ class DossierService end def dossier_informations! - entreprise_thread = Thread.new { - @entreprise_adapter = SIADE::EntrepriseAdapter.new(DossierService.siren @siret) + @entreprise_adapter = SIADE::EntrepriseAdapter.new(DossierService.siren @siret) - @dossier.create_entreprise(@entreprise_adapter.to_params) - } + @dossier.create_entreprise(@entreprise_adapter.to_params) + @etablissement_adapter = SIADE::EtablissementAdapter.new(@siret) - etablissement_thread = Thread.new { - @etablissement_adapter = SIADE::EtablissementAdapter.new(@siret) + @dossier.create_etablissement(@etablissement_adapter.to_params) - @dossier.create_etablissement(@etablissement_adapter.to_params) - } + @rna_adapter = SIADE::RNAAdapter.new(@siret) + @dossier.entreprise.create_rna_information(@rna_adapter.to_params) - rna_thread = Thread.new { - @rna_adapter = SIADE::RNAAdapter.new(@siret) - - sleep(0.1) while entreprise_thread.alive? - - @dossier.entreprise.create_rna_information(@rna_adapter.to_params) - } - - exercices_thread = Thread.new { - @exercices_adapter = SIADE::ExercicesAdapter.new(@siret) - - sleep(0.1) while etablissement_thread.alive? - - @dossier.etablissement.exercices.create(@exercices_adapter.to_params) - } - - sleep(0.1) while entreprise_thread.alive? || - etablissement_thread.alive? || - rna_thread.alive? || - exercices_thread.alive? + @exercices_adapter = SIADE::ExercicesAdapter.new(@siret) + @dossier.etablissement.exercices.create(@exercices_adapter.to_params) @dossier.update_attributes(mandataire_social: mandataire_social?(@entreprise_adapter.mandataires_sociaux)) @dossier.etablissement.update_attributes(entreprise: @dossier.entreprise)