diff --git a/app/models/message.rb b/app/models/message.rb index 7d8126f..1511d60 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -2,6 +2,12 @@ BLADE_BUCKET_NAME = 'blade.ruby-lang.org' class Message < ApplicationRecord + # Not really sure we will utlize this configuration, + # but I don't want to make this column. + # https://blade.ruby-lang.org/ruby-talk/1 is JST. + # https://blade.ruby-lang.org/ruby-talk/410000 is not. + self.skip_time_zone_conversion_for_attributes = [:published_at] + 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}") @@ -18,6 +24,13 @@ def self.from_string(str) line.split(/:\s+/, 2) }.to_h - self.new(body: body, subject: headers['Subject'], from: headers['From']) + published_at = DateTime.strptime(headers['Date'], '%Y-%m-%dT%H:%M:%S%:z') + + self.new( + body: body, + subject: headers['Subject'], + from: headers['From'], + published_at: published_at, + ) end end diff --git a/db/migrate/20241010055923_add_published_at_to_messages.rb b/db/migrate/20241010055923_add_published_at_to_messages.rb new file mode 100644 index 0000000..03a5f02 --- /dev/null +++ b/db/migrate/20241010055923_add_published_at_to_messages.rb @@ -0,0 +1,5 @@ +class AddPublishedAtToMessages < ActiveRecord::Migration[7.1] + def change + add_column :messages, :published_at, :timestamp + end +end diff --git a/db/migrate/20241010062050_published_at_time_zone.rb b/db/migrate/20241010062050_published_at_time_zone.rb new file mode 100644 index 0000000..36569d7 --- /dev/null +++ b/db/migrate/20241010062050_published_at_time_zone.rb @@ -0,0 +1,6 @@ +class PublishedAtTimeZone < ActiveRecord::Migration[7.1] + # https://github.com/rails/rails/pull/41084 + def change + change_column :messages, :published_at, :timestamptz + end +end diff --git a/db/schema.rb b/db/schema.rb index 09eb37c..41f918a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_10_08_063550) do +ActiveRecord::Schema[7.1].define(version: 2024_10_10_062050) do create_table "messages", force: :cascade do |t| t.string "subject" t.string "from" @@ -19,6 +19,7 @@ t.datetime "updated_at", null: false t.integer "list_id" t.integer "list_seq" + t.datetime "published_at", precision: nil t.index ["body"], name: "index_messages_on_body" end diff --git a/test/models/message_test.rb b/test/models/message_test.rb index 03547a4..62095d7 100644 --- a/test/models/message_test.rb +++ b/test/models/message_test.rb @@ -5,11 +5,13 @@ class MessageTest < ActiveSupport::TestCase m = Message.from_string(<