traco 1.2.3 → 1.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
- set_up_once
8
- store_as_translatable_attributes attributes.map(&:to_sym), options
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 set_up_once
19
- return if respond_to?(:translatable_attributes)
20
-
21
- class_attribute :translatable_attributes
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 store_as_translatable_attributes(attributes, options)
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?(TRACO_INSTANCE_METHODS_MODULE_NAME, _search_ancestors = false)
36
- include const_set(TRACO_INSTANCE_METHODS_MODULE_NAME, Module.new)
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
- traco_instance_methods_module.module_eval do
49
- define_method(attribute) do
50
- @localized_readers ||= {}
51
- @localized_readers[attribute] ||= Traco::LocalizedReader.new(self, attribute, :fallback => fallback)
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
- traco_instance_methods_module.module_eval do
59
- define_method("#{attribute}=") do |value|
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 traco_instance_methods_module
66
- const_get(TRACO_INSTANCE_METHODS_MODULE_NAME)
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
@@ -1,3 +1,3 @@
1
1
  module Traco
2
- VERSION = "1.2.3"
2
+ VERSION = "1.2.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: traco
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 1.2.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: