Module: SingleTableGlobalize3::ActiveRecord::InstanceMethods
- Includes:
- AttributeReadWrite
- Defined in:
- lib/single_table_globalize3/active_record/instance_methods.rb
Class Method Summary
collapse
Instance Method Summary
collapse
#attribute_names, #attributes, #deprecated_options, #read_attribute, #translated?, #translated_attributes, #untranslated_attributes, #write_attribute
Class Method Details
.included(base) ⇒ Object
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/single_table_globalize3/active_record/instance_methods.rb', line 10
def self.included(base)
if base.method_defined?(:assign_attributes)
base.class_eval %{
def assign_attributes(attributes, options = {})
with_given_locale(attributes) { super }
end
}
else
base.class_eval %{
def attributes=(attributes, *args)
with_given_locale(attributes) { super }
end
def update_attributes!(attributes, *args)
with_given_locale(attributes) { super }
end
def update_attributes(attributes, *args)
with_given_locale(attributes) { super }
end
}
end
end
|
Instance Method Details
#clone ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/single_table_globalize3/active_record/instance_methods.rb', line 54
def clone
obj = super
return obj unless respond_to?(:translated_attribute_names)
obj.instance_variable_set(:@translations, nil) if new_record? obj.instance_variable_set(:@globalize, nil )
each_locale_and_translated_attribute do |locale, name|
obj.globalize.write(locale, name, globalize.fetch(locale, name) )
end
return obj
end
|
#dup ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/single_table_globalize3/active_record/instance_methods.rb', line 67
def dup
obj = super
obj.instance_variable_set(:@translations, nil)
obj.instance_variable_set(:@globalize, nil )
each_locale_and_translated_attribute do |locale, name|
obj.globalize.write(locale, name, globalize.fetch(locale, name) )
end
return obj
end
|
#globalize ⇒ Object
6
7
8
|
# File 'lib/single_table_globalize3/active_record/instance_methods.rb', line 6
def globalize
@globalize ||= Adapter.new(self)
end
|
#globalize_fallbacks(locale) ⇒ Object
108
109
110
|
# File 'lib/single_table_globalize3/active_record/instance_methods.rb', line 108
def globalize_fallbacks(locale)
SingleTableGlobalize3.fallbacks(locale)
end
|
#reload(options = nil) ⇒ Object
47
48
49
50
51
52
|
# File 'lib/single_table_globalize3/active_record/instance_methods.rb', line 47
def reload(options = nil)
translation_caches.clear
translated_attribute_names.each { |name| @attributes.delete(name.to_s) }
globalize.reset
super(options)
end
|
#rollback ⇒ Object
79
80
81
82
|
# File 'lib/single_table_globalize3/active_record/instance_methods.rb', line 79
def rollback
instance_variable_set('@globalize', previous_version.try(:globalize))
self.version = versions.for_this_locale.last
end
|
#set_translations(options) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/single_table_globalize3/active_record/instance_methods.rb', line 35
def set_translations(options)
options.keys.each do |locale|
options[locale].each do |key, value|
raise NoMethodError.new("unknown attribute: #{key}") unless attribute_names.flatten.include?(key.to_s)
translation = translation_for(locale, key.to_s)
translation.value = value
translation.save!
end
end
globalize.reset
end
|
#translation ⇒ Object
84
85
86
|
# File 'lib/single_table_globalize3/active_record/instance_methods.rb', line 84
def translation
translations_for_locale(::SingleTableGlobalize3.locale)
end
|
#translation_caches ⇒ Object
104
105
106
|
# File 'lib/single_table_globalize3/active_record/instance_methods.rb', line 104
def translation_caches
@translation_caches ||= HashWithIndifferentAccess.new
end
|
#translation_for(locale, name, build_if_missing = true) ⇒ Object
88
89
90
91
92
|
# File 'lib/single_table_globalize3/active_record/instance_methods.rb', line 88
def translation_for(locale, name, build_if_missing = true)
translation_caches[locale] ||= HashWithIndifferentAccess.new
translation_caches[locale][name] ||= (translations.detect{|t| t.locale == locale && t.attribute_name == name.to_s}) ||
(translations.build(:locale => locale, :attribute_name => name) if build_if_missing)
end
|
#translations_for_locale(locale) ⇒ Object
94
95
96
97
98
99
100
101
102
|
# File 'lib/single_table_globalize3/active_record/instance_methods.rb', line 94
def translations_for_locale(locale)
translation_caches[locale] = if translations.loaded?
translations.select{|t| t.locale.to_s == locale.to_s }
else
translations.with_locale(locale)
end.inject(HashWithIndifferentAccess.new) do |hash, t|
hash.update(t.attribute_name => t)
end
end
|