Class: Tag

Inherits:
Object
  • Object
show all
Defined in:
lib/kete_translatable_content/extensions/models/tag.rb

Class Method Summary collapse

Class Method Details

.find_or_create_with_like_by_name(name) ⇒ Object

redefine so if no tag is found initially, we look for translations too this method is used by acts-as-taggable-on for all creations of tags



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/kete_translatable_content/extensions/models/tag.rb', line 25

def self.find_or_create_with_like_by_name(name)
  # first, see if any tags exist with this name
  existing_tag = first(:conditions => ["name LIKE ?", name])

  # if no tag exists, look through tag translations for the current locale
  if !existing_tag && existing_tag = Tag::Translation.first(:name => name, :locale => I18n.locale)
    # if a translation is found, return the original Tag object, but with the values of the locale
    existing_tag = Tag.swap_in_translation_for_single(existing_tag.translatable)
  end

  existing_tag || create(:name => name, :locale => I18n.locale)
end