Class: Translatable::ActiveRecord::Adapter
- Inherits:
-
Object
- Object
- Translatable::ActiveRecord::Adapter
- Defined in:
- lib/translatable/active_record/adapter.rb
Instance Attribute Summary collapse
-
#record ⇒ Object
readonly
The cache caches attributes that already were looked up for read access.
-
#stash ⇒ Object
readonly
The cache caches attributes that already were looked up for read access.
-
#translations ⇒ Object
The cache caches attributes that already were looked up for read access.
Instance Method Summary collapse
- #fetch(locale, name, value = nil) ⇒ Object
- #fetch_stash(locale, name) ⇒ Object
-
#initialize(record) ⇒ Adapter
constructor
A new instance of Adapter.
- #reset ⇒ Object
- #save_translations! ⇒ Object
- #stash_contains?(locale, name) ⇒ Boolean
- #write(locale, name, value) ⇒ Object
Constructor Details
#initialize(record) ⇒ Adapter
Returns a new instance of Adapter.
9 10 11 12 |
# File 'lib/translatable/active_record/adapter.rb', line 9 def initialize(record) self.record = record self.stash = Attributes.new end |
Instance Attribute Details
#record ⇒ Object
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.
6 7 8 |
# File 'lib/translatable/active_record/adapter.rb', line 6 def record @record end |
#stash ⇒ Object
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.
6 7 8 |
# File 'lib/translatable/active_record/adapter.rb', line 6 def stash @stash end |
#translations ⇒ Object
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.
6 7 8 |
# File 'lib/translatable/active_record/adapter.rb', line 6 def translations @translations end |
Instance Method Details
#fetch(locale, name, value = nil) ⇒ Object
24 25 26 27 28 |
# 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) ⇒ Object
14 15 16 17 18 |
# 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 ⇒ Object
49 50 51 |
# File 'lib/translatable/active_record/adapter.rb', line 49 def reset stash.clear end |
#save_translations! ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# 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) ⇒ Boolean
20 21 22 |
# File 'lib/translatable/active_record/adapter.rb', line 20 def stash_contains?(locale, name) stash.contains?(locale, name) end |
#write(locale, name, value) ⇒ Object
30 31 32 |
# File 'lib/translatable/active_record/adapter.rb', line 30 def write(locale, name, value) stash.write(locale, name, value) end |