Class: Globalize::ActiveRecord::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/globalize/active_record/adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#recordObject

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

#stashObject

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

#translationsObject

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

#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

#resetObject



52
53
54
# File 'lib/globalize/active_record/adapter.rb', line 52

def reset
  stash.clear
end

#save_translations!Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/globalize/active_record/adapter.rb', line 38

def save_translations!
  stash.each do |locale, attrs|
    if attrs.any?
      translation = record.translations.find_or_initialize_by_locale(locale.to_s)
      attrs.each { |name, value| translation[name] = value }
      translation.save!
    end
  end

  record.translations.each(&:reload)
  record.translations.reset
  reset
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