Module: Mongoid::Globalize

Extended by:
ActiveSupport::Concern
Includes:
Methods
Defined in:
lib/mongoid_globalize/attributes.rb,
lib/mongoid_globalize/document_translation.rb,
lib/mongoid_globalize/fields_builder.rb,
lib/mongoid_globalize/class_methods.rb,
lib/mongoid_globalize/act_macro.rb,
lib/mongoid_globalize/methods.rb,
lib/mongoid_globalize/adapter.rb,
lib/mongoid_globalize.rb

Overview

Helper class for storing values per locale. Used by Mongoid::Globalize::Adapter to stash and cache attribute values.

Defined Under Namespace

Modules: ActMacro, ClassMethods, Methods Classes: Adapter, Attributes, DocumentTranslation, FieldsBuilder

Class Method Summary collapse

Class Method Details

.fallbacks(locale = self.locale) ⇒ Object

Returns fallback locales for given locale if any. Returns Array of Symbols



90
91
92
# File 'lib/mongoid_globalize.rb', line 90

def fallbacks(locale = self.locale)
  fallbacks? ? I18n.fallbacks[locale] : [locale.to_sym]
end

.fallbacks?Boolean

Checks whether I18n respond to fallbacks method. Returns true or false

Returns:

  • (Boolean)


84
85
86
# File 'lib/mongoid_globalize.rb', line 84

def fallbacks?
  I18n.respond_to?(:fallbacks)
end

.localeObject

Get current locale. If curent locale doesn’t set obviously for Mongoid::Globalize, returns I18n locale

Mongoid::Globalize.locale   #=> :en

Returns Symbol



45
46
47
# File 'lib/mongoid_globalize.rb', line 45

def locale
  read_locale || I18n.locale
end

.locale=(locale) ⇒ Object

Set current locale by saving it in current thread.

Mongoid::Globalize.locale = 'ru'    #=> :ru

Param String or Symbol Returns Symbol or nil



53
54
55
# File 'lib/mongoid_globalize.rb', line 53

def locale=(locale)
  set_locale(locale)
end

.with_locale(locale, &block) ⇒ Object

Runs block as if given locale is setted. Don’t touch current locale. Yelds locale into block.

Mongoid::Globalize.with_locale(:de) { post.title = 'Titel' }

Param String or Symbol Param Proc Returns result from block



63
64
65
66
67
68
69
# File 'lib/mongoid_globalize.rb', line 63

def with_locale(locale, &block)
  previous_locale = read_locale
  set_locale(locale)
  result = yield(locale)
  set_locale(previous_locale)
  result
end

.with_locales(*locales, &block) ⇒ Object

Runs block for each given locale.

Mongoid::Globalize.with_locale(:ru, [:de, :fr]) { post.title = 'Title' }

Params String or Symbol or Array of Strings or Symbols Param Proc Returns Array with results from block for each locale



76
77
78
79
80
# File 'lib/mongoid_globalize.rb', line 76

def with_locales(*locales, &block)
  locales.flatten.map do |locale|
    with_locale(locale, &block)
  end
end