Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: brandonarbini/snippets
base: fde91291a1
...
head fork: brandonarbini/snippets
compare: 827150619c
  • 3 commits
  • 15 files changed
  • 0 commit comments
  • 1 contributor
View
1  CHANGELOG.md
@@ -1,3 +1,4 @@
+* Comments on snippets.
--------------------------------------------------------------------------------
^ ADD NEW CHANGES ABOVE ^
--------------------------------------------------------------------------------
View
23 app/controllers/comments_controller.rb
@@ -0,0 +1,23 @@
+class CommentsController < ApplicationController
+
+ before_filter :get_snippet
+
+ def create
+ comment = @snippet.comments.build(params[:comment])
+ comment.user = current_user
+ if comment.save
+ render :partial => "snippets/comment", :object => comment
+ else
+ render :text => comment.errors.full_messages.to_sentence,
+ :status => :unprocessable_entity
+ end
+ end
+
+
+protected
+
+ def get_snippet
+ @snippet = Snippet.find_by_id(params[:snippet_id])
+ end
+
+end
View
7 app/mailers/notifications.rb
@@ -15,4 +15,11 @@ def digest(user, date)
mail :subject => "[SNIPPETS] Digest for Week #{@week}, #{@year}",
:to => "#{user.name} <#{user.email}>"
end
+
+ def comment(comment)
+ @comment = comment
+ @snippet = @comment.snippet
+ mail :subject => "[SNIPPETS] Comment by #{@comment.user.name} on Snippet for Week #{@snippet.week}, #{@snippet.year}",
+ :to => "#{@snippet.user.name} <#{@snippet.user.email}>"
+ end
end
View
14 app/models/comment.rb
@@ -0,0 +1,14 @@
+class Comment < ActiveRecord::Base
+
+ attr_accessible :content
+
+ belongs_to :snippet
+ belongs_to :user
+
+ validates_presence_of :content
+
+ after_create do |comment|
+ Notifications.deliver_comment(comment) unless comment.user == comment.snippet.user
+ end
+
+end
View
3  app/models/snippet.rb
@@ -1,6 +1,9 @@
class Snippet < ActiveRecord::Base
+ attr_accessible :content, :state, :year, :week, :finished_at
+
belongs_to :user
+ has_many :comments
scope :for_week, lambda{|date|
where("year = ? and week = ?", date.year, date.to_date.cweek)
View
1  app/models/user.rb
@@ -10,6 +10,7 @@ class User < ActiveRecord::Base
attr_accessor :registration_key
has_many :snippets
+ has_many :comments
validates :name, :presence => true
validates :email, :presence => true
View
6 app/views/layouts/application.html.erb
@@ -19,10 +19,10 @@
<%= link_to "SNIPPETS.", root_path, :class => "logo" %>
<span id="nav">
<%- if logged_in? -%>
- <%= link_to "[#{current_user.name.upcase}]", edit_user_registration_path %>
- <%= link_to "[SIGN OUT]", sign_out_path %>
+ <%= link_to "#{current_user.name.upcase}", edit_user_registration_path %>
+ <%= link_to "SIGN OUT", sign_out_path %>
<%- else -%>
- <%= link_to "[SIGN IN]", sign_in_path %>
+ <%= link_to "SIGN IN", sign_in_path %>
<%- end -%>
</span>
</h1>
View
7 app/views/notifications/comment.text.erb
@@ -0,0 +1,7 @@
+============================================================
+COMMENT ON SNIPPET FOR WEEK <%= @snippet.week %>, <%= @snippet.year %>
+============================================================
+
+<%= @comment.content %>
+
+By <%= @comment.user.name %> @ <%= @comment.created_at %>.
View
6 app/views/snippets/_comment.html.erb
@@ -0,0 +1,6 @@
+<div class="comment">
+ <pre><%=raw auto_link(h(comment.content)) %></pre>
+ <span class="comment-detail">
+ By <%= comment.user.name %> @ <%= comment.created_at %>.
+ </span>
+</div>
View
17 app/views/snippets/show.html.erb
@@ -21,7 +21,22 @@
<%- end -%>
</div>
- <pre><%=raw auto_link(@snippet.content) %></pre>
+ <pre><%=raw auto_link(h(@snippet.content)) %></pre>
+
+ <div id="snippet-comments">
+ <h3>COMMENTS</h3>
+ <%= render :partial => "comment", :collection => @snippet.comments %>
+ <%- if @snippet.comments.empty? -%>
+ <span id="no-comments">No comments.</span>
+ <%- end -%>
+ </div>
+ <div id="new-comment">
+ <div id="new-comment-error" style="display:none"></div>
+ <%= simple_form_for [@snippet, @snippet.comments.build], :remote => true, :format => :html do |f| %>
+ <%= f.input :content %>
+ <%= f.button :submit, "Leave comment" %>
+ <%- end -%>
+ </div>
<div id="snippet-nav">
<%= link_to "&larr; Back".html_safe, week_path(:year => @snippet.year, :week => @snippet.week), :id => "back" %>
View
4 config/routes.rb
@@ -9,7 +9,9 @@
resources :users, :only => :show
get "/users" => redirect("/users/edit")
- resources :snippets, :except => [:index, :destroy]
+ resources :snippets, :except => [:index, :destroy] do
+ resources :comments, :only => [:create]
+ end
get "/snippets" => redirect("/")
controller :weeks, :constraints => { :year => /\d{4}/, :week => /\d{1,2}/ } do
View
16 db/migrate/20110903195604_create_comments.rb
@@ -0,0 +1,16 @@
+class CreateComments < ActiveRecord::Migration
+
+ def self.up
+ create_table :comments do |t|
+ t.integer :snippet_id
+ t.integer :user_id
+ t.text :content
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :comments
+ end
+
+end
View
10 db/schema.rb
@@ -10,7 +10,15 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20110818032200) do
+ActiveRecord::Schema.define(:version => 20110903195604) do
+
+ create_table "comments", :force => true do |t|
+ t.integer "snippet_id"
+ t.integer "user_id"
+ t.text "content"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
create_table "snippets", :force => true do |t|
t.integer "user_id"
View
13 public/javascripts/application.js
@@ -10,6 +10,17 @@ $(function(){
});
-$("form").live("ajax:aborted:required", function(event, elements){
+$("#new_comment").live("ajax:success", function(xhr, data, status){
+ $("#no-comments").remove();
+ $("#new_comment textarea").val("");
+ $("#new-comment-error").hide().html();
+ $("#snippet-comments").append(data);
+});
+
+$("#new_comment").live("ajax:error", function(xhr, status, error){
+ $("#new-comment-error").html(status.responseText).show();
+});
+
+$("form").live("ajax:aborted:required", function(){
return false;
});
View
18 public/stylesheets/master.css
@@ -38,7 +38,8 @@ tr.none .name { background: #f5f5f5; }
tr.draft .name { background: #fff5e5; }
tr.finished .name { background: #e5ffe5; }
-form { width: 95%; margin: 0 auto; border: 4px solid #000; border-width: 4px 0; padding: 20px 0; position: relative; }
+form.snippet,
+form.user { width: 95%; margin: 0 auto; border: 4px solid #000; border-width: 4px 0; padding: 20px 0; position: relative; }
form.user .input.string label { display: inline-block; width: 20%; font-size: 1.2em; text-align: right; padding-right: 8px; }
form.user input.string { font-size: 1.2em; width: 75%; }
@@ -50,6 +51,10 @@ form.snippet label { display: none; }
form.snippet textarea { width: 99%; height: 300px; font-size: 1.2em; font-family: Menlo, "Courier New", Courier, monospace; }
form.snippet input.button { font-size: 1.2em; }
+form.comment label { display: none; }
+form.comment textarea { width: 99%; height: 100px; font-size: 1.2em; font-family: Menlo, "Courier New", Courier, monospace; }
+form.comment input.button { font-size: 1.2em; }
+
.field_with_errors { margin: 10px 0; }
.field_with_errors .error { display: block; margin-left: 20%; position: relative; left: 8px; color: red; }
@@ -68,6 +73,17 @@ form.snippet input.button { font-size: 1.2em; }
#snippet-detail .next-week { right: 0; }
#snippet-detail .name { position: absolute; left: 40px; top: 15px; font-size: 1.6em; text-transform: uppercase; }
+#snippet-comments { border-top: 1px dashed #000; margin-top: 20px; padding-top: 10px; padding-left: 130px; position: relative; }
+#snippet-comments h3 { display: inline-block; padding: 4px; background: yellow; position: absolute; left: 0; top: 10px; }
+#snippet-comments #no-comments { margin: 5px 0 10px; display: block; }
+#snippet-comments .comment { margin: 5px 0 25px; }
+#snippet-comments form.comment { margin-bottom: 0; }
+#snippet-comments .comment pre { margin-bottom: 10px; }
+#snippet-comments .comment-detail { font-size: 0.8em; text-transform: uppercase; background: #ccc; color: #fff; display: inline-block; padding: 5px; }
+
+#new-comment { padding-left: 130px; }
+#new-comment-error { color: #fff; background: red; padding: 5px; display: inline-block; text-transform: uppercase; border-radius: 5px 5px 0 0; -webkit-border-radius: 5px 5px 0 0; -moz-border-radius: 5px 5px 0 0; }
+
footer { width: 95%; margin: 40px auto 0; color: #aaa; text-align: right; font-size: 0.8em; line-height: 1.5; }
footer a { color: #aaa; border-color: #aaa; }
footer a:visited { color: #aaa; border-color: #aaa; }

No commit comments for this range

Something went wrong with that request. Please try again.