From 525e7969a897b3979a5f96a31788da432dadbe0d Mon Sep 17 00:00:00 2001 From: Kazuyoshi Kato Date: Tue, 8 Oct 2024 00:07:24 -0700 Subject: [PATCH] Add list_id and list_seq --- app/controllers/messages_controller.rb | 3 ++- app/models/list.rb | 23 +++++++++++++++++++++++ app/models/message.rb | 6 +++++- app/views/messages/index.html.erb | 4 +++- db/migrate/20241008063550_add_list.rb | 7 +++++++ db/schema.rb | 4 +++- import.rb | 13 +++++++++++++ test/fixtures/messages.yml | 4 ++++ test/models/list_test.rb | 7 +++++++ 9 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 app/models/list.rb create mode 100644 db/migrate/20241008063550_add_list.rb create mode 100644 import.rb create mode 100644 test/models/list_test.rb 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 @@