2024-04-29 18:20:55 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class WatchedWordGroup < ActiveRecord::Base
|
|
|
|
validates :action, presence: true
|
2024-05-30 09:02:57 +08:00
|
|
|
validate :watched_words_validation
|
2024-04-29 18:20:55 +08:00
|
|
|
|
|
|
|
has_many :watched_words, dependent: :destroy
|
|
|
|
|
2024-05-30 09:02:57 +08:00
|
|
|
def watched_words_validation
|
|
|
|
watched_words.each { |word| errors.merge!(word.errors) }
|
|
|
|
errors.add(:watched_words, :empty) if watched_words.empty?
|
|
|
|
end
|
|
|
|
|
2024-04-29 18:20:55 +08:00
|
|
|
def create_or_update_members(words, params)
|
|
|
|
WatchedWordGroup.transaction do
|
2024-05-01 00:08:37 +08:00
|
|
|
self.action = WatchedWord.actions[params[:action_key].to_sym]
|
2024-04-29 18:20:55 +08:00
|
|
|
|
|
|
|
words.each do |word|
|
2024-05-30 09:02:57 +08:00
|
|
|
watched_word = WatchedWord.create_or_update_word(params.merge(word: word))
|
|
|
|
self.watched_words << watched_word
|
2024-04-29 18:20:55 +08:00
|
|
|
end
|
2024-05-30 09:02:57 +08:00
|
|
|
|
|
|
|
self.save!
|
2024-04-29 18:20:55 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def action_log_details
|
2024-05-01 00:08:37 +08:00
|
|
|
"#{WatchedWord.actions.key(self.action)} → #{watched_words.pluck(:word).join(", ")}"
|
2024-04-29 18:20:55 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: watched_word_groups
|
|
|
|
#
|
|
|
|
# id :bigint not null, primary key
|
|
|
|
# action :integer not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
#
|