Skip to content

Commit

Permalink
Nickname モデルを作成した (#86)
Browse files Browse the repository at this point in the history
* feat: 🎸 g model Nickname

* feat: 🎸 GssCharacterToNickname モデル(中間テーブル)を作成した

* feat: 🎸 db:migrate

* fix: 🐛 Gss::Character の name を sosenkyo_name から取得するように修正した

* feat: 🎸 Nickname モデルのインポータ作成

* style: 💄 RuboCop の指摘に従い修正した

* test: 💍 Spec を追加した
  • Loading branch information
nikukyugamer authored Jul 16, 2023
1 parent b28bf24 commit 83d63b1
Show file tree
Hide file tree
Showing 32 changed files with 2,097 additions and 17 deletions.
8 changes: 8 additions & 0 deletions app/models/gss/character.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,13 @@ class Character < ApplicationRecord
through: :gss_character_to_product_titles,
class_name: 'ProductTitle',
foreign_key: :product_title_id

has_many :gss_character_to_nicknames,
class_name: 'GssCharacterToNickname',
foreign_key: :gss_character_id
has_many :nicknames,
through: :gss_character_to_nicknames,
class_name: 'Nickname',
foreign_key: :nickname_id
end
end
4 changes: 4 additions & 0 deletions app/models/gss_character_to_nickname.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class GssCharacterToNickname < ApplicationRecord
belongs_to :gss_character, class_name: 'Gss::Character', foreign_key: :gss_character_id
belongs_to :nickname, class_name: 'Nickname', foreign_key: :nickname_id
end
4 changes: 4 additions & 0 deletions app/models/nickname.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Nickname < ApplicationRecord
has_many :gss_character_to_nicknames, class_name: 'GssCharacterToNickname'
has_many :gss_characters, through: :gss_character_to_nicknames, class_name: 'Gss::Character'
end
2 changes: 0 additions & 2 deletions app/models/on_sheet.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# OnSheet:: ではそれ以上の名前空間に分けない(2階層以上にしたくない)
# 総選挙のテーブルであれ、その他のテーブルであれ、OnSheet:: で統一する
module OnSheet
def self.table_name_prefix
'on_sheet_'
Expand Down
4 changes: 4 additions & 0 deletions app/models/on_sheet/nickname.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module OnSheet
class Nickname < ApplicationRecord
end
end
2 changes: 2 additions & 0 deletions app/service/fetch_data_table_service/from_spreadsheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def initialize(spreadsheet_id:, worksheet_name:)
@worksheet_manager = worksheet_manager(sheets_api, spreadsheet, worksheet_name)
rescue StandardError => e
puts "#{Time.zone.now} [LOG] エラー: ワークシートの読み込みに失敗しました。"
puts '※このエラーは Rate Limits の際にも出ることがあります'
puts "ワークシート名: #{worksheet_name}"
puts e.message
puts
Expand All @@ -21,6 +22,7 @@ def headers
@worksheet_manager.headers
rescue StandardError
puts "#{Time.zone.now} [LOG] エラー: ワークシートの読み込みに失敗しました。"
puts '※このエラーは Rate Limits の際にも出ることがあります'
puts "ワークシート名: #{worksheet_name}"
puts e.message
puts
Expand Down
8 changes: 7 additions & 1 deletion app/service/import_service/gss/character.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ module Gss
class Character < Base
def rows
on_sheet_gss_characters = ::OnSheet::GssCharacter.all
on_sheet_gss_character_names = on_sheet_gss_characters.map(&:name)
on_sheet_gss_character_names = on_sheet_gss_characters.map do |character|
if character.sosenkyo_name.present?
character.sosenkyo_name
else
character.name
end
end

on_sheet_gss_character_names.map do |on_sheet_gss_character_name|
[
Expand Down
46 changes: 46 additions & 0 deletions app/service/import_service/gss_character_to_nickname.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module ImportService
class GssCharacterToNickname < Base
def rows
# columns
# gss_character_id
# nickname_id

rows = []
nickname_records = ::OnSheet::Nickname.all

nickname_records.each do |nickname_record|
gss_character_name = nickname_record.name
gss_character_id = ::Gss::Character.find_by(name: gss_character_name).id

nicknames = [
nickname_record.nickname_01,
nickname_record.nickname_02,
nickname_record.nickname_03,
nickname_record.nickname_04,
nickname_record.nickname_05,
nickname_record.nickname_06,
nickname_record.nickname_07,
nickname_record.nickname_08,
nickname_record.nickname_09,
nickname_record.nickname_10,
nickname_record.nickname_11,
nickname_record.nickname_12,
nickname_record.nickname_13,
].compact_blank

nicknames.each do |nickname|
next if nickname == gss_character_name

nickname_id = ::Nickname.find_by(name: nickname).id

rows << [
gss_character_id,
nickname_id,
]
end
end

rows
end
end
end
34 changes: 34 additions & 0 deletions app/service/import_service/nickname.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module ImportService
class Nickname < Base
def rows
# columns
# name

nickname_records = ::OnSheet::Nickname.all

nicknames = nickname_records.map do |nickname_record|
[
nickname_record.nickname_01,
nickname_record.nickname_02,
nickname_record.nickname_03,
nickname_record.nickname_04,
nickname_record.nickname_05,
nickname_record.nickname_06,
nickname_record.nickname_07,
nickname_record.nickname_08,
nickname_record.nickname_09,
nickname_record.nickname_10,
nickname_record.nickname_11,
nickname_record.nickname_12,
nickname_record.nickname_13,
].compact_blank
end.flatten

nicknames.uniq.map do |nickname|
[
nickname,
]
end
end
end
end
6 changes: 6 additions & 0 deletions app/service/import_service/on_sheet/nickname.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module ImportService
module OnSheet
class Nickname < Base
end
end
end
5 changes: 5 additions & 0 deletions config/initializers/constant_value.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# rubocop:disable Metrics/AbcSize
module Vault
class ConstantValue
class << self
Expand All @@ -23,9 +24,13 @@ def class_name_to_csv_filepath_map
),
'ImportService::OnSheet::Character' => Rails.root.join(
Rails.root.join('db/csv_on_sheet/characters/bases.csv')
),
'ImportService::OnSheet::Nickname' => Rails.root.join(
Rails.root.join('db/csv_on_sheet/characters/nicknames.csv')
)
}
end
end
end
end
# rubocop:enable Metrics/AbcSize
2 changes: 2 additions & 0 deletions config/spreadsheet/worksheets_on_id.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ characters:
id: ENV.fetch('SPREADSHEET_ID_CHARACTERS')
worksheet_names:
- bases
- nicknames

gss_hq_characters_section:
id: ENV.fetch('SPREADSHEET_ID_GSS_HQ_CHARACTERS_SECTION')
Expand All @@ -35,3 +36,4 @@ gss_character:
id: ENV.fetch('SPREADSHEET_ID_GSS_CHARACTER')
worksheet_names:
- web_format_data
- DB_nicknames
Loading

0 comments on commit 83d63b1

Please sign in to comment.