From a07ccf5449a4473373cdc15ce4705bcd4f167593 Mon Sep 17 00:00:00 2001 From: Rasmus Kjellberg <2277443+kjellberg@users.noreply.github.com> Date: Mon, 22 Apr 2024 13:12:42 +0200 Subject: [PATCH] refactor: move preferences controller to kiqr gem --- Gemfile.lock | 42 +++++++++---------- .../users/preferences_controller.rb | 27 ------------ .../preferences/_form.html.erb | 0 .../{users => kiqr}/preferences/edit.html.erb | 0 config/locales/kiqr.en.yml | 16 ------- config/routes.rb | 1 - config/routes/authentication.rb | 4 -- .../kiqr/preferences_controller.rb | 29 +++++++++++++ gems/kiqr/config/locales/kiqr/en.yml | 15 +++++++ gems/kiqr/lib/kiqr/rails/routes.rb | 2 + .../account_users_controller_test.rb | 2 +- .../accounts/invitations_controller_test.rb | 0 .../{ => kiqr}/accounts_controller_test.rb | 2 +- .../invitations_controller_test.rb | 2 +- .../onboarding_controller_test.rb | 2 +- .../preferences_controller_test.rb | 2 +- .../registrations_controller_test.rb | 2 +- .../sessions_controller_test.rb | 2 +- .../two_factor_controller_test.rb | 2 +- 19 files changed, 75 insertions(+), 77 deletions(-) delete mode 100644 app/controllers/users/preferences_controller.rb rename app/views/{users => kiqr}/preferences/_form.html.erb (100%) rename app/views/{users => kiqr}/preferences/edit.html.erb (100%) delete mode 100644 config/routes/authentication.rb create mode 100644 gems/kiqr/app/controllers/kiqr/preferences_controller.rb rename test/controllers/{ => kiqr}/account_users_controller_test.rb (95%) rename test/controllers/{ => kiqr}/accounts/invitations_controller_test.rb (100%) rename test/controllers/{ => kiqr}/accounts_controller_test.rb (96%) rename test/controllers/{users => kiqr}/invitations_controller_test.rb (94%) rename test/controllers/{users => kiqr}/onboarding_controller_test.rb (92%) rename test/controllers/{users => kiqr}/preferences_controller_test.rb (87%) rename test/controllers/{users => kiqr}/registrations_controller_test.rb (90%) rename test/controllers/{users => kiqr}/sessions_controller_test.rb (95%) rename test/controllers/{users => kiqr}/two_factor_controller_test.rb (96%) diff --git a/Gemfile.lock b/Gemfile.lock index 7cd5899..e2a8b2e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -121,7 +121,7 @@ GEM debug (1.9.2) irb (~> 1.10) reline (>= 0.3.8) - devise (4.9.3) + devise (4.9.4) bcrypt (~> 3.0) orm_adapter (~> 0.1) railties (>= 4.1.0) @@ -165,7 +165,7 @@ GEM jbuilder (2.11.5) actionview (>= 5.0.0) activesupport (>= 5.0.0) - json (2.7.1) + json (2.7.2) language_server-protocol (3.17.0.3) launchy (3.0.0) addressable (~> 2.8) @@ -188,9 +188,9 @@ GEM net-smtp marcel (1.0.4) matrix (0.4.2) - meta-tags (2.20.0) + meta-tags (2.21.0) actionpack (>= 6.0.0, < 7.2) - method_source (1.0.0) + method_source (1.1.0) mini_mime (1.1.5) minitest (5.22.3) msgpack (1.7.2) @@ -205,17 +205,17 @@ GEM net-smtp (0.5.0) net-protocol nio4r (2.7.1) - nokogiri (1.16.3-aarch64-linux) + nokogiri (1.16.4-aarch64-linux) racc (~> 1.4) - nokogiri (1.16.3-arm-linux) + nokogiri (1.16.4-arm-linux) racc (~> 1.4) - nokogiri (1.16.3-arm64-darwin) + nokogiri (1.16.4-arm64-darwin) racc (~> 1.4) - nokogiri (1.16.3-x86-linux) + nokogiri (1.16.4-x86-linux) racc (~> 1.4) - nokogiri (1.16.3-x86_64-darwin) + nokogiri (1.16.4-x86_64-darwin) racc (~> 1.4) - nokogiri (1.16.3-x86_64-linux) + nokogiri (1.16.4-x86_64-linux) racc (~> 1.4) orm_adapter (0.5.0) parallel (1.24.0) @@ -224,7 +224,7 @@ GEM racc psych (5.1.2) stringio - public_suffix (5.0.4) + public_suffix (5.0.5) public_uid (2.2.0) activerecord (> 4.2) puma (6.4.2) @@ -272,11 +272,11 @@ GEM thor (~> 1.0, >= 1.2.2) zeitwerk (~> 2.6) rainbow (3.1.1) - rake (13.1.0) + rake (13.2.1) rdoc (6.6.3.1) psych (>= 4.0.0) regexp_parser (2.9.0) - reline (0.5.0) + reline (0.5.2) io-console (~> 0.5) responders (3.1.1) actionpack (>= 5.2) @@ -303,7 +303,7 @@ GEM rubocop-performance (1.20.2) rubocop (>= 1.48.1, < 2.0) rubocop-ast (>= 1.30.0, < 2.0) - ruby-next-core (1.0.2) + ruby-next-core (1.0.3) ruby-progressbar (1.13.0) rubyzip (2.3.2) selenium-webdriver (4.19.0) @@ -349,17 +349,17 @@ GEM stimulus-rails (1.3.3) railties (>= 6.0.0) stringio (3.1.0) - tailwindcss-rails (2.3.0) + tailwindcss-rails (2.4.0) railties (>= 6.0.0) - tailwindcss-rails (2.3.0-aarch64-linux) + tailwindcss-rails (2.4.0-aarch64-linux) railties (>= 6.0.0) - tailwindcss-rails (2.3.0-arm-linux) + tailwindcss-rails (2.4.0-arm-linux) railties (>= 6.0.0) - tailwindcss-rails (2.3.0-arm64-darwin) + tailwindcss-rails (2.4.0-arm64-darwin) railties (>= 6.0.0) - tailwindcss-rails (2.3.0-x86_64-darwin) + tailwindcss-rails (2.4.0-x86_64-darwin) railties (>= 6.0.0) - tailwindcss-rails (2.3.0-x86_64-linux) + tailwindcss-rails (2.4.0-x86_64-linux) railties (>= 6.0.0) thor (1.3.1) timeout (0.4.1) @@ -370,7 +370,7 @@ GEM tzinfo (2.0.6) concurrent-ruby (~> 1.0) unicode-display_width (2.5.0) - view_component (3.11.0) + view_component (3.12.1) activesupport (>= 5.2.0, < 8.0) concurrent-ruby (~> 1.0) method_source (~> 1.0) diff --git a/app/controllers/users/preferences_controller.rb b/app/controllers/users/preferences_controller.rb deleted file mode 100644 index a430466..0000000 --- a/app/controllers/users/preferences_controller.rb +++ /dev/null @@ -1,27 +0,0 @@ -class Users::PreferencesController < ApplicationController - def edit - @user = current_user - end - - def update - @user = current_user - if @user.update(preferences_params) - redirect_to edit_user_preferences_path, notice: t(".updated") - else - render :edit, status: :unprocessable_entity - end - end - - private - - def options_for_locale - I18n.available_locales.map do |locale| - [I18n.t("languages.#{locale}"), locale] - end - end - helper_method :options_for_locale - - def preferences_params - params.require(:user).permit(:time_zone, :locale) - end -end diff --git a/app/views/users/preferences/_form.html.erb b/app/views/kiqr/preferences/_form.html.erb similarity index 100% rename from app/views/users/preferences/_form.html.erb rename to app/views/kiqr/preferences/_form.html.erb diff --git a/app/views/users/preferences/edit.html.erb b/app/views/kiqr/preferences/edit.html.erb similarity index 100% rename from app/views/users/preferences/edit.html.erb rename to app/views/kiqr/preferences/edit.html.erb diff --git a/config/locales/kiqr.en.yml b/config/locales/kiqr.en.yml index b7aa858..25976c3 100644 --- a/config/locales/kiqr.en.yml +++ b/config/locales/kiqr.en.yml @@ -32,22 +32,6 @@ en: languages: en: "English" sv: "Swedish" - users: - preferences: - edit: - title: "User preferences" - description: "Update your user preferences." - form: - time_zone: - label: "Time zone" - placeholder: "Select a time zone" - locale: - label: "Language" - prompt: "Select a language" - placeholder: "Select your preferred language" - submit_button: "Save changes" - update: - updated: "User preferences have been updated." account_mailer: invitation_email: welcome: "Welcome to %{app_name}" diff --git a/config/routes.rb b/config/routes.rb index 925f43d..1649fb3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,7 +9,6 @@ # These routes are required for the KIQR core to function properly. # Do not remove this unless you know what you're doing. kiqr_routes - draw :authentication # => Teamable scope # Routes inside this block will be prefixed with /team/ if diff --git a/config/routes/authentication.rb b/config/routes/authentication.rb deleted file mode 100644 index 8cf5a32..0000000 --- a/config/routes/authentication.rb +++ /dev/null @@ -1,4 +0,0 @@ -# User personal accounts. -scope module: :users, path: :users do - resource :preferences, only: %i[edit update], as: :user_preferences -end diff --git a/gems/kiqr/app/controllers/kiqr/preferences_controller.rb b/gems/kiqr/app/controllers/kiqr/preferences_controller.rb new file mode 100644 index 0000000..aedc5da --- /dev/null +++ b/gems/kiqr/app/controllers/kiqr/preferences_controller.rb @@ -0,0 +1,29 @@ +module Kiqr + class PreferencesController < ApplicationController + def edit + @user = current_user + end + + def update + @user = current_user + if @user.update(preferences_params) + redirect_to edit_user_preferences_path, notice: t(".updated") + else + render :edit, status: :unprocessable_entity + end + end + + private + + def options_for_locale + I18n.available_locales.map do |locale| + [I18n.t("languages.#{locale}"), locale] + end + end + helper_method :options_for_locale + + def preferences_params + params.require(:user).permit(:time_zone, :locale) + end + end +end diff --git a/gems/kiqr/config/locales/kiqr/en.yml b/gems/kiqr/config/locales/kiqr/en.yml index 657a7a0..75fab57 100644 --- a/gems/kiqr/config/locales/kiqr/en.yml +++ b/gems/kiqr/config/locales/kiqr/en.yml @@ -91,6 +91,21 @@ en: new: title: "Setup your personal account" description: "Finish the registration by setting up your user profile." + preferences: + edit: + title: "User preferences" + description: "Update your user preferences." + form: + time_zone: + label: "Time zone" + placeholder: "Select a time zone" + locale: + label: "Language" + prompt: "Select a language" + placeholder: "Select your preferred language" + submit_button: "Save changes" + update: + updated: "User preferences have been updated." registrations: new: heading: diff --git a/gems/kiqr/lib/kiqr/rails/routes.rb b/gems/kiqr/lib/kiqr/rails/routes.rb index 1d405bf..c3fba25 100644 --- a/gems/kiqr/lib/kiqr/rails/routes.rb +++ b/gems/kiqr/lib/kiqr/rails/routes.rb @@ -18,6 +18,7 @@ def kiqr_routes(options = {}) get "two-factor/disable", controller: options[:controllers][:two_factor], action: "disable", as: :disable_two_factor post "two-factor/verify", controller: options[:controllers][:two_factor], action: "verify", as: :verify_two_factor delete "two-factor/destroy", controller: options[:controllers][:two_factor], action: "destroy", as: :destroy_two_factor + resource :preferences, controller: options[:controllers][:preferences], only: %i[edit update], as: :user_preferences end end @@ -34,6 +35,7 @@ def default_controllers(options) options[:controllers][:registrations] ||= "kiqr/registrations" options[:controllers][:sessions] ||= "kiqr/sessions" options[:controllers][:two_factor] ||= "kiqr/two_factor" + options[:controllers][:preferences] ||= "kiqr/preferences" options end diff --git a/test/controllers/account_users_controller_test.rb b/test/controllers/kiqr/account_users_controller_test.rb similarity index 95% rename from test/controllers/account_users_controller_test.rb rename to test/controllers/kiqr/account_users_controller_test.rb index 4ddd748..abc8e51 100644 --- a/test/controllers/account_users_controller_test.rb +++ b/test/controllers/kiqr/account_users_controller_test.rb @@ -1,6 +1,6 @@ require "test_helper" -class AccountUsersControllerTest < ActionDispatch::IntegrationTest +class Kiqr::AccountUsersControllerTest < ActionDispatch::IntegrationTest test "can't show members as personal account" do user = create(:user) sign_in user diff --git a/test/controllers/accounts/invitations_controller_test.rb b/test/controllers/kiqr/accounts/invitations_controller_test.rb similarity index 100% rename from test/controllers/accounts/invitations_controller_test.rb rename to test/controllers/kiqr/accounts/invitations_controller_test.rb diff --git a/test/controllers/accounts_controller_test.rb b/test/controllers/kiqr/accounts_controller_test.rb similarity index 96% rename from test/controllers/accounts_controller_test.rb rename to test/controllers/kiqr/accounts_controller_test.rb index 1141905..9f3a634 100644 --- a/test/controllers/accounts_controller_test.rb +++ b/test/controllers/kiqr/accounts_controller_test.rb @@ -1,6 +1,6 @@ require "test_helper" -class AccountsControllerTest < ActionDispatch::IntegrationTest +class Kiqr::AccountsControllerTest < ActionDispatch::IntegrationTest test "can render new account page" do sign_in create(:user) get new_account_path diff --git a/test/controllers/users/invitations_controller_test.rb b/test/controllers/kiqr/invitations_controller_test.rb similarity index 94% rename from test/controllers/users/invitations_controller_test.rb rename to test/controllers/kiqr/invitations_controller_test.rb index 073ce33..a3b3e01 100644 --- a/test/controllers/users/invitations_controller_test.rb +++ b/test/controllers/kiqr/invitations_controller_test.rb @@ -1,6 +1,6 @@ require "test_helper" -class Users::InvitationsControllerTest < ActionDispatch::IntegrationTest +class Kiqr::InvitationsControllerTest < ActionDispatch::IntegrationTest test "can view page without beeing signed in" do invitation = create(:account_invitation) get invitation_path(invitation) diff --git a/test/controllers/users/onboarding_controller_test.rb b/test/controllers/kiqr/onboarding_controller_test.rb similarity index 92% rename from test/controllers/users/onboarding_controller_test.rb rename to test/controllers/kiqr/onboarding_controller_test.rb index 1b25c47..dad2457 100644 --- a/test/controllers/users/onboarding_controller_test.rb +++ b/test/controllers/kiqr/onboarding_controller_test.rb @@ -1,6 +1,6 @@ require "test_helper" -class Users::OnboardingControllerTest < ActionDispatch::IntegrationTest +class Kiqr::OnboardingControllerTest < ActionDispatch::IntegrationTest test "redirects to dashboard_path if user is already onboarded" do sign_in create(:user) get onboarding_path diff --git a/test/controllers/users/preferences_controller_test.rb b/test/controllers/kiqr/preferences_controller_test.rb similarity index 87% rename from test/controllers/users/preferences_controller_test.rb rename to test/controllers/kiqr/preferences_controller_test.rb index 25c3629..57e0f8f 100644 --- a/test/controllers/users/preferences_controller_test.rb +++ b/test/controllers/kiqr/preferences_controller_test.rb @@ -1,6 +1,6 @@ require "test_helper" -class Users::PreferencesControllerTest < ActionDispatch::IntegrationTest +class Kiqr::PreferencesControllerTest < ActionDispatch::IntegrationTest test "should get edit page" do user = create(:user) sign_in(user) diff --git a/test/controllers/users/registrations_controller_test.rb b/test/controllers/kiqr/registrations_controller_test.rb similarity index 90% rename from test/controllers/users/registrations_controller_test.rb rename to test/controllers/kiqr/registrations_controller_test.rb index fc94807..3e9c50d 100644 --- a/test/controllers/users/registrations_controller_test.rb +++ b/test/controllers/kiqr/registrations_controller_test.rb @@ -1,6 +1,6 @@ require "test_helper" -class Users::RegistrationControllerTest < ActionDispatch::IntegrationTest +class Kiqr::RegistrationControllerTest < ActionDispatch::IntegrationTest test "can view cancellation page" do sign_in create(:user) get cancel_user_registration_path diff --git a/test/controllers/users/sessions_controller_test.rb b/test/controllers/kiqr/sessions_controller_test.rb similarity index 95% rename from test/controllers/users/sessions_controller_test.rb rename to test/controllers/kiqr/sessions_controller_test.rb index 12992af..db432ce 100644 --- a/test/controllers/users/sessions_controller_test.rb +++ b/test/controllers/kiqr/sessions_controller_test.rb @@ -1,6 +1,6 @@ require "test_helper" -class Users::SessionsControllerTest < ActionDispatch::IntegrationTest +class Kiqr::SessionsControllerTest < ActionDispatch::IntegrationTest test "can sign in if two factor is disabled" do user = create(:user) post user_session_path, params: {user: {email: user.email, password: user.password}} diff --git a/test/controllers/users/two_factor_controller_test.rb b/test/controllers/kiqr/two_factor_controller_test.rb similarity index 96% rename from test/controllers/users/two_factor_controller_test.rb rename to test/controllers/kiqr/two_factor_controller_test.rb index d5895f5..2238639 100644 --- a/test/controllers/users/two_factor_controller_test.rb +++ b/test/controllers/kiqr/two_factor_controller_test.rb @@ -1,6 +1,6 @@ require "test_helper" -class Users::TwoFactorControllerTest < ActionDispatch::IntegrationTest +class Kiqr::TwoFactorControllerTest < ActionDispatch::IntegrationTest test "should not be able to setup 2fa if already enabled" do sign_in create(:user, :otp_enabled) get setup_two_factor_path