From 4d1cb9ecfe42e27e74f579b5e2b7b16b9796c8a3 Mon Sep 17 00:00:00 2001 From: Xavier J Date: Thu, 26 May 2016 15:59:50 +0200 Subject: [PATCH] Connexion page is contextualized with procedure title and description when is access with link. --- app/controllers/users/sessions_controller.rb | 27 ++++++++++++--- app/models/procedure.rb | 4 +++ app/services/number_service.rb | 5 +++ .../users/dossiers/_title_procedure.html.haml | 11 ++++++ app/views/users/dossiers/new.html.haml | 17 ++-------- app/views/users/sessions/new.html.haml | 8 ++++- config/routes.rb | 1 + .../users/sessions_controller_spec.rb | 29 ++++++++++++++++ .../users/sessions/new.html.haml_spec.rb | 34 +++++++++++++++++++ 9 files changed, 115 insertions(+), 21 deletions(-) create mode 100644 app/services/number_service.rb create mode 100644 app/views/users/dossiers/_title_procedure.html.haml create mode 100644 spec/views/users/sessions/new.html.haml_spec.rb diff --git a/app/controllers/users/sessions_controller.rb b/app/controllers/users/sessions_controller.rb index c3221593f..40c598220 100644 --- a/app/controllers/users/sessions_controller.rb +++ b/app/controllers/users/sessions_controller.rb @@ -11,7 +11,13 @@ 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)) + end + @user = User.new + rescue ActiveRecord::RecordNotFound + error_procedure end #POST /resource/sign_in @@ -39,10 +45,21 @@ class Users::SessionsController < Sessions::SessionsController end end -# protected + def no_procedure + session['user_return_to'] = nil + redirect_to new_user_session_path + end -# You can put the params you want to permit in the empty array. -# def configure_sign_in_params -# devise_parameter_sanitizer.for(:sign_in) << :attribute -# end + private + + def error_procedure + flash.alert = t('errors.messages.procedure_not_found') + redirect_to url_for root_path + end + + def user_return_to_procedure_id + return nil if session["user_return_to"].nil? + + NumberService.to_number session["user_return_to"].split("?procedure_id=").second + end end diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 8f3fa1eef..41aee684c 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -25,6 +25,10 @@ class Procedure < ActiveRecord::Base types_de_champ.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 return false if index_of_first_element < 0 types_de_champ_tmp = types_de_champ_ordered diff --git a/app/services/number_service.rb b/app/services/number_service.rb new file mode 100644 index 000000000..24a52f9b2 --- /dev/null +++ b/app/services/number_service.rb @@ -0,0 +1,5 @@ +class NumberService + def self.to_number string + string.to_s if Float(string) rescue nil + end +end \ 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 new file mode 100644 index 000000000..160a05412 --- /dev/null +++ b/app/views/users/dossiers/_title_procedure.html.haml @@ -0,0 +1,11 @@ +- 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 index 2103965e1..992b2f842 100644 --- a/app/views/users/dossiers/new.html.haml +++ b/app/views/users/dossiers/new.html.haml @@ -1,20 +1,7 @@ .container.center#users_siret_index .row - %p.lead{id: 'pro_section'} - %br - %br - - - 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 + %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| diff --git a/app/views/users/sessions/new.html.haml b/app/views/users/sessions/new.html.haml index e8e893304..028fd6d2a 100644 --- a/app/views/users/sessions/new.html.haml +++ b/app/views/users/sessions/new.html.haml @@ -1,6 +1,12 @@ #form_login - = image_tag('logo-tps.png') %br + - if @dossier + = link_to 'X', users_no_procedure_url, class: 'btn btn-xs', style:'float: right' + = render partial: 'users/dossiers/title_procedure' + - else + = image_tag('logo-tps.png', {id: 'logo_tps'}) + %br + %h2#login_user Connexion %a.btn_fc#btn_fcp{href: '/france_connect/particulier'} diff --git a/config/routes.rb b/config/routes.rb index 06c348e58..37caf1f5a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -18,6 +18,7 @@ Rails.application.routes.draw do devise_scope :user do get '/users/sign_in/demo' => 'users/sessions#demo' + get '/users/no_procedure' => 'users/sessions#no_procedure' end devise_scope :gestionnaire do diff --git a/spec/controllers/users/sessions_controller_spec.rb b/spec/controllers/users/sessions_controller_spec.rb index 7f9e26027..256913b1f 100644 --- a/spec/controllers/users/sessions_controller_spec.rb +++ b/spec/controllers/users/sessions_controller_spec.rb @@ -67,4 +67,33 @@ describe Users::SessionsController, type: :controller do end end end + + describe '.new' do + subject { get :new } + + context 'when procedure_id is not present in user_return_to session params' do + it { expect(subject.status).to eq 200} + end + + context 'when procedure_id is present in user_return_to session params' do + context 'when procedure_id does not exist' do + before do + session["user_return_to"] = '?procedure_id=0' + 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 } + + before do + session["user_return_to"] = "?procedure_id=#{procedure.id}" + end + + it { expect(subject.status).to eq 200} + end + end + end end \ No newline at end of file diff --git a/spec/views/users/sessions/new.html.haml_spec.rb b/spec/views/users/sessions/new.html.haml_spec.rb new file mode 100644 index 000000000..d7f6989b9 --- /dev/null +++ b/spec/views/users/sessions/new.html.haml_spec.rb @@ -0,0 +1,34 @@ +require 'spec_helper' + +describe 'users/sessions/new.html.haml', type: :view do + let(:dossier) { create :dossier } + + before(:each) do + allow(view).to receive(:devise_mapping).and_return(Devise.mappings[:user]) + allow(view).to receive(:resource_name).and_return(:user) + end + + before do + assign(:user, User.new) + end + + context 'when user_return_to session params contains a procedure_id' do + before do + assign(:dossier, dossier) + render + end + + it { expect(rendered).to have_selector('#form_login #logo_procedure') } + it { expect(rendered).to have_selector('#form_login #titre_procedure') } + it { expect(rendered).to have_content(dossier.procedure.libelle) } + it { expect(rendered).to have_content(dossier.procedure.description) } + end + + context 'when user_return_to session params not contains a procedure_id' do + before do + render + end + + it { expect(rendered).to have_selector('#form_login #logo_tps') } + end +end \ No newline at end of file