Reputation: 3085
In my app I have single and recurring events.
Event(..., starts_at: datetime, is_recurrent: boolean, period: datetime, ends_at: datetime)
If event is single I am using starts_at
field to determine when it occurs. For the recurring events I am using ends_at
and period
fields as well.
For the just single events I can simply write something like this:
controller:
@events_by_date = @events.group_by{ |event| event.starts_at.to_date }
view:
<%= render :partial => "event", :collection => @events_by_date[date] %>
But I have no idea how I can implement this for example for daily events.
Now all events (both, single and recurring) are shown only when they start(because I'm using group_by{ |event| event.starts_at.to_date }
). I want to make so if event starts at 24 of March and ends at 30 of March, this event must be shown on the each day cell before 31 of March.
Could I use group_by method for these purposes?
Upvotes: 0
Views: 418
Reputation: 29349
I am not sure if you can do it in single group_by
@events_by_date = @events.reject{|event| event.is_recurrent}.group_by{ |event| event.starts_at.to_date }
@events.select{|event| event.is_recurrent}.map do |event|
(event.starts_at..event.ends_at).each do |time|
@events_by_date[time.to_date] ||= [];
@events_by_date[time.to_date] << event
end
end
Upvotes: 1