Skip to content

Commit

Permalink
Allow sending multiple keys to message source as fallbacks/defaults
Browse files Browse the repository at this point in the history
Lookup ["field.#{field_name}", "label.#{field_name}", field_name, "text.#{field_name}"] for bootstrap form fields.
  • Loading branch information
UweKubosch committed Jun 23, 2023
1 parent 5db4b19 commit 6f3dc65
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group 'no.datek'
version '0.4.0'
version '0.5.0'
final String JRUBY_VERSION = '9.4.3.0';

repositories {
Expand Down
22 changes: 14 additions & 8 deletions src/main/resources/ruby/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module FormHelper
def checkbox(object, field_name, **opts)
hide_label = opts.delete(:hide_label) || opts.delete(:no_label)
label_key = opts.delete(:label_key) || "text.#{field_name}"
label_key = label_key_opt(opts, field_name)
label_style = opts.delete(:label_style)
label_class = opts.delete(:label_class)
label_suffix = opts.delete(:hide_label_suffix) ? nil : ':'
Expand Down Expand Up @@ -71,7 +71,7 @@ def bootstrap_checkbox(object, field_name, **opts)

def custom_checkbox(object, field_name, **opts)
hide_label = opts.delete(:hide_label) || opts.delete(:no_label)
label_key = opts.delete(:label_key) || "text.#{field_name}"
label_key = label_key_opt(opts, field_name)
label_style = opts.delete(:label_style)
label_class = opts.delete(:label_class)
label = opts.delete(:label) || "#{message[label_key]}"
Expand Down Expand Up @@ -168,7 +168,7 @@ def text_input(object, field_name, **opts)
def bootstrap_text_input(object, field_name, **opts)
classes = field_classes(object, field_name, opts.delete(:class))
hide_label = opts.delete(:hide_label) || opts.delete(:no_label)
label_key = opts.delete(:label_key) || "text.#{field_name}"
label_key = label_key_opt(opts, field_name)
hide_label_suffix = opts.key?(:hide_label_suffix) ? opts.delete(:hide_label_suffix) : true
label_suffix = hide_label_suffix ? nil : ':'
label = opts.delete(:label) || "#{message[label_key]}#{label_suffix}"
Expand Down Expand Up @@ -209,7 +209,7 @@ def hidden_input(object, field_name, **opts)
def textarea(object, field_name, **opts)
hide_label = opts.delete(:hide_label) || opts.delete(:no_label)
label_style = opts.delete(:label_style)
label_key = opts.delete(:label_key) || "text.#{field_name}"
label_key = label_key_opt(opts, field_name)
label_suffix = opts.delete(:hide_label_suffix) ? nil : ':'
label = opts.delete(:label) || "#{message[label_key]}#{label_suffix}"
id_name = opts.delete(:id) || field_name
Expand Down Expand Up @@ -316,7 +316,7 @@ def bootstrap_select_input(object, field_name, option_map = [], **opts, &block)
hide_label = opts.delete(:hide_label) || opts.delete(:no_label)
label_style = opts.delete(:label_style)
label_class = opts.delete(:label_class) || 'form-label'
label_key = opts.delete(:label_key) || "text.#{field_name}"
label_key = label_key_opt(opts, field_name)
hide_label_suffix = opts.key?(:hide_label_suffix) ? opts.delete(:hide_label_suffix) : true
label_suffix = hide_label_suffix ? nil : ':'
label = opts.delete(:label) || "#{message[label_key]}#{label_suffix}"
Expand All @@ -340,7 +340,7 @@ def datetime_input(object, field_name, **opts)
label_class = opts.delete(:label_class) || 'form-label'
label_style = opts.delete(:label_style)
input_group_style = opts.delete(:input_group_style)
label_key = opts.delete(:label_key) || "text.#{field_name}"
label_key = label_key_opt(opts, field_name)
id_name = opts.delete(:id) || field_name
classes = opts.delete(:class)
wrapper_class = opts.key?(:wrapper_class) ? opts.delete(:wrapper_class) : 'mb-3'
Expand Down Expand Up @@ -382,7 +382,7 @@ def bootstrap_date_input(object, field_name, **opts)
label_class = opts.delete(:label_class) || 'form-label'
label_style = opts.delete(:label_style)
input_group_style = opts.delete(:input_group_style)
label_key = opts.delete(:label_key) || "text.#{field_name}"
label_key = label_key_opt(opts, field_name)
id_name = opts.delete(:id) || field_name
classes = opts.delete(:class)
max_date = opts.delete(:max_date)
Expand Down Expand Up @@ -456,7 +456,7 @@ def bootstrap_file_field(object, field_name, **opts)
hide_label = opts.delete(:hide_label) || opts.delete(:no_label)
label_class = opts.delete(:label_class) || 'form-label'
label_style = opts.delete(:label_style)
label_key = opts.delete(:label_key) || "text.#{field_name}"
label_key = label_key_opt(opts, field_name)
wrapper_class = opts.key?(:wrapper_class) ? opts.delete(:wrapper_class) : 'mb-3'

if hide_label
Expand Down Expand Up @@ -486,4 +486,10 @@ def escape_javascript(javascript)
javascript.gsub(/(\\|<\/|\r\n|\342\200\250|\342\200\251|[\n\r"']|[`]|[$])/u, JS_ESCAPE_MAP)
end
end

private

def label_key_opt(opts, field_name)
opts.delete(:label_key) || ["field.#{field_name}", "label.#{field_name}", field_name, "text.#{field_name}"]
end
end
8 changes: 6 additions & 2 deletions src/main/resources/ruby/message_source_accessor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ def initialize(message_source, locale)
@locale = locale
end

def [](key, *args)
@message_source.get_message(key, args.to_java, "???#{key}???", @locale)
def [](keys, *args)
[*keys].each do |key|
m = @message_source.get_message(key.to_s, args.to_java, nil, @locale)
return m if m
end
"???#{keys}???"
end
end

0 comments on commit 6f3dc65

Please sign in to comment.