class Translatable::ActiveRecord::Adapter
Attributes
record[RW]
The cache caches attributes that already were looked up for read access. The stash keeps track of new or changed values that need to be saved.
stash[RW]
The cache caches attributes that already were looked up for read access. The stash keeps track of new or changed values that need to be saved.
translations[RW]
The cache caches attributes that already were looked up for read access. The stash keeps track of new or changed values that need to be saved.
Public Class Methods
new(record)
click to toggle source
# File lib/translatable/active_record/adapter.rb, line 9 def initialize(record) self.record = record self.stash = Attributes.new end
Public Instance Methods
fetch(locale, name, value = nil)
click to toggle source
# File lib/translatable/active_record/adapter.rb, line 24 def fetch(locale, name, value = nil) value = stash_contains?(locale, name) ? fetch_stash(locale, name) : fetch_attribute(locale, name, value) return value end
fetch_stash(locale, name)
click to toggle source
# File lib/translatable/active_record/adapter.rb, line 14 def fetch_stash(locale, name) value = stash.read(locale, name) return value if value return nil end
reset()
click to toggle source
# File lib/translatable/active_record/adapter.rb, line 49 def reset stash.clear end
save_translations!()
click to toggle source
# File lib/translatable/active_record/adapter.rb, line 34 def save_translations! stash.reject {|locale, attrs| attrs.empty?}.each do |locale, attrs| translations = record.translations_by_locale[locale] attrs.each { |name, value| translation = translations && translations[name] || record.translations.build(:locale => locale.to_s, :scope => record.class.table_name, :key => name) translation['record_id'] = record.id translation['value'] = value translation.save! } end reset end
stash_contains?(locale, name)
click to toggle source
# File lib/translatable/active_record/adapter.rb, line 20 def stash_contains?(locale, name) stash.contains?(locale, name) end
write(locale, name, value)
click to toggle source
# File lib/translatable/active_record/adapter.rb, line 30 def write(locale, name, value) stash.write(locale, name, value) end
Protected Instance Methods
fetch_attribute(locale, name, value = nil)
click to toggle source
# File lib/translatable/active_record/adapter.rb, line 55 def fetch_attribute(locale, name, value = nil) if value.nil? translation = record.translation_for(locale, name) return translation && translation.send(:value) else translation = record.translation_for_serialized(locale, name, value) return translation end end