Class: Globalize::ActiveRecord::Adapter
- Inherits:
-
Object
- Object
- Globalize::ActiveRecord::Adapter
- Defined in:
- lib/globalize/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
- #all_blank?(locale, attrs) ⇒ Boolean
- #fetch(locale, name) ⇒ Object
- #fetch_stash(locale, name) ⇒ Object
-
#initialize(record) ⇒ Adapter
constructor
A new instance of Adapter.
- #reset ⇒ Object
- #save_translations! ⇒ Object
- #write(locale, name, value) ⇒ Object
Constructor Details
#initialize(record) ⇒ Adapter
Returns a new instance of Adapter.
11 12 13 14 |
# File 'lib/globalize/active_record/adapter.rb', line 11 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/globalize/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/globalize/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/globalize/active_record/adapter.rb', line 6 def translations @translations end |
Instance Method Details
#all_blank?(locale, attrs) ⇒ Boolean
52 53 54 |
# File 'lib/globalize/active_record/adapter.rb', line 52 def all_blank?(locale, attrs) [attrs].flatten.collect { |name| fetch(locale, name).blank? }.all? end |
#fetch(locale, name) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/globalize/active_record/adapter.rb', line 22 def fetch(locale, name) Globalize.fallbacks(locale).each do |fallback| value = fetch_stash(fallback, name) || fetch_attribute(fallback, name) unless fallbacks_for?(value) (value, :locale => fallback, :requested_locale => locale) return value end end return nil end |
#fetch_stash(locale, name) ⇒ Object
16 17 18 19 20 |
# File 'lib/globalize/active_record/adapter.rb', line 16 def fetch_stash(locale, name) value = stash.read(locale, name) return value if value return nil end |
#reset ⇒ Object
48 49 50 |
# File 'lib/globalize/active_record/adapter.rb', line 48 def reset stash.clear end |
#save_translations! ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/globalize/active_record/adapter.rb', line 38 def save_translations! stash.each do |locale, attrs| translation = record.translations.find_or_initialize_by_locale(locale.to_s) attrs.each { |name, value| translation[name] = value } translation.save! end record.translations.reset stash.clear end |
#write(locale, name, value) ⇒ Object
34 35 36 |
# File 'lib/globalize/active_record/adapter.rb', line 34 def write(locale, name, value) stash.write(locale, name, value) end |