mirror of
https://0xacab.org/sutty/sutty
synced 2025-02-22 13:51:46 +00:00
52 lines
1.1 KiB
Ruby
52 lines
1.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
# Helpers
|
|
module ApplicationHelper
|
|
# Devuelve el atributo name de un campo posiblemente anidado
|
|
def field_name_for_post(names)
|
|
return ['post', names] if names.is_a? String
|
|
|
|
names = names.dup
|
|
root = 'post'
|
|
name = names.pop
|
|
names.each do |n|
|
|
root = "#{root}[#{n}]"
|
|
end
|
|
|
|
[root, name]
|
|
end
|
|
|
|
def field_name_for_post_as_string(names)
|
|
f = field_name_for_post(names)
|
|
|
|
"#{f.first}[#{f.last}]"
|
|
end
|
|
|
|
def distance_of_time_in_words_if_more_than_a_minute(seconds)
|
|
if seconds > 60
|
|
distance_of_time_in_words seconds
|
|
else
|
|
I18n.t('seconds', seconds: seconds)
|
|
end
|
|
end
|
|
|
|
def sanitize_markdown(text, options = {})
|
|
sanitize(CommonMarker.render_html(text), options)
|
|
end
|
|
|
|
def invalid?(model, field)
|
|
model.errors.messages[field].present?
|
|
end
|
|
|
|
def form_control(model, field)
|
|
if invalid? model, field
|
|
'form-control is-invalid'
|
|
else
|
|
'form-control'
|
|
end
|
|
end
|
|
|
|
def form_class(model)
|
|
model.errors.messages.empty? ? 'needs-validation' : 'was-validated'
|
|
end
|
|
end
|