2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-28 16:56:44 -04:00
|
|
|
require_dependency 'enum'
|
|
|
|
|
|
|
|
class WatchedWord < ActiveRecord::Base
|
|
|
|
|
|
|
|
def self.actions
|
|
|
|
@actions ||= Enum.new(
|
|
|
|
block: 1,
|
|
|
|
censor: 2,
|
|
|
|
require_approval: 3,
|
|
|
|
flag: 4
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
MAX_WORDS_PER_ACTION = 1000
|
|
|
|
|
|
|
|
before_validation do
|
|
|
|
self.word = self.class.normalize_word(self.word)
|
|
|
|
end
|
|
|
|
|
|
|
|
validates :word, presence: true, uniqueness: true, length: { maximum: 50 }
|
|
|
|
validates :action, presence: true
|
|
|
|
validates_each :word do |record, attr, val|
|
|
|
|
if WatchedWord.where(action: record.action).count >= MAX_WORDS_PER_ACTION
|
|
|
|
record.errors.add(:word, :too_many)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
after_save :clear_cache
|
|
|
|
after_destroy :clear_cache
|
|
|
|
|
|
|
|
scope :by_action, -> { order("action ASC, word ASC") }
|
|
|
|
|
|
|
|
def self.normalize_word(w)
|
2018-01-09 16:51:45 -05:00
|
|
|
w.strip.squeeze('*')
|
2017-06-28 16:56:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.create_or_update_word(params)
|
2018-01-09 16:51:45 -05:00
|
|
|
new_word = normalize_word(params[:word])
|
|
|
|
w = WatchedWord.where("word ILIKE ?", new_word).first || WatchedWord.new(word: new_word)
|
2017-06-28 16:56:44 -04:00
|
|
|
w.action_key = params[:action_key] if params[:action_key]
|
|
|
|
w.action = params[:action] if params[:action]
|
|
|
|
w.save
|
|
|
|
w
|
|
|
|
end
|
|
|
|
|
|
|
|
def action_key=(arg)
|
|
|
|
self.action = self.class.actions[arg.to_sym]
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear_cache
|
|
|
|
WordWatcher.clear_cache!
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2017-07-27 22:37:10 -04:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: watched_words
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# word :string not null
|
|
|
|
# action :integer not null
|
2019-01-11 14:29:56 -05:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2017-07-27 22:37:10 -04:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_watched_words_on_action_and_word (action,word) UNIQUE
|
|
|
|
#
|