Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
adding special skip mark
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanb committed Aug 22, 2009
1 parent b9b7701 commit 518f011
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/controllers/marks_controller.rb
@@ -1,7 +1,7 @@
class MarksController < ApplicationController
def create
@stamp = Stamp.find(params[:stamp_id])
@mark = @stamp.marks.create!(:marked_on => params[:date], :position_x => params[:x], :position_y => params[:y])
@mark = @stamp.marks.create!(:marked_on => params[:date], :position_x => params[:x], :position_y => params[:y], :skip => (params[:skip] == "true"))
respond_to do |format|
format.html { redirect_to root_url }
format.js
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/application_helper.rb
Expand Up @@ -5,7 +5,7 @@ def mark_image(mark, size = 65)
if mark.position_x && mark.position_y
options[:style] = "margin-left: #{mark.position_x-size/2-6}px; margin-top: #{mark.position_y-size/2-6}px;"
end
image_tag "mark.png", options
image_tag (mark.skip? ? "skip.png" : "mark.png"), options
end

def points(num)
Expand Down
5 changes: 5 additions & 0 deletions app/models/score_tracker.rb
Expand Up @@ -19,6 +19,11 @@ def mark
@positive_points
end

def skip
# keep scores as they are
0
end

def miss
@position = 0 if @negative_points.zero?
if @position == @negative_points
Expand Down
12 changes: 10 additions & 2 deletions app/models/stamp.rb
Expand Up @@ -12,7 +12,11 @@ def day_points(date)
def month_points(date)
@tracker = last_month_tracker(date) || ScoreTracker.new
(date.beginning_of_month..date.end_of_month).map do |day|
mark_on_day(day) ? @tracker.mark : @tracker.miss
if mark_on_day(day)
mark_on_day(day).skip? ? @tracker.skip : @tracker.mark
else
@tracker.miss
end
end
end
memoize :month_points
Expand All @@ -32,7 +36,11 @@ def marks_in_month(date)
def month_tracker(date)
@tracker = last_month_tracker(date) || ScoreTracker.new
(date.beginning_of_month..date.end_of_month).each do |day|
mark_on_day(day) ? @tracker.mark : @tracker.miss
if mark_on_day(day)
mark_on_day(day).skip? ? @tracker.skip : @tracker.mark
else
@tracker.miss
end
end
@tracker
end
Expand Down
Binary file added public/images/skip.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion public/javascripts/stamp.js
Expand Up @@ -14,7 +14,7 @@ $(function() {
var p = $(this).position();
var x = (event.pageX - p.left);
var y = (event.pageY - p.top);
$.post($(this).children("a.mark_link").attr("href"), { x: x, y: y}, null, "script");
$.post($(this).children("a.mark_link").attr("href"), { x: x, y: y, skip: "true" }, null, "script");
}
}
return false;
Expand Down
7 changes: 7 additions & 0 deletions spec/controllers/marks_controller_spec.rb
Expand Up @@ -14,6 +14,13 @@
mark.position_y.should == 456
end

it "create action should create mark with skip" do
post :create, :stamp_id => Stamp.first.id, :date => "2009-02-01", :skip => "true"
response.should redirect_to(root_url)
mark = Mark.last
mark.skip.should be_true
end

it "create action should render template when js action" do
post :create, :format => "js", :stamp_id => Stamp.first.id, :date => "2009-02-01"
response.should render_template("create")
Expand Down
4 changes: 4 additions & 0 deletions spec/models/score_tracker_spec.rb
Expand Up @@ -42,4 +42,8 @@
@tracker.miss, @tracker.miss, @tracker.miss, @tracker.miss, @tracker.mark].should ==
[1, 2, 2, -1, 2, 2, -1, -2, -2, -3, 1]
end

it "skip should continue on as if nothing happened" do
[@tracker.mark, @tracker.skip, @tracker.skip, @tracker.mark].should == [1, 0, 0, 2]
end
end
7 changes: 7 additions & 0 deletions spec/models/stamp_spec.rb
Expand Up @@ -33,4 +33,11 @@
stamp.marks.create!(:marked_on => "2009-04-01")
stamp.day_points(Date.new(2009, 4, 2)).should == -1
end

it "month points should be 1 on first mark but -1 on next miss" do
stamp = Stamp.create!
stamp.marks.create!(:marked_on => "2009-04-01")
stamp.marks.create!(:marked_on => "2009-04-02", :skip => true)
stamp.month_points(Date.new(2009, 4)).should == [1, 0, -1] + [0]*27
end
end

0 comments on commit 518f011

Please sign in to comment.