diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb
index 2c38c38..27dd437 100644
--- a/app/controllers/messages_controller.rb
+++ b/app/controllers/messages_controller.rb
@@ -21,7 +21,8 @@ def show
if params[:id]
@message = Message.find(params[:id])
else
- @message = Message.from_s3(params[:list_name], params[:list_seq])
+ list = List.find_by_name(params[:list_name])
+ @message = Message.find_by(list_id: list.id, list_seq: params[:list_seq])
end
end
diff --git a/app/models/list.rb b/app/models/list.rb
new file mode 100644
index 0000000..96741ea
--- /dev/null
+++ b/app/models/list.rb
@@ -0,0 +1,23 @@
+class List
+ def initialize(name, id)
+ @name = name
+ @id = id
+ end
+ attr_reader :name, :id
+
+ # Ordered by the established dates. ruby-list was started in 1995.
+ LISTS = [
+ List.new('ruby-list', 1),
+ List.new('ruby-dev', 2),
+ List.new('ruby-core', 3),
+ List.new('ruby-talk', 4),
+ ]
+
+ def self.find_by_name(name)
+ LISTS.find { |list| list.name == name }
+ end
+
+ def self.find_by_id(id)
+ LISTS.find { |list| list.id == id }
+ end
+end
diff --git a/app/models/message.rb b/app/models/message.rb
index 56c9368..7d8126f 100644
--- a/app/models/message.rb
+++ b/app/models/message.rb
@@ -5,7 +5,11 @@ class Message < ApplicationRecord
def self.from_s3(list_name, list_seq)
client = Aws::S3::Client.new(region: BLADE_BUCKET_REGION)
obj = client.get_object(bucket: BLADE_BUCKET_NAME, key: "#{list_name}/#{list_seq}")
- self.from_string(obj.body.read)
+
+ m = self.from_string(obj.body.read)
+ m.list_id = List.find_by_name(list_name).id
+ m.list_seq = list_seq
+ m
end
def self.from_string(str)
diff --git a/app/views/messages/index.html.erb b/app/views/messages/index.html.erb
index 0d32916..c39ff86 100644
--- a/app/views/messages/index.html.erb
+++ b/app/views/messages/index.html.erb
@@ -5,7 +5,9 @@
<% @messages.each do |message| %>
-
- <%= link_to message.subject, message %>
+ <% list_name = List.find_by_id(message.list_id).name %>
+ <%= list_name %>:<%= message.list_seq %>
+ <%= link_to message.subject, "/#{list_name}/#{message.list_seq}" %>
<%= message.body %>
<% end %>
diff --git a/db/migrate/20241008063550_add_list.rb b/db/migrate/20241008063550_add_list.rb
new file mode 100644
index 0000000..cd10d2d
--- /dev/null
+++ b/db/migrate/20241008063550_add_list.rb
@@ -0,0 +1,7 @@
+class AddList < ActiveRecord::Migration[7.1]
+ def change
+ add_column(:messages, :list_id, :integer)
+ add_column(:messages, :list_seq, :integer)
+ add_index(:messages, [:list_id, :list_seq], unique: true)
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index fcde555..09eb37c 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,13 +10,15 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema[7.1].define(version: 2024_08_10_064037) do
+ActiveRecord::Schema[7.1].define(version: 2024_10_08_063550) do
create_table "messages", force: :cascade do |t|
t.string "subject"
t.string "from"
t.text "body"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
+ t.integer "list_id"
+ t.integer "list_seq"
t.index ["body"], name: "index_messages_on_body"
end
diff --git a/import.rb b/import.rb
new file mode 100644
index 0000000..81feb1a
--- /dev/null
+++ b/import.rb
@@ -0,0 +1,13 @@
+require 'optparse'
+
+params = {}
+OptionParser.new do |opts|
+ opts.on('--list LIST')
+ opts.on('--from FROM', Integer)
+ opts.on('--to TO', Integer)
+end.parse!(into: params)
+
+(params[:from]..params[:to]).each do |seq|
+ message = Message.from_s3(params[:list], seq)
+ message.save
+end
diff --git a/test/fixtures/messages.yml b/test/fixtures/messages.yml
index 468a0b8..24fc45e 100644
--- a/test/fixtures/messages.yml
+++ b/test/fixtures/messages.yml
@@ -4,8 +4,12 @@ one:
subject: MyString
from: MyString
body: MyText
+ list_id: 1
+ list_seq: 123
two:
subject: MyString
from: MyString
body: MyText
+ list_id: 2
+ list_seq: 234
diff --git a/test/models/list_test.rb b/test/models/list_test.rb
new file mode 100644
index 0000000..44985f9
--- /dev/null
+++ b/test/models/list_test.rb
@@ -0,0 +1,7 @@
+require "test_helper"
+
+class ListTest < ActiveSupport::TestCase
+ test 'name' do
+ assert_equal 'ruby-list', List::find_by_name('ruby-list').name
+ end
+end