このコミットが含まれているのは:
@@ -2,6 +2,13 @@ class Post < ApplicationRecord
|
||||
require 'mini_magick'
|
||||
require 'stringio'
|
||||
|
||||
ORIGINAL_CREATED_INVALID_MESSAGE = 'オリジナルの作成日時の形式が不正です.'.freeze
|
||||
ORIGINAL_CREATED_MINUTE_PRECISION_MESSAGE =
|
||||
'オリジナルの作成日時は分単位で入力してください.'.freeze
|
||||
ORIGINAL_CREATED_ORDER_MESSAGE = 'オリジナルの作成日時の順番がをかしぃです.'.freeze
|
||||
ORIGINAL_CREATED_MINIMUM_RANGE_MESSAGE =
|
||||
'オリジナルの作成日時の範囲は1分以上必要です.'.freeze
|
||||
|
||||
def self.resized_thumbnail_attachment(upload)
|
||||
upload.rewind
|
||||
image = MiniMagick::Image.read(upload.read)
|
||||
@@ -143,16 +150,17 @@ class Post < ApplicationRecord
|
||||
private
|
||||
|
||||
def validate_original_created_range
|
||||
f = original_created_from
|
||||
b = original_created_before
|
||||
return if f.blank? || b.blank?
|
||||
f = parse_original_created_value(:original_created_from)
|
||||
b = parse_original_created_value(:original_created_before)
|
||||
return if f.nil? || b.nil?
|
||||
|
||||
f = Time.zone.parse(f) if String === f
|
||||
b = Time.zone.parse(b) if String === b
|
||||
return if !(f) || !(b)
|
||||
if b <= f
|
||||
errors.add :original_created_at, ORIGINAL_CREATED_ORDER_MESSAGE
|
||||
return
|
||||
end
|
||||
|
||||
if f >= b
|
||||
errors.add :original_created_at, 'オリジナルの作成日時の順番がをかしぃです.'
|
||||
if b - f < 1.minute
|
||||
errors.add :original_created_at, ORIGINAL_CREATED_MINIMUM_RANGE_MESSAGE
|
||||
end
|
||||
end
|
||||
|
||||
@@ -175,4 +183,49 @@ class Post < ApplicationRecord
|
||||
|
||||
self.url = PostUrlNormaliser.normalise(url) || url.strip
|
||||
end
|
||||
|
||||
def parse_original_created_value field
|
||||
raw_value = public_send("#{ field }_before_type_cast")
|
||||
value = public_send(field)
|
||||
return nil if raw_value.blank? && value.blank?
|
||||
|
||||
time =
|
||||
case raw_value
|
||||
when String
|
||||
parse_original_created_string(raw_value)
|
||||
when Time, ActiveSupport::TimeWithZone
|
||||
raw_value.in_time_zone
|
||||
else
|
||||
value&.in_time_zone
|
||||
end
|
||||
if time.nil?
|
||||
errors.add field, ORIGINAL_CREATED_INVALID_MESSAGE
|
||||
return nil
|
||||
end
|
||||
unless minute_precision_time?(time)
|
||||
errors.add field, ORIGINAL_CREATED_MINUTE_PRECISION_MESSAGE
|
||||
end
|
||||
time
|
||||
end
|
||||
|
||||
def parse_original_created_string raw_value
|
||||
value = raw_value.to_s.strip
|
||||
return nil if value.blank?
|
||||
|
||||
if value.match?(/\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}\z/)
|
||||
return Time.zone.local(value[0, 4].to_i,
|
||||
value[5, 2].to_i,
|
||||
value[8, 2].to_i,
|
||||
value[11, 2].to_i,
|
||||
value[14, 2].to_i)
|
||||
end
|
||||
|
||||
Time.iso8601(value).in_time_zone
|
||||
rescue ArgumentError, TypeError
|
||||
nil
|
||||
end
|
||||
|
||||
def minute_precision_time? value
|
||||
value.sec.zero? && value.nsec.zero?
|
||||
end
|
||||
end
|
||||
|
||||
新しい課題から参照
ユーザをブロックする