Month Dependent Text in Rails - ruby-on-rails

I have a bit of test that needs to only appear on my site during the months of Aug through Nov, and all other months the text should be something different. What is the best way to accomplish this?
I would think it would look something like the following, but I am not sure how to limit it to just the month, so that the switch will happen every year.
IF Date.today IS BETWEEN ???August AND ???Nov DO
...
ELSE
...

Use .month and check if it's value included into expected list of accepted months
accepted_months = Set.new([8, 9])
if accepted_months.include?(Time.current.month)
else
end

Related

Rails - define custom year

I need to define a custom month range as a project_year.
Rather than a year starting in January and ending at the end of December, I need to define a project_year as starting August 1st and ending on the last day of July.
I need to group all reports by project_year and then by month. Report has a report_month attribute(dateTime). Essentially I would need to display the reports like this:
Projects:
2015
August
September
October
November
Etc
2014
August
September
October
November
Etc
2013
August
September
October
November
Etc
I've been playing around with the Array.sample method, but without success.
reports = #station.reports.order("report_month asc")
range = reports.last.report_month.year..reports.first.report_month.year
range.to_a.each do |year|
start = year.beginning_of_year + 7.months
finish = (year.beginning_of_year + 1.year - 6.months).end_of_month
yr = reports.sample{ |r| report.report_month >= start and <= finish }
<h1><%= year.strftime("%Y")</h1>
yr.each do |m|
<h1><%= m.strftime("%b")</h1>
end
end
I realize the above isnt appropriate view code, Im just trying to indicate the manner I was trying to use. Which became cumbersome and hard to troubleshoot. After struggling with this for a while I decided to ask here.
Ultimately my the ideal would be to use the group_by feature outlined here:
http://railscasts.com/episodes/29-group-by-month Only instead using group_by project_year.
Is this possible with Rails? Or is there a better way to group by a custom month range?
Does it help if you add a the customer field on the model such as quarter then use the group_by api.
def quarter
"#{created_at.month/4}, #{created_at.year}"
end

Rails age of an article in days?

I need to get the age of the article in days. For example, the article was written on Tue, 01 Apr 2014 18:31:07 EDT -04:00 and now I need the days from that date to now printed as an integer. How can I do so?
Please try something like this:
gem install time_diff
install the gem.
require 'time_diff'
time_diff_components = Time.diff(start_date_time, end_date_time)
time_diff_components[:year], time_diff_components[:month], time_diff_components[:week]
This will give more option.
More detail click
This isn't the cleverest way, but it's probably the simplest: use a "magic number": 86400, which is the number of seconds in a day. (you probably already know there are 3600 seconds in an hour, mentally file this number alongside that)
Differences between Time/DateTime objects will be in seconds (as a float). If you divide this by 86400 you get the difference in days, as a float. You can then call to_i on this to get it as an integer if you want.
eg
((Time.now - #article.created_at)/86400).to_i
It's probably worth saving this as a constant, egs SECONDS_IN_A_DAY or something, to avoid mistyping.
With the Date class you can do
(Date.today - #article.created_at.to_date).to_i
to get the number of days between the two dates.

Converting a month and year to a date

In my Rails 4 app I receive a month and a year from the user which I would like to make into a date. The new date should be the 1st of the following month but I'm not sure on the best way to tackle this.
At the moment I have
expiry = Date.new(params[:expYear],params[:expMonth].to_i + 1,1)
but this isn't ideal because if someone entered 12 as the month my approach would make the month 13 which of course doesn't exist (and also the year wouldn't get updated either).
Are there any useful functions that could help here?
You can do this:
year,month = params[:expYear].to_i,params[:expMonth].to_i
Date.new(year,month) + 1.month
or use the end_of_month as #dax wrote:
Date.new(year,month).end_of_month + 1.day
That will resolve your problem
I think this would be more simple
expiry = Date.new(params[:expYear],params[:expMonth].to_i).next_month
I solved it using this - sorry just before your answer user2503775
expiry = Date.new(params[:expYear].to_i,params[:expMonth].to_i) + 1.month
Note You have to convert to integers before this can be done. I also found out that if the day param is left out it defaults to 1 anyway so that's handy
I would do something like
Date.new(params[:expYear].to_i,params[:expMonth].to_i,1).advance(months: 1)
Do something like
Time.zone.local(params[:expYear].to_i, params[:expMonth].to_i, 1, 0, 0, 0)

Rails 3: Is it possible to access a model's attribute in a query?

Sorry if that question sounds strange, but I'm diving into Rails and I'm still learning the jargon. Basically, I'm trying to create a single-pass query that uses the value of one of the model's attributes in a calculation in the query (assuming that's even possible).
I have a Tournament model that has a start_date attribute that is a DateTime object. I'm trying to create a query that returns all the Tournaments that have a start_date no older than 1 hour + the length of the tournament, or put another way, all tournaments that haven't yet started or have started, but haven't ended longer than an hour ago. My current query, which doesn't work, looks like this...
validTourneys = Tournament.where("start_date > (? - duration_in_mins)", (DateTime.now.utc - 1.hour))
where duration_in_mins is an integer attribute of the Tournament model, but this query doesn't work and it seems to be returning all the Tournaments all the time. I'd like to include duration_in_mins in the (DateTime.now.utc - 1.hour) part of the calculation, but I don't know how to reference it, which is why I included it in the string part of the query, hoping that would work. Am I at least on the right track?
I should mention I'm using SQLite for development and PostgreSQL for production.
Thanks for your wisdom!
The problem is that if you subtract minutes from a DateTime object, you are not subtracting minutes but days.
# This works as expected
dt = DateTime.now # Thu, 28 Apr 2011 09:55:14 +0900
an_hour_ago = dt - 1.hour # Thu, 28 Apr 2011 08:55:14 +0900
# But, this does not...
two_hours_in_minutes = 120
two_hours_ago = dt - two_hours_in_minutes # Wed, 29 Dec 2010 09:55:14 +0900
In the last example 120 days are subtracted instead of minutes. This is probably also happening in your query. You have to convert duration_in_minutes to days and then subtract.
I don't know enough about SQL to answer your question directly (I think this will probably also depend on what database you're using, so you might want to mention that).
Have you considered, though, having start_date and end_date as DateTime columns instead of start_date and duration_in_mins? If this is going to be a common query, that would certainly make it more performant, as well as making your code easier to read and understand.
This query will only work if your database is smart enough to know how to add (what I am assuming) is a DateTime and and integer. And I can't think of a database that will do that correctly the way you have it coded. No database will assume minutes. Some might do ticks, seconds, or days.
This part of the calculation
(? - duration_in_mins)
is going to happen on the database, not in Ruby-land.

Rails customized "time_ago_in_words"-ish type model

Morning, everyone!
I'm working on a Rails app that is just a simple check yes or no, then it displays the created_at row and whichever box was checked. My issue is that I need to display the created_at as a different value, not the time. If someone created an entry between 4:00 AM - 10:00 AM, it would display Breakfast in the view for the created_at field. If they created it between 10:00 AM and 2:00 PM, it would display Lunch.
I've Googled for hours, and the closest thing I could come up with is some variation of time_ago_in_words but nothing really works.
How would I go about implementing something of this nature?
Full disclosure: I'm a bit of an amateur at Ruby/RoR. Be gentle...
You can create your own
case created_at.hour
when (4..10)
"Breakfast"
when (10..14)
"Lunch"
when (14..22)
"Supper"
else
"Night"
end
You can also wrap it as a method or extend DateTime class with it

Resources