From 9dfb13eff63eceae4e832e0f6c89d711c3712ec9 Mon Sep 17 00:00:00 2001 From: Kevin Vinhas Date: Tue, 26 Jan 2021 23:42:36 +0100 Subject: [PATCH] Adding notifications_enabled attribute to users This diff just contains the migration and profile edition. Refers to [This Project Card](https://github.com/riggraz/astuto/projects/1#card-31194036) and #33 --- app/controllers/application_controller.rb | 4 +-- app/views/devise/registrations/edit.html.erb | 5 +++ app/views/devise/registrations/new.html.erb | 9 ++++-- ...dd_notifications_enabled_field_to_users.rb | 5 +++ db/schema.rb | 3 +- spec/factories/users.rb | 7 ++-- spec/system/user_edit_profile_spec.rb | 32 ++++++++++++++++--- spec/system/user_sign_up_spec.rb | 21 +++++++++--- 8 files changed, 70 insertions(+), 16 deletions(-) create mode 100644 db/migrate/20210126215831_add_notifications_enabled_field_to_users.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index b79cc64f..182b54ef 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -5,8 +5,8 @@ class ApplicationController < ActionController::Base protected def configure_permitted_parameters - devise_parameter_sanitizer.permit(:sign_up, keys: [:full_name]) - devise_parameter_sanitizer.permit(:account_update, keys: [:full_name]) + devise_parameter_sanitizer.permit(:sign_up, keys: [:full_name, :notifications_enabled]) + devise_parameter_sanitizer.permit(:account_update, keys: [:full_name, :notifications_enabled]) end def load_boards diff --git a/app/views/devise/registrations/edit.html.erb b/app/views/devise/registrations/edit.html.erb index 862eb5b1..d1b3557d 100644 --- a/app/views/devise/registrations/edit.html.erb +++ b/app/views/devise/registrations/edit.html.erb @@ -16,6 +16,11 @@ +
+ <%= f.label :notifications_enabled %> + <%= f.check_box :notifications_enabled %> +
+ <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
Currently waiting confirmation for: <%= resource.unconfirmed_email %>
<% end %> diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb index b7e0b4ca..87d74f36 100644 --- a/app/views/devise/registrations/new.html.erb +++ b/app/views/devise/registrations/new.html.erb @@ -7,7 +7,7 @@ <%= f.label :full_name, class: "sr-only" %> <%= f.text_field :full_name, autofocus: true, placeholder: "Full name", required: true, class: "form-control" %> - +
<%= f.label :email, class: "sr-only" %> <%= f.email_field :email, autocomplete: "email", placeholder: "Email address", required: true, class: "form-control" %> @@ -23,9 +23,14 @@ <%= f.password_field :password_confirmation, placeholder: "Password confirmation", required: true, class: "form-control" %>
+
+ <%= f.label :notifications_enabled %> + <%= f.check_box :notifications_enabled %> +
+
<%= f.submit "Sign up", class: "btn btn-block" %>
<% end %> -<%= render "devise/shared/links" %> \ No newline at end of file +<%= render "devise/shared/links" %> diff --git a/db/migrate/20210126215831_add_notifications_enabled_field_to_users.rb b/db/migrate/20210126215831_add_notifications_enabled_field_to_users.rb new file mode 100644 index 00000000..dad89dc8 --- /dev/null +++ b/db/migrate/20210126215831_add_notifications_enabled_field_to_users.rb @@ -0,0 +1,5 @@ +class AddNotificationsEnabledFieldToUsers < ActiveRecord::Migration[6.0] + def change + add_column :users, :notifications_enabled, :boolean, null: false, default: true + end +end diff --git a/db/schema.rb b/db/schema.rb index c16adc13..cab0fff9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_10_01_160859) do +ActiveRecord::Schema.define(version: 2021_01_26_215831) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -84,6 +84,7 @@ ActiveRecord::Schema.define(version: 2019_10_01_160859) do t.datetime "updated_at", precision: 6, null: false t.integer "role" t.string "full_name" + t.boolean "notifications_enabled", default: true, null: false t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true diff --git a/spec/factories/users.rb b/spec/factories/users.rb index a47f4030..4bbbccb5 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -1,14 +1,15 @@ FactoryBot.define do factory :user do sequence(:email) { |n| "user#{n}@example.com" } - + full_name { 'First Last' } + notifications_enabled { true } password { 'password' } end factory :moderator, class: User do sequence(:email) { |n| "mod#{n}@example.com" } - + full_name { 'First Last' } password { 'password' } role { 'moderator' } @@ -16,7 +17,7 @@ FactoryBot.define do factory :admin, class: User do sequence(:email) { |n| "admin#{n}@example.com" } - + full_name { 'First Last' } password { 'password' } role { 'admin' } diff --git a/spec/system/user_edit_profile_spec.rb b/spec/system/user_edit_profile_spec.rb index f2380ce1..cd5d7858 100644 --- a/spec/system/user_edit_profile_spec.rb +++ b/spec/system/user_edit_profile_spec.rb @@ -4,10 +4,10 @@ require 'rails_helper' feature 'edit user profile settings', type: :system do let(:user) { FactoryBot.create(:user) } - before(:each) do + before(:each) do user.confirm # devise helper to confirm user account sign_in user # devise helper to login user - + # check that user is confirmed and saved in the db expect(user.confirmed_at).not_to be_nil expect(User.count).to eq(1) @@ -44,6 +44,30 @@ feature 'edit user profile settings', type: :system do expect(page).to have_css('.notice') end + scenario 'turns on notifications' do + user.update(notifications_enabled: false) + + visit edit_user_registration_path + check 'Notifications enabled' + fill_in 'Current password', with: user.password + click_button 'Update profile' + + user.reload + expect(user.notifications_enabled).to eq(true) + end + + scenario 'turns off notifications' do + user.update(notifications_enabled: user) + + visit edit_user_registration_path + uncheck 'Notifications enabled' + fill_in 'Current password', with: user.password + click_button 'Update profile' + + user.reload + expect(user.notifications_enabled).to eq(false) + end + # Remember that 'password' is just a virtual attribute (i.e. it is not stored in the db) # so updating the user account password does not update it, but only the # 'encrypted_password' attribute in the db @@ -91,9 +115,9 @@ feature 'edit user profile settings', type: :system do visit edit_user_registration_path click_button 'Cancel my account' page.driver.browser.switch_to.alert.accept # accepts js pop up - + expect(page).to have_current_path(root_path) expect(User.count).to eq(user_count - 1) expect(page).to have_css('.notice') end -end \ No newline at end of file +end diff --git a/spec/system/user_sign_up_spec.rb b/spec/system/user_sign_up_spec.rb index 7540f767..8ac4241f 100644 --- a/spec/system/user_sign_up_spec.rb +++ b/spec/system/user_sign_up_spec.rb @@ -9,6 +9,7 @@ feature 'sign up', type: :system do fill_in 'Email', with: user.email fill_in 'Password', with: user.password fill_in 'Password confirmation', with: user.password + check 'Notifications enabled' click_button 'Sign up' end @@ -19,14 +20,14 @@ feature 'sign up', type: :system do scenario 'with valid fields' do user_count = User.count - + sign_up_as user - + expect(User.count).to eq(user_count + 1) expect(page).to have_current_path(root_path) expect(page).to have_css('.notice') end - + scenario 'with invalid Full Name' do user_count = User.count @@ -75,4 +76,16 @@ feature 'sign up', type: :system do expect_to_be_on_sign_up_page expect(page).to have_css('.alert') end -end \ No newline at end of file + + scenario 'and disables notifications' do + visit new_user_registration_path + fill_in 'Full name', with: user.full_name + fill_in 'Email', with: user.email + fill_in 'Password', with: user.password + fill_in 'Password confirmation', with: user.password + uncheck 'Notifications enabled' + click_button 'Sign up' + + expect(User.last.notifications_enabled).to eq(false) + end +end