diff --git a/app/models/saved_search.rb b/app/models/saved_search.rb index 53b664163..df4c2dddc 100644 --- a/app/models/saved_search.rb +++ b/app/models/saved_search.rb @@ -2,6 +2,17 @@ class SavedSearch < ApplicationRecord REDIS_EXPIRY = 1.hour QUERY_LIMIT = 1000 + attr_reader :disable_labels + belongs_to :user + + before_validation :normalize_query + before_validation :normalize_labels + validates :query, presence: true + validate :validate_count + + scope :labeled, ->(label) { where_array_includes_any_lower(:labels, [normalize_label(label)]) } + scope :has_tag, ->(name) { where_regex(:query, "(^| )[~-]?#{Regexp.escape(name)}( |$)", flags: "i") } + concerning :Redis do extend Memoist @@ -161,15 +172,6 @@ class SavedSearch < ApplicationRecord end end - attr_reader :disable_labels - belongs_to :user - validates :query, presence: true - validate :validate_count - before_validation :normalize_query - before_validation :normalize_labels - scope :labeled, ->(label) { where_array_includes_any_lower(:labels, [normalize_label(label)]) } - scope :has_tag, ->(name) { where_regex(:query, "(^| )[~-]?#{Regexp.escape(name)}( |$)", flags: "i") } - def validate_count if user.saved_searches.count + 1 > user.max_saved_searches self.errors[:user] << "can only have up to #{user.max_saved_searches} " + "saved search".pluralize(user.max_saved_searches)