Skip to content

Commit

Permalink
refactor: separated services for users and teams. closes #25, closes #26
Browse files Browse the repository at this point in the history
, closes #17
  • Loading branch information
kjellberg committed Jun 1, 2024
1 parent c4de253 commit 703a3e0
Show file tree
Hide file tree
Showing 22 changed files with 220 additions and 176 deletions.
8 changes: 8 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ def ensure_onboarded
redirect_to onboarding_path if user_signed_in? && !current_user.onboarded?
end

# Get the options for the locale form select field
def options_for_locale
I18n.available_locales.map do |locale|
[I18n.t("languages.#{locale}"), locale]
end
end
helper_method :options_for_locale

# Override the method to change the sign-in redirect path
def after_sign_in_path_for(resource)
if session[:after_sign_in_path].present?
Expand Down
2 changes: 1 addition & 1 deletion app/views/kiqr/onboarding/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
description: t(".description")
) do %>
<main class="flex flex-col gap-4 w-full max-w-2xl">
<%= render "kiqr/accounts/form", account: @account %>
<%= render "kiqr/settings/form", user: @user %>
</main>
<% end %>
<% end %>
4 changes: 2 additions & 2 deletions app/views/kiqr/settings/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%= simple_form_for(user, url: settings_path, method: :patch) do |f| %>
<%= simple_form_for(user, url: form_submit_path, method: form_method) do |f| %>

<%= f.simple_fields_for :personal_account, user.personal_account do |pa| %>
<%= pa.input :name %>
<%= pa.input :name, label: t(".personal_account.name.label"), placeholder: t(".personal_account.name.placeholder") %>
<% end %>

<%= f.input :locale, label: t(".locale.label"), placeholder: t(".locale.placeholder"), required: true, autofocus: true, prompt: t(".locale.prompt"), as: :select, collection: options_for_locale %>
Expand Down
13 changes: 13 additions & 0 deletions gems/kiqr/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ PATH
kiqr (0.1.0)
devise (~> 4.9, >= 4.9.3)
devise-two-factor (~> 5.0.0)
omniauth (~> 2.1.1)
omniauth-rails_csrf_protection (~> 1.0.1)
public_uid (~> 2.2)
rails (>= 7.1.3.2)

Expand Down Expand Up @@ -112,6 +114,7 @@ GEM
i18n (>= 1.8.11, < 2)
globalid (1.2.1)
activesupport (>= 6.1)
hashie (5.0.0)
i18n (1.14.4)
concurrent-ruby (~> 1.0)
io-console (0.7.2)
Expand Down Expand Up @@ -142,6 +145,13 @@ GEM
nio4r (2.7.1)
nokogiri (1.16.3-arm64-darwin)
racc (~> 1.4)
omniauth (2.1.2)
hashie (>= 3.4.6)
rack (>= 2.2.3)
rack-protection
omniauth-rails_csrf_protection (1.0.1)
actionpack (>= 4.2)
omniauth (~> 2.0)
orm_adapter (0.5.0)
psych (5.1.2)
stringio
Expand All @@ -151,6 +161,9 @@ GEM
nio4r (~> 2.0)
racc (1.7.3)
rack (3.0.10)
rack-protection (4.0.0)
base64 (>= 0.1.0)
rack (>= 3.0.0, < 4)
rack-session (2.0.0)
rack (>= 3.0.0)
rack-test (2.1.0)
Expand Down
4 changes: 2 additions & 2 deletions gems/kiqr/app/controllers/kiqr/accounts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def create
@account = Account.new(account_params)

if @account.valid?
Kiqr::Services::Accounts::Create.call!(account: @account, user: current_user)
Kiqr::Services::Teams::Create.call!(account: @account, user: current_user)
kiqr_flash_message(:notice, :account_created)
redirect_to dashboard_path(account_id: @account)
else
Expand All @@ -22,7 +22,7 @@ def update
@account.assign_attributes(account_params)

if @account.valid?
Kiqr::Services::Accounts::Update.call!(account: @account, user: current_user)
Kiqr::Services::Teams::Update.call!(account: @account, user: current_user)
kiqr_flash_message(:notice, :account_updated)
redirect_to edit_account_path
else
Expand Down
29 changes: 21 additions & 8 deletions gems/kiqr/app/controllers/kiqr/onboarding_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,38 @@ class OnboardingController < KiqrController
end

def new
@account = current_user.build_personal_account(personal: true)
@user = current_user
@user.build_personal_account
end

def create
@account = Account.new(account_params)
@user = current_user
@user.assign_attributes(user_params)
@user.personal_account&.personal = true

if @account.valid?
Kiqr::Services::Accounts::Create.call!(account: @account, user: current_user, personal: true)
kiqr_flash_message(:notice, :account_created)
redirect_to after_onboarding_path
if @user.valid?
Kiqr::Services::Users::Update.call!(user: @user)
kiqr_flash_message(:notice, :settings_updated)
redirect_to after_sign_in_path_for(@user)
else
render :new, status: :unprocessable_entity
end
# @account = Account.new(account_params)

# if @account.valid?
# Kiqr::Services::Accounts::Create.call!(account: @account, user: current_user, personal: true)
# kiqr_flash_message(:notice, :account_created)
# redirect_to after_onboarding_path
# else
# render :new, status: :unprocessable_entity
# end
end

private

def account_params
params.require(:account).permit(Kiqr.config.account_attributes)
def user_params
personal_account_attributes = Kiqr.config.account_attributes.prepend(:id)
params.require(:user).permit(:time_zone, :locale, personal_account_attributes: personal_account_attributes)
end

# This is the path to redirect to after the onboarding process
Expand Down
24 changes: 15 additions & 9 deletions gems/kiqr/app/controllers/kiqr/settings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,34 @@ class SettingsController < KiqrController
before_action :setup_user, only: %i[edit update]

def update
if @user.update(preferences_params)
@user.assign_attributes(user_params)

if @user.valid?
Kiqr::Services::Users::Update.call!(user: @user)
kiqr_flash_message(:notice, :settings_updated)
redirect_to edit_settings_path
else
render :edit, status: :unprocessable_entity
end
end

def form_submit_path
settings_path
end
helper_method :form_submit_path

def form_method
:patch
end
helper_method :form_method

private

def setup_user
@user = current_user
end

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
def user_params
personal_account_attributes = Kiqr.config.account_attributes.prepend(:id)
params.require(:user).permit(:time_zone, :locale, personal_account_attributes: personal_account_attributes)
end
Expand Down
4 changes: 4 additions & 0 deletions gems/kiqr/config/locales/kiqr/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ en:
title: "Settings"
description: "Personal profile & settings"
form:
personal_account:
name:
label: "Full name"
placeholder: "Enter your full name"
time_zone:
label: "Time zone"
placeholder: "Select a time zone"
Expand Down
10 changes: 7 additions & 3 deletions gems/kiqr/lib/kiqr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ module Kiqr
autoload :Config, "kiqr/config"

module Services
module Accounts
autoload :Create, "kiqr/services/accounts/create"
autoload :Update, "kiqr/services/accounts/update"
module Teams
autoload :Create, "kiqr/services/teams/create"
autoload :Update, "kiqr/services/teams/update"
end

module Invitations
Expand All @@ -24,6 +24,10 @@ module Invitations
autoload :Destroy, "kiqr/services/invitations/destroy"
autoload :Reject, "kiqr/services/invitations/reject"
end

module Users
autoload :Update, "kiqr/services/users/update"
end
end

module Errors
Expand Down
42 changes: 0 additions & 42 deletions gems/kiqr/lib/kiqr/services/accounts/create.rb

This file was deleted.

24 changes: 24 additions & 0 deletions gems/kiqr/lib/kiqr/services/teams/create.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Kiqr
module Services
module Teams
# Create account service
# User can create a personal account or a team account.
# User can have only one personal account.
# User can be part of multiple team accounts.
class Create < Kiqr::ApplicationService
def call(account:, user:)
@account, @user = account, user

account.account_users.new(user: user, owner: true)
account.save!

success account
end

private

attr_reader :account, :user
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
module Kiqr
module Services
module Accounts
module Teams
# Update account service
# User can update their personal account or if they are part of the team.
class Update < Kiqr::ApplicationService
def call(account:, user:)
raise StandardError, "Account is not a team" unless account.team?

@account, @user = account, user

permission_check
Expand All @@ -18,7 +20,7 @@ def call(account:, user:)
# Check if user has permission to edit the account
# User can edit their personal account or if they are part of the team.
def permission_check
return if user.personal_account == account || account.account_users.find_by(user: user)
return if account.account_users.find_by(user: user)
raise StandardError, "User does not have permission to edit this account"
end

Expand Down
21 changes: 21 additions & 0 deletions gems/kiqr/lib/kiqr/services/users/update.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Kiqr
module Services
module Users
# Update account service
# User can update their personal account or if they are part of the team.
class Update < Kiqr::ApplicationService
def call(user:)
@user = user

user.save!

success user
end

private

attr_reader :user
end
end
end
end
18 changes: 17 additions & 1 deletion gems/kiqr/test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_04_08_210931) do
ActiveRecord::Schema[7.1].define(version: 2024_04_24_103325) do
create_table "account_invitations", force: :cascade do |t|
t.string "public_uid"
t.integer "account_id", null: false
Expand Down Expand Up @@ -47,6 +47,21 @@
t.index ["public_uid"], name: "index_accounts_on_public_uid", unique: true
end

create_table "omniauth_identities", force: :cascade do |t|
t.string "public_uid"
t.integer "user_id", null: false
t.string "provider", null: false
t.string "provider_uid", null: false
t.text "credentials"
t.text "info"
t.text "extra"
t.datetime "expires_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["public_uid"], name: "index_omniauth_identities_on_public_uid", unique: true
t.index ["user_id"], name: "index_omniauth_identities_on_user_id"
end

create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
Expand Down Expand Up @@ -84,5 +99,6 @@
add_foreign_key "account_invitations", "accounts"
add_foreign_key "account_users", "accounts"
add_foreign_key "account_users", "users"
add_foreign_key "omniauth_identities", "users"
add_foreign_key "users", "accounts", column: "personal_account_id"
end
Loading

0 comments on commit 703a3e0

Please sign in to comment.