Created
January 24, 2014 20:56
-
-
Save paulca/8606241 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def display_date_or_range(event, sup = true, html = true, short = false) | |
return if event.start_date.blank? and event.end_date.blank? | |
if event.single_day? | |
if short | |
out = event.start_date.strftime("%b") | |
else | |
out = event.start_date.strftime("%B") | |
end | |
out << " " | |
if sup === false | |
out << event.start_date.day.ordinalize | |
else | |
out << ordinalize_with_sup(event.start_date) | |
end | |
out << ", #{event.start_date.year}" | |
return out.html_safe | |
end | |
if event.multi_day? | |
out = '' | |
if short | |
out << event.start_date.strftime("%b ") | |
else | |
out << event.start_date.strftime("%B ") | |
end | |
if sup === false | |
out << event.start_date.day.ordinalize | |
else | |
out << ordinalize_with_sup(event.start_date) | |
end | |
if event.spans_different_years? | |
out << ", #{event.start_date.year}" | |
end | |
if html | |
out << '–' | |
else | |
out << HTMLEntities.new.decode('–') | |
end | |
if event.spans_different_months? or (!event.spans_different_months? && event.spans_different_years?) | |
out << event.end_date.strftime("%B ") | |
end | |
if sup === false | |
out << event.end_date.day.ordinalize | |
else | |
out << ordinalize_with_sup(event.end_date) | |
end | |
out << ", #{event.end_date.year}" | |
out.html_safe | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DateOrRange | |
attr_accessor :start_date, :end_date, :options | |
def initialize(options = {}) | |
@start_date = options.delete(:start_date) | |
@end_date = options.delete(:end_date) | |
@end_date = nil if @start_date == @end_date | |
@options = options | |
end | |
def single_day? | |
start_date.present? && end_date.blank? | |
end | |
def start_date_month | |
return if start_date.blank? | |
start_date.strftime("%B") | |
end | |
def start_date_month_word | |
start_date_month | |
end | |
def start_date_number | |
start_date.day.ordinalize | |
end | |
def start_date_year | |
start_date.year | |
end | |
def start_date_year_word | |
start_date_year if end_date.blank? || (start_date_year != end_date_year) | |
end | |
def start_date_part | |
return if start_date.blank? | |
[ | |
start_date_month_word, | |
"#{start_date_number}#{',' if start_date_year != end_date_year}", | |
(start_date_year_word) | |
].reject(&:nil?).join(' ') | |
end | |
def end_date_month | |
end_date.strftime("%B") | |
end | |
def end_date_month_word | |
end_date_month if (end_date_month != start_date_month) || (start_date_year != end_date_year) | |
end | |
def end_date_number | |
end_date.day.ordinalize | |
end | |
def end_date_year | |
end_date.year if end_date.present? | |
end | |
def end_date_part | |
return if end_date.blank? | |
[ | |
end_date_month_word, | |
"#{end_date_number},", | |
end_date_year | |
].reject(&:nil?).join(' ') | |
end | |
def to_s | |
[ | |
start_date_part, | |
end_date_part | |
].reject(&:nil?).join('–') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I like
reject(&:blank?)
because it also copes with empty strings.whereas:
I've sometimes done it like this:
That doesn't work as well with more than two words where one in the middle is of zero length (you get a double space).
To my shame, I've also used the very inelegant
.select {|w| !w.blank? }
so I'm glad to have been reminded aboutreject
.See also my gist about titleizing names, which could incorporate some of these ideas too.