nicoolas25
11/22/2013 - 10:51 AM

Add the :required_mark option to the labels. (Rails 4)

Add the :required_mark option to the labels. (Rails 4)

class ActionView::Helpers::FormBuilder
    alias :original_label :label

    # Add the :required_mark option to the label helper that add a mark before the content.
    def label(method, content_or_options = nil, options = nil, &block)
      if content_or_options && content_or_options.class == Hash
        options = content_or_options
      else
        content = content_or_options
      end

      # WARNING: This is the way we internationnalize thing in THIS project.
      content ||= I18n.t("activerecord.attributes.#{object_name}.#{method}", default: method.to_s.humanize)
      content = "* #{content}" if options && options.delete(:required_mark)

      original_label(method, content, options || {}, &block)
    end
end