I have a form that shows me the business days for a two-week period (10 days). I want to loop over each day and check to see if each day lands on a holiday. If it does, I want to show a label on that row that displays the name and description of the holiday.
Currently my code works to show a holiday. However, it will only show 1 holiday in any two-week period. I believe this is because I have the .first method tacked onto my map and select blocks. But if I remove the .first I receive an error on my calendar helper tool tip because it can no longer access the holiday description attribute.
How can I change this code so that it loops through each day, checks to see if it's a holiday, and then accesses the instance of the holiday class so I can get to the value of the name and description attributes?
Right now I'm just trying to make this work on the _form, not worrying about the index page at this point.
Any help would be appreciated!
Contoller:
class TimesheetsController < ApplicationController
def index
@all_timesheets_for_user = current_user.timesheets
@timesheets = current_user.timesheets.yearly_timesheets(@year).by_pay_period_ending_desc
current_user.log("Timesheets", "User's List", nil, current_admin, browser)
@holiday_dates = Holiday.all
end
def new
@timesheet = Timesheet.new(user_id: current_user.id, pay_period_ending: params[:pay_period], timesheet_type: 'salary')
@holiday_dates = Holiday.all
@timesheet.build_days(@timesheet.timesheet_days.map{|d| d.day})
end
def edit
@timesheet = Timesheet.find(params[:id])
@holiday_dates = Holiday.all
@timesheet.build_days(@timesheet.timesheet_days.map{|d| d.day})
@timesheet.timesheet_days.sort_by!{|d| d.day}
end
end
The holiday model includes the following attributes:
# id :integer not null, primary key
# start_date :date
# end_date :date
# name :string(255)
# description :text
# all_day :boolean
# date_type :string(255)
# created_at :datetime not null
# updated_at :datetime not null
This is the _form.html.haml that is causing the issue.
.table-responsive
%table.table.table-condensed.table-striped.table-bordered.table-hover
%tr
%th Day
%th Date
%th Time Reporting Code
= f.fields_for :timesheet_days do |d|
%tr
%td
= d.object.day.strftime("%A")
= d.hidden_field :day
.type= d.hidden_field :timesheet_type
%td
= d.object.day.strftime("%b %d, %Y")
# -----HOW CAN I ACCESS THE INSTANCES OF THE HOLIDAY CLASS WITHOUT USING FIRST HERE?-----
- if @holiday_dates.map{|h| (h[:start_date]..h[:end_date]).include?(d.object.day)}.first
- holiday = @holiday_dates.select{|h| d.object.day >= h[:start_date] && d.object.day <= h[:end_date]}.first
.message.label.label-xs.bold.render-tooltip.pointer{title: "#{date_tool_tip(holiday)}", id: "event-#{holiday.id}", class: "label-#{date_classes(holiday)} event-#{holiday.id}", data: {id: "#{holiday.id}"}, style: "z-index: 1000; word-wrap: break-word;"}= holiday.name
:javascript
$('.render-tooltip').tooltip();
- else
# do other stuff
Aucun commentaire:
Enregistrer un commentaire