traco 1.2.3 → 1.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/traco/translates.rb +53 -32
- data/lib/traco/version.rb +1 -1
- metadata +1 -1
data/lib/traco/translates.rb
CHANGED
@@ -1,40 +1,61 @@
|
|
1
1
|
module Traco
|
2
2
|
module Translates
|
3
|
-
TRACO_INSTANCE_METHODS_MODULE_NAME = "TracoInstanceMethods"
|
4
|
-
|
5
3
|
def translates(*attributes)
|
6
4
|
options = attributes.extract_options!
|
7
|
-
|
8
|
-
|
5
|
+
attributes = attributes.map(&:to_sym)
|
6
|
+
AttributeSetup.new(self).set_up(attributes, options)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
ActiveRecord::Base.send :extend, Traco::Translates
|
12
|
+
|
13
|
+
|
14
|
+
module Traco
|
15
|
+
class AttributeSetup
|
16
|
+
INSTANCE_METHODS_MODULE_NAME = "TracoInstanceMethods"
|
17
|
+
|
18
|
+
def initialize(klass)
|
19
|
+
@klass = klass
|
20
|
+
end
|
21
|
+
|
22
|
+
def set_up(attributes, options)
|
23
|
+
ensure_class_methods
|
24
|
+
ensure_attribute_list
|
25
|
+
ensure_instance_methods_module
|
26
|
+
add_attributes attributes, options
|
9
27
|
end
|
10
28
|
|
11
29
|
private
|
12
30
|
|
31
|
+
def ensure_class_methods
|
32
|
+
klass.extend Traco::ClassMethods
|
33
|
+
end
|
34
|
+
|
13
35
|
# Only called once per class or inheritance chain (e.g. once
|
14
36
|
# for the superclass, not at all for subclasses). The separation
|
15
37
|
# is important if we don't want to overwrite values if running
|
16
38
|
# multiple times in the same class or in different classes of
|
17
39
|
# an inheritance chain.
|
18
|
-
def
|
19
|
-
return if respond_to?(:translatable_attributes)
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
self.translatable_attributes = []
|
24
|
-
extend Traco::ClassMethods
|
40
|
+
def ensure_attribute_list
|
41
|
+
return if klass.respond_to?(:translatable_attributes)
|
42
|
+
klass.class_attribute :translatable_attributes
|
43
|
+
klass.translatable_attributes = []
|
25
44
|
end
|
26
45
|
|
27
|
-
def
|
28
|
-
fallback = options.fetch(:fallback, true)
|
29
|
-
|
30
|
-
self.translatable_attributes |= attributes
|
31
|
-
|
46
|
+
def ensure_instance_methods_module
|
32
47
|
# Instance methods are defined on an included module, so your class
|
33
48
|
# can just redefine them and call `super`, if you need to.
|
34
49
|
# http://thepugautomatic.com/2013/07/dsom/
|
35
|
-
unless const_defined?(
|
36
|
-
include const_set(
|
50
|
+
unless klass.const_defined?(INSTANCE_METHODS_MODULE_NAME, _search_ancestors = false)
|
51
|
+
klass.send :include, klass.const_set(INSTANCE_METHODS_MODULE_NAME, Module.new)
|
37
52
|
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def add_attributes(attributes, options)
|
56
|
+
fallback = options.fetch(:fallback, true)
|
57
|
+
|
58
|
+
klass.translatable_attributes |= attributes
|
38
59
|
|
39
60
|
attributes.each do |attribute|
|
40
61
|
define_localized_reader attribute, :fallback => fallback
|
@@ -45,27 +66,27 @@ module Traco
|
|
45
66
|
def define_localized_reader(attribute, options)
|
46
67
|
fallback = options[:fallback]
|
47
68
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
@localized_readers[attribute].value
|
53
|
-
end
|
69
|
+
custom_define_method(attribute) do
|
70
|
+
@localized_readers ||= {}
|
71
|
+
@localized_readers[attribute] ||= Traco::LocalizedReader.new(self, attribute, :fallback => fallback)
|
72
|
+
@localized_readers[attribute].value
|
54
73
|
end
|
55
74
|
end
|
56
75
|
|
57
76
|
def define_localized_writer(attribute)
|
58
|
-
|
59
|
-
|
60
|
-
send("#{attribute}_#{I18n.locale}=", value)
|
61
|
-
end
|
77
|
+
custom_define_method("#{attribute}=") do |value|
|
78
|
+
send("#{attribute}_#{I18n.locale}=", value)
|
62
79
|
end
|
63
80
|
end
|
64
81
|
|
65
|
-
def
|
66
|
-
const_get(
|
82
|
+
def custom_define_method(name, &block)
|
83
|
+
klass.const_get(INSTANCE_METHODS_MODULE_NAME).module_eval do
|
84
|
+
define_method(name, &block)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def klass
|
89
|
+
@klass
|
67
90
|
end
|
68
91
|
end
|
69
92
|
end
|
70
|
-
|
71
|
-
ActiveRecord::Base.send :extend, Traco::Translates
|
data/lib/traco/version.rb
CHANGED