Workweek_select

I’m having difficulty figuring out the ruby neccessary to do this so I
thought I would try the list. I’m hoping to get a date_select to only
display certain days (specifically only fridays) but rails doesn’t
seem to have the flexibility so I’ve been going at it in ruby to no
avail.

I’ve looked on google but couldn’t find it (because it isn’t there or
I don’t know exactly what to search for). I’m not concerned about
having the full railsy date select but it would be nice if it’s
possible.

This is the code so far:

def select_friday(in_model, in_attr)
# Scope breaker
friday = “”

# This seems to never trigger at all. A breakpoint before the

conditional is never honored.
Date.today.step(7, 1) do |day|
if day.wday == 5
friday = day
end
end

@dates = []
friday.step(20, -7){|day| @dates << day}
friday.step(20, 7){|day| @dates << day}
@dates.sort!

select in_model, in_attr, @dates

end

Thanks for any suggestions.

Cheers,
Chuck V.

Chuck V. wrote:

I’m having difficulty figuring out the ruby neccessary to do this so I
thought I would try the list. I’m hoping to get a date_select to only
display certain days (specifically only fridays) but rails doesn’t
seem to have the flexibility so I’ve been going at it in ruby to no
avail.

I’ve looked on google but couldn’t find it (because it isn’t there or
I don’t know exactly what to search for). I’m not concerned about
having the full railsy date select but it would be nice if it’s
possible.

This is the code so far:

Cheers,
Chuck V.

This will get the next friday, including today if it is a friday:

def get_next_weekday(weekday)
date = Date.today
until date.wday == weekday
date += 1
end
date
end

Then you can do:

def select_friday(in_model, in_attr)
friday = get_next_weekday(5)

@dates = []
friday.step(20, -7){|day| @dates << day}
friday.step(20, 7){|day| @dates << day}
@dates.sort!

select in_model, in_attr, @dates

end

And to display them, you should be able to use the standard (non date
specific) select tag:

<%= select ‘model’, ‘date_field’,
@dates.collect {|date| } %>

The value of each friday on the list is “2006-07-21”, ready to be sent
to the DB, but the user sees “Jul 21, 2006” or whatever format is
assigned to the :short symbol.

Alex W. wrote:

This will get the next friday, including today if it is a friday:

def get_next_weekday(weekday)
date = Date.today
until date.wday == weekday
date += 1
end
date
end

That’s an error, it should say that it will get the next specified
weekday, obviously.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs