module Mongoid::Globalize::Methods

Public Instance Methods

attribute_names() click to toggle source

Mongoid documents haven’t attribute_names method, so I replace super with +@attributes.keys.sort+. So this method returns only translated and existing attribute names (but not all available names as in AR or G3)

# File lib/mongoid_globalize/methods.rb, line 76
def attribute_names
  translated_attribute_names.map(&:to_s) + @attributes.keys.sort
end
attributes() click to toggle source

The most trouble method of Mongoid::Globalize :-( Extends reader for @attributes. Mixes translated attributes to Mongoid @attributes. Return: Hash

Calls superclass method
# File lib/mongoid_globalize/methods.rb, line 16
def attributes
  unless @stop_merging_translated_attributes || @attributes.frozen?
    @attributes.merge! translated_attributes
  end
  super
end
clone() click to toggle source

Extends Mongoid::Document’s method clone. Adds to cloned object all translations from original object.

Calls superclass method
# File lib/mongoid_globalize/methods.rb, line 129
def clone
  obj = super
  return obj unless respond_to?(:translated_attribute_names)
  obj.instance_variable_set(:@globalize, nil )
  each_locale_and_translated_attribute do |locale, name|
    obj.globalize.write(locale, name, globalize.fetch(locale, name) )
  end
  return obj
end
globalize() click to toggle source

Reader for adapter, where translations stashing during lifecicle. At first use creates new one. Return: Mongoid::Globalize::Adapter

# File lib/mongoid_globalize/methods.rb, line 8
def globalize
  @globalize ||= Adapter.new(self)
end
process(attributes, *args) click to toggle source

Extends Mongoid::Document’s method process. Pocesses given attributes in consideration of possible :locale key. Used by Mongoid for all attribute- related operations, such as create, update etc. Param: Hash of attributes Other params will be transmitted into Mongoid::Document’s method process as is.

Calls superclass method
# File lib/mongoid_globalize/methods.rb, line 29
def process(attributes, *args)
  with_given_locale(attributes) { super }
end
read_attribute(name, options = {}) click to toggle source

Extends Mongoid::Document’s method read_attribute. If writed attribute is translateble, it is readed from adapter’s stash. Param: String or Symbol - name of attribute Param: Hash of options Return: Object - value of attribute

Calls superclass method
# File lib/mongoid_globalize/methods.rb, line 59
def read_attribute(name, options = {})
  options = {:translated => true, :locale => nil}.merge(options)
  if translated?(name) and options[:translated]
    globalize.fetch(options[:locale] || Mongoid::Globalize.locale, name)
  else
    super(name)
  end
end
reload() click to toggle source

Extends Mongoid::Document’s method reload. Resets all translation changes.

Calls superclass method
# File lib/mongoid_globalize/methods.rb, line 121
def reload
  translated_attribute_names.each { |name| @attributes.delete(name.to_s) }
  globalize.reset
  super
end
remove_attribute(name) click to toggle source

TODO

Calls superclass method
# File lib/mongoid_globalize/methods.rb, line 69
def remove_attribute(name)
  super name
end
set_translations(options) click to toggle source

Updates fields separately for each given locale

post.set_translations(
  :en => { :title => "updated title" },
  :de => { :content => "geänderter Inhalt" }
)

Param: Hash, where keys are locales and values are Hashes of name-value pairs for fields.

# File lib/mongoid_globalize/methods.rb, line 112
def set_translations(options)
  options.keys.each do |locale|
    translation = translation_for(locale) || translations.build(:locale => locale.to_s)
    translation.update_attributes!(options[locale])
  end
end
translated?(name) click to toggle source

Checks whether field with given name is translated field. Param String or Symbol Returns true or false

# File lib/mongoid_globalize/methods.rb, line 83
def translated?(name)
  self.class.translated?(name)
end
translated_attributes() click to toggle source

Returns translations for current locale. Is used for initial mixing into @attributes hash. Actual translations are in @translated_attributes hash. Return Hash

# File lib/mongoid_globalize/methods.rb, line 90
def translated_attributes
  @translated_attributes ||= translated_attribute_names.inject({}) do |attrs, name|
    attrs.merge(name.to_s => translation.send(name))
  end
end
translation() click to toggle source

Returns instance of Translation for current locale.

# File lib/mongoid_globalize/methods.rb, line 140
def translation
  translation_for(Mongoid::Globalize.locale)
end
translation_for(locale) click to toggle source

Returns instance of Translation for given locale. Param String or Symbol

# File lib/mongoid_globalize/methods.rb, line 146
def translation_for(locale)
  @translation_caches ||= {}
  # Need to temporary switch of merging, because #translations uses
  # #attributes method too, to avoid stack level too deep error.
  @stop_merging_translated_attributes = true
  unless @translation_caches[locale]
    _translation = translations.find_by_locale(locale)
    _translation ||= translations.build(:locale => locale)
    @translation_caches[locale] = _translation
  end
  @stop_merging_translated_attributes = false
  @translation_caches[locale]
end
untranslated_attributes() click to toggle source

TODO:

# File lib/mongoid_globalize/methods.rb, line 97
def untranslated_attributes
  attrs = {}
  attribute_names.each do |name|
    attrs[name] = read_attribute(name, {:translated => false})
  end
  attrs
end
write_attribute(name, value, options = {}) click to toggle source

Extends Mongoid::Document’s method write_attribute. If writed attribute is translateble, it is placed into adapter’s stash. Param: String or Symbol - name of attribute Param: Object - value of attribute Param: Hash of options

Calls superclass method
# File lib/mongoid_globalize/methods.rb, line 38
def write_attribute(name, value, options = {})
  if translated?(name)
    options = {:locale => nil}.merge(options)
    access = name.to_s
    unless attributes[access] == value || attribute_changed?(access)
      attribute_will_change! access
    end
    @translated_attributes[access] = value
    the_locale = options[:locale] || Mongoid::Globalize.locale
    self.translations.reject!{ |t| t.new_record? && t.locale != the_locale } if self.class.fallbacks_for_empty_translations
    globalize.write(the_locale, name, value)
  else
    super(name, value)
  end
end

Protected Instance Methods

clear_translations!() click to toggle source

After save callback. Reset some values.

# File lib/mongoid_globalize/methods.rb, line 190
def clear_translations!
  @translation_caches = {}
  @stop_merging_translated_attributes = nil
end
each_locale_and_translated_attribute() { |locale, name| ... } click to toggle source

Executes given block for each locale and translated attribute name for this document.

# File lib/mongoid_globalize/methods.rb, line 163
def each_locale_and_translated_attribute
  used_locales.each do |locale|
    translated_attribute_names.each do |name|
      yield locale, name
    end
  end
end
prepare_translations!() click to toggle source

Before save callback. Cleans @attributes hash from translated attributes and prepares them for persisting.

# File lib/mongoid_globalize/methods.rb, line 180
def prepare_translations!
  @stop_merging_translated_attributes = true
  translated_attribute_names.each do |name|
    @attributes.delete name.to_s
    @changed_attributes.delete name.to_s if @changed_attributes
  end
  globalize.prepare_translations!
end
used_locales() click to toggle source

Return Array with locales, used for translation of this document

# File lib/mongoid_globalize/methods.rb, line 172
def used_locales
  locales = globalize.stash.keys.concat(globalize.stash.keys).concat(translations.translated_locales)
  locales.uniq!
  locales
end
with_given_locale(attributes) { || ... } click to toggle source

Detects locale in given attributes and executes given block for it. Param: Hash of attributes Param: Proc

# File lib/mongoid_globalize/methods.rb, line 198
def with_given_locale(attributes, &block)
  attributes.symbolize_keys! if attributes.respond_to?(:symbolize_keys!)
  if locale = attributes.try(:delete, :locale)
    Mongoid::Globalize.with_locale(locale, &block)
  else
    yield
  end
end