Skip to content

Commit 4895d98

Browse files
committed
Prefer polymorphic routes
1 parent 6fd391f commit 4895d98

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

app/models/message.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ def list
130130
@list ||= List.find(list_id)
131131
end
132132

133+
def to_param
134+
#NOTE: This value isn't unique system-wide. Ideally, this should return a combination of list_name and list_seq
135+
list_seq
136+
end
137+
133138
def count_recursively(count = 0)
134139
count + 1 + (children&.sum(&:count_recursively) || 0)
135140
end

app/views/messages/_thread.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<div class="thread-message" style="margin-left: <%= depth * 6 %>px;" <% if (depth == 0) && message.children&.any? %>data-controller="thread"<% end %>>
22
<% if depth == 0 %>
3-
<%= link_to "/#{list.name}/#{message.list_seq}", class: 'message-item block bg-white dark:bg-gray-800 rounded-lg shadow-md border border-gray-200 dark:border-gray-700 overflow-hidden hover:shadow-lg transition-shadow', data: {turbo_frame: 'message_content', turbo_action: 'advance', action: 'click->message-list#select'} do %>
3+
<%= link_to [list, message], class: 'message-item block bg-white dark:bg-gray-800 rounded-lg shadow-md border border-gray-200 dark:border-gray-700 overflow-hidden hover:shadow-lg transition-shadow', data: {turbo_frame: 'message_content', turbo_action: 'advance', action: 'click->message-list#select'} do %>
44
<div class="p-5">
55
<div class="flex items-start justify-between gap-4">
66
<div class="flex-1 min-w-0">
@@ -35,7 +35,7 @@
3535
</div>
3636
<% end %>
3737
<% else %>
38-
<%= link_to "/#{list.name}/#{message.list_seq}", class: 'message-item block py-2 border-l-2 border-gray-200 dark:border-gray-700 pl-4 hover:border-red-400 dark:hover:border-red-500 transition-colors', data: {turbo_frame: 'message_content', turbo_action: 'advance', action: 'click->message-list#select'} do %>
38+
<%= link_to [list, message], class: 'message-item block py-2 border-l-2 border-gray-200 dark:border-gray-700 pl-4 hover:border-red-400 dark:hover:border-red-500 transition-colors', data: {turbo_frame: 'message_content', turbo_action: 'advance', action: 'click->message-list#select'} do %>
3939
<div class="flex items-start gap-2 text-sm">
4040
<svg class="w-4 h-4 text-gray-400 dark:text-gray-500 mt-0.5 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
4141
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 10h10a8 8 0 018 8v2M3 10l6 6m-6-6l6-6"></path>

app/views/messages/search.html.erb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@
2727

2828
<% @messages.each do |message| %>
2929
<div class="search-result">
30-
<% list_name = List.find(message.list_id).name %>
3130
<h2 class="subject">
32-
<span class="prefix"><%= list_name %>:<%= message.list_seq %></span>
33-
<%= link_to without_list_prefix(message.subject), "/#{list_name}/#{message.list_seq}" %>
31+
<span class="prefix"><%= message.list.name %>:<%= message.list_seq %></span>
32+
<%= link_to without_list_prefix(message.subject), [message.list, message] %>
3433
</h2>
3534
<div class="snippet"><%= search_snippet(message.body, params[:q]) %></div>
3635
</div>

config/routes.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
Rails.application.routes.draw do
2-
get '/:list_name/:list_seq', to: 'messages#show'
32
get '/:list_name/', to: 'messages#index', as: :list
4-
3+
resources :list, only: [], path: '', param: :name do
4+
resources :messages, only: :show, path: '', param: :list_seq
5+
end
56
get '/attachments/:encoded_key/*filename' => 'attachments#show', as: :attachment
67

78
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

0 commit comments

Comments
 (0)