Class: WatchedWord

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/watched_word.rb

Constant Summary collapse

MAX_WORDS_PER_ACTION =
2000

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.actionsObject



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'app/models/watched_word.rb', line 4

def self.actions
  @actions ||=
    Enum.new(
      block: 1,
      censor: 2,
      require_approval: 3,
      flag: 4,
      link: 8,
      replace: 5,
      tag: 6,
      silence: 7,
    )
end

.create_or_update_word(params) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'app/models/watched_word.rb', line 68

def self.create_or_update_word(params)
  new_word = normalize_word(params[:word])
  w = self.for(word: new_word).first_or_initialize(word: new_word)
  w.replacement = params[:replacement] if params[:replacement]
  w.action_key = params[:action_key] if params[:action_key]
  w.action = params[:action] if params[:action]
  w.case_sensitive = params[:case_sensitive] if !params[:case_sensitive].nil?
  w.save
  w
end

.has_replacement?(action) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'app/models/watched_word.rb', line 79

def self.has_replacement?(action)
  action == :replace || action == :tag || action == :link
end

.normalize_word(w) ⇒ Object



52
53
54
# File 'app/models/watched_word.rb', line 52

def self.normalize_word(w)
  w.strip.squeeze("*")
end

Instance Method Details

#action_key=(arg) ⇒ Object



83
84
85
# File 'app/models/watched_word.rb', line 83

def action_key=(arg)
  self.action = self.class.actions[arg.to_sym]
end

#action_log_detailsObject



87
88
89
90
91
92
93
# File 'app/models/watched_word.rb', line 87

def action_log_details
  if replacement.present?
    "#{word}#{replacement}"
  else
    word
  end
end

#clear_cacheObject



95
96
97
# File 'app/models/watched_word.rb', line 95

def clear_cache
  WordWatcher.clear_cache!
end

#replacement_is_tag_listObject



60
61
62
63
64
65
66
# File 'app/models/watched_word.rb', line 60

def replacement_is_tag_list
  tag_list = replacement&.split(",")
  tags = Tag.where(name: tag_list)
  if (tag_list.blank? || tags.empty? || tag_list.size != tags.size)
    errors.add(:base, :invalid_tag_list)
  end
end

#replacement_is_urlObject



56
57
58
# File 'app/models/watched_word.rb', line 56

def replacement_is_url
  errors.add(:base, :invalid_url) if !(replacement =~ URI.regexp)
end