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



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/globalize/active_record/adapter.rb', line 26

def fetch(locale, name)
  record.globalize_fallbacks(locale).each do |fallback|
    value = stash.contains?(fallback, name) ? 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



62
63
64
# File 'lib/globalize/active_record/adapter.rb', line 62

def reset
  stash.clear
end

#save_translations!Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/globalize/active_record/adapter.rb', line 43

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

  reset
end

#stash_contains?(locale, name) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/globalize/active_record/adapter.rb', line 22

def stash_contains?(locale, name)
  stash.contains?(locale, name)
end

#write(locale, name, value) ⇒ Object



39
40
41
# File 'lib/globalize/active_record/adapter.rb', line 39

def write(locale, name, value)
  stash.write(locale, name, value)
end