Skip to content

Commit

Permalink
Implemented Page#dates_in_range method. All green.
Browse files Browse the repository at this point in the history
  • Loading branch information
nelstrom committed Jun 7, 2009
1 parent e892e29 commit 34608a3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 22 deletions.
37 changes: 33 additions & 4 deletions lib/page_event/page_extensions.rb
Expand Up @@ -30,7 +30,7 @@ def events_by_month(date = Time.now, status = nil)
def event_count_by_month(date = Time.now)
month_start = date.at_beginning_of_month
condition_str = "(event_datetime_start >= :month_start AND event_datetime_start < :month_end)"
condition_str << " OR (event_datetime_end >= :month_start AND event_datetime_end < :month_end)"
condition_str << " OR (event_datetime_end >= :month_start AND event_datetime_end < :month_end)"
Page.count(:conditions => [condition_str,
{
:month_start => month_start,
Expand All @@ -54,13 +54,36 @@ def upcoming_events(limit = 3)


def events_in_range(start=nil, finish=nil)
[]
begin
if start and finish
start_date = Time.local(*parse_date_string(start))
end_date = Time.local(*parse_date_string(finish))
if end_date < start_date
start_date, end_date = end_date, start_date
end
end_date = end_date.tomorrow
elsif start
year, month, day = parse_date_string(start)
start_date = Time.local(year, month, day)
end_date = case
when (!year.nil? and !month.nil? and !day.nil?) then start_date.tomorrow
when (!year.nil? and !month.nil?) then start_date.next_month
when (!year.nil?) then start_date.next_year
end
else
raise ArgumentError, "You must provide at least one date, in 'yyyy(/mm(/dd))' format"
end
rescue
raise ArgumentError, "Dates should be provided in 'yyyy(/mm(/dd))' format."
end
condition_str = "(event_datetime_start >= :start_date AND event_datetime_start < :end_date)"
condition_str << " OR (event_datetime_end >= :start_date AND event_datetime_end < :end_date)"
conditions = [condition_str, {:start_date => start_date, :end_date => end_date}]
Page.find(:all, :conditions => conditions)
end





def next_eventful_month(date=Time.now)
last_day_of_month = Date.new(date.year, date.month, -1)

Expand All @@ -87,5 +110,11 @@ def last_eventful_month(date=Time.now)
end
end

private

def parse_date_string(input)
input.split(/\/|-/).map{|n| n.to_i}
end

end
end
35 changes: 17 additions & 18 deletions spec/models/page_extensions_spec.rb
Expand Up @@ -70,35 +70,34 @@
describe "#events_in_range" do

it "should find all events in a given year" do
Page.events_in_range("2009").should include( pages(:march_first, :march_tenth, :april_first, :august_sixth, :next_christmas) )
Page.events_in_range("2009").
should == pages(:march_first, :march_tenth, :april_first, :august_sixth, :next_christmas)
end

it "should find only events in a given year" do
Page.events_in_range("2009").should_not include( pages(:last_christmas) )
end

it "should find all events in a given month" do
Page.events_in_range("2009/03").should include( pages(:march_first, :march_tenth) )
Page.events_in_range("2009/04").should include( pages(:april_first) )
Page.events_in_range("2009/08").should include( pages(:august_sixth) )
end

it "should find only events of a given month" do
Page.events_in_range("2009/03").should_not include( pages(:april_first, :august_sixth) )
Page.events_in_range("2009/04").should_not include( pages(:august_sixth) )
Page.events_in_range("2009/08").should_not include( pages(:april_first) )
Page.events_in_range("2009/03").should == pages(:march_first, :march_tenth)
Page.events_in_range("2009/04").should == [pages(:april_first)]
Page.events_in_range("2009/08").should == [pages(:august_sixth)]
end

it "should find events on a given day" do
Page.events_in_range("2009/04/01").should == [pages(:april_first)]
end

it "should find only events on a given day" do
Page.events_in_range("2009/04/01").should_not == [pages(:march_first)]
it "should find all events within a given range, inclusive" do
Page.events_in_range("2009/03/01","2009/03/10").should == pages(:march_first, :march_tenth)
end

it "should find all events within a given range" do
Page.events_in_range("2009/03/01","2009/03/10").should == [pages(:march_first, :march_tenth)]
it "should find all events within a given range, even if start/end dates are in wrong order" do
Page.events_in_range("2009/03/10","2009/03/01").should == pages(:march_first, :march_tenth)
end

it "should raise an exception when given an invalid date" do
lambda { Page.events_in_range("3009/04/01") }.should raise_error(ArgumentError)
end

it "should raise an exception when given no arguments" do
lambda { Page.events_in_range() }.should raise_error(ArgumentError)
end

end
Expand Down

0 comments on commit 34608a3

Please sign in to comment.