class Mongoid::Globalize::Adapter
The Adapter
class used for stashing translates and changes in its before they will be persisted or rejected.
Attributes
Public Class Methods
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
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
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
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
Clears stash.
# File lib/mongoid_globalize/adapter.rb, line 60 def reset stash.clear end
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
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
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
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