2010-01-09 15:05:02 +00:00
|
|
|
module SimpleForm
|
|
|
|
module Components
|
|
|
|
module Labels
|
2011-09-03 12:40:47 +00:00
|
|
|
extend ActiveSupport::Concern
|
2010-01-09 15:05:02 +00:00
|
|
|
|
|
|
|
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
|
2012-01-26 21:30:45 +00:00
|
|
|
if generate_label_for_attribute?
|
2011-12-03 15:02:31 +00:00
|
|
|
@builder.label(label_target, label_text, label_html_options)
|
|
|
|
else
|
|
|
|
template.label_tag(nil, label_text, label_html_options)
|
|
|
|
end
|
2010-01-09 15:05:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def label_text
|
2012-03-03 17:36:02 +00:00
|
|
|
SimpleForm.label_text.call(raw_label_text, required_label_text).strip.html_safe
|
2010-01-09 15:05:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def label_target
|
|
|
|
attribute_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def label_html_options
|
2012-02-17 00:36:34 +00:00
|
|
|
label_html_classes = SimpleForm.additional_classes_for(:label) {
|
|
|
|
[input_type, required_class, SimpleForm.label_class].compact
|
|
|
|
}
|
2012-02-16 17:42:47 +00:00
|
|
|
|
2012-02-16 17:53:09 +00:00
|
|
|
label_options = html_options_for(:label, label_html_classes)
|
2012-02-17 00:36:34 +00:00
|
|
|
if options.key?(:input_html) && options[:input_html].key?(:id)
|
|
|
|
label_options[:for] = options[:input_html][:id]
|
|
|
|
end
|
2010-01-09 15:05:02 +00:00
|
|
|
label_options
|
|
|
|
end
|
|
|
|
|
2012-01-26 19:22:05 +00:00
|
|
|
protected
|
2010-01-09 15:05:02 +00:00
|
|
|
|
|
|
|
def raw_label_text #:nodoc:
|
|
|
|
options[:label] || label_translation
|
|
|
|
end
|
|
|
|
|
|
|
|
# Default required text when attribute is required.
|
|
|
|
def required_label_text #:nodoc:
|
2011-12-04 12:00:39 +00:00
|
|
|
required_field? ? self.class.translate_required_html.dup : ''
|
2010-01-09 15:05:02 +00:00
|
|
|
end
|
|
|
|
|
2010-12-08 19:30:53 +00:00
|
|
|
# First check labels translation and then human attribute name.
|
2010-01-09 15:05:02 +00:00
|
|
|
def label_translation #:nodoc:
|
2011-12-10 18:00:36 +00:00
|
|
|
if SimpleForm.translate_labels && (translated_label = translate(:labels))
|
|
|
|
translated_label
|
|
|
|
elsif object.class.respond_to?(:human_attribute_name)
|
2010-01-09 15:05:02 +00:00
|
|
|
object.class.human_attribute_name(reflection_or_attribute_name.to_s)
|
|
|
|
else
|
|
|
|
attribute_name.to_s.humanize
|
|
|
|
end
|
|
|
|
end
|
2011-12-03 15:02:31 +00:00
|
|
|
|
2012-01-26 21:30:45 +00:00
|
|
|
def generate_label_for_attribute?
|
|
|
|
true
|
2011-12-03 15:02:31 +00:00
|
|
|
end
|
2010-01-09 15:05:02 +00:00
|
|
|
end
|
|
|
|
end
|
2010-12-08 19:30:53 +00:00
|
|
|
end
|