Skip to content

Commit 79f3b96

Browse files
committed
Change to disallow not available languages
1 parent 5880f25 commit 79f3b96

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

app/models/custom_message_setting.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class CustomMessageSetting < Setting
2-
validate :add_errors, :convertible_to_yaml, :custom_message_keys_are_available
2+
validate :add_errors, :convertible_to_yaml,
3+
:custom_message_languages_are_available, :custom_message_keys_are_available
34

45
def self.find_or_default
56
super('plugin_redmine_message_customize')
@@ -122,6 +123,18 @@ def custom_message_keys_are_available
122123
end
123124
end
124125

126+
def custom_message_languages_are_available
127+
return false if self.value[:custom_messages].is_a?(Hash) == false || self.errors.present?
128+
unavailable_languages =
129+
custom_messages.keys.compact.reject do |language|
130+
I18n.available_locales.include?(language.to_sym)
131+
end
132+
if unavailable_languages.present?
133+
self.errors.add(:base, l(:error_unavailable_languages) + " [#{unavailable_languages.join(', ')}]")
134+
false
135+
end
136+
end
137+
125138
def convertible_to_yaml
126139
YAML.dump(self.value[:custom_messages])
127140
end

config/locales/en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ en:
77
label_home: 'Home2'
88
text_placeholder_choose_key: Please select the message you want to change.
99
error_unavailable_keys: The keys not present in Redmine are used.
10+
error_unavailable_languages: The languages not present in Redmine are used.
1011
error_invalid_yaml_format: The format of yaml is invalid.
1112
label_normal_tab: Normal mode
1213
label_yaml_tab: YAML mode

config/locales/ja.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ ja:
66
ja:
77
label_home: 'ホーム2'
88
text_placeholder_choose_key: 変更したい文言を選択してください。
9-
error_unavailable_keys: Redmineに存在しないキーが利用されています
9+
error_unavailable_keys: Redmineに存在しないキーが利用されています。
10+
error_unavailable_languages: Redmineで利用できない言語が利用されています。
1011
error_invalid_yaml_format: YAMLの形式が正しくありません。
1112
label_normal_tab: 通常モード
1213
label_yaml_tab: YAMLモード

test/unit/custom_message_setting_test.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ def test_validate_with_not_available_keys_should_return_false
1414
assert_equal "#{l(:error_unavailable_keys)} keys: [foobar]", @custom_message_setting.errors[:base].first
1515
end
1616

17+
def test_validate_with_not_available_languages_should_return_false
18+
@custom_message_setting.value = { custom_messages: { 'foo' => {'label_home' => 'Home' }} }
19+
assert_not @custom_message_setting.save
20+
assert_equal "#{l(:error_unavailable_languages)} [foo]", @custom_message_setting.errors[:base].first
21+
end
22+
1723
def test_find_or_default
1824
assert_equal @custom_message_setting, CustomMessageSetting.find_or_default
1925
end
@@ -34,7 +40,7 @@ def test_custom_messages_to_yaml
3440
assert_equal 'test', @custom_message_setting.custom_messages_to_yaml
3541
end
3642

37-
def test_update_with_custom_messages_if_custom_messages_is_present
43+
def test_update_with_custom_messages_if_custom_messages_is_exist
3844
flatten_hash = {'label_home' => 'Home3', 'time.am' => 'foo'}
3945

4046
assert @custom_message_setting.update_with_custom_messages(flatten_hash, 'en')

0 commit comments

Comments
 (0)