Skip to content

Commit 2b53937

Browse files
authored
Merge pull request #79 from amatsuda/list_polymorphic
Refactor List to be a model
2 parents fc867f6 + 4895d98 commit 2b53937

File tree

6 files changed

+30
-14
lines changed

6 files changed

+30
-14
lines changed

app/controllers/messages_controller.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def index
66
if (list_name = params[:list_name])
77
@list = List.find_by_name list_name
88

9-
messages = Message.with_recursive(parent_and_children: [Message.where(list_id: @list.id, parent_id: nil).order(:id).limit(100), Message.joins('inner join parent_and_children on messages.parent_id = parent_and_children.id')])
9+
messages = Message.with_recursive(parent_and_children: [Message.where(list_id: @list, parent_id: nil).order(:id).limit(100), Message.joins('inner join parent_and_children on messages.parent_id = parent_and_children.id')])
1010
.joins('inner join parent_and_children on parent_and_children.id = messages.id')
1111
@messages = compose_tree(messages)
1212
elsif (query = params[:q])
@@ -22,8 +22,8 @@ def index
2222

2323
# GET /messages/ruby-dev/1
2424
def show
25-
list = List.find_by_name(params[:list_name])
26-
@message = Message.find_by(list_id: list.id, list_seq: params[:list_seq])
25+
@list = List.find_by_name(params[:list_name])
26+
@message = Message.find_by(list_id: @list, list_seq: params[:list_seq])
2727
end
2828

2929
private

app/models/list.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
class List
2+
include ActiveModel::Model
3+
24
def initialize(name, id)
35
@name = name
46
@id = id
57
end
8+
69
attr_reader :name, :id
710

811
# Ordered by the established dates. ruby-list was started in 1995.
@@ -24,4 +27,12 @@ def find_by_id(id)
2427

2528
alias find find_by_id
2629
end
30+
31+
def to_param
32+
name
33+
end
34+
35+
def persisted?
36+
true
37+
end
2738
end

app/models/message.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ def from_mail(mail, list, list_seq)
4848

4949
# mail.in_reply_to returns strange Array object in some cases (?), so let's use the raw value
5050
parent_message_id_header = extract_message_id_from_in_reply_to(mail.header[:in_reply_to]&.value)
51-
self.parent_id = Message.where(list_id: list.id, message_id_header: parent_message_id_header).pick(:id) if parent_message_id_header
51+
self.parent_id = Message.where(list_id: list, message_id_header: parent_message_id_header).pick(:id) if parent_message_id_header
5252
if !self.parent_id && (String === mail.references)
53-
self.parent_id = Message.where(list_id: list.id, message_id_header: mail.references).pick(:id)
53+
self.parent_id = Message.where(list_id: list, message_id_header: mail.references).pick(:id)
5454
end
5555
if !self.parent_id && (Array === mail.references)
5656
mail.references.compact.each do |ref|
57-
break if (self.parent_id = Message.where(list_id: list.id, message_id_header: ref).pick(:id))
57+
break if (self.parent_id = Message.where(list_id: list, message_id_header: ref).pick(:id))
5858
end
5959
end
6060

@@ -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: 4 additions & 3 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'
3-
get '/:list_name/', to: 'messages#index'
4-
2+
get '/:list_name/', to: 'messages#index', as: :list
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)