# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
# Display a start/end datetime span in human readable form. If
# both are given, ' to ' is placed in the middle of the string.
#
# +start_datetime+ is a Datetime instance,
# +end_datetime+ is optional.
def datetime_span(start_datetime, end_datetime=nil)
str = start_datetime.strftime('%Y-%m-%d@%H:%M')
if end_datetime
str += ' to ' + end_datetime.strftime('%Y-%m-%d@%H:%M')
end
str
end
# Display a default message for empty fields.
# +field_value+ is the value to process.
def d(field_value=nil)
if field_value.blank?
return content_tag('em', 'not specified')
else
return field_value
end
end
# Display date in human-readable format, e.g. "8th January 1968".
#
# Returns +nil+ if +date_to_format+ is blank.
def human_date(date_to_format)
if date_to_format.blank?
out = nil
else
# Get the day part of the date with
# the "ordinal suffix" (th, rd, nd) appended
day = date_to_format.day.ordinalize
# strftime accepts a formatting string, which specifies
# which parts of the date to include in the output string
out = date_to_format.strftime("#{day} %B %Y")
end
out
end
# Display +field_value+ followed by a element,
# but only if +field_value+ is set; otherwise returns nil.
def field_with_break(field_value)
unless field_value.blank?
return field_value + tag('br')
else
return nil
end
end
# ... other helpers ...
# Format a label element for a form field.
#
# +options+ can include:
#
# [:required] If +true+, an asterisk is added to the label.
# [:field_name] If true, the for attribute on the label
# is set from +model+ + +field_name+;
# otherwise, for attribute is set from
# +model+ + lowercased and underscored +label_text+.
#
# Example call:
# label(:person, 'Email')
#
# Example output:
# :
def label(model, label_text, options={})
# Use the field_name option if supplied
field_name = options[:field_name]
field_name ||= label_text.gsub(' ', '_')
# The value for the for attribute.
label_for = (model.to_s + '_' + field_name).downcase
# The