Skip to content

Commit

Permalink
Fixed wrong date in cyclic goals [#3 state:resolved]
Browse files Browse the repository at this point in the history
  • Loading branch information
sejtenik committed Apr 1, 2009
1 parent 3ee533c commit 181cf11
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 22 deletions.
4 changes: 2 additions & 2 deletions app/models/goal.rb
Expand Up @@ -169,8 +169,8 @@ def create_next_goal_in_cycle
throw "Period type mismatch: #{period_type}" if period_type == :SELECTED
throw 'Goal is not cyclic' unless is_cyclic
new_goal = self.clone
new_goal.period_start = self.period_start.shift(Date::period_category(period_type))
new_goal.period_end = self.period_end.shift(Date::period_category(period_type))
new_goal.period_start = self.period_end + 1 #self.period_start.shift(Date::period_category(period_type))
new_goal.period_end = new_goal.period_start.shift(Date::period_category(period_type))
same_goal = Goal.first :conditions => ['period_start = ? AND period_end = ? AND cycle_group = ?', new_goal.period_start, new_goal.period_end, cycle_group]
throw 'There is already goal in database' unless same_goal.nil?
return new_goal
Expand Down
20 changes: 10 additions & 10 deletions lib/date_extensions.rb
Expand Up @@ -101,16 +101,16 @@ def last_week

def shift(period_category)
case period_category
when :DAY then self.advance(:days => 1)
when :WEEK then self.advance(:weeks => 1)
when :MONTH then self.advance(:months => 1)
when :QUARTER then self.advance(:quarters => 1)
when :YEAR then self.advance(:years => 1)
when :A_7_DAYS then self.advance(:days => 7)
when :A_4_WEEKS then self.advance(:weeks => 4)
when :A_3_MONTHS then self.advance(:months => 3)
when :A_90_DAYS then self.advance(:days => 90)
when :A_12_MONTHS then self.advance(:months => 12)
when :DAY then self.advance(:days => 1)-1
when :WEEK then self.advance(:weeks => 1)-1
when :MONTH then self.advance(:months => 1)-1
when :QUARTER then self.advance(:quarters => 1)-1
when :YEAR then self.advance(:years => 1)-1
when :A_7_DAYS then self.advance(:days => 7)-1
when :A_4_WEEKS then self.advance(:weeks => 4)-1
when :A_3_MONTHS then self.advance(:months => 3)-1
when :A_90_DAYS then self.advance(:days => 90)-1
when :A_12_MONTHS then self.advance(:months => 12)-1
else
raise "Unrecognized period symbol: #{period_category}"
end
Expand Down
20 changes: 10 additions & 10 deletions test/unit/date_test.rb
Expand Up @@ -258,16 +258,16 @@ def test_calculate
def test_shift
date = '16.07.2008'.to_date #sroda, 3 tydzień lipca, III kwartał, 29 tydzień 2008

assert_equal '17.07.2008'.to_date, date.shift(:DAY)
assert_equal '23.07.2008'.to_date, date.shift(:WEEK)
assert_equal '16.08.2008'.to_date, date.shift(:MONTH)
assert_equal '16.10.2008'.to_date, date.shift(:QUARTER)
assert_equal '16.07.2009'.to_date, date.shift(:YEAR)
assert_equal '23.07.2008'.to_date, date.shift(:A_7_DAYS)
assert_equal '13.08.2008'.to_date, date.shift(:A_4_WEEKS)
assert_equal '16.10.2008'.to_date, date.shift(:A_3_MONTHS)
assert_equal '14.10.2008'.to_date, date.shift(:A_90_DAYS)
assert_equal '16.07.2009'.to_date, date.shift(:A_12_MONTHS)
assert_equal '16.07.2008'.to_date, date.shift(:DAY)
assert_equal '22.07.2008'.to_date, date.shift(:WEEK)
assert_equal '15.08.2008'.to_date, date.shift(:MONTH)
assert_equal '15.10.2008'.to_date, date.shift(:QUARTER)
assert_equal '15.07.2009'.to_date, date.shift(:YEAR)
assert_equal '22.07.2008'.to_date, date.shift(:A_7_DAYS)
assert_equal '12.08.2008'.to_date, date.shift(:A_4_WEEKS)
assert_equal '15.10.2008'.to_date, date.shift(:A_3_MONTHS)
assert_equal '13.10.2008'.to_date, date.shift(:A_90_DAYS)
assert_equal '15.07.2009'.to_date, date.shift(:A_12_MONTHS)

end

Expand Down
19 changes: 19 additions & 0 deletions test/unit/goal_test.rb
Expand Up @@ -36,12 +36,31 @@ def test_create_next_goal_in_cycle
assert_raise RuntimeError, NameError do
g.create_next_goal_in_cycle
end
end

def test_next_goal_in_cycle_has_proper_date
g = create_goal
g.period_type = :NEXT_MONTH
g.period_start = '01.01.2008'.to_date
g.period_end = '31.01.2008'.to_date
g.is_cyclic = true
g.save!
new_g = g.create_next_goal_in_cycle
assert_not_nil new_g
new_g.save!
assert_equal '01.02.2008'.to_date, new_g.period_start
assert_equal '29.02.2008'.to_date, new_g.period_end

new_g = new_g.create_next_goal_in_cycle
assert_not_nil new_g
new_g.save!
assert_equal '01.03.2008'.to_date, new_g.period_start
assert_equal '31.03.2008'.to_date, new_g.period_end

end



def test_set_cycle_group_on_save
g = create_goal(false)
g.period_type = :NEXT_WEEK
Expand Down

0 comments on commit 181cf11

Please sign in to comment.