class Mongoid::Globalize::Adapter

The Adapter class used for stashing translates and changes in its before they will be persisted or rejected.

Attributes

record[RW]
stash[RW]
translations[RW]

Public Class Methods

new(record) click to toggle source

Initialises new instance of Adapter. Creates empty stash for storing translates. Param: translatable Class

# File lib/mongoid_globalize/adapter.rb, line 11
def initialize(record)
  self.record = record
  self.stash = Attributes.new
end

Public Instance Methods

fetch(locale, name) click to toggle source

Returns value of attribute for given locale or it’s fallbacks. Param: String or Symbol - name of locale Param: String or Symbol - name of attribute Returns nil if no value finded

# File lib/mongoid_globalize/adapter.rb, line 30
def fetch(locale, name)
  Mongoid::Globalize.fallbacks(locale).each do |fallback|
    value = fetch_stash(fallback, name) || fetch_attribute(fallback, name)
    return value unless fallbacks_for?(value)
  end
  return nil
end
fetch_stash(locale, name) click to toggle source

Returns value of attribute from stash for given locale. Param: String or Symbol - name of locale Param: String or Symbol - name of attribute Returns nil if no value finded

# File lib/mongoid_globalize/adapter.rb, line 20
def fetch_stash(locale, name)
  value = stash.read(locale, name)
  return value if value
  return nil
end
prepare_translations!() click to toggle source

Prepares data from stash for persisting in embeded Translation documents. Also clears stash for further operations.

# File lib/mongoid_globalize/adapter.rb, line 48
def prepare_translations!
  stash.each do |locale, attrs|
    if attrs.any?
      translation = record.translations.find_by_locale(locale)
      translation ||= record.translations.build(:locale => locale)
      attrs.each{ |name, value| translation[name] = value }
    end
  end
  reset
end
reset() click to toggle source

Clears stash.

# File lib/mongoid_globalize/adapter.rb, line 60
def reset
  stash.clear
end
write(locale, name, value) click to toggle source

Writes value of attribute for given locale into stash. Param: String or Symbol - name of locale Param: String or Symbol - name of attribute Param: Object - value of attribute

# File lib/mongoid_globalize/adapter.rb, line 42
def write(locale, name, value)
  stash.write(locale, name, value)
end

Protected Instance Methods

fallbacks_for?(object) click to toggle source

Checks if object needs fallbacks Param: Object Result: true or false

# File lib/mongoid_globalize/adapter.rb, line 74
def fallbacks_for?(object)
  object.nil? || (fallbacks_for_empty_translations? && object.blank?)
end
fallbacks_for_empty_translations?() click to toggle source

Checks option fallbacks_for_empty_translations

# File lib/mongoid_globalize/adapter.rb, line 79
def fallbacks_for_empty_translations?
  record.fallbacks_for_empty_translations
end
fetch_attribute(locale, name) click to toggle source

Returns persisted value of attribute for given locale or nil.

# File lib/mongoid_globalize/adapter.rb, line 66
def fetch_attribute(locale, name)
  translation = record.translation_for(locale)
  return translation && translation.send(name)
end