-
-
Notifications
You must be signed in to change notification settings - Fork 498
Expand file tree
/
Copy pathadapter.rb
More file actions
99 lines (78 loc) · 2.74 KB
/
adapter.rb
File metadata and controls
99 lines (78 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
module Globalize
module ActiveRecord
class Adapter
# 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.
attr_accessor :record, :stash, :translations
private :record=, :stash=
delegate :translation_class, :to => :'record.class'
def initialize(record)
@record = record
@stash = Attributes.new
end
def fetch_stash(locale, name)
stash.read(locale, name)
end
delegate :contains?, :to => :stash, :prefix => :stash
delegate :write, :to => :stash
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)
set_metadata(value, :locale => fallback, :requested_locale => locale)
return value
end
end
return nil
end
def save_translations!
stash.each do |locale, attrs|
next if attrs.empty?
translation = record.translations_by_locale[locale] ||
record.translations.build(locale: locale.to_s)
attrs.each do |name, value|
value = value.val if value.is_a?(Arel::Nodes::Casted)
translation[name] = value
end
end
reset
end
def reset
stash.clear
end
protected
def type_cast(name, value)
return value.presence unless column = column_for_attribute(name)
column.type_cast value
end
def column_for_attribute(name)
translation_class.columns_hash[name.to_s]
end
def unserializable_attribute?(name, column)
column.text? && translation_class.serialized_attributes[name.to_s]
end
def fetch_attribute(locale, name)
translation = record.translation_for(locale, false)
if translation
translation.send(name)
else
record.class.translation_class.new.send(name)
end
end
def set_metadata(object, metadata)
object.translation_metadata.merge!(metadata) if object.respond_to?(:translation_metadata)
object
end
def translation_metadata_accessor(object)
return if obj.respond_to?(:translation_metadata)
class << object; attr_accessor :translation_metadata end
object.translation_metadata ||= {}
end
def fallbacks_for?(object)
object.nil? || (fallbacks_for_empty_translations? && object.blank?)
end
delegate :fallbacks_for_empty_translations?, :to => :record, :prefix => false
prepend AdapterDirty
end
end
end