Skip to content

Commit 19589a8

Browse files
authored
Merge pull request #15 from ruby/published-at
Add published_at
2 parents d689c48 + 7e4280e commit 19589a8

File tree

5 files changed

+30
-3
lines changed

5 files changed

+30
-3
lines changed

app/models/message.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
BLADE_BUCKET_NAME = 'blade.ruby-lang.org'
33

44
class Message < ApplicationRecord
5+
# Not really sure we will utlize this configuration,
6+
# but I don't want to make this column.
7+
# https://blade.ruby-lang.org/ruby-talk/1 is JST.
8+
# https://blade.ruby-lang.org/ruby-talk/410000 is not.
9+
self.skip_time_zone_conversion_for_attributes = [:published_at]
10+
511
def self.from_s3(list_name, list_seq)
612
client = Aws::S3::Client.new(region: BLADE_BUCKET_REGION)
713
obj = client.get_object(bucket: BLADE_BUCKET_NAME, key: "#{list_name}/#{list_seq}")
@@ -18,6 +24,13 @@ def self.from_string(str)
1824
line.split(/:\s+/, 2)
1925
}.to_h
2026

21-
self.new(body: body, subject: headers['Subject'], from: headers['From'])
27+
published_at = DateTime.strptime(headers['Date'], '%Y-%m-%dT%H:%M:%S%:z')
28+
29+
self.new(
30+
body: body,
31+
subject: headers['Subject'],
32+
from: headers['From'],
33+
published_at: published_at,
34+
)
2235
end
2336
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddPublishedAtToMessages < ActiveRecord::Migration[7.1]
2+
def change
3+
add_column :messages, :published_at, :timestamp
4+
end
5+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class PublishedAtTimeZone < ActiveRecord::Migration[7.1]
2+
# https://github.com/rails/rails/pull/41084
3+
def change
4+
change_column :messages, :published_at, :timestamptz
5+
end
6+
end

db/schema.rb

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/models/message_test.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ class MessageTest < ActiveSupport::TestCase
55
m = Message.from_string(<<END_OF_BODY)
66
Subject: [ruby-list:1] Hello
77
From: alice@...
8-
Date: Mon, 01 Jan 2022 12:34:56 +0900
8+
Date: 2005-12-15T19:32:40+09:00
99
1010
Hello, world!
1111
END_OF_BODY
1212
assert_equal "Hello, world!\n", m.body
1313
assert_nil m.id
14+
15+
assert_equal DateTime.parse('2005-12-15T19:32:40+09:00'), m.published_at
1416
end
1517
end

0 commit comments

Comments
 (0)