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

Commit

Permalink
adding watched stamps feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanb committed Sep 12, 2009
1 parent 6d26f12 commit 39cfd26
Show file tree
Hide file tree
Showing 18 changed files with 219 additions and 10 deletions.
23 changes: 23 additions & 0 deletions 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
10 changes: 6 additions & 4 deletions 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)
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/favorites_helper.rb
@@ -0,0 +1,2 @@
module FavoritesHelper
end
6 changes: 6 additions & 0 deletions 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
2 changes: 2 additions & 0 deletions app/models/user.rb
Expand Up @@ -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?
Expand Down
25 changes: 25 additions & 0 deletions 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 %>
1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Expand Up @@ -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>
Expand Down
4 changes: 2 additions & 2 deletions 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>
Expand Down
7 changes: 6 additions & 1 deletion app/views/stamps/show.html.erb
Expand Up @@ -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 } %>
Expand Down
2 changes: 2 additions & 0 deletions 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'
Expand Down
13 changes: 13 additions & 0 deletions 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
9 changes: 8 additions & 1 deletion db/schema.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
58 changes: 58 additions & 0 deletions 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;
}
4 changes: 2 additions & 2 deletions public/stylesheets/stamp.css
Expand Up @@ -137,7 +137,7 @@ h1 {
font-weight: bold;
}

#score_bar {
#score .score_bar {
width: 200px;
border: solid 1px #777;
background-color: #D9D9D9;
Expand All @@ -151,7 +151,7 @@ h1 {
background-position: -200px;
}

#score_goal {
#score .score_goal {
margin-left: 210px;
padding-top: 3px;
padding-bottom: 7px;
Expand Down
49 changes: 49 additions & 0 deletions 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

3 changes: 3 additions & 0 deletions 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)
Expand Down
7 changes: 7 additions & 0 deletions spec/fixtures/favorites.yml
@@ -0,0 +1,7 @@
one:
user: one
stamp: one

two:
user: two
stamp: two
4 changes: 4 additions & 0 deletions 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.