Skip to content

Commit

Permalink
feat: kiqr_routes and kiqr_flash_message
Browse files Browse the repository at this point in the history
  • Loading branch information
kjellberg committed Apr 10, 2024
1 parent 5bc4724 commit 68dc3ea
Show file tree
Hide file tree
Showing 16 changed files with 138 additions and 52 deletions.
4 changes: 2 additions & 2 deletions app/views/kiqr/accounts/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

<%= fullscreen do |screen| %>
<%= screen.with_form(
title: t(".heading.title", app_name: Kiqr::Config.app_name),
description: t(".heading.description")
title: t(".title", app_name: Kiqr::Config.app_name),
description: t(".description")
) do %>
<main class="flex flex-col gap-4 w-full max-w-xl">
<%= render "form", account: @account %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/kiqr/accounts/select.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

<%= fullscreen do |screen| %>
<%= screen.with_form(
title: t(".heading.title", app_name: Kiqr::Config.app_name),
description: t(".heading.description")
title: t(".title", app_name: Kiqr::Config.app_name),
description: t(".description")
) do %>
<main class="flex flex-col gap-4 max-w-xl">
<div class="flex flex-col gap-2">
Expand Down
24 changes: 0 additions & 24 deletions config/locales/kiqr.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,6 @@ en:
en: "English"
sv: "Swedish"
accounts:
new:
heading:
title: "Create a new team account"
description: "Complete the form below to create a new team account"
edit:
title: "Profile settings"
description: "Edit your account profile."
update:
success: "Your account profile has been updated successfully."
create:
success: "Your account has been created successfully."
select:
or: "or"
continue_as: "Continue as %{name}"
heading:
title: "Select account"
description: "Select a team account to manage."
form:
name:
personal: "Your full name"
team: "Team / organization name"
button:
save: "Save changes"
create: "Create new account"
members:
index:
title: "Team members"
Expand Down
7 changes: 4 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@

# => KIQR core routes
# These routes are required for the KIQR core to function properly.
# Do not remove or modify these routes unless you know what you're doing.
# Do not remove this unless you know what you're doing.
kiqr_routes
draw :development
draw :authentication

# => Application routes
# => Teamable scope
# Routes inside this block will be prefixed with /team/<team_id> if
# the user is signed in to a team account. Otherwise, they won't be prefixed at all.
scope "(/team/:account_id)", account_id: %r{[^/]+} do
teamable_scope do
get "dashboard" => "dashboard#show"
end

Expand Down
6 changes: 3 additions & 3 deletions gems/kiqr/app/controllers/kiqr/accounts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Kiqr
class AccountsController < ApplicationController
class AccountsController < KiqrController
before_action :setup_account, only: %i[edit update]

def new
Expand All @@ -11,7 +11,7 @@ def create

if @account.valid?
Kiqr::Services::Accounts::Create.call!(account: @account, user: current_user)
flash[:notice] = I18n.t("accounts.create.success")
kiqr_flash_message(:notice, :account_created)
redirect_to dashboard_path(account_id: @account)
else
render :new, status: :unprocessable_entity
Expand All @@ -23,7 +23,7 @@ def update

if @account.valid?
Kiqr::Services::Accounts::Update.call!(account: @account, user: current_user)
flash[:notice] = I18n.t("accounts.update.success")
kiqr_flash_message(:notice, :account_updated)
redirect_to edit_account_path
else
render :edit, status: :unprocessable_entity
Expand Down
7 changes: 7 additions & 0 deletions gems/kiqr/app/controllers/kiqr_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class KiqrController < ApplicationController
private

def kiqr_flash_message(type, message)
flash[type] = I18n.t("kiqr.flash_messages.#{message}")
end
end
24 changes: 24 additions & 0 deletions gems/kiqr/config/locales/kiqr/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
en:
kiqr:
flash_messages:
account_created: "Your account has been created successfully."
account_updated: "Your account has been updated successfully."
accounts:
new:
title: "Create a new team account"
description: "Complete the form below to create a new team account"
edit:
title: "Profile settings"
description: "Edit your account profile."
select:
title: "Select account"
description: "Select a team account to manage."
continue_as: "Continue as %{name}"
or: "or"
form:
name:
personal: "Your full name"
team: "Team / organization name"
button:
save: "Save changes"
create: "Create new account"
15 changes: 0 additions & 15 deletions gems/kiqr/config/routes.rb

This file was deleted.

4 changes: 4 additions & 0 deletions gems/kiqr/lib/kiqr/application_service.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
module Kiqr
# ApplicationService is a base class for all services in the application.
# It provides a common interface for all services and a way to handle errors.
# It also provides a way to propagate errors to the caller or to handle them
# internally.
class ApplicationService
Response = Struct.new(:success?, :payload, :error) do
def failure?
Expand Down
2 changes: 2 additions & 0 deletions gems/kiqr/lib/kiqr/engine.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "kiqr/rails/routes"

module Kiqr
class Engine < ::Rails::Engine
end
Expand Down
52 changes: 52 additions & 0 deletions gems/kiqr/lib/kiqr/rails/routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module ActionDispatch
module Routing
class Mapper
def kiqr_routes(options = {})
# Set default options for controllers if not set.
options = default_controllers(options)

account_routes(options)
devise_routes(options)
end

private

# => Default controllers
# Set default options for controllers if not set.
def default_controllers(options)
options[:controllers] ||= {}
options[:controllers][:accounts] ||= "kiqr/accounts"
options[:controllers][:sessions] ||= "users/sessions"
options[:controllers][:registrations] ||= "users/registrations"
options
end

# => Account routes
def account_routes(options)
resources :accounts, controller: options[:controllers][:accounts], only: [:new, :create]
get "select-account", controller: options[:controllers][:accounts], action: :select, as: :select_account

scope "(/team/:account_id)", account_id: %r{[^/]+} do
resource :account, only: [:edit, :update], path: "profile", controller: options[:controllers][:accounts]
end
end

# => Authentication routes
def devise_routes(options)
devise_for :users, path_names: {sign_in: "login", sign_up: "create-account"}, controllers: {
registrations: options[:controllers][:registrations],
sessions: options[:controllers][:sessions]
}
end

# => Teamable routes
# Routes inside this block will be prefixed with /team/<team_id> if
# the user is signed in to a team account. Otherwise, they won't be prefixed at all.
def teamable_scope(&)
scope "(/team/:account_id)", account_id: %r{[^/]+} do
yield
end
end
end
end
end
4 changes: 4 additions & 0 deletions gems/kiqr/lib/kiqr/services/accounts/create.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module Kiqr
module Services
module Accounts
# 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:, personal: false)
@account, @user = account, user
Expand Down
2 changes: 2 additions & 0 deletions gems/kiqr/lib/kiqr/services/accounts/update.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Kiqr
module Services
module Accounts
# 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:)
@account, @user = account, user
Expand Down
2 changes: 2 additions & 0 deletions gems/kiqr/test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
# Can be used by load balancers and uptime monitors to verify that the app is live.
get "up" => "rails/health#show", :as => :rails_health_check

kiqr_routes

# Defines the root path route ("/")
# root "posts#index"
end
27 changes: 27 additions & 0 deletions gems/kiqr/test/routing_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require "test_helper"

class RoutingTest < ActionDispatch::IntegrationTest
include ActionDispatch::Routing::UrlFor
include Rails.application.routes.url_helpers

setup do
@routes = Rails.application.routes
end

test "kiqr routes setup default controllers" do
assert_recognizes({controller: "kiqr/accounts", action: "new"}, "/accounts/new")
assert_recognizes({controller: "kiqr/accounts", action: "select"}, "/select-account")
# assert_recognizes({controller: "users/sessions", action: "new"}, "/users/login")
end

test "account routes are correctly configured" do
assert_routing "/accounts/new", {controller: "kiqr/accounts", action: "new"}
assert_routing "/select-account", {controller: "kiqr/accounts", action: "select"}
assert_routing({method: "post", path: "/accounts"}, {controller: "kiqr/accounts", action: "create"})
end

# test "devise routes for users are correctly configured" do
# assert_routing "/users/login", {controller: "users/sessions", action: "new"}
# assert_routing "/users/create-account", {controller: "users/registrations", action: "new"}
# end
end
6 changes: 3 additions & 3 deletions test/system/accounts/accounts_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class EditAccountsTest < ApplicationSystemTestCase
fill_in_account_fields

click_on "commit"
assert_text I18n.t("accounts.update.success")
assert_text I18n.t("kiqr.flash_messages.account_updated")

user.personal_account.reload
assert_equal "New name", user.personal_account.name
Expand All @@ -30,7 +30,7 @@ class EditAccountsTest < ApplicationSystemTestCase
fill_in_account_fields

click_on "commit"
assert_text I18n.t("accounts.update.success")
assert_text I18n.t("kiqr.flash_messages.account_updated")

team_account.reload
assert_equal "New name", team_account.name
Expand All @@ -47,7 +47,7 @@ class EditAccountsTest < ApplicationSystemTestCase
fill_in_account_fields

click_on "commit"
assert_text I18n.t("accounts.create.success")
assert_text I18n.t("kiqr.flash_messages.account_created")

account = user.reload.accounts.last
assert_equal "New name", account.name
Expand Down

0 comments on commit 68dc3ea

Please sign in to comment.