has_localization_table 0.0.2 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
@@ -42,7 +42,7 @@ module HasLocalizationTable
|
|
42
42
|
reject_empty_strings
|
43
43
|
end
|
44
44
|
|
45
|
-
# Add validation to ensure a string for the primary
|
45
|
+
# Add validation to ensure a string for the primary locale exists if the string is required
|
46
46
|
validate do
|
47
47
|
if self.class.localization_table_options[:required] || false
|
48
48
|
errors.add(association_name, :primary_lang_string_required) unless send(association_name).any? do |string|
|
@@ -66,15 +66,15 @@ module HasLocalizationTable
|
|
66
66
|
private :reject_empty_strings
|
67
67
|
|
68
68
|
# Find a record by multiple string values
|
69
|
-
define_singleton_method :find_by_localized_attributes do |attributes,
|
70
|
-
string_record = association.klass.where({ HasLocalizationTable.config.locale_foreign_key =>
|
69
|
+
define_singleton_method :find_by_localized_attributes do |attributes, locale = HasLocalizationTable.current_locale|
|
70
|
+
string_record = association.klass.where({ HasLocalizationTable.config.locale_foreign_key => locale.id }.merge(attributes)).first
|
71
71
|
string_record.send(klass.to_s.underscore.to_sym) rescue nil
|
72
72
|
end
|
73
73
|
private_class_method :find_by_localized_attributes
|
74
74
|
end
|
75
75
|
|
76
76
|
klass.localized_attributes.each do |attribute|
|
77
|
-
# Add validation to make all string fields required for the primary
|
77
|
+
# Add validation to make all string fields required for the primary locale
|
78
78
|
association.klass.class_eval do
|
79
79
|
validates attribute, presence: { message: :custom_this_field_is_required },
|
80
80
|
if: proc { |model| klass.name.constantize.localized_attribute_required?(attribute) && model.send(HasLocalizationTable.config.locale_foreign_key) == HasLocalizationTable.current_locale.id }
|
@@ -82,14 +82,14 @@ module HasLocalizationTable
|
|
82
82
|
|
83
83
|
# Set up accessors and ordering named_scopes for each non-FK attribute on the base model
|
84
84
|
klass.class_eval do
|
85
|
-
define_method attribute do |
|
86
|
-
# Try to load a string for the given
|
87
|
-
# If that fails, try for the primary
|
88
|
-
|
85
|
+
define_method attribute do |locale = HasLocalizationTable.current_locale|
|
86
|
+
# Try to load a string for the given locale
|
87
|
+
# If that fails, try for the primary locale
|
88
|
+
get_cached_localized_attribute(locale, association_name, attribute) || get_cached_localized_attribute(HasLocalizationTable.primary_locale, association_name, attribute)
|
89
89
|
end
|
90
90
|
|
91
91
|
define_method "#{attribute}=" do |value|
|
92
|
-
|
92
|
+
cache_localized_attribute(HasLocalizationTable.current_locale, association_name, attribute, value)
|
93
93
|
end
|
94
94
|
|
95
95
|
define_singleton_method "ordered_by_#{attribute}" do |direction = :asc|
|
@@ -157,29 +157,29 @@ module HasLocalizationTable
|
|
157
157
|
private
|
158
158
|
# Both strings and the associations are memoized, so that if an association adds more than one attribute to the main model, the association doesn't need
|
159
159
|
# to be loaded each time a different attribute is accessed.
|
160
|
-
def
|
160
|
+
def get_cached_localized_attribute(locale, association, attribute)
|
161
161
|
@_localized_attribute_cache ||= {}
|
162
162
|
@_localized_attribute_cache[attribute] ||= {}
|
163
163
|
|
164
164
|
@_localized_association_cache ||= {}
|
165
165
|
@_localized_association_cache[association] ||= {}
|
166
166
|
|
167
|
-
@_localized_attribute_cache[attribute][
|
168
|
-
@_localized_association_cache[association][
|
169
|
-
s = @_localized_association_cache[association][
|
167
|
+
@_localized_attribute_cache[attribute][locale.id] ||= begin
|
168
|
+
@_localized_association_cache[association][locale.id] ||= send(association).detect{ |a| a.send(HasLocalizationTable.config.locale_foreign_key) == locale.id }
|
169
|
+
s = @_localized_association_cache[association][locale.id].send(attribute) rescue nil
|
170
170
|
s.blank? ? nil : s
|
171
171
|
end
|
172
172
|
end
|
173
173
|
|
174
|
-
def
|
175
|
-
string = send(association).detect{ |a| a.send(HasLocalizationTable.config.locale_foreign_key) ==
|
174
|
+
def cache_localized_attribute(locale, association, attribute, value)
|
175
|
+
string = send(association).detect{ |a| a.send(HasLocalizationTable.config.locale_foreign_key) == locale.id } || send(association).build(HasLocalizationTable.config.locale_foreign_key => locale.id)
|
176
176
|
value = value.to_s
|
177
177
|
|
178
178
|
string.send(:"#{attribute}=", value)
|
179
179
|
|
180
180
|
@_localized_attribute_cache ||= {}
|
181
181
|
@_localized_attribute_cache[attribute] ||= {}
|
182
|
-
@_localized_attribute_cache[attribute][
|
182
|
+
@_localized_attribute_cache[attribute][locale.id] = value.blank? ? nil : value
|
183
183
|
end
|
184
184
|
end
|
185
185
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_localization_table
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-08-15 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &21033000 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 3.0.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *21033000
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: activerecord
|
27
|
-
requirement: &
|
27
|
+
requirement: &21032340 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 3.0.0
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *21032340
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: minitest
|
38
|
-
requirement: &
|
38
|
+
requirement: &21031960 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *21031960
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: sqlite3
|
49
|
-
requirement: &
|
49
|
+
requirement: &21031480 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *21031480
|
58
58
|
description: Automatically sets up usage of a relational table to contain user-created
|
59
59
|
multi-locale string attributes
|
60
60
|
email:
|