Module: FriendlyId::Mobility
- Defined in:
- lib/friendly_id/mobility.rb,
lib/friendly_id/mobility/version.rb
Defined Under Namespace
Modules: FinderMethods
Constant Summary collapse
- VERSION =
"1.0.3"
Class Method Summary collapse
- .advise_against_untranslated_model(model) ⇒ Object
- .included(model_class) ⇒ Object
- .setup(model_class) ⇒ Object
Instance Method Summary collapse
- #set_friendly_id(text, locale = nil) ⇒ Object
- #set_slug(normalized_slug = nil) ⇒ Object
- #should_generate_new_friendly_id? ⇒ Boolean
Class Method Details
.advise_against_untranslated_model(model) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/friendly_id/mobility.rb', line 32 def advise_against_untranslated_model(model) field = model.friendly_id_config.query_field if model.included_modules.grep(::Mobility::Translations).empty? || model.mobility_attributes.exclude?(field) raise "[FriendlyId] You need to translate the '#{field}' field with " \ "Mobility (add 'translates :#{field}' in your model '#{model.name}')" end end |
.included(model_class) ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/friendly_id/mobility.rb', line 21 def included(model_class) advise_against_untranslated_model(model_class) mod = Module.new do def friendly super.extending(::Mobility::Plugins::ActiveRecord::Query::QueryExtension) end end model_class.send :extend, mod end |
.setup(model_class) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/friendly_id/mobility.rb', line 8 def setup(model_class) model_class.friendly_id_config.use :slugged if model_class.friendly_id_config.uses? :history model_class.instance_eval do friendly_id_config.finder_methods = FriendlyId::Mobility::FinderMethods end end if model_class.friendly_id_config.uses? :finders warn "[FriendlyId] The Mobility add-on is not compatible with the Finders add-on. " \ "Please remove one or the other from the #{model_class} model." end end |
Instance Method Details
#set_friendly_id(text, locale = nil) ⇒ Object
42 43 44 45 46 |
# File 'lib/friendly_id/mobility.rb', line 42 def set_friendly_id(text, locale = nil) ::Mobility.with_locale(locale || ::Mobility.locale) do set_slug normalize_friendly_id(text) end end |
#set_slug(normalized_slug = nil) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/friendly_id/mobility.rb', line 52 def set_slug(normalized_slug = nil) super changed.each do |change| if change =~ /\A(?:#{friendly_id_config.base}|#{friendly_id_config.slug_column})_([a-z]{2}(_[a-z]{2})?)\Z/ locale, suffix = $1.split('_'.freeze) locale = "#{locale}-#{suffix.upcase}".freeze if suffix ::Mobility.with_locale(locale) { super } end end end |
#should_generate_new_friendly_id? ⇒ Boolean
48 49 50 |
# File 'lib/friendly_id/mobility.rb', line 48 def should_generate_new_friendly_id? send(friendly_id_config.slug_column, locale: ::Mobility.locale).nil? end |