From f8e3397f824e49e300c66dec620d5c36d2a66f26 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 4 May 2022 12:41:39 +0200 Subject: [PATCH] refactor(turbo): password complexity check use turbo --- app/views/password_complexity/_field.html.haml | 2 +- app/views/password_complexity/show.js.erb | 3 --- app/views/password_complexity/show.turbo_stream.haml | 6 ++++++ spec/controllers/password_complexity_controller_spec.rb | 6 +++--- 4 files changed, 10 insertions(+), 7 deletions(-) delete mode 100644 app/views/password_complexity/show.js.erb create mode 100644 app/views/password_complexity/show.turbo_stream.haml diff --git a/app/views/password_complexity/_field.html.haml b/app/views/password_complexity/_field.html.haml index eaf29e700..2e031f574 100644 --- a/app/views/password_complexity/_field.html.haml +++ b/app/views/password_complexity/_field.html.haml @@ -1,4 +1,4 @@ -= form.password_field :password, autofocus: true, autocomplete: 'off', placeholder: 'Mot de passe', data: { remote: test_complexity, url: show_password_complexity_path } += form.password_field :password, autofocus: true, autocomplete: 'off', placeholder: 'Mot de passe', data: { controller: test_complexity ? 'turbo-input' : false, turbo_input_url_value: show_password_complexity_path } - if test_complexity #complexity-bar.password-complexity diff --git a/app/views/password_complexity/show.js.erb b/app/views/password_complexity/show.js.erb deleted file mode 100644 index 1d83ac45a..000000000 --- a/app/views/password_complexity/show.js.erb +++ /dev/null @@ -1,3 +0,0 @@ -<%= render_to_element('#complexity-label', partial: 'label', outer: true) %> -<%= render_to_element('#complexity-bar', partial: 'bar', outer: true) %> -<%= raw("document.querySelector('#submit-password').disabled = #{@score < @min_complexity || @length < @min_length};") %> diff --git a/app/views/password_complexity/show.turbo_stream.haml b/app/views/password_complexity/show.turbo_stream.haml new file mode 100644 index 000000000..3fd3648f6 --- /dev/null +++ b/app/views/password_complexity/show.turbo_stream.haml @@ -0,0 +1,6 @@ += turbo_stream.replace 'complexity-label', partial: 'label' += turbo_stream.replace 'complexity-bar', partial: 'bar' +- if @score < @min_complexity || @length < @min_length + = turbo_stream.disable 'submit-password' +- else + = turbo_stream.enable 'submit-password' diff --git a/spec/controllers/password_complexity_controller_spec.rb b/spec/controllers/password_complexity_controller_spec.rb index 7c60de3fc..b04985806 100644 --- a/spec/controllers/password_complexity_controller_spec.rb +++ b/spec/controllers/password_complexity_controller_spec.rb @@ -4,7 +4,7 @@ describe PasswordComplexityController, type: :controller do { user: { password: 'moderately complex password' } } end - subject { get :show, format: :js, params: params, xhr: true } + subject { get :show, format: :turbo_stream, params: params } it 'computes a password score' do subject @@ -27,8 +27,8 @@ describe PasswordComplexityController, type: :controller do it 'renders Javascript that updates the password complexity meter' do subject - expect(response.body).to include('#complexity-label') - expect(response.body).to include('#complexity-bar') + expect(response.body).to include('complexity-label') + expect(response.body).to include('complexity-bar') end end end