Skip to content

Instantly share code, notes, and snippets.

@nicolasblanco
Created August 26, 2010 12:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicolasblanco/551333 to your computer and use it in GitHub Desktop.
Save nicolasblanco/551333 to your computer and use it in GitHub Desktop.
module FormBuilderHelpers
class Html5
def self.options_from(object, attribute)
return {} unless object.class.respond_to?(:validators_on)
{}.tap do |options|
options[:required] = true if object.class.validators_on(attribute).any? { |validator| validator.is_a?(ActiveModel::Validations::PresenceValidator) }
if validator = object.class.validators_on(attribute).select { |validator| validator.is_a?(ActiveModel::Validations::InclusionValidator) && validator.options[:in].is_a?(Range) }.last
options[:min] = validator.options[:in].min
options[:max] = validator.options[:in].max
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment