1
1
class CustomMessageSetting < Setting
2
- before_validation :change_format_to_hash
3
- validate :custom_message_keys_are_available
2
+ validate :add_errors , :convertible_to_yaml , :custom_message_keys_are_available
4
3
5
4
def self . find_or_default
6
5
super ( 'plugin_redmine_message_customize' )
@@ -28,20 +27,43 @@ def custom_messages_to_yaml
28
27
end
29
28
end
30
29
31
- def update_custom_messages ( messages )
30
+ def update_with_custom_messages ( custom_messages , lang )
31
+ value = CustomMessageSetting . nested_hash ( custom_messages )
32
+ original_custom_messages = self . custom_messages
33
+ messages =
34
+ if value . present?
35
+ original_custom_messages . merge ( { lang => value } )
36
+ else
37
+ original_custom_messages . delete ( lang )
38
+ original_custom_messages
39
+ end
40
+
32
41
self . value = { custom_messages : ( messages . present? ? messages : { } ) }
33
42
self . save
34
43
end
35
44
45
+ def update_with_custom_messages_yaml ( yaml )
46
+ begin
47
+ messages = YAML . load ( yaml )
48
+ @errs = { base : l ( :error_invalid_yaml_format ) } if messages . is_a? ( Hash ) == false && messages . present?
49
+ self . value = { custom_messages : ( messages . present? ? messages : { } ) }
50
+ rescue => e
51
+ @errs = { base : e . message }
52
+ self . value = { custom_messages : yaml }
53
+ end
54
+ self . save
55
+ end
56
+
36
57
def self . available_messages ( lang = 'en' )
37
- messages = I18n . backend . translations [ self . find_language ( lang ) . to_sym ] || { }
58
+ messages = I18n . backend . translations [ self . find_language ( lang ) . to_sym ]
38
59
if messages . nil?
39
60
CustomMessageSetting . reload_translations! ( [ lang ] )
40
61
messages = I18n . backend . translations [ lang . to_sym ] || { }
41
62
end
42
63
self . flatten_hash ( messages )
43
64
end
44
65
66
+ # { date: { formats: { defaults: '%m/%d/%Y'}}} to {'date.formats.defaults' => '%m/%d/%Y'}
45
67
def self . flatten_hash ( hash = nil )
46
68
hash = self . to_hash unless hash
47
69
hash . each_with_object ( { } ) do |( key , value ) , content |
@@ -52,8 +74,8 @@ def self.flatten_hash(hash=nil)
52
74
end
53
75
end
54
76
77
+ # {'date.formats.defaults' => '%m/%d/%Y'} to { date: { formats: { defaults: '%m/%d/%Y'}}}
55
78
def self . nested_hash ( hash = nil )
56
- hash = self . to_hash unless hash
57
79
new_hash = { }
58
80
hash . each do |key , value |
59
81
h = value
@@ -87,27 +109,27 @@ def custom_message_keys_are_available
87
109
return false if self . value [ :custom_messages ] . is_a? ( Hash ) == false || self . errors . present?
88
110
89
111
custom_messages_hash = { }
90
- custom_messages . values . each do |hash |
91
- custom_messages_hash = self . class . flatten_hash ( custom_messages_hash . merge ( hash ) )
112
+ custom_messages . values . compact . each do |val |
113
+ custom_messages_hash = self . class . flatten_hash ( custom_messages_hash . merge ( val ) ) if val . is_a? ( Hash )
92
114
end
93
115
available_keys = self . class . flatten_hash ( self . class . available_messages ) . keys
94
116
unavailable_keys = custom_messages_hash . keys . reject { |k |available_keys . include? ( k . to_sym ) }
95
117
if unavailable_keys . present?
96
118
self . errors . add ( :base , l ( :error_unavailable_keys ) + "keys: [#{ unavailable_keys . join ( ', ' ) } ]" )
97
- return false
119
+ false
98
120
end
99
121
end
100
122
101
- def change_format_to_hash
102
- if self . value [ :custom_messages ] . is_a? ( Hash )
103
- YAML . dump ( self . value [ :custom_messages ] )
104
- else
105
- custom_messages = YAML . load ( self . value [ :custom_messages ] )
106
- raise l ( :error_invalid_yaml_format ) unless custom_messages . is_a? ( Hash )
107
- self . value = { custom_messages : custom_messages }
123
+ def convertible_to_yaml
124
+ YAML . dump ( self . value [ :custom_messages ] )
125
+ end
126
+
127
+ def add_errors
128
+ unless @errs . blank?
129
+ @errs . each do |key , value |
130
+ self . errors . add ( key , value )
131
+ end
132
+ false
108
133
end
109
- rescue => e
110
- self . errors . add ( :base , e . message )
111
- false
112
134
end
113
135
end
0 commit comments