diff --git a/app/models/content.rb b/app/models/content.rb index b4bb05c..466af71 100644 --- a/app/models/content.rb +++ b/app/models/content.rb @@ -3,6 +3,7 @@ class Content < ApplicationRecord has_many :user_contents, dependent: :destroy has_many :users, through: :user_contents has_many :line_flags, dependent: :destroy + has_many :line_notifications belongs_to :master enum stream: { default: 0, Mon: 1, Tue: 2, Wed: 3, Thu: 4, Fri: 5, Sat: 6, Sun: 7 } @@ -36,4 +37,12 @@ def line_on def line_off update(line_flag: false) end + + def self.total_line_notification + all.sum(:line_notifications_count) + end + + def self.reset_counter + update_all(line_notifications_count: 0) + end end diff --git a/app/models/line_notification.rb b/app/models/line_notification.rb index c5edd65..5b5a183 100644 --- a/app/models/line_notification.rb +++ b/app/models/line_notification.rb @@ -1,6 +1,6 @@ class LineNotification < ApplicationRecord - belongs_to :master - counter_culture :master + belongs_to :content + counter_culture :content scope :monthly_total, -> { where(month: Date.today.month) } @@ -8,7 +8,7 @@ def self.can_notify? monthly_total.size < 1000 end - def self.create_record(master, month) - create(master_id: master.id, month: month) + def self.create_record(content, month) + create(content_id: content.id, month: month) end end diff --git a/db/migrate/20220106041056_create_line_notifications.rb b/db/migrate/20220106041056_create_line_notifications.rb index 22b21b1..8e902c1 100644 --- a/db/migrate/20220106041056_create_line_notifications.rb +++ b/db/migrate/20220106041056_create_line_notifications.rb @@ -1,7 +1,7 @@ class CreateLineNotifications < ActiveRecord::Migration[5.2] def change create_table :line_notifications do |t| - t.references :master, foreign_key: true + t.references :content, foreign_key: true t.integer :month, null: false t.timestamps end diff --git a/db/migrate/20220107013746_add_line_notifications_count_to_masters.rb b/db/migrate/20220107013746_add_line_notifications_count_to_masters.rb deleted file mode 100644 index 996405b..0000000 --- a/db/migrate/20220107013746_add_line_notifications_count_to_masters.rb +++ /dev/null @@ -1,9 +0,0 @@ -class AddLineNotificationsCountToMasters < ActiveRecord::Migration[5.2] - def self.up - add_column :masters, :line_notifications_count, :integer, null: false, default: 0 - end - - def self.down - remove_column :masters, :line_notifications_count - end -end diff --git a/db/migrate/20220125023304_add_content_to_master_colmuns.rb b/db/migrate/20220125023304_add_content_to_master_colmuns.rb new file mode 100644 index 0000000..a18dff4 --- /dev/null +++ b/db/migrate/20220125023304_add_content_to_master_colmuns.rb @@ -0,0 +1,13 @@ +class AddContentToMasterColmuns < ActiveRecord::Migration[5.2] + def up + add_column :contents, :season, :string + add_column :contents, :dummy_episode, :integer, default: 0 + add_column :contents, :update_day, :string + end + + def down + remove_column :contents, :season, :string + remove_column :contents, :dummy_episode, :integer, default: 0 + remove_column :contents, :update_day, :string + end +end diff --git a/db/migrate/20220125025052_add_line_notifications_count_to_contents.rb b/db/migrate/20220125025052_add_line_notifications_count_to_contents.rb new file mode 100644 index 0000000..da14837 --- /dev/null +++ b/db/migrate/20220125025052_add_line_notifications_count_to_contents.rb @@ -0,0 +1,9 @@ +class AddLineNotificationsCountToContents < ActiveRecord::Migration[5.2] + def self.up + add_column :contents, :line_notifications_count, :integer, null: false, default: 0 + end + + def self.down + remove_column :contents, :line_notifications_count + end +end diff --git a/db/schema.rb b/db/schema.rb index 52a0bb9..bde0b30 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.define(version: 2022_01_18_131731) do +ActiveRecord::Schema.define(version: 2022_01_25_025052) do create_table "contents", force: :cascade do |t| t.string "title", null: false @@ -24,6 +24,10 @@ t.integer "episode", default: 0 t.boolean "line_flag", default: false, null: false t.integer "media", default: 0, null: false + t.string "season" + t.integer "dummy_episode", default: 0 + t.string "update_day" + t.integer "line_notifications_count", default: 0, null: false end create_table "line_flags", force: :cascade do |t| @@ -36,11 +40,11 @@ end create_table "line_notifications", force: :cascade do |t| - t.integer "master_id" + t.integer "content_id" t.integer "month", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.index ["master_id"], name: "index_line_notifications_on_master_id" + t.index ["content_id"], name: "index_line_notifications_on_content_id" end create_table "masters", force: :cascade do |t| @@ -53,7 +57,6 @@ t.integer "episode", default: 0 t.string "season" t.string "update_day" - t.integer "line_notifications_count", default: 0, null: false t.integer "dummy_episode", default: 0 t.integer "media", default: 0, null: false end diff --git a/lib/tasks/reset_counter.rake b/lib/tasks/reset_counter.rake index 3be97b7..000a347 100644 --- a/lib/tasks/reset_counter.rake +++ b/lib/tasks/reset_counter.rake @@ -3,11 +3,11 @@ namespace :reset_counter do task line_notifications: :environment do include Day if first_day? - p "#{last_month}のLINE通知数は#{Master.total_line_notification}件でした" - Master.reset_counter + p "#{last_month}のLINE通知数は#{Content.total_line_notification}件でした" + Content.reset_counter p "LINE通知数をリセットしました" else - p "#{yesterday}時点のLINE通知数は#{Master.total_line_notification}件です" + p "#{yesterday}時点のLINE通知数は#{Content.total_line_notification}件です" end end end