Browse files

adding goal system for each stamp - closes #13

  • Loading branch information...
1 parent 0c89d02 commit d9d635a216f6668a0a1cab5b3238e26458a38935 @ryanb committed Sep 5, 2009
View
6 app/controllers/stamps_controller.rb
@@ -1,5 +1,5 @@
class StampsController < ApplicationController
- before_filter :login_required, :only => [:new, :edit, :update, :destroy]
+ before_filter :login_required, :only => [:new, :edit, :edit_goal, :update, :destroy]
def index
if current_user && current_user.current_stamp
@@ -43,6 +43,10 @@ def edit
@stamp = current_user.stamps.find(params[:id])
end
+ def edit_goal
+ @stamp = current_user.stamps.find(params[:id])
+ end
+
def update
@stamp = current_user.stamps.find(params[:id])
if @stamp.update_attributes(params[:stamp])
View
22 app/models/stamp.rb
@@ -6,12 +6,14 @@ class Stamp < ActiveRecord::Base
has_many :marks, :dependent => :destroy
has_many :month_caches, :dependent => :destroy
- attr_accessible :name, :private, :color, :stamp_image_id
+ attr_accessible :name, :private, :color, :stamp_image_id, :goal_score, :goal_reward
validates_presence_of :name
named_scope :non_private, :conditions => ["private != ?", true]
named_scope :recent, :conditions => "score_cache > 0", :order => "updated_at desc"
+ before_create :default_goal
+
def day_points(date)
month_points(date.beginning_of_month)[date.day-1]
end
@@ -30,6 +32,20 @@ def color
read_attribute(:color).blank? ? "red" : read_attribute(:color)
end
+ def goal_progress
+ if goal_reached?
+ 100
+ elsif goal_score.nil? || goal_score.zero?
+ 0
+ else
+ (score.to_f/goal_score*100).floor
+ end
+ end
+
+ def goal_reached?
+ score >= (goal_score || 0)
+ end
+
private
def mark_on_day(date)
@@ -83,4 +99,8 @@ def calculate_score
0
end
end
+
+ def default_goal
+ self.goal_score ||= 100
+ end
end
View
2 app/views/marks/_reset_points.js.erb
@@ -3,4 +3,4 @@
$("#day_<%= @mark.marked_on.strftime("%Y%m#{(index+1).to_s.rjust(2, '0')}") %> .points").html("<%= escape_javascript points(day_points) %>");
<% end %>
<% end %>
-$("#score_value").html("<%= @mark.stamp.score %>");
+$("#score").html("<%= escape_javascript render(:partial => "stamps/score", :locals => { :stamp => @mark.stamp }) %>");
View
11 app/views/stamps/_score.html.erb
@@ -0,0 +1,11 @@
+<div id="score_bar" style="background-position: <%= -200 + (stamp.goal_progress*2) %>px 0">score <%=h stamp.score %></div>
+<div id="score_goal">
+ <%= stamp.goal_score %>
+ <span id="change_goal">(<%= link_to "change goal", edit_goal_stamp_path(stamp) %>)</span>
+</div>
+<% if stamp.goal_reached? && !stamp.goal_reward.empty? %>
+ <div id="goal_reward">
+ <strong>Reward:</strong>
+ <%=h stamp.goal_reward %>
+ </div>
+<% end %>
View
26 app/views/stamps/edit_goal.html.erb
@@ -0,0 +1,26 @@
+<% title "Change Goal" %>
+<% stylesheet "generic", "edit_goal" %>
+
+<% form_for @stamp do |f| %>
+ <div id="suggestions">
+ <h3>suggestions</h3>
+ <ul>
+ <li>buy a new video game</li>
+ <li>get a manicure</li>
+ <li>go to the movies</li>
+ <li>spend $50 on myself</li>
+ <li>go out to eat</li>
+ <li>take a vacation</li>
+ </ul>
+ </div>
+ <p>
+ <%= label_tag :goal_score, "Target Score" %><br />
+ <%= f.text_field :goal_score, :size => 10 %>
+ </p>
+ <p>
+ <%= label_tag :goal_reward, "Reward Yourself" %><br />
+ <%= f.text_field :goal_reward, :size => 45 %>
+ </p>
+ <p><%= f.submit "Submit" %> or <%= link_to "cancel", @stamp %></p>
+ <div class="clear"></div>
+<% end %>
View
4 app/views/stamps/show.html.erb
@@ -44,7 +44,9 @@
<% else %>
<p>Stamp by <%=h stamp.user.display_name if stamp.user %></p>
<% end %>
- <p id="score">Score: <span id="score_value"><%=h @stamp.score %></span></p>
+ <div id="score">
+ <%= render :partial => "score", :locals => { :stamp => @stamp } %>
+ </div>
<% if stamp_owner? && stamp.score.zero? %>
<%= image_tag "instructions/instruction1.gif", :size => "135x59", :id => "instructions" %>
<% end %>
View
11 config/routes.rb
@@ -1,16 +1,13 @@
ActionController::Routing::Routes.draw do |map|
- map.resources :stamp_images
-
- map.resources :marks
-
- map.resources :stamps
-
map.signup 'signup', :controller => 'users', :action => 'new'
map.logout 'logout', :controller => 'user_sessions', :action => 'destroy'
map.login 'login', :controller => 'user_sessions', :action => 'new'
+
map.resources :user_sessions
-
map.resources :users
+ map.resources :stamp_images
+ map.resources :marks
+ map.resources :stamps, :member => { :edit_goal => :get }
map.root :stamps
end
View
12 db/migrate/20090905213233_add_goal_to_stamps.rb
@@ -0,0 +1,12 @@
+class AddGoalToStamps < ActiveRecord::Migration
+ def self.up
+ add_column :stamps, :goal_score, :integer
+ add_column :stamps, :goal_reward, :text
+ Stamp.update_all("goal_score = 100")
+ end
+
+ def self.down
+ remove_column :stamps, :goal_reward
+ remove_column :stamps, :goal_score
+ end
+end
View
4 db/schema.rb
@@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20090823183047) do
+ActiveRecord::Schema.define(:version => 20090905213233) do
create_table "delayed_jobs", :force => true do |t|
t.integer "priority", :default => 0
@@ -80,6 +80,8 @@
t.integer "score_cache"
t.string "color"
t.integer "stamp_image_id"
+ t.integer "goal_score"
+ t.text "goal_reward"
end
create_table "users", :force => true do |t|
View
BIN public/images/greenbar.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
22 public/stylesheets/edit_goal.css
@@ -0,0 +1,22 @@
+#suggestions {
+ float: right;
+ color: #555;
+}
+
+#suggestions h3 {
+ font-size: 16px;
+ font-weight: bold;
+ margin-top: 0;
+ margin-bottom: 3px;
+}
+
+#suggestions ul {
+ margin: 0;
+ padding: 0;
+ list-style: none;
+}
+
+#suggestions li {
+ margin: 0;
+ padding: 2px 0;
+}
View
33 public/stylesheets/stamp.css
@@ -133,15 +133,40 @@ h1 {
}
#score {
- font-size: 20px;
- font-weight: bold;
padding: 10px 0;
+ font-weight: bold;
+}
+
+#score_bar {
+ width: 200px;
+ border: solid 1px #777;
+ background-color: #D9D9D9;
+ text-align: center;
+ padding: 2px 0;
+ float: left;
+ -moz-border-radius: 5px;
+ -webkit-border-radius: 5px;
+ background-image: url(/images/greenbar.png);
+ background-repeat: no-repeat;
+ background-position: -200px;
+}
+
+#score_goal {
+ margin-left: 210px;
+ padding-top: 3px;
+ padding-bottom: 7px;
+}
+
+#change_goal {
+ font-weight: normal;
+ font-size: 11px;
}
#stamp_details p {
margin: 0;
}
-#stamp_details a {
- font-size: 14px;
+#goal_reward {
+ font-size: 11px;
+ font-weight: normal;
}
View
10 spec/controllers/stamps_controller_spec.rb
@@ -37,6 +37,11 @@
response.should redirect_to(login_path)
end
+ it "edit_goal action should redirect to login" do
+ get :edit_goal, :id => Stamp.first
+ response.should redirect_to(login_path)
+ end
+
it "update action should redirect to login" do
Stamp.any_instance.stubs(:valid?).returns(false)
put :update, :id => Stamp.first
@@ -84,6 +89,11 @@
response.should render_template(:edit)
end
+ it "edit_goal action should render edit_goal template" do
+ get :edit_goal, :id => Stamp.first
+ response.should render_template(:edit_goal)
+ end
+
it "update action should render edit template when model is invalid" do
Stamp.any_instance.stubs(:valid?).returns(false)
put :update, :id => Stamp.first
View
12 spec/models/stamp_spec.rb
@@ -84,4 +84,16 @@
it "should default color to 'red'" do
Stamp.new.color.should == "red"
end
+
+ it "should default goal_score to 100 when not set" do
+ Factory(:stamp, :goal_score => "").goal_score.should == 100
+ end
+
+ it "should have a goal progress as percentage" do
+ Factory(:stamp, :score_cache => 5, :goal_score => 10).goal_progress.should == 50
+ end
+
+ it "should not have goal progress go above 100" do
+ Factory(:stamp, :score_cache => 15, :goal_score => 10).goal_progress.should == 100
+ end
end

0 comments on commit d9d635a

Please sign in to comment.