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
|
# File 'lib/puffer_pages/backends/models/mixins/translatable.rb', line 8
def translatable *fields
if PufferPages.localize
translates *fields, fallbacks_for_empty_translations: true
translation_class.send(:include, ActiveUUID::UUID)
def self.globalize_migrator
@globalize_migrator ||= PufferPages::Globalize::Migrator.new(self)
end
fields.each do |field|
define_method "#{field}_translations" do
result = translations.each_with_object(HashWithIndifferentAccess.new) do |translation, result|
result[translation.locale] = translation.send(field)
end
globalize.stash.keys.each_with_object(result) do |locale, result|
result[locale] = globalize.stash.read(locale, field) if globalize.stash.contains?(locale, field)
end
end
define_method "#{field}_translations=" do |value|
value.each do |(locale, value)|
write_attribute(field, value, locale: locale)
end
end
end
define_method :serializable_hash_with_translations do |options = nil|
options ||= {}
except = Array.wrap(options[:except])
options[:except] = except +
self.class.translated_attribute_names.map(&:to_s)
methods = Array.wrap(options[:methods])
options[:methods] = methods +
self.class.translated_attribute_names.map { |name| "#{name}_translations" }
serializable_hash_without_translations options
end
alias_method_chain :serializable_hash, :translations
end
end
|