Topic: [SOLVED] need week_of_month but cannot find

Hi,
I'm programming my first rails Project for a kitchen that makes meals for smaller companys.
They also need a menue for the school which they are serving soon.
I've gone really far with the help of this forum but now I'm stucked.
The menue for the school should be a whole month on a sheet, the logo and two weeks on page one and the three other weeks on page 2.

The meals are save daily.
My current try is to pass through all meals in the month displaying them and separating the sheets when day is monday and week is 3rd.
I'm now using the workaround with (d.day+4)/7).ceil .

I've slowly accustomed to the "It cannot be that easy"-Experience (search 2 hours for solution, make 10-lines workaround, get enlightened 3 days later on searching in another problem).
So there must be also a easier solution for that problem like Date.week_of_month (tried but didnt work).

Can U help out?

Much thx in advise!

Last edited by eisfuerst (2009-10-07 05:15:56)

Re: [SOLVED] need week_of_month but cannot find

eisfuerst wrote:

Hi,
I'm programming my first rails Project for a kitchen that makes meals for smaller companys.
They also need a menue for the school which they are serving soon.
I've gone really far with the help of this forum but now I'm stucked.
The menue for the school should be a whole month on a sheet, the logo and two weeks on page one and the three other weeks on page 2.

The meals are save daily.
My current try is to pass through all meals in the month displaying them and separating the sheets when day is monday and week is 3rd.
I'm now using the workaround with (d.day+4)/7).ceil .

I've slowly accustomed to the "It cannot be that easy"-Experience (search 2 hours for solution, make 10-lines workaround, get enlightened 3 days later on searching in another problem).
So there must be also a easier solution for that problem like Date.week_of_month (tried but didnt work).

Can U help out?

Much thx in advise!

Ruby has lots of date methods that can help you out
http://rubybrain.com/api/ruby-1.8.7/doc … ;name=Date

and rails adds more
http://www.railsbrain.com/api/rails-2.2 … lculations

For example the Date#cweek method gives you the week of the year, and rolls over on mondays.  So you could get the week of the month like

Date.today.cweek - Date.today.at_beginning_of_month.cweek

This will give you the number of weeks (where a week starts on monday) since the start of the month (starting on 0, so today it would be 1, since we're in the second week of the month).  If you want you could add 1 to this to make it so that the first week of the month has the number 1 instead of 0.

You could add this as a method of the meal model, like so: (i'm assuming that meals have a method called 'date')

def in_first_half_of_month?
  (self.date.cweek - self.date.at_beginning_of_month.cweek) < 3 #maybe this should be < 2 ?
end

Then on the page, or in the controller, you can split your meals up based on this method
@page_1_meals = @meals.select(&:in_first_half_of_month?)
@page_2_meals = @meals - @page_1_meals

Last edited by Max Williams (2009-08-04 08:54:14)

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: [SOLVED] need week_of_month but cannot find

Thx, great idea...
I decided to make an array[week][day_of_week] for better use in different views and with this to include a "week_of_month"-function directly in the model.

Btw, i have a value "day" in my school_menue, but i dont know what i should rather use as column name... "day" or "date".

And do you have a tip where to find a good tutorial how to generally add a "Date::week_of_month" to my application?

thx

Re: [SOLVED] need week_of_month but cannot find

eisfuerst wrote:

Thx, great idea...
I decided to make an array[week][day_of_week] for better use in different views and with this to include a "week_of_month"-function directly in the model.

Btw, i have a value "day" in my school_menue, but i dont know what i should rather use as column name... "day" or "date".

And do you have a tip where to find a good tutorial how to generally add a "Date::week_of_month" to my application?

thx

I would avoid 2-d arrays if i were you, you'll get yourself tied up in knots real quickly.  What's wrong with object for rows and attributes/methods for columns?

wrt to adding a week_of_month method to Date, the following is a simple and clean way you can monkey patch* date in your app.

in your lib folder, make a file called core_extensions.rb:

#lib/core_extensions.rb
class Date
  def week_of_month
    self.cweek - self.at_beginning_of_month.cweek
  end
end

now you just need to require this in the place where you do any other requires for your app - in a file in config/initializers, or in your environment.rb file:

require 'core_extensions'

*monkey patching is when you dynamically add methods to existing classes from outside, usually at load time.  It's a bit frowned upon but generally if you add new methods, rather than override existing ones, it's ok.

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: [SOLVED] need week_of_month but cannot find

eisfuerst wrote:

Thx, great idea...
I decided to make an array[week][day_of_week] for better use in different views and with this to include a "week_of_month"-function directly in the model.

Btw, i have a value "day" in my school_menue, but i dont know what i should rather use as column name... "day" or "date".

And do you have a tip where to find a good tutorial how to generally add a "Date::week_of_month" to my application?

thx

I would avoid 2-d arrays if i were you, you'll get yourself tied up in knots real quickly.  What's wrong with object for rows and attributes/methods for columns?

wrt to adding a week_of_month method to Date, the following is a simple and clean way you can monkey patch* date in your app.

in your lib folder, make a file called core_extensions.rb:

#lib/core_extensions.rb
class Date
  def week_of_month
    self.cweek - self.at_beginning_of_month.cweek
  end
end

now you just need to require this in the place where you do any other requires for your app - in a file in config/initializers, or in your environment.rb file:

require 'core_extensions'

*monkey patching is when you dynamically add methods to existing classes from outside, usually at load time.  It's a bit frowned upon but generally if you add new methods, rather than override existing ones, it's ok.

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: [SOLVED] need week_of_month but cannot find

use
  gem 'week_of_month'

  date = Date.new(2012,1, 1)
  date.week_of_month
  => 1