このコミットが含まれているのは:
2026-07-14 19:37:04 +09:00
コミット f9463f383f
11個のファイルの変更202行の追加92行の削除
+29 -10
ファイルの表示
@@ -16,8 +16,14 @@ class PostImportPreviewer
def preview_rows rows:, fetch_metadata: true, metadata_cache: { }
urls = {}
rows.map { |row|
preview_row(row, urls:, fetch_metadata:, metadata_cache:)
prepared_rows = rows.map { prepare_row(_1) }
existing_posts =
Post.where(url: prepared_rows.map { _1[:normal_url] }.compact.uniq).index_by(&:url)
prepared_rows.map { |row|
preview_row(row, urls:,
fetch_metadata:,
metadata_cache:,
existing_posts:)
}
end
@@ -27,22 +33,30 @@ class PostImportPreviewer
private
def preview_row row, urls:, fetch_metadata:, metadata_cache:
row = row.symbolize_keys
def prepare_row row
source = row.symbolize_keys
url = source[:url].to_s.strip
normal_url = normalised_url(url)
source.merge(url_text: url, normal_url:)
end
def preview_row row, urls:, fetch_metadata:, metadata_cache:, existing_posts:
attributes = initial_attributes(row)
provenance = initial_provenance(row)
tag_sources = initial_tag_sources(row, attributes, provenance)
field_warnings = initial_field_warnings(row)
base_warnings = initial_base_warnings(row)
url = row[:url].to_s.strip
url = row[:url_text]
provenance['url'] = 'manual'
normal_url = normalised_url(url)
normal_url = row[:normal_url]
url_for_metadata = normal_url || url
existing_post = normal_url.present? ? Post.find_by(url: normal_url) : nil
existing_post = normal_url.present? ? existing_posts[normal_url] : nil
validation_errors = {}
validation_errors[:url] = ['URL が不正です.'] if normal_url.blank?
validation_errors[:url] = ['URL が重複しています.'] if normal_url.present? && urls.key?(normal_url)
if normal_url.present? && urls.key?(normal_url)
validation_errors[:url] = ['URL が重複しています.']
end
urls[normal_url] = true if normal_url.present?
if row[:metadata_url].present? && row[:metadata_url] != url_for_metadata
@@ -50,7 +64,7 @@ class PostImportPreviewer
clear_fetch_warnings!(field_warnings)
end
if normal_url.present? && existing_post
if validation_errors.blank? && normal_url.present? && existing_post
add_field_warning!(field_warnings, 'url', EXISTING_SKIP_WARNING)
attributes['tags'] = merged_tags(tag_sources, provenance['tags'])
warnings_present = field_warnings.values.any?(&:present?) || base_warnings.present?
@@ -267,9 +281,14 @@ class PostImportPreviewer
ids = raw.to_s.split.map { Integer(_1, exception: false) }
return if ids.compact.empty? && raw.to_s.blank?
errors[:parent_post_ids] = ['親投稿 Id. が不正です.'] and return if ids.any? { _1.nil? || _1 <= 0 }
if ids.any? { _1.nil? || _1 <= 0 }
errors[:parent_post_ids] = ['親投稿 Id. が不正です.']
return
end
if Post.where(id: ids).count != ids.uniq.length
errors[:parent_post_ids] = ['存在しない親投稿 Id. があります.']
end
end
private :prepare_row
end