I am using a calendar gem to generate a basic calendar in an Events class of mine.
In my application helper, I create a helper method to call it:
def calendar
@calendar ||= Cal::MonthlyCalendar.from_params params[:month], start_week_on: :sunday
end
The calendar works fine looping in the default way through days and weeks to create a monthly calendar, but I’d like to create a calendar showing only the current week and the upcoming week. Currently, I’m doing this via a big if statement in the view:
...
<% calendar.weeks.each do |week| %>
<tr>
<% week.each do |day| %>
<% if day.number >= Date.today.beginning_of_week(start_day = :sunday).day && day.date.month == Date.today.month %>
...
As you can see, it’s not very proper. Is there any way to create a “class method” or “scope” for this even though its not an object itself? Obviously, the above doesn’t show prior or future months, meaning it will have to be even more verbose to work, so I was looking for a more elegant solution. But, where would I put the method?
Thanks in advance for any help!
I ended up creating my own method to generate calendars that worked better than a gem. For those stumbling upon this question looking for an answer, I’d recommend just throwing the third party gem into a model class (if possible) and then just defining scopes that way. But for my purposes, I only needed a very basic calendar function, so I just created some methods that basically cycle through days using some built-in Rails functions.