Skip to content

Instantly share code, notes, and snippets.

@Oshuma
Created December 31, 2008 20:13
Show Gist options
  • Select an option

  • Save Oshuma/42109 to your computer and use it in GitHub Desktop.

Select an option

Save Oshuma/42109 to your computer and use it in GitHub Desktop.
# in the model:
def self.all_by_date(year, month, order = [:created_at.desc])
year = year.to_i
start_month = month ? month.to_i : 1
start_day = 1
start_date = Date.new(year, start_month, start_day)
end_month = month ? month.to_i : 12
end_day = days_in_month(year, end_month)
end_date = Date.new(year, end_month, end_day)
all(:created_at => start_date..end_date, :order => order)
end
def self.days_in_month(year, month)
(Date.new(year, 12, 31) << (12 - month)).day
end
# the failing spec:
# @attrs = {:user => User.first, :title => 'New Article', :content => 'Some content.'}
it 'should find all articles on the given date' do
year = Time.now.year
month = Time.now.month
day = Time.now.day
Article.new(@attrs).save
@article = Article.first
@article.update_attributes(:created_at => "#{year}-#{month}-#{day}")
@articles = Article.all_by_date(year, month)
@articles.should_not be_empty
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment