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

Commit

Permalink
adding total score to stamp
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanb committed Aug 22, 2009
1 parent 0692c9b commit fb9268f
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 6 deletions.
8 changes: 8 additions & 0 deletions app/models/mark.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
class Mark < ActiveRecord::Base
attr_accessible :skip, :marked_on, :position_x, :position_y
belongs_to :stamp
after_save :reset_score
after_destroy :reset_score

private

def reset_score
stamp.update_attribute(:score_cache, nil) if stamp
end
end
17 changes: 16 additions & 1 deletion app/models/stamp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def month_points(date)
end
memoize :month_points

def score
score_cache || calculate_score
end

private

def mark_on_day(date)
Expand All @@ -40,7 +44,9 @@ def track_month_points(tracker, date)
0
elsif mark_on_day(day)
finished = true if mark_on_day(day) == last_mark
mark_on_day(day).skip? ? tracker.skip : tracker.mark
points = mark_on_day(day).skip? ? tracker.skip : tracker.mark
update_attribute(:score_cache, tracker.score) if finished
points
else
tracker.miss
end
Expand All @@ -56,4 +62,13 @@ def last_month_tracker(date)
def last_mark
@last_mark ||= marks.last(:order => "marked_on")
end

def calculate_score
if last_mark
month_points(last_mark.marked_on)
score_cache
else
0
end
end
end
3 changes: 2 additions & 1 deletion app/views/marks/_reset_points.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<% @mark.stamp.month_points(@mark.marked_on).each_with_index do |day_points, index| %>
$("#day_<%= @mark.marked_on.strftime("%Y%m#{(index+1).to_s.rjust(2, '0')}") %> .points").html("<%= escape_javascript points(day_points) %>");
<% end %>
<% end %>
<% end %>
$("#score_value").html("<%= @mark.stamp.score %>");
5 changes: 5 additions & 0 deletions app/views/stamps/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@
<%=h @stamp.private %>
</p>


<p>
<%= link_to "Edit", edit_stamp_path(@stamp) %> |
<%= link_to "Destroy", @stamp, :confirm => 'Are you sure?', :method => :delete %> |
<%= link_to "View All", stamps_path %>
</p>

<div id="score">
Score: <span id="score_value"><%=h @stamp.score %></span>
</div>

<p id="stamper"><%= link_to image_tag("stamper.png", :size => "65x65"), "#stamptoday" %></p>

<div id="calendar">
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20090822161359_add_score_cache_to_stamps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddScoreCacheToStamps < ActiveRecord::Migration
def self.up
add_column :stamps, :score_cache, :integer
end

def self.down
remove_column :stamps, :score_cache
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20090822040759) do
ActiveRecord::Schema.define(:version => 20090822161359) do

create_table "marks", :force => true do |t|
t.integer "stamp_id"
Expand All @@ -27,6 +27,7 @@
t.boolean "private"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "score_cache"
end

create_table "users", :force => true do |t|
Expand Down
7 changes: 6 additions & 1 deletion public/stylesheets/calendar.css
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@

#calendar .points .negative {
color: #D00;
}
}

#score {
font-size: 30px;
font-weight: bold;
}
15 changes: 13 additions & 2 deletions spec/models/mark_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe Mark do
it "should be valid" do
Mark.new.should be_valid
it "should clear score cache of stamp when creating" do
stamp = Stamp.create!
stamp.update_attribute(:score_cache, 123)
stamp.marks.create!(:marked_on => Date.today)
stamp.reload.score_cache.should be_nil
end

it "should clear score cache of stamp when destroying" do
stamp = Stamp.create!
stamp.marks.create!(:marked_on => Date.today)
stamp.update_attribute(:score_cache, 123)
stamp.marks.first.destroy
stamp.reload.score_cache.should be_nil
end
end
18 changes: 18 additions & 0 deletions spec/models/stamp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,22 @@
@stamp.month_points(Date.new(2009, 4)).should == [0]*30
end
end

it "should use score cache if there is one" do
stamp = Stamp.new
stamp.score_cache = 123
stamp.score.should == 123
end

it "should calculate score and set cache" do
stamp = Stamp.create!
stamp.marks.create!(:marked_on => "2009-01-01")
stamp.marks.create!(:marked_on => "2009-01-02")
stamp.score.should == 3
stamp.reload.score_cache.should == 3
end

it "should have zero score if no marks" do
Stamp.new.score.should be_zero
end
end

0 comments on commit fb9268f

Please sign in to comment.