Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added ability to comment on games
  • Loading branch information
stevetuckner@stewdle.com committed Sep 19, 2011
1 parent ae2d008 commit 8503f90
Show file tree
Hide file tree
Showing 18 changed files with 158 additions and 11 deletions.
17 changes: 17 additions & 0 deletions app/controllers/comments_controller.rb
@@ -0,0 +1,17 @@
class CommentsController < ApplicationController
actions :create, :update
belongs_to :game

def create
@comment = Comment.new(params[:comment])
@comment.user = current_user
@comment.game = Game.find(params[:game_id])
puts "********** = #{request.host}"
puts "********** = #{request.port}"
puts "********** = #{request.protocol}"
UserMailer.comment_email(@comment, @comment.game.players_and_commenters - [@comment.user], request).deliver
create! do |format|
format.html {redirect_to game_path(@comment.game)}
end
end
end
2 changes: 2 additions & 0 deletions app/controllers/games_controller.rb
Expand Up @@ -2,6 +2,8 @@ class GamesController < ApplicationController
def show
show! do
@page_title = @game.description
@new_comment = Comment.new(:game => @game, :user => current_user)
@comments = @game.comments
end
end

Expand Down
7 changes: 7 additions & 0 deletions app/helpers/application_helper.rb
Expand Up @@ -50,5 +50,12 @@ def player_name(player)
"#{player.name} (OUT)"
end
end

def image_url(source, request)
rel_path = image_path(source)
abs_prefix = "#{request.protocol}#{request.host}"
abs_prefix << ":#{request.port}" if request.port != 80
"#{abs_prefix}/#{rel_path}"
end
end

11 changes: 11 additions & 0 deletions app/mailers/user_mailer.rb
@@ -0,0 +1,11 @@
class UserMailer < ActionMailer::Base
helper :application

def comment_email(comment, to, request)
@request = request
@comment = comment
mail( :from => "no-reply@chess-talk.com",
:to => to.map{|user| user.email},
:subject => "#{comment.user.name} has commented on the game #{comment.game.description}")
end
end
2 changes: 2 additions & 0 deletions app/models/comment.rb
@@ -1,4 +1,6 @@
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :game

default_scope :order => "id desc"
end
8 changes: 8 additions & 0 deletions app/models/game.rb
Expand Up @@ -105,6 +105,14 @@ def editable_by_user(user)
self.game_roles.map{|gr| gr.user}.include?(user) || user.admin?
end

def players_and_commenters
(self.players + self.comments(:include => :user).map{|c| c.user}).uniq
end

def players
self.users
end

private

def set_role(user_id, color)
Expand Down
3 changes: 3 additions & 0 deletions app/views/comments/_form.html.haml
@@ -0,0 +1,3 @@
= f.input :move, :as => :hidden
= f.input :text, :size => 5
= f.submit "Add Comment"
3 changes: 3 additions & 0 deletions app/views/comments/_new.html.haml
@@ -0,0 +1,3 @@
= semantic_form_for [game, comment] do |f|
= f.inputs do
= render :partial => "comments/form", :locals => {:f => f}
12 changes: 12 additions & 0 deletions app/views/comments/_show.html.haml
@@ -0,0 +1,12 @@
.comment
.medsmall-title Name
.medsmall-value= comment.user.name
/.medsmall-title When
/.medsmall-value= comment.datetime
- if comment.move
.medsmall-title Move
.medsmall-value= comment.move
.medsmall-title Comment
.medsmall-value= comment.text
.clear

30 changes: 29 additions & 1 deletion app/views/games/show.html.haml
Expand Up @@ -44,9 +44,37 @@
= "[Result \"#{@game.result}\"]"
= @game.pgn
%div{:id => "pgn_board"}
%section.comments
%h3
Comments for
%span#current-move all moves
.comment-links
%span#show-comment-form= link_to("New Comment", "#", :class => "button")
%span#hide-comment-form= link_to("Hide Comment", "#", :class => "button")
= render :partial => "comments/new", :locals => {:comment => @new_comment, :user => current_user, :game => @game}
- @comments.each do |comment|
= render :partial => "comments/show", :locals => {:comment => comment}
.clear
.clear
%script{:type => "text/javascript"}
:javascript
var brd = new Board('pgn', {'showMovesPane':true, 'movesPaneWidth':'100px'});
brd.init();

$(function() {
$("#new_comment").hide();
$("#hide-comment-form").hide();
$("#show-comment-form").click(function() {
$("#new_comment").show();
$("#hide-comment-form").show();
$(this).hide();
return false;
});
$("#hide-comment-form").click(function() {
$("#new_comment").hide();
$("#show-comment-form").show();
$(this).hide();
return false;
});
});
11 changes: 11 additions & 0 deletions app/views/user_mailer/comment_email.html.haml
@@ -0,0 +1,11 @@
%div{:style => "background-color: #FF0000; color: white;padding:20px;"}
%div{:style => "background-color: #DD0000;"}
%h1
%img{:src => image_url("logo-small.png", @request)}
Chess-Talk Comment
%div{:style => "background-color: #770000; padding:5px; border-radius:3px;"}
%p
= link_to @comment.user.name, user_url(@comment.user), :style => "color: white; link-decoration: none; font-weight: bold;"
has commented on the game
= link_to @comment.game.description, game_url(@comment.game), :style => "color: white; link-decoration: none; font-weight: bold;"
%p= @comment.text
5 changes: 5 additions & 0 deletions app/views/user_mailer/comment_email.txt.erb
@@ -0,0 +1,5 @@
Chess-Talk comment:

<%= @comment.user.name %> has commented on the game <%= @comment.game.description %>
<%= @comment.text %>
8 changes: 4 additions & 4 deletions config/database.yml
Expand Up @@ -10,10 +10,10 @@ development:
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
adapter: postgresql
database: chess_talk_test
user: sat
password:

production:
adapter: sqlite3
Expand Down
2 changes: 1 addition & 1 deletion config/environments/development.rb
Expand Up @@ -20,7 +20,7 @@
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "coderow.com",
:domain => "chess-talk.com",
:authentication => :plain,
:user_name => "stevetuckner@gmail.com",
:password => "greplock",
Expand Down
8 changes: 5 additions & 3 deletions config/routes.rb
@@ -1,13 +1,15 @@
ChessTalk::Application.routes.draw do

devise_for :users #, :controllers => {:sessions => "users/sessions"}
resources :users do
resources :users, :shallow => true do |user|
member {get :dashboard}
end

resources :leagues
resources :schedules
resources :games
resources :comments
resources :games do
resources :comments, :shallow => true
end
resources :tournaments

match "admin" => "admin/users#dashboard"
Expand Down
3 changes: 2 additions & 1 deletion public/stylesheets/sass/partials/_base.sass
Expand Up @@ -4,6 +4,7 @@ $inner_container_background_color: #DD0000
$content_background_color: #770000
$article_background_color: #770000
$section_background_color: #770000
$comment_background_color: #660000

$tab_background_color: #404040
$tab_hover_background_color: #808080
Expand All @@ -14,7 +15,7 @@ $base_color: #550000
$normal_text_color: #FFFFFF
$passive_color: #8a8989

$link_color: #FFF
$link_color: #EEEEEE
$link_hover_color: #888
$section_color: #0AE
$highlighted_section_color: #440000
Expand Down
17 changes: 17 additions & 0 deletions public/stylesheets/sass/partials/_styles.sass
Expand Up @@ -166,6 +166,23 @@ section.edit-user-form form
section.edit-user-form
padding-bottom: 10px

section.comments
margin-top: 10px
a#show-comment-form
font-size: 10pt
a#hide-comment-form
font-size: 10pt
form textarea
height: 40px

.comment
background-color: $comment_background_color
margin-top: 5px
margin-bottom: 5px
padding: 5px
+border-radius(5px)


// These can be used to pull an image at the start of a paragraph, so
// that the text flows around it (usage: <p><img class="left">Text</p>)
Expand Down
20 changes: 19 additions & 1 deletion public/stylesheets/screen.css
Expand Up @@ -145,6 +145,24 @@ section.edit-user-form form {
section.edit-user-form {
padding-bottom: 10px; }

section.comments {
margin-top: 10px; }
section.comments a#show-comment-form {
font-size: 10pt; }
section.comments a#hide-comment-form {
font-size: 10pt; }
section.comments form textarea {
height: 40px; }

.comment {
background-color: #660000;
margin-top: 5px;
margin-bottom: 5px;
padding: 5px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border-radius: 5px; }

.left {
float: left;
margin: 1.5em 1.5em 1.5em 0;
Expand Down Expand Up @@ -182,7 +200,7 @@ section.edit-user-form {
display: block; } }

a {
color: white;
color: #eeeeee;
text-decoration: none; }
a:hover {
text-decoration: none; }
Expand Down

0 comments on commit 8503f90

Please sign in to comment.