I want to be able to mark dates between start date and end date of my calendar in rails app
Question: I have a rails app that uses the calendar helper to plot plot_start and leave_end on my calendar. I want to be able to mark dates in between I've tried a few things and still have no luck that I could however get leave_start and leave_end to show, but can't mark dates in between. Any help would be very much appreciated !!!!
Here is what I have tried so far.
This is my input controller ...
class EntryController < ApplicationController
def index
entry_by_start_date = Entry.where("emp_id = ?", 'BILL').group_by {|i| i.leave_start.to_date
entry_by_end_date = Entry.where("emp_id = ?", 'BILL').group_by {|i| i.leave_end.to_date
@entry_by_date = entry_by_start_date.merge(entry_by_end_date) {|key, oldval, newval| newval }
####^ this works but will only plot leave_start and leave_end on my calendar.
# this is how I tried to plot all the dates between
## leave_start and leave_end to be plotted on my calendar as well
e_by_s = Entry.where("emp_id = ?", 'BILL').pluck(:leave_start)
e_by_e = Entry.where("emp_id = ?", 'BILL').pluck(:leave_end)
r_j = e_by_s.to_s
r_g = e_by_e.to_s
p_g = r_j.to_date
t_g = r_g.to_date
days_between = p_g.upto(t_g) { |date| puts date }
@entry_by_all_dates = days_between.merge(days_between) {|key, oldval, newval| newval }
@date = params[:date] ? Date.parse(params[:date]) : Date.today
respond_to do |format|
format.html # index.html.haml
format.json { render :json => @entry }
end
end
So for example: my vacation date will be 2015-05-25
my vacation date will be 2015-05-29
days between both dates will be
leave_start 2015-05-25
2015-05-26 2015-05-27 2015-05-28
leave_end 2015-05-29
Additional Information
here is my calendar helper
module CalendarHelper
def calendar(date = Date.today, &block)
Calendar.new(self, date, block).table
end
class Calendar < Struct.new(:view, :date, :callback)
HEADER = %w[Sun Mon Tues Wed Thur Fri Sat]
START_DAY = :sunday
delegate :content_tag, to: :view
def table
content_tag :table, class: "calendar" do
header + week_rows
end
end
def header
content_tag :tr do
HEADER.map { |day| content_tag :th, day }.join.html_safe
end
end
def week_rows
weeks.map do |week|
content_tag :tr do
week.map { |day| day_cell(day) }.join.html_safe
end
end.join.html_safe
end
def day_cell(day)
content_tag :td, view.capture(day, &callback), class: day_classes(day)
end
def day_classes(day)
classes = []
classes << "today" if day == Date.today
classes << "notmonth" if day.month != date.month
classes.empty? ? nil : classes.join(" ")
end
def weeks
first = date.beginning_of_month.beginning_of_week(START_DAY)
last = date.end_of_month.end_of_week(START_DAY)
(first..last).to_a.in_groups_of(7)
end
Also here is my take on my calendar
#entry
%h2#month
= link_to "<", date: @date.prev_month
= @date.strftime("%B %Y")
= link_to ">", date: @date.next_month
= calendar @date do |date|
= date.day
- if @entry_by_date[date]
%ul
- @entry_by_date[date].each do |entry|
%li
%span.u= entry.emp_id
%ul
%span.u= entry.leave_start.strftime('%m/%d')
%span.u --
%span.u= entry.leave_end.strftime('%m/%d')
-elsif @entry_by_all_dates
%ul
- @entry_by_all_dates[date].each do |t|
%li
%span.u= t.emp_id
%ul
%span.u= t.leave_start.strftime('%m/%d')
%span.u --
%span.u= t.leave_end.strftime('%m/%d')
Update ---
Tried this
e_by_s = Entry.where("emp_id = ?", 'BILL').pluck(:leave_start).first
e_by_e = Entry.where("emp_id = ?", 'BILL').pluck(:leave_end).first
r_j = e_by_s.to_s
r_g = e_by_e.to_s
p_g = r_j.to_date
t_g = r_g.to_date
@days_between = p_g..t_g
@entry_by_all_dates = (@days_between).map(&:to_s)
@date = params[:date] ? Date.parse(params[:date]) : Date.today
respond_to do |format|
format.html # index.html.haml
format.json { render :json => @entry }
end
end
and in my opinion
#entry
%h2#month
= link_to "<", date: @date.prev_month
= @date.strftime("%B %Y")
= link_to ">", date: @date.next_month
= calendar @date do |date|
= date.day
- if @entry_by_date[date]
%ul
- @entry_by_date[date].each do |entry|
%li
%span.u= entry.emp_id
%ul
%span.u Type --
%span.u= entry.indirect_id
%ul
%span.u= entry.leave_start.strftime('%m/%d')
%span.u --
%span.u= entry.leave_end.strftime('%m/%d')
%ul
%span.u Shift --
%span.u= entry.am_pm
- @entry_by_all_dates[date].each do |t|
%li
%span.u= t.emp_id
%ul
%span.u= t.leave_start.strftime('%m/%d')
%span.u --
%span.u= t.leave_end.strftime('%m/%d')
For some odd reason I am getting this error ...
ActionView::Template::Error (can't convert Date into Integer):
I have attached a screenshot of my calendar showing the start and end dates. I want the dates in between to display as well.
source to share
require 'date'
start_date = Date.parse('2015-05-25')
end_date = Date.parse('2015-05-29')
date_range = start_date..end_date
(date_range).map(&:to_s)
This will output an array of date range between (start_date..end_date) like this
Example:
["2015-05-25", "2015-05-26", "2015-05-27", "2015-05-28", "2015-05-29"]
source to share