module FriendlyId::JsonTranslate
Public Class Methods
included(model_class)
click to toggle source
# File lib/friendly_id/json_translate.rb, line 10 def included(model_class) advise_against_untranslated_model(model_class) end
setup(model_class)
click to toggle source
# File lib/friendly_id/json_translate.rb, line 6 def setup(model_class) model_class.friendly_id_config.use :slugged end
Private Class Methods
advise_against_untranslated_model(model)
click to toggle source
# File lib/friendly_id/json_translate.rb, line 14 def advise_against_untranslated_model(model) field = model.friendly_id_config.query_field unless model.respond_to?('translated_attribute_names') || model.translated_attribute_names.exclude?(field.to_sym) raise "[FriendlyId] You need to translate the '#{field}' field with " \ "json_translates (add 'translates :#{field}' in your model '#{model.name}')" end end
Public Instance Methods
execute_with_locale(locale = ::I18n.locale, &block)
click to toggle source
Auxiliar function to execute a block with other locale set
# File lib/friendly_id/json_translate.rb, line 66 def execute_with_locale(locale = ::I18n.locale, &block) actual_locale = ::I18n.locale ::I18n.locale = locale block.call ::I18n.locale = actual_locale end
exists_by_friendly_id?(id)
click to toggle source
# File lib/friendly_id/json_translate.rb, line 79 def exists_by_friendly_id?(id) send("with_#{friendly_id_config.query_field}_translation", id).exists? end
first_by_friendly_id(id)
click to toggle source
# File lib/friendly_id/json_translate.rb, line 83 def first_by_friendly_id(id) send("with_#{friendly_id_config.query_field}_translation", id).first end
get_field_translations_hash()
click to toggle source
# File lib/friendly_id/json_translate.rb, line 60 def get_field_translations_hash self.send("#{friendly_id_config.query_field}_translations") end
set_friendly_id(text, locale = nil)
click to toggle source
# File lib/friendly_id/json_translate.rb, line 26 def set_friendly_id(text, locale = nil) execute_with_locale(locale || ::I18n.locale) do set_slug normalize_friendly_id(text) end end
set_slug(normalized_slug = nil)
click to toggle source
# File lib/friendly_id/json_translate.rb, line 46 def set_slug(normalized_slug = nil) Cdx::Setting.current.available_locales.each do |locale| execute_with_locale(locale) { super_set_slug(normalized_slug) } end end
should_generate_new_friendly_id?()
click to toggle source
# File lib/friendly_id/json_translate.rb, line 32 def should_generate_new_friendly_id? translations = get_field_translations_hash return true unless translations if self.respond_to?("#{friendly_id_config.base}_translations") return self.send "#{friendly_id_config.base}_translations_changed?" else return self.send "#{friendly_id_config.base}_changed?" end false end
super_set_slug(normalized_slug = nil)
click to toggle source
# File lib/friendly_id/json_translate.rb, line 52 def super_set_slug(normalized_slug = nil) if should_generate_new_friendly_id? candidates = FriendlyId::Candidates.new(self, normalized_slug || send(friendly_id_config.base)) slug = slug_generator.generate(candidates) || resolve_friendly_id_conflict(candidates) self.send("#{friendly_id_config.query_field}=", slug) end end