sutty/app/helpers/application_helper.rb

53 lines
1.1 KiB
Ruby
Raw Normal View History

2019-03-26 15:32:20 +00:00
# frozen_string_literal: true
2019-07-31 20:55:34 +00:00
# Helpers
2018-01-02 17:19:25 +00:00
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
2019-08-02 00:20:42 +00:00
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
2019-07-31 20:55:34 +00:00
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
2018-01-02 17:19:25 +00:00
end