Skip to content

Commit bd86206

Browse files
committed
Use alba-inertia
1 parent 5b094df commit bd86206

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+291
-137
lines changed

Gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,8 @@ gem "vite_rails", "~> 3.0"
7070
gem "devise", "~> 4.9"
7171

7272
gem "pagy", "~> 43.0"
73+
74+
gem "typelizer", "~> 0.5.3"
75+
gem "alba", "~> 3.10"
76+
gem "alba-inertia", "~> 0.1.1"
77+
gem "listen", "~> 3.9"

Gemfile.lock

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,16 @@ GEM
7777
uri (>= 0.13.1)
7878
addressable (2.8.7)
7979
public_suffix (>= 2.0.2, < 7.0)
80+
alba (3.10.0)
81+
alba-inertia (0.1.1)
82+
alba
83+
inertia_rails
84+
zeitwerk
8085
ast (2.4.3)
8186
base64 (0.3.0)
8287
bcrypt (3.1.20)
8388
bcrypt_pbkdf (1.1.1)
89+
benchmark (0.5.0)
8490
bigdecimal (3.3.1)
8591
bindex (0.8.1)
8692
bootsnap (1.18.6)
@@ -129,6 +135,13 @@ GEM
129135
railties (>= 6.1.0)
130136
faker (3.5.2)
131137
i18n (>= 1.8.11, < 2)
138+
ffi (1.17.2-aarch64-linux-gnu)
139+
ffi (1.17.2-aarch64-linux-musl)
140+
ffi (1.17.2-arm-linux-gnu)
141+
ffi (1.17.2-arm-linux-musl)
142+
ffi (1.17.2-arm64-darwin)
143+
ffi (1.17.2-x86_64-linux-gnu)
144+
ffi (1.17.2-x86_64-linux-musl)
132145
fugit (1.12.1)
133146
et-orbi (~> 1.4)
134147
raabro (~> 1.4)
@@ -160,6 +173,9 @@ GEM
160173
zeitwerk (>= 2.6.18, < 3.0)
161174
language_server-protocol (3.17.0.5)
162175
lint_roller (1.1.0)
176+
listen (3.9.0)
177+
rb-fsevent (~> 0.10, >= 0.10.3)
178+
rb-inotify (~> 0.9, >= 0.9.10)
163179
logger (1.7.0)
164180
loofah (2.24.1)
165181
crass (~> 1.0.2)
@@ -272,6 +288,9 @@ GEM
272288
zeitwerk (~> 2.6)
273289
rainbow (3.1.1)
274290
rake (13.3.1)
291+
rb-fsevent (0.11.2)
292+
rb-inotify (0.11.1)
293+
ffi (~> 1.0)
275294
rdoc (6.15.1)
276295
erb
277296
psych (>= 4.0.0)
@@ -375,6 +394,9 @@ GEM
375394
thruster (0.1.16-x86_64-linux)
376395
timeout (0.4.4)
377396
tsort (0.2.0)
397+
typelizer (0.5.3)
398+
benchmark
399+
railties (>= 6.0.0)
378400
tzinfo (2.0.6)
379401
concurrent-ruby (~> 1.0)
380402
unicode-display_width (3.2.0)
@@ -421,6 +443,8 @@ PLATFORMS
421443
x86_64-linux-musl
422444

423445
DEPENDENCIES
446+
alba (~> 3.10)
447+
alba-inertia (~> 0.1.1)
424448
bootsnap
425449
brakeman
426450
bundler-audit
@@ -432,6 +456,7 @@ DEPENDENCIES
432456
inertia_rails (~> 3.12)
433457
jbuilder
434458
kamal
459+
listen (~> 3.9)
435460
pagy (~> 43.0)
436461
propshaft
437462
puma (>= 5.0)
@@ -444,6 +469,7 @@ DEPENDENCIES
444469
solid_queue
445470
sqlite3 (>= 2.1)
446471
thruster
472+
typelizer (~> 0.5.3)
447473
tzinfo-data
448474
vite_rails (~> 3.0)
449475
web-console

app/controllers/application_controller.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
class ApplicationController < ActionController::Base
22
include Pagy::Method
3+
include Alba::Inertia::Controller
34

45
# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
56
allow_browser versions: :modern
67
layout :set_layout
78

89
before_action :configure_permitted_parameters, if: :devise_controller?
910

10-
inertia_share categories: -> {
11-
Category.includes(:topics).all.as_json(methods: :topics_count)
12-
}
13-
14-
inertia_share current_user: -> {
15-
current_user&.as_json(only: [ :username ])
16-
}
11+
inertia_share { SharedPropsResource.new(self).to_inertia }
1712

1813
def after_sign_in_path_for(resource)
1914
topics_path
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
class CategoriesController < ApplicationController
22
def show
3-
category = Category
3+
@category = Category
44
.includes(topics: [ :user, :category, messages: :user ])
55
.find(params[:id])
6-
.as_json(include: [
7-
topics: { include: [ :user, :category, messages: { include: :user } ] }
8-
])
9-
10-
render inertia: { category: }
116
end
127
end
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class LandingController < ApplicationController
22
def index
3-
render inertia: {}
43
end
54
end
Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
class MyTopicsController < ApplicationController
22
def index
3-
topics = Topic.includes(:user, :category, messages: :user)
4-
.joins(:messages)
5-
.where(messages: { user_id: current_user.id })
6-
.distinct
3+
@topics = Topic.includes(:user, :category, messages: :user)
4+
.where(id: current_user.messages.select(:topic_id))
75
.order(created_at: :desc)
8-
.to_a
9-
.as_json(include: [
10-
:user,
11-
:category,
12-
messages: { include: :user }
13-
])
146

15-
render inertia: { topics: }
7+
# Custom Serializer Selection
8+
render_inertia(serializer: TopicsIndexResource)
169
end
1710
end
Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,13 @@
11
class TopicsController < ApplicationController
22
def index
3-
topics = Topic.all
3+
@topics = Topic.all
44
.includes(:user, :category, messages: :user)
55
.order(created_at: :desc)
6-
.as_json(include: [
7-
:user,
8-
:category,
9-
messages: { include: :user }
10-
])
11-
12-
render inertia: { topics: }
136
end
147

158
def show
16-
topic = Topic
9+
@topic = Topic
1710
.includes(:user, :category, messages: :user)
1811
.find(params[:id])
19-
.as_json(include: [
20-
:user,
21-
:category,
22-
messages: { include: :user }
23-
])
24-
render inertia: { topic: }
2512
end
2613
end

app/frontend/components/Avatar.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { User } from '../types'
1+
import { BaseUser } from '@/types'
22

3-
const Avatar = ({ user }: { user: User }) => {
3+
const Avatar = ({ user }: { user: BaseUser }) => {
44
const { username } = user;
55

6-
if (!user) {
6+
if (!username) {
77
return null;
88
}
99

@@ -17,4 +17,4 @@ const Avatar = ({ user }: { user: User }) => {
1717
)
1818
}
1919

20-
export default Avatar
20+
export default Avatar

app/frontend/components/Sidebar.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import { Link, usePage } from "@inertiajs/react";
2-
import { Category } from "@/types";
3-
42

53
function getLinkClasses(currentUrl: string, targetUrl: string) {
64
const isActive = currentUrl === targetUrl
@@ -61,7 +59,7 @@ export default function Sidebar({ isOpen }: { isOpen: boolean }) {
6159
Categories
6260
</h3>
6361
<div className="space-y-1">
64-
{categories.map((category: Category) => {
62+
{categories.map((category) => {
6563
const categoryUrl = `/categories/${category.id}`
6664
const classes = getLinkClasses(currentUrl, categoryUrl)
6765
return (
@@ -72,7 +70,7 @@ export default function Sidebar({ isOpen }: { isOpen: boolean }) {
7270
>
7371
<span>{category.name}</span>
7472
<span className={`text-xs ${classes.count}`}>
75-
{category.topics_count}
73+
{category.topicsCount}
7674
</span>
7775
</Link>
7876
)
@@ -83,4 +81,4 @@ export default function Sidebar({ isOpen }: { isOpen: boolean }) {
8381
)}
8482
</aside>
8583
)
86-
}
84+
}

app/frontend/components/UserProfile.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ type UserProfileProps = {
66
}
77

88
export default function UserProfile({user, onClose}: UserProfileProps) {
9-
const { username, email, topics_count: topicsCount, messages_count: messagesCount, about_me: aboutMe } = user || {};
9+
const { username, email, topicsCount, messagesCount, aboutMe } = user || {};
1010

1111
return (
1212
<div className="space-y-4">

0 commit comments

Comments
 (0)