mirror of
https://github.com/internetee/registry.git
synced 2025-05-17 01:47:18 +02:00
30 lines
1.1 KiB
Ruby
30 lines
1.1 KiB
Ruby
class ActionView::Helpers::FormBuilder
|
|
alias_method :orig_label, :label
|
|
|
|
# add a 'required' CSS class to the field label if the field is required
|
|
# rubocop:disable Metrics/CyclomaticComplexity
|
|
# rubocop:disable Metrics/PerceivedComplexity
|
|
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
|
|
|
|
# rubocop:disable Style/MultilineOperationIndentation
|
|
if object.class.respond_to?(:validators_on) &&
|
|
object.class.validators_on(method).map(&:class).include?(ActiveRecord::Validations::PresenceValidator)
|
|
|
|
if options.class != Hash
|
|
options = { class: 'required' }
|
|
else
|
|
options[:class] = ((options[:class] || "") + ' required').split(' ').uniq.join(' ')
|
|
end
|
|
end
|
|
# rubocop:enable Style/MultilineOperationIndentation
|
|
|
|
orig_label(method, content, options || {}, &block)
|
|
end
|
|
# rubocop:enable Metrics/PerceivedComplexity
|
|
# rubocop:enable Metrics/CyclomaticComplexity
|
|
end
|