Class: PhrasingPhrase

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

Class Method Summary collapse

Class Method Details

.find_phrase(key) ⇒ Object



11
12
13
# File 'app/models/phrasing_phrase.rb', line 11

def self.find_phrase(key)
  where(key: key, locale: I18n.locale.to_s).first || search_i18n_and_create_phrase(key)
end

.fuzzy_search(search_term, locale) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/models/phrasing_phrase.rb', line 15

def self.fuzzy_search(search_term, locale)
  query = order(:key)
  query = query.where(locale: locale) if locale.present?

  if search_term.present?
    key_like   = PhrasingPhrase.arel_table[:key].matches("%#{search_term}%")
    value_like = PhrasingPhrase.arel_table[:value].matches("%#{search_term}%")
    query.where(key_like.or(value_like))
  else
    # because we want to have non nil values first.
    query.where("value is not null") + query.where("value is null")
  end
end