mirror of
https://github.com/heartcombo/simple_form.git
synced 2022-11-09 12:19:26 -05:00
71 lines
2.1 KiB
Ruby
71 lines
2.1 KiB
Ruby
module SimpleForm
|
|
module Components
|
|
module Labels
|
|
extend ActiveSupport::Concern
|
|
|
|
module ClassMethods #:nodoc:
|
|
def translate_required_html
|
|
i18n_cache :translate_required_html do
|
|
I18n.t(:"simple_form.required.html", :default =>
|
|
%[<abbr title="#{translate_required_text}">#{translate_required_mark}</abbr>]
|
|
)
|
|
end
|
|
end
|
|
|
|
def translate_required_text
|
|
I18n.t(:"simple_form.required.text", :default => 'required')
|
|
end
|
|
|
|
def translate_required_mark
|
|
I18n.t(:"simple_form.required.mark", :default => '*')
|
|
end
|
|
end
|
|
|
|
def label
|
|
if input_type_should_generate_for_attribute?
|
|
@builder.label(label_target, label_text, label_html_options)
|
|
else
|
|
template.label_tag(nil, label_text, label_html_options)
|
|
end
|
|
end
|
|
|
|
def label_text
|
|
SimpleForm.label_text.call(raw_label_text, required_label_text).html_safe
|
|
end
|
|
|
|
def label_target
|
|
attribute_name
|
|
end
|
|
|
|
def label_html_options
|
|
label_options = html_options_for(:label, [input_type, required_class, SimpleForm.label_class])
|
|
label_options[:for] = options[:input_html][:id] if options.key?(:input_html) && options[:input_html].key?(:id)
|
|
label_options
|
|
end
|
|
|
|
protected
|
|
|
|
def raw_label_text #:nodoc:
|
|
options[:label] || label_translation
|
|
end
|
|
|
|
# Default required text when attribute is required.
|
|
def required_label_text #:nodoc:
|
|
required_field? ? self.class.translate_required_html.dup : ''
|
|
end
|
|
|
|
# First check labels translation and then human attribute name.
|
|
def label_translation #:nodoc:
|
|
(SimpleForm.translate_labels && translate(:labels)) || if object.class.respond_to?(:human_attribute_name)
|
|
object.class.human_attribute_name(reflection_or_attribute_name.to_s)
|
|
else
|
|
attribute_name.to_s.humanize
|
|
end
|
|
end
|
|
|
|
def input_type_should_generate_for_attribute?
|
|
[:check_boxes, :radio].exclude?(input_type)
|
|
end
|
|
end
|
|
end
|
|
end
|