How can I make a `weeks_ago` helper? - ruby-on-rails

How can I make a helper that will tell me how many weeks ago (rounded down) with rails? It could be based of the time_ago_in_words helper however I'd want it to return: "last week" then afterwards just two weeks ago, three weeks ago, etc...

Try this:
def my_time_ago_in_words(from_time, include_seconds = false)
to_time = Time.now
weeks_ago = ((to_time - from_time)/1.week).abs
[nil, "last week", "two weeks ago", "three weeks ago"][weeks_ago] ||
distance_of_time_in_words(from_time, to_time, include_seconds)
end
This function will behave the same as time_ago_in_words. When the from_time is between 1 - 3 week ago, this will print last week, two weeks ago, three weeks ago otherwise it will print the usual.

That would be a nice patch to distance_of_time_in_words:
http://github.com/rails/rails/blob/4cbb9db0a5ff65b0a626a5b043331abefd89e717/actionpack/lib/action_view/helpers/date_helper.rb#L68-103

Also, you can write custom time descriptions for the other levels (day, month, year).
http://robots.thoughtbot.com/post/392707640/the-more-you-know-custom-time-descriptions

Related

Display time remaining with distance_of_time_in_words

So i want item to display to remaining on Items after 7 days the item will be deleted. ive tried
<%= distance_of_time_in_words(item.created_at, item.created_at + 7.days) %>
but all i get is "7 Days" on all items. Can anyone simply how this helper method works ?
Lets looks at the documentation to see what distance_of_time_in_words does:
distance_of_time_in_words(from_time, to_time = 0, options = {})
Reports the approximate distance in time between two Time, Date or DateTime objects or integers as seconds.
So it reports the time difference of the first argument and the second argument. Now, you're doing:
distance_of_time_in_words(item.created_at, item.created_at + 7.days)
The difference between item.created_at and item.created_at plus seven days is always ... seven days ;-)
I assume that this is something that will always be deleted after seven days? In that case, what you want, is the difference between the current date and the creation date plus seven days, which you can get with:
distance_of_time_in_words(Time.now, item.created_at + 7.days)

How do I sort by subcategories of time using the updated_at tag?

#todo = Todo.where(done:true,user:current_user.email).where("updated_at >= ?", Time.zone.now.beginning_of_day)
So this is an example of a call I'm making. Now how can I sort by subcategories of time? Eg. Grab all the entries from six days ago, all the entries from five days ago, and perhaps even from every week this month?
Here's an example of querying a date range. It'll find all tasks from one week ago to today.
#todo = Todo.where(done: true,
user: current_user.email,
updated_at: 1.week.ago..Date.today)

Rails created_at format to show only minute, day, or week

I'd like to Post's to be displayed either showing how many minutes ago they were posted or weeks ago.
For example, if the post was created 5 minutes ago, it should be displayed as
5m
5 days ago:
5d
10 weeks ago:
10w
and so on.
Previously I was just using time_ago_in_words, but would prefer a simpler view.
Check out strftime, it will probably help you.
If it doesn't, take a look at to_formatted_s.
Hope it helps =]
Looks like you need to create your own solution, for example:
In your ApplicationHelper:
def short_time_ago_in_words(time)
case time
when (1.minute.ago)...(Time.now) then "1m"
when...
when (Date.today - 1)..(Date.today) then "1d"
when (Date.today - 7)..(Date.today) then "1w"
when...
end
end

show weeks_ago based off week number?

Im trying to show the number of weeks ago calculated from the week number
" x weeks ago "
this should just increment one week ago, two weeks ago, three weeks ago, 1 month ago, 2 months ago, 1 year ago.. etc
Im using method helper:
def weeks_ago_in_words(from_time, include_seconds = false)
to_time = Time.now
weeks_ago = ((to_time - from_time)/1.week).abs
[nil, "last week", "two weeks ago", "three weeks ago"][weeks_ago] ||
distance_of_time_in_words(from_time, to_time, include_seconds)
end
In view:
= weeks_ago_in_words(Time.local(2013) + week.to_i)
week = a week number like 1,10,28,52 etc
This does not give me correct resuls, is there a better way to calculate the "x weeks ago " based on the weeknumer?
If you are comparing two dates from the same year here is the code using cweek:
require 'date'
Time.now.to_date.cweek - Date.new(2013,1,1).cweek
If you are comparing two different years you'll have to add some conditional logic to determine your year multiplier as cweek returns the same value for Dec. 31, 2012 and Jan. 1, 2013.

Shoter time_ago_in_words eg. 1d ago

In my design i have a latest posts block, in this block there is very little space to say when the post was posted. Therefore, I did it as "1d" instead of "1 day ago". I would like it to be either "Today", "Yesterday" or "xd"(1d, 2d, 3d, etc)
The main reason for not using hours and minutes(and seconds..) is that my format is like this posted_date="2013-01-04", so no hours, minutes, seconds etc.
Is this posible?
def days_ago(date)
days = ((Time.now - date) / 24 / 60 / 60).round
case days
when 0 then 'Today'
when 1 then 'Yesterday'
else "#{days}d"
end
end

Resources