Browse files

adding watched stamps feature

  • Loading branch information...
1 parent 6d26f12 commit 39cfd26599a8b6f70891dd38311fdedb8fcb384b @ryanb committed Sep 11, 2009
View
23 app/controllers/favorites_controller.rb
@@ -0,0 +1,23 @@
+class FavoritesController < ApplicationController
+ before_filter :login_required
+
+ def index
+ @favorites = current_user.favorites
+ end
+
+ def create
+ @favorite = current_user.favorites.build(:stamp_id => params[:stamp_id])
+ if @favorite.save
+ flash[:notice] = "Started watching stamp."
+ else
+ flash[:error] = "Unable to watch this stamp."
+ end
+ redirect_to favorites_url
+ end
+
+ def destroy
+ @favorite = current_user.favorites.find(params[:id])
+ @favorite.destroy
+ redirect_to favorites_url
+ end
+end
View
10 app/helpers/application_helper.rb
@@ -1,11 +1,13 @@
# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
def mark_image(mark, size = 70)
- options = {:size => "#{size}x#{size}", :class => "mark"}
- 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;"
+ if mark
+ options = {:size => "#{size}x#{size}", :class => "mark"}
+ 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.skip? ? "skip.png" : (mark.image_path || "question_mark.png")), options
end
- image_tag (mark.skip? ? "skip.png" : (mark.image_path || "question_mark.png")), options
end
def points(num)
View
2 app/helpers/favorites_helper.rb
@@ -0,0 +1,2 @@
+module FavoritesHelper
+end
View
6 app/models/favorite.rb
@@ -0,0 +1,6 @@
+class Favorite < ActiveRecord::Base
+ attr_accessible :stamp_id
+ belongs_to :user
+ belongs_to :stamp
+ validates_presence_of :stamp_id
+end
View
2 app/models/user.rb
@@ -15,6 +15,8 @@ class User < ActiveRecord::Base
has_many :stamps
has_many :marks, :through => :stamps
has_many :stamp_images
+ has_many :favorites, :dependent => :destroy
+ has_many :favorite_stamps, :through => :favorites, :source => :stamp
belongs_to :current_stamp, :class_name => "Stamp"
def guest_or_openid?
View
25 app/views/favorites/index.html.erb
@@ -0,0 +1,25 @@
+<% title "Watched Stamps" %>
+<% stylesheet "favorites" %>
+
+<% if @favorites.empty? %>
+ <p>
+ You currently are not watching any stamps.<br />
+ Ask your friends to share their stamp URL with you.
+ </p>
+<% else %>
+ <% for favorite in @favorites %>
+ <div class="stamp">
+ <div class="score">
+ <%= render :partial => "stamps/score", :locals => { :stamp => favorite.stamp } %>
+ </div>
+ <div class="stamper"><%= link_to image_tag("stamper/#{h(favorite.stamp.color)}/small.png", :size => "40x52", :alt => h(favorite.stamp.name)), favorite.stamp %></div>
+ <h2><%=h favorite.stamp.name %></h2>
+ <p>
+ Stamp by <%=h favorite.stamp.user.display_name if favorite.stamp.user %> |
+ <%= link_to "View Details", favorite.stamp %> |
+ <%= link_to "Stop Watching", favorite, :method => :delete, :confirm => "Are you sure you want to stop watching this stamp?" %>
+ </p>
+ <div class="clear"></div>
+ </div>
+ <% end %>
+<% end %>
View
1 app/views/layouts/application.html.erb
@@ -21,6 +21,7 @@
<div class="message">Welcome <%=h current_user.username %>!</div>
<div class="links">
<%= link_to "Your Stamps", current_user.current_stamp %> |
+ <%= link_to "Watched Stamps", favorites_path %> |
<%= link_to "Update Profile", edit_user_path(:current) %> |
<%= link_to "Log out", logout_path %>
</div>
View
4 app/views/stamps/_score.html.erb
@@ -1,5 +1,5 @@
-<div id="score_bar" style="background-position: <%= -200 + (stamp.goal_progress*2) %>px 0">score <%=h stamp.score %></div>
-<div id="score_goal">
+<div class="score_bar" style="background-position: <%= -200 + (stamp.goal_progress*2) %>px 0">score <%=h stamp.score %></div>
+<div class="score_goal">
<%= stamp.goal_score %>
<% if stamp.user == current_user %>
<span id="change_goal">(<%= link_to "change goal", edit_goal_stamp_path(stamp) %>)</span>
View
7 app/views/stamps/show.html.erb
@@ -42,7 +42,12 @@
<%= link_to "Delete", @stamp, :method => :delete, :confirm => "Are you sure you want to delete this stamp? This operation cannot be undone." %>
</p>
<% else %>
- <p>Stamp by <%=h stamp.user.display_name if stamp.user %></p>
+ <p>
+ Stamp by <%=h stamp.user.display_name if stamp.user %>
+ <% if logged_in? %>
+ | <%= link_to "Watch this stamp", favorites_url(:stamp_id => stamp.id), :method => :post %>
+ <% end %>
+ </p>
<% end %>
<div id="score">
<%= render :partial => "score", :locals => { :stamp => @stamp } %>
View
2 config/routes.rb
@@ -1,4 +1,6 @@
ActionController::Routing::Routes.draw do |map|
+ map.resources :favorites
+
map.signup 'signup', :controller => 'users', :action => 'new'
map.logout 'logout', :controller => 'user_sessions', :action => 'destroy'
map.login 'login', :controller => 'user_sessions', :action => 'new'
View
13 db/migrate/20090912015155_create_favorites.rb
@@ -0,0 +1,13 @@
+class CreateFavorites < ActiveRecord::Migration
+ def self.up
+ create_table :favorites do |t|
+ t.integer :user_id
+ t.integer :stamp_id
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :favorites
+ end
+end
View
9 db/schema.rb
@@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20090905213233) do
+ActiveRecord::Schema.define(:version => 20090912015155) do
create_table "delayed_jobs", :force => true do |t|
t.integer "priority", :default => 0
@@ -24,6 +24,13 @@
t.datetime "updated_at"
end
+ create_table "favorites", :force => true do |t|
+ t.integer "user_id"
+ t.integer "stamp_id"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
create_table "marks", :force => true do |t|
t.integer "stamp_id"
t.boolean "skip", :default => false, :null => false
View
58 public/stylesheets/favorites.css
@@ -0,0 +1,58 @@
+.stamp {
+ border: solid 2px #CCC;
+ padding: 10px 20px;
+ -moz-border-radius: 10px;
+ -webkit-border-radius: 10px;
+ margin-bottom: 10px;
+}
+
+.stamp p {
+ margin: 0;
+}
+
+.stamp h2 {
+ margin: 0;
+ margin-bottom: 3px;
+}
+
+.stamper {
+ float: left;
+ padding-right: 15px;
+}
+
+.score {
+ padding-top: 15px;
+ font-weight: bold;
+ float: right;
+}
+
+.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;
+}
+
+.days {
+ border-collapse: collapse;
+ float: right;
+}
+
+.day {
+ border: solid 1px #000;
+ width: 30px;
+ height: 30px;
+}
View
4 public/stylesheets/stamp.css
@@ -137,7 +137,7 @@ h1 {
font-weight: bold;
}
-#score_bar {
+#score .score_bar {
width: 200px;
border: solid 1px #777;
background-color: #D9D9D9;
@@ -151,7 +151,7 @@ h1 {
background-position: -200px;
}
-#score_goal {
+#score .score_goal {
margin-left: 210px;
padding-top: 3px;
padding-bottom: 7px;
View
49 spec/controllers/favorites_controller_spec.rb
@@ -0,0 +1,49 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+
+describe FavoritesController, "as guest" do
+ fixtures :all
+ integrate_views
+
+ it "index action should redirect to login" do
+ get :index
+ response.should redirect_to(login_path)
+ end
+
+ it "create action should redirect to login" do
+ post :create
+ response.should redirect_to(login_path)
+ end
+
+ it "destroy action should redirect to login" do
+ delete :destroy, :id => Favorite.first
+ response.should redirect_to(login_path)
+ end
+end
+
+describe FavoritesController, "as owner" do
+ fixtures :all
+ integrate_views
+
+ before(:each) do
+ activate_authlogic
+ UserSession.create(Favorite.first.user)
+ end
+
+ it "index action should render index template" do
+ get :index
+ response.should render_template(:index)
+ end
+
+ it "create action should redirect to index" do
+ post :create, :stamp_id => Stamp.first
+ response.should redirect_to(favorites_url)
+ end
+
+ it "destroy action should destroy model and redirect to index action" do
+ favorite = Favorite.first
+ delete :destroy, :id => favorite
+ response.should redirect_to(favorites_url)
+ Favorite.exists?(favorite.id).should be_false
+ end
+end
+
View
3 spec/controllers/marks_controller_spec.rb
@@ -1,6 +1,9 @@
require File.dirname(__FILE__) + '/../spec_helper'
describe MarksController, "as guest" do
+ fixtures :all
+ integrate_views
+
it "create action should redirect to login" do
post :create
response.should redirect_to(login_path)
View
7 spec/fixtures/favorites.yml
@@ -0,0 +1,7 @@
+one:
+ user: one
+ stamp: one
+
+two:
+ user: two
+ stamp: two
View
4 spec/models/favorite_spec.rb
@@ -0,0 +1,4 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+
+describe Favorite do
+end

0 comments on commit 39cfd26

Please sign in to comment.