I''m developing an app that I want to have switch from its normal weekday view to a weekend-only view (essentially a look at the previous week). While I understand the concept of what I need to do: if the day = saturday or sunday display Y I have no good foothold for the actual code (still learning this stuff). Any help would be huge! Dan
If it is just the data that is the issue (as opposed to different layouts/views), then filtering the results of the controller.index method is the way to go. On Jun 17, 4:20 pm, dansinker <dansin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m developing an app that I want to have switch from its normal > weekday view to a weekend-only view (essentially a look at the > previous week). > > While I understand the concept of what I need to do: > > if the day = saturday or sunday display Y > > I have no good foothold for the actual code (still learning this > stuff). > > Any help would be huge! > > Dan
On Jun 17, 2009, at 8:08 PM, E. Litwin wrote:> If it is just the data that is the issue (as opposed to different > layouts/views), then filtering the results of the controller.index > method is the way to go. > > On Jun 17, 4:20 pm, dansinker <dansin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> I''m developing an app that I want to have switch from its normal >> weekday view to a weekend-only view (essentially a look at the >> previous week). >> >> While I understand the concept of what I need to do: >> >> if the day = saturday or sunday display Y >> >> I have no good foothold for the actual code (still learning this >> stuff). >> >> Any help would be huge! >> >> Danin your controller: if (1..5).include?(Date.today.wday) # set the appropriate variables # @daystuff = Stuff.today render ''weekday'' else # @weekstuff = Stuff.last_week render ''weekend'' end You might be able to use the same view depending on how similar the display of the daily stuff and the weekly (weekend) stuff is. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org +1 513-295-4739 Skype: rob.biedenharn
Excellent. This gets me moving in the right direction. Thanks so much! On Jun 17, 7:33 pm, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> On Jun 17, 2009, at 8:08 PM, E. Litwin wrote: > > > > > > > If it is just the data that is the issue (as opposed to different > > layouts/views), then filtering the results of the controller.index > > method is the way to go. > > > On Jun 17, 4:20 pm, dansinker <dansin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> I''m developing an app that I want to have switch from its normal > >> weekday view to a weekend-only view (essentially a look at the > >> previous week). > > >> While I understand the concept of what I need to do: > > >> if the day = saturday or sunday display Y > > >> I have no good foothold for the actual code (still learning this > >> stuff). > > >> Any help would be huge! > > >> Dan > > in your controller: > > if (1..5).include?(Date.today.wday) > # set the appropriate variables > # @daystuff = Stuff.today > render ''weekday'' > else > # @weekstuff = Stuff.last_week > render ''weekend'' > end > > You might be able to use the same view depending on how similar the > display of the daily stuff and the weekly (weekend) stuff is. > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org > +1 513-295-4739 > Skype: rob.biedenharn
I was able to take that code and flip it to this, just as a quick way to make sure I got it: <% if (1..5).include?(Date.today.wday) flash[:notice] = ''weekday!'' else flash[:notice] = ''weekend!'' end %> And that totally worked. So I definitely know where to take this. But-- and I will say again, I''m new at this--WHY did it work? Specifically, what''s the (1..5) thing? I''ve not seen that before. Thanks again! Dan On Jun 17, 7:33 pm, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> On Jun 17, 2009, at 8:08 PM, E. Litwin wrote: > > > > > > > If it is just the data that is the issue (as opposed to different > > layouts/views), then filtering the results of the controller.index > > method is the way to go. > > > On Jun 17, 4:20 pm, dansinker <dansin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> I''m developing an app that I want to have switch from its normal > >> weekday view to a weekend-only view (essentially a look at the > >> previous week). > > >> While I understand the concept of what I need to do: > > >> if the day = saturday or sunday display Y > > >> I have no good foothold for the actual code (still learning this > >> stuff). > > >> Any help would be huge! > > >> Dan > > in your controller: > > if (1..5).include?(Date.today.wday) > # set the appropriate variables > # @daystuff = Stuff.today > render ''weekday'' > else > # @weekstuff = Stuff.last_week > render ''weekend'' > end > > You might be able to use the same view depending on how similar the > display of the daily stuff and the weekly (weekend) stuff is. > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org > +1 513-295-4739 > Skype: rob.biedenharn
On Jun 17, 2009, at 9:55 PM, sinker wrote:> I was able to take that code and flip it to this, just as a quick way > to make sure I got it: > > <% if (1..5).include?(Date.today.wday) > flash[:notice] = ''weekday!'' > else > flash[:notice] = ''weekend!'' > end %> > > And that totally worked. So I definitely know where to take this. > But-- > and I will say again, I''m new at this--WHY did it work? > > Specifically, what''s the (1..5) thing? I''ve not seen that before. > > Thanks again! > > DanDate#wday gives the "weekday" number: 0=Sunday, 6=Saturday 1..5 is an inclusive Range (1..5).include?(x) is a predicate that is true if x falls in the Range, it''s like (1 <= x && 5 <= x) I see that you put the code into your view. You probably want to keep this kind of logic in your controller (or possibly a helper). In any case, in a view you could put: <%= (1..5).include?(Date.today.wday) ? "weekday" : "weekend" %> <% %> bracket Ruby code <%= %> do too, but the value of the contained expression as a string is output to the page. -Rob> On Jun 17, 7:33 pm, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> > wrote: >> On Jun 17, 2009, at 8:08 PM, E. Litwin wrote: >>> If it is just the data that is the issue (as opposed to different >>> layouts/views), then filtering the results of the controller.index >>> method is the way to go. >> >>> On Jun 17, 4:20 pm, dansinker <dansin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>>> I''m developing an app that I want to have switch from its normal >>>> weekday view to a weekend-only view (essentially a look at the >>>> previous week). >> >>>> While I understand the concept of what I need to do: >> >>>> if the day = saturday or sunday display Y >> >>>> I have no good foothold for the actual code (still learning this >>>> stuff). >> >>>> Any help would be huge! >> >>>> Dan >> >> in your controller: >> >> if (1..5).include?(Date.today.wday) >> # set the appropriate variables >> # @daystuff = Stuff.today >> render ''weekday'' >> else >> # @weekstuff = Stuff.last_week >> render ''weekend'' >> end >> >> You might be able to use the same view depending on how similar the >> display of the daily stuff and the weekly (weekend) stuff is. >> >> -Rob >> >> Rob Biedenharn http://agileconsultingllc.com >> R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.orgRob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org
Thanks, that totally makes sense. I just tossed it in the view to make sure it worked. Moved it into the index controller and the final looks like this: if (1..5).include?(Date.today.wday) else redirect_to :controller => ''libraries'', :action => ''index'' end Thank you again!!! On Jun 18, 12:24 am, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> On Jun 17, 2009, at 9:55 PM, sinker wrote: > > > > > > > I was able to take that code and flip it to this, just as a quick way > > to make sure I got it: > > > <% if (1..5).include?(Date.today.wday) > > flash[:notice] = ''weekday!'' > > else > > flash[:notice] = ''weekend!'' > > end %> > > > And that totally worked. So I definitely know where to take this. > > But-- > > and I will say again, I''m new at this--WHY did it work? > > > Specifically, what''s the (1..5) thing? I''ve not seen that before. > > > Thanks again! > > > Dan > > Date#wday gives the "weekday" number: 0=Sunday, 6=Saturday > 1..5 is an inclusive Range > (1..5).include?(x) is a predicate that is true if x falls in the > Range, it''s like (1 <= x && 5 <= x) > > I see that you put the code into your view. You probably want to keep > this kind of logic in your controller (or possibly a helper). > > In any case, in a view you could put: > > <%= (1..5).include?(Date.today.wday) ? "weekday" : "weekend" %> > > <% %> bracket Ruby code > <%= %> do too, but the value of the contained expression as a string > is output to the page. > > -Rob > > > > > > > On Jun 17, 7:33 pm, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> > > wrote: > >> On Jun 17, 2009, at 8:08 PM, E. Litwin wrote: > >>> If it is just the data that is the issue (as opposed to different > >>> layouts/views), then filtering the results of the controller.index > >>> method is the way to go. > > >>> On Jun 17, 4:20 pm, dansinker <dansin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >>>> I''m developing an app that I want to have switch from its normal > >>>> weekday view to a weekend-only view (essentially a look at the > >>>> previous week). > > >>>> While I understand the concept of what I need to do: > > >>>> if the day = saturday or sunday display Y > > >>>> I have no good foothold for the actual code (still learning this > >>>> stuff). > > >>>> Any help would be huge! > > >>>> Dan > > >> in your controller: > > >> if (1..5).include?(Date.today.wday) > >> # set the appropriate variables > >> # @daystuff = Stuff.today > >> render ''weekday'' > >> else > >> # @weekstuff = Stuff.last_week > >> render ''weekend'' > >> end > > >> You might be able to use the same view depending on how similar the > >> display of the daily stuff and the weekly (weekend) stuff is. > > >> -Rob > > >> Rob Biedenharn http://agileconsultingllc.com > >> R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org > > Rob Biedenharn http://agileconsultingllc.com > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org
2009/6/18 sinker <dansinker-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > Thanks, that totally makes sense. I just tossed it in the view to make > sure it worked. Moved it into the index controller and the final looks > like this: > > if (1..5).include?(Date.today.wday) > else > redirect_to :controller => ''libraries'', :action => ''index'' > end >Have you considered the time zone issue if this is for a public website? Does Date.today know the timezone of the user? If not and it works in the local timezone of the server (or maybe the rails timezone setting, or UTC) then if a user is in a different timezone his weekend may be up to 23 hours adrift from that (depending on the server/rails timezone setting). Colin
On Thu, Jun 18, 2009 at 8:52 AM, Colin Law<clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> > 2009/6/18 sinker <dansinker-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: >> >> Thanks, that totally makes sense. I just tossed it in the view to make >> sure it worked. Moved it into the index controller and the final looks >> like this: >> >> if (1..5).include?(Date.today.wday) >> else >> redirect_to :controller => ''libraries'', :action => ''index'' >> end >> > > Have you considered the time zone issue if this is for a public > website? Does Date.today know the timezone of the user? If not and > it works in the local timezone of the server (or maybe the rails > timezone setting, or UTC) then if a user is in a different timezone > his weekend may be up to 23 hours adrift from that (depending on the > server/rails timezone setting).You could use Time.zone = current_user.timezone # which would probably be in a before_filter and then Time.zone.now instead of Date.today -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale
2009/6/18 Rick DeNatale <rick.denatale-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > On Thu, Jun 18, 2009 at 8:52 AM, Colin Law<clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> >> 2009/6/18 sinker <dansinker-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: >>> >>> Thanks, that totally makes sense. I just tossed it in the view to make >>> sure it worked. Moved it into the index controller and the final looks >>> like this: >>> >>> if (1..5).include?(Date.today.wday) >>> else >>> redirect_to :controller => ''libraries'', :action => ''index'' >>> end >>> >> >> Have you considered the time zone issue if this is for a public >> website? Does Date.today know the timezone of the user? If not and >> it works in the local timezone of the server (or maybe the rails >> timezone setting, or UTC) then if a user is in a different timezone >> his weekend may be up to 23 hours adrift from that (depending on the >> server/rails timezone setting). > > You could use > > Time.zone = current_user.timezone # which would probably be in a before_filterHow does current_user.timezone work? Colin> > and then > > Time.zone.now > > instead of Date.today > > > -- > Rick DeNatale > > Blog: http://talklikeaduck.denhaven2.com/ > Twitter: http://twitter.com/RickDeNatale > WWR: http://www.workingwithrails.com/person/9021-rick-denatale > LinkedIn: http://www.linkedin.com/in/rickdenatale > > > >
On Thu, Jun 18, 2009 at 12:38 PM, Colin Law<clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> > 2009/6/18 Rick DeNatale <rick.denatale-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:>> You could use >> >> Time.zone = current_user.timezone # which would probably be in a before_filter > > How does current_user.timezone work?Well there''s a little handwaving here. The assumption is that current_user refers to a user model instance set up by restful_authentication or some other login code, and that the model contains the user''s preferred timezone probably set by a profile ui. -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale
Here - http://blog.redfin.com/devblog/2007/08/getting_the_time_zone_from_a_web_browser.html - is a way (allegedly) of getting the user''s timezone using javascript, which could then be passed to the server. I cannot vouch for whether it works or not. Colin 2009/6/19 Rick DeNatale <rick.denatale-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > On Thu, Jun 18, 2009 at 12:38 PM, Colin Law<clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> >> 2009/6/18 Rick DeNatale <rick.denatale-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > >>> You could use >>> >>> Time.zone = current_user.timezone # which would probably be in a before_filter >> >> How does current_user.timezone work? > > Well there''s a little handwaving here. The assumption is that > current_user refers to a user model instance set up by > restful_authentication or some other login code, and that the model > contains the user''s preferred timezone probably set by a profile ui. > > -- > Rick DeNatale > > Blog: http://talklikeaduck.denhaven2.com/ > Twitter: http://twitter.com/RickDeNatale > WWR: http://www.workingwithrails.com/person/9021-rick-denatale > LinkedIn: http://www.linkedin.com/in/rickdenatale > > > >
Another weekend-related question: I have a variable that displays something scheduled for tomorrow. Since I''m skipping weekends, on Friday I want it to show what''s scheduled on Monday. I know how to get the query right, but I''m not sure about the if statement: Essentially, I need to say if Date.today = friday.... but I can''t seem to get the syntax right. The rest would simply be: mystery if statement here @tomorrow = Story.find(:first, :conditions => [''rundate =?'', Date.today+3]) else @tomorrow = Story.find(:first, :conditions => [''rundate =?'', Date.today+1]) end Thanks!! On Jun 19, 2:11 am, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> Here -http://blog.redfin.com/devblog/2007/08/getting_the_time_zone_from_a_w... > - is a way (allegedly) of getting the user''s timezone using > javascript, which could then be passed to the server. I cannot vouch > for whether it works or not. > > Colin > > 2009/6/19 Rick DeNatale <rick.denat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > > > > On Thu, Jun 18, 2009 at 12:38 PM, Colin Law<clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > >> 2009/6/18 Rick DeNatale <rick.denat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > >>> You could use > > >>> Time.zone = current_user.timezone # which would probably be in a before_filter > > >> How does current_user.timezone work? > > > Well there''s a little handwaving here. The assumption is that > > current_user refers to a user model instance set up by > > restful_authentication or some other login code, and that the model > > contains the user''s preferred timezone probably set by a profile ui. > > > -- > > Rick DeNatale > > > Blog:http://talklikeaduck.denhaven2.com/ > > Twitter:http://twitter.com/RickDeNatale > > WWR:http://www.workingwithrails.com/person/9021-rick-denatale > > LinkedIn:http://www.linkedin.com/in/rickdenatale
2009/6/19 sinker <dansinker-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > Another weekend-related question: > > I have a variable that displays something scheduled for tomorrow. > Since I''m skipping weekends, on Friday I want it to show what''s > scheduled on Monday. > > I know how to get the query right, but I''m not sure about the if > statement: > > Essentially, I need to say if Date.today = friday.... but I can''t seem > to get the syntax right. The rest would simply be: > > mystery if statement here > @tomorrow = Story.find(:first, :conditions => [''rundate =?'', > Date.today+3]) > else > @tomorrow = Story.find(:first, :conditions => [''rundate =?'', > Date.today+1]) > endQuoting from Rob Biedenharn''s earlier post:> Date#wday gives the "weekday" number: 0=Sunday, 6=SaturdaySo the test for friday is if Date.today.wday == 5 Don''t forget to allow for Saturday in your code though. As it stands it will set @tomorrow to Sunday if today is Saturday. I would advise against calling it @tomorrow if it is not tomorrow, it will confuse someone sometime. Something like @next_working_day might be better. Colin> > Thanks!! > > On Jun 19, 2:11 am, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> Here -http://blog.redfin.com/devblog/2007/08/getting_the_time_zone_from_a_w... >> - is a way (allegedly) of getting the user''s timezone using >> javascript, which could then be passed to the server. I cannot vouch >> for whether it works or not. >> >> Colin >> >> 2009/6/19 Rick DeNatale <rick.denat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: >> >> >> >> >> >> > On Thu, Jun 18, 2009 at 12:38 PM, Colin Law<clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> >> >> 2009/6/18 Rick DeNatale <rick.denat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: >> >> >>> You could use >> >> >>> Time.zone = current_user.timezone # which would probably be in a before_filter >> >> >> How does current_user.timezone work? >> >> > Well there''s a little handwaving here. The assumption is that >> > current_user refers to a user model instance set up by >> > restful_authentication or some other login code, and that the model >> > contains the user''s preferred timezone probably set by a profile ui. >> >> > -- >> > Rick DeNatale >> >> > Blog:http://talklikeaduck.denhaven2.com/ >> > Twitter:http://twitter.com/RickDeNatale >> > WWR:http://www.workingwithrails.com/person/9021-rick-denatale >> > LinkedIn:http://www.linkedin.com/in/rickdenatale > > >