Group results into sections by date - ruby-on-rails

I need to figure out how to group results into different sections by date.
I have a page that has reservations with different dates, instead of having a table sorted by dates, I want to create each reservation in its own section on that date.
ex.
January 27
John Doe - 01/27/17
Jane doe - 01/27/17
January 28
Ricky Bobby - 01/28/17
January 29
Baby Jesus - 01/29/17

You can use group_by on a collection of dates, then pass in a method like .month or .yday
array = [Date.today, Date.today, Date.today.next, Date.new(2017,12,31)]
array.group_by(&:yday)
This returns the list of dates by the day in the year. Its basically equivalent to Hash[yday] = Array.new(matching_dates).

Related

count how many specific days are in a time period

I want to count say how many Mondays we have from 2022-02-01 - 2022-03-01. I found smth like this:
=SUMPRODUCT(WEEKDAY(B4:C4)=2) - B4 and C4 are the dates
But it returns 0. I assume it only checks if specific date is the specific day. Any ideas how I can do this but for a date range? So how count how many Mondays there are in February
I also found this
=NETWORKDAYS.INTL(B4;C4;"1000000")
but this returns 25
You can take advantage of the NETWORKDAYS.INTL function by using the string method to make all the days as weekend except for Monday.
The String method states:
weekends can be specified using seven 0’s and 1’s, where the first number in the set represents Monday and the last number is for Sunday. A zero means that the day is a work day, a 1 means that the day is a weekend. For example, “0000011” would mean Saturday and Sunday are weekends.
In this case since you only want to know the Mondays, the string would be "0111111" and the function would look like:
=NETWORKDAYS.INTL(StartDate,EndDate,"0111111")
I think this is right. It's counting inclusively so you would get five Mondays starting on Monday 7th Feb 2022 and ended on Monday 7th March 2022 for example.
=floor((B2-(A2+7-weekday(A2,12)))/7)+1
where A2 and B2 contain the start date end end date.
Obvs nul points for me again but for the record this could be generalised if you put the day number in C2 (e.g. 1 if you want to find Sundays, 2 for Mondays):
=floor((B2-(A2+7-weekday(A2,10+C2)))/7)+1

Select a "tax month" with a non standard month start/end

I have a Google Sheet with data like this:
Date
In
Out
July 13
£40
July 21
£60
etc.
I'd like to add another column "month" which specifies what month the entry was made in. Only problem is I can't use the standard MONTH() function because for accounting purposes, the tax month is 16th - 15th. So July 13 would be considered to be in the June/July tax month, while July 21 would be considered to be in the July/August tax month.
I'm assuming I will need to maintain a table of the specific cut off dates like so:
Month
Start
End
Jun/July
16th June
15th July
etc.
But I can't work out how to use this as a lookup table to achieve what I want. Any thoughts appreciated.
I think this should work if your date is in A1:
=IF(DAY(A1)>15,TEXT(A1,"MMMM")&"/"&TEXT(A1+20,"MMMM"),TEXT(A1-20,"MMMM")&"/"&TEXT(A1,"MMMM"))
If sort order is important (e.g. in a subsequent pivot table) I would use this:
=IF(DAY(A1)>15,TEXT(A1,"MM MMMM")&"/"&TEXT(A1+20,"MMMM"),TEXT(A1-20,"MM MMMM")&"/"&TEXT(A1,"MMMM"))
Another way to specify the sort order is maintaining a separate table with sort order column. It's a completely different approach.
use:
=ARRAYFORMULA(IF(A2:A="",,IF(DAY(A2:A)<16,
TEXT("1/"&MONTH(A2:A)-1, "mmmm")&"/"&TEXT("1/"&MONTH(A2:A), "mmmm"),
TEXT("1/"&MONTH(A2:A), "mmmm")&"/"&TEXT("1/"&MONTH(A2:A)+1, "mmmm"))))

Timetable App for Rails 4

I am trying to build a timetable setup to work in conjunction with one of my Models. Before I state my question, I will give some context into the problem.
I have a Model called Appointments with the following columns:
Name(String)
Start_time(time)
End_time(time)
Appt_Date(date)
Now I can have multiple appointments in 1 day.
For Example, let's say I have the following 2 Appointment objects:
Appointment 1:
Name: Makeup Appointment
Start_time: 11:00 am
End_time: 1:00 pm
Date: March 30, 2016
Appointment 2:
Name: Daily Meetup
Start-time: 2:00 pm
End_time: 3:00 pm
Date: March 30, 2016
I would like to implement a date-picker form where you can select a date and it would render 24 rows(1 for each hour of the day) and fill in the rows with the times not available based on the appointments on that day.
For example, if I select March 30, 2016 from the date-picker, I would like to render the 24 rows and have the rows for 11am-1pm and 2:00pm-3:00pm shaded out.
The setup is like google calendars(how time slots are colored in) but with a day-to-day basis. I don't need to be able to edit these rows. I just need to view them and have them rendered with colored cells based on Appointment objects for that specific day.
My issue is, I don't know where to begin to be able to design these 24 rows that interact with appointment objects. I was thinking that perhaps I build a helper method, however even that I am pretty lost. I would appreciate some guidance on how to approach this.
Below will help you:
1.Create a file in initializer which contains list of available time slots like 10 am to 10 pm.
2.For displaying date time field , use some jquery date time picker plugin.
3.Fire an ajax when clicked on datetime picker.
4.Ajax request will come on the controller & where you have to create an active record query which will fetch all apointments according to the particular date.
5.MAke an array variable & store the time which are already booked in the above appointments.
6.Compare the available time with the booked time & edit datetimepikcer.js to shaded the booked date.

How do I create a hash of weekdays matched with dates of objects for a calendar?

I'm building a cashflow management tool as an exercise.
I've a table to display a budget for a week. It has weekdays as column headers, and Income/Expenditure Categories as headers for rows (the first ). Each row is then filled with cells from the Incomes/Expenditures for that category and for that week.
The problem is the Income/Expenditure Cells aren't mapped to their weekdays, so if a day doesn't have an income or expenditure value the layout of the row breaks - the cell values don't match the column headers for weekdays.
I have Income, Expenditure and Category models. Category's have many Expenditures and Incomes etc.
I think I need to create an #week_of_incomes_by_category object in my show controller method that takes a week of previous Incomes from Monday to Friday, and then puts the results into an array of ["Monday", "Tuesday", "Wednesday, "Thursday, "Friday"] - however I'm not sure how best to do this.
I can get a week of values using .where and .order, I don't know how to create that matched Income.date with the right day value. I'm also open to alternative solutions for my weekday calendar problem.
Thanks for the help.

How to display a record set grouped by month?

I'm interested in display a record set of timeline type events in the format:
2011
APR
- Record Out
- Record Out
AUG
- Record Out
- Record Out
SEP
- Record Out
- Record Out
OCT
- Record Out
- Record Out
2009
OCT
- Record Out
- Record Out
The trick here being it only display the year if there are items in that year to show.. Same for a month, the month grouping only renders if there are records for that month to display. Does rails have any built in grouping for this type of output? Or would this be 100% custom to implement? Thanks for any pointers.
The grouping can be done by Ruby's built-in Enumerable#group_by method:
events.group_by(&:year).each do |year, year_events|
puts year, year_events
end
I assume you have event.year method. The same thing with grouping by months.

Resources