Class: SingleTableGlobalize3::ActiveRecord::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/single_table_globalize3/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/single_table_globalize3/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/single_table_globalize3/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/single_table_globalize3/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/single_table_globalize3/active_record/adapter.rb', line 6

def translations
  @translations
end

Instance Method Details

#fetch(locale, name) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/single_table_globalize3/active_record/adapter.rb', line 34

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_locales(name) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/single_table_globalize3/active_record/adapter.rb', line 20

def fetch_locales(name)
  stash.keys.inject({}) do |result, locale|
    if stash_contains?(locale, name)
      result.update(locale => fetch_stash(locale, name))
    else
      result
    end
  end
end

#fetch_stash(locale, name) ⇒ Object



16
17
18
# File 'lib/single_table_globalize3/active_record/adapter.rb', line 16

def fetch_stash(locale, name)
  stash.read(locale, name)
end

#resetObject



64
65
66
# File 'lib/single_table_globalize3/active_record/adapter.rb', line 64

def reset
  stash.clear
end

#save_translations!Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/single_table_globalize3/active_record/adapter.rb', line 51

def save_translations!
  stash.each do |locale, attrs|
    locale_str = locale.to_s
    attrs.each do |name, value|
      translation = record.translations.detect {|t| t.locale.to_s == locale_str && t.attribute_name == name } ||
        record.translations.build(:locale => locale_str, :attribute_name => name)
      translation.update_attribute(:value, value)
    end
  end

  reset
end

#stash_contains?(locale, name) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/single_table_globalize3/active_record/adapter.rb', line 30

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

#write(locale, name, value) ⇒ Object



47
48
49
# File 'lib/single_table_globalize3/active_record/adapter.rb', line 47

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