Skip to content

Instantly share code, notes, and snippets.

@tordans
Created October 24, 2011 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tordans/1309595 to your computer and use it in GitHub Desktop.
Save tordans/1309595 to your computer and use it in GitHub Desktop.
Helpermethods for relative DateTime Stuff in StayScout
# Ausgabe: "Vor mehr als 5 Monaten"/"Vor etwa einem Jahr" — statt "Dauer: mehr als 5 Monate"/"Dauer: etwa 1 Jahr", wie es die Originalfunktion liefert
# Original-File: actionpack/lib/action_view/helpers/date_helper.rb, line 63
# Dokumenation: http://apidock.com/rails/v2.3.8/ActionView/Helpers/DateHelper/distance_of_time_in_words
# Changelog: I18n.with_options-Scope geändert auf "distance_in_words_gebeugt"
def distance_of_time_in_words_gebeugt(from_time, to_time = 0, include_seconds = false, options = {})
from_time = from_time.to_time if from_time.respond_to?(:to_time)
to_time = to_time.to_time if to_time.respond_to?(:to_time)
distance_in_minutes = (((to_time - from_time).abs)/60).round
distance_in_seconds = ((to_time - from_time).abs).round
I18n.with_options :locale => options[:locale], :scope => 'datetime.distance_in_words_gebeugt' do |locale|
case distance_in_minutes
when 0..1
return distance_in_minutes == 0 ?
locale.t(:less_than_x_minutes, :count => 1) :
locale.t(:x_minutes, :count => distance_in_minutes) unless include_seconds
case distance_in_seconds
when 0..4 then locale.t :less_than_x_seconds, :count => 5
when 5..9 then locale.t :less_than_x_seconds, :count => 10
when 10..19 then locale.t :less_than_x_seconds, :count => 20
when 20..39 then locale.t :half_a_minute
when 40..59 then locale.t :less_than_x_minutes, :count => 1
else locale.t :x_minutes, :count => 1
end
when 2..44 then locale.t :x_minutes, :count => distance_in_minutes
when 45..89 then locale.t :about_x_hours, :count => 1
when 90..1439 then locale.t :about_x_hours, :count => (distance_in_minutes.to_f / 60.0).round
when 1440..2529 then locale.t :x_days, :count => 1
when 2530..43199 then locale.t :x_days, :count => (distance_in_minutes.to_f / 1440.0).round
when 43200..86399 then locale.t :about_x_months, :count => 1
when 86400..525599 then locale.t :x_months, :count => (distance_in_minutes.to_f / 43200.0).round
else
distance_in_years = distance_in_minutes / 525600
minute_offset_for_leap_year = (distance_in_years / 4) * 1440
remainder = ((distance_in_minutes - minute_offset_for_leap_year) % 525600)
if remainder < 131400
locale.t(:about_x_years, :count => distance_in_years)
elsif remainder < 394200
locale.t(:over_x_years, :count => distance_in_years)
else
locale.t(:almost_x_years, :count => distance_in_years + 1)
end
end
end
end
# File actionpack/lib/action_view/helpers/date_helper.rb, line 115
def time_ago_in_words_gebeugt(from_time, include_seconds = false)
distance_of_time_in_words_gebeugt(from_time, Time.now, include_seconds)
end
alias distance_of_time_in_words_to_now_gebeugt time_ago_in_words_gebeugt
def distance_of_time_in_words_with_abbr(from_time, to_time = 0, include_seconds = false, options = {})
title_attribute = from_time.iso8601
title_attribute << " - #{to_time.iso8601}" if to_time
options[:before] = "#{options[:before].strip} " unless options[:before].blank?
options[:after] = " #{options[:after].strip}" unless options[:after].blank?
"#{options[:before]}<abbr title='#{title_attribute}'>#{options[:gebeugt] == true ? distance_of_time_in_words_gebeugt(from_time, to_time, include_seconds, options) : distance_of_time_in_words(from_time, to_time, include_seconds, options)}</abbr>#{options[:after]}"
end
def distance_of_time_in_words_gebeugt_with_abbr(from_time, to_time = 0, include_seconds = false, options = {})
options[:gebeugt] = true
distance_of_time_in_words_with_abbr(from_time, to_time, include_seconds, options)
end
def distance_of_time_in_words_to_now_with_abbr(from_time, include_seconds = false, options = {})
options[:before] = "#{options[:before].strip} " unless options[:before].blank?
options[:after] = " #{options[:after].strip}" unless options[:after].blank?
"#{options[:before]}<abbr title='#{from_time.iso8601}'>#{options[:gebeugt] == true ? distance_of_time_in_words_to_now_gebeugt(from_time, include_seconds) : distance_of_time_in_words_to_now(from_time, include_seconds)}</abbr>#{options[:after]}"
end
def distance_of_time_in_words_to_now_gebeugt_with_abbr(from_time, include_seconds = false, options = {})
options[:gebeugt] = true
distance_of_time_in_words_to_now_with_abbr(from_time, include_seconds, options)
end
def before_or_after_label(date, label_before = "Vor ", label_after = "In ")
date > Time.now ? label_after : label_before
end
def past_or_future_text_by_date(text_before, text_after, date)
# date = sql-date-object
date.to_date < Date.today ? text_before : text_after
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment