Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow basic posting of comments

There's still no validation or stylesheets.  The various fields in
Comment are chosen to include the fields required by the rakismet
spam filtering service.
  • Loading branch information...
commit 547eda031133db7b4f55be9986bd5794430b5445 1 parent bdf0773
@emk emk authored
Showing with 195 additions and 22 deletions.
  1. +2 −2 TODO.txt
  2. +2 −0  app/assets/javascripts/rails_blog_engine/comments.js
  3. +4 −0 app/assets/stylesheets/rails_blog_engine/comments.css.scss
  4. +1 −1  app/assets/stylesheets/rails_blog_engine/simple_form.css.scss
  5. +19 −0 app/controllers/rails_blog_engine/application_controller.rb
  6. +16 −0 app/controllers/rails_blog_engine/comments_controller.rb
  7. +1 −17 app/controllers/rails_blog_engine/posts_controller.rb
  8. +4 −0 app/helpers/rails_blog_engine/comments_helper.rb
  9. +8 −0 app/models/rails_blog_engine/comment.rb
  10. +1 −0  app/models/rails_blog_engine/post.rb
  11. +7 −0 app/views/rails_blog_engine/comments/_comment.html.haml
  12. +6 −0 app/views/rails_blog_engine/comments/_form.html.haml
  13. +8 −0 app/views/rails_blog_engine/posts/_post.html.haml
  14. +11 −0 config/locales/rails_blog_engine.en.yml
  15. +3 −1 config/routes.rb
  16. +21 −0 db/migrate/20111125111958_create_rails_blog_engine_comments.rb
  17. +12 −0 spec/acceptance/rails_blog_engine/posts_spec.rb
  18. +22 −0 spec/controllers/rails_blog_engine/comments_controller_spec.rb
  19. +19 −1 spec/dummy/db/schema.rb
  20. +14 −0 spec/helpers/rails_blog_engine/comments_helper_spec.rb
  21. +8 −0 spec/models/rails_blog_engine/comment_spec.rb
  22. +2 −0  spec/models/rails_blog_engine/post_spec.rb
  23. +4 −0 spec/support/blueprints.rb
View
4 TODO.txt
@@ -15,6 +15,6 @@ Needed for use as a product or company blog
Needed for 1.0
- Comments
- Spam filtering for comments
+, Comments
+ Spam filtering for comments: rakismet
Recent articles list
View
2  app/assets/javascripts/rails_blog_engine/comments.js
@@ -0,0 +1,2 @@
+// Place all the behaviors and hooks related to the matching controller here.
+// All this logic will automatically be available in application.js.
View
4 app/assets/stylesheets/rails_blog_engine/comments.css.scss
@@ -0,0 +1,4 @@
+/*
+ Place all the styles related to the matching controller here.
+ They will automatically be included in application.css.
+*/
View
2  app/assets/stylesheets/rails_blog_engine/simple_form.css.scss
@@ -1,4 +1,4 @@
-.rails_blog_engine_post.simple_form {
+.rails_blog_engine_post.simple_form, .rails_blog_engine_post .simple_form {
// Copied from
// https://github.com/plataformatec/simple_form/wiki/CSS-for-simple_form,
View
19 app/controllers/rails_blog_engine/application_controller.rb
@@ -3,5 +3,24 @@ class ApplicationController < ActionController::Base
# Use our parent application's layout, so that we "auto-blend" into
# the existing look-and-feel of the site.
layout 'application'
+
+ helper_method :post_permalink_url
+ helper_method :post_permalink_path
+
+ protected
+
+ def post_permalink_local_path(post)
+ date = post.published_at.utc
+ sprintf('%04d/%02d/%02d/%s', date.year, date.month,
+ date.day, post.permalink)
+ end
+
+ def post_permalink_path(post)
+ root_path + post_permalink_local_path(post)
+ end
+
+ def post_permalink_url(post)
+ root_url + post_permalink_local_path(post)
+ end
end
end
View
16 app/controllers/rails_blog_engine/comments_controller.rb
@@ -0,0 +1,16 @@
+module RailsBlogEngine
+ class CommentsController < ApplicationController
+ before_filter :load_post
+
+ def create
+ @comment = @post.comments.create(params[:comment])
+ redirect_to post_permalink_path(@post)
+ end
+
+ protected
+
+ def load_post
+ @post = Post.find(params[:post_id])
+ end
+ end
+end
View
18 app/controllers/rails_blog_engine/posts_controller.rb
@@ -1,8 +1,5 @@
module RailsBlogEngine
class PostsController < ApplicationController
- helper_method :post_permalink_url
- helper_method :post_permalink_path
-
before_filter :load_recently_published, :only => :index
before_filter :load_by_permalink, :only => :show
@@ -30,6 +27,7 @@ def create
end
def show
+ @comment = Comment.new {|c| c.post = @post }
end
def edit
@@ -47,20 +45,6 @@ def update
protected
- def post_permalink_local_path(post)
- date = post.published_at.utc
- sprintf('%04d/%02d/%02d/%s', date.year, date.month,
- date.day, post.permalink)
- end
-
- def post_permalink_path(post)
- root_path + post_permalink_local_path(post)
- end
-
- def post_permalink_url(post)
- root_url + post_permalink_local_path(post)
- end
-
def load_recently_published
@posts = Post.recently_published
end
View
4 app/helpers/rails_blog_engine/comments_helper.rb
@@ -0,0 +1,4 @@
+module RailsBlogEngine
+ module CommentsHelper
+ end
+end
View
8 app/models/rails_blog_engine/comment.rb
@@ -0,0 +1,8 @@
+module RailsBlogEngine
+ class Comment < ActiveRecord::Base
+ belongs_to :post, :class_name => 'RailsBlogEngine::Post'
+
+ validates :author_byline, :presence => true
+ validates :body, :presence => true
+ end
+end
View
1  app/models/rails_blog_engine/post.rb
@@ -1,6 +1,7 @@
module RailsBlogEngine
class Post < ActiveRecord::Base
belongs_to :author, :polymorphic => true
+ has_many :comments, :class_name => 'RailsBlogEngine::Comment'
validates :title, :presence => true
validates :body, :presence => true
View
7 app/views/rails_blog_engine/comments/_comment.html.haml
@@ -0,0 +1,7 @@
+.rails_blog_engine_comment
+ %p.byline
+ Posted
+ = distance_of_time_in_words_to_now(comment.created_at)
+ ago by
+ = comment.author_byline
+ %div.body~ markdown(comment.body)
View
6 app/views/rails_blog_engine/comments/_form.html.haml
@@ -0,0 +1,6 @@
+= simple_form_for(comment, :url => post_comments_path(comment.post)) do |f|
+ = f.input :author_byline
+ = f.input :author_email
+ = f.input :author_url
+ = f.input :body
+ = f.button :submit, :class => "new_comment btn primary"
View
8 app/views/rails_blog_engine/posts/_post.html.haml
@@ -11,3 +11,11 @@
= post.author_byline
%div.body~ markdown(post.body)
+
+ - if current_page?(post_permalink_path(post))
+ = render post.comments
+ = render(:partial => 'rails_blog_engine/comments/form',
+ :locals => { :comment => @comment })
+ - else
+ %p.links
+ = link_to("Comment on this post", post_permalink_path(post))
View
11 config/locales/rails_blog_engine.en.yml
@@ -2,3 +2,14 @@ en:
rails_blog_engine:
blog:
title: "Blog"
+ activerecord:
+ attributes:
+ rails_blog_engine/comment:
+ author_byline: "Your name"
+ author_email: "Your email"
+ author_url: "Your website"
+ body: "Comment"
+ helpers:
+ submit:
+ comment:
+ create: "Post Comment"
View
4 config/routes.rb
@@ -13,5 +13,7 @@
:constraints => { :year => /\d{4,}/, :month => /\d\d/, :day => /\d\d/ })
# A regular resource interface for everything else.
- resources :posts, :except => [:index, :show, :delete]
+ resources :posts, :except => [:index, :show, :delete] do
+ resources :comments, :only => [:create]
+ end
end
View
21 db/migrate/20111125111958_create_rails_blog_engine_comments.rb
@@ -0,0 +1,21 @@
+class CreateRailsBlogEngineComments < ActiveRecord::Migration
+ def change
+ create_table :rails_blog_engine_comments do |t|
+ t.references :post
+ t.string :author_byline
+ t.string :author_email
+ t.string :author_url
+ t.string :author_ip
+ t.string :author_user_agent
+ t.boolean :author_can_post
+ t.string :referrer
+ t.string :state
+ t.text :body
+
+ t.timestamps
+ end
+
+ add_index :rails_blog_engine_comments, :post_id
+ add_index :rails_blog_engine_comments, :author_email
+ end
+end
View
12 spec/acceptance/rails_blog_engine/posts_spec.rb
@@ -45,4 +45,16 @@
page.should have_content("Test Post")
current_path.should == '/blog/page/2'
end
+
+ scenario 'Adding a comment' do
+ visit '/blog'
+ click_on 'Comment'
+ fill_in "Your name", :with => "Jane Doe"
+ fill_in "Comment", :with => "Test comment"
+ click_on "Post Comment"
+ page.should have_content("Jane Doe")
+ page.should have_content("Test comment")
+ end
+
+ # TODO: Comment validation & errors.
end
View
22 spec/controllers/rails_blog_engine/comments_controller_spec.rb
@@ -0,0 +1,22 @@
+require 'spec_helper'
+
+# This spec was generated by rspec-rails when you ran the scaffold generator.
+# It demonstrates how one might use RSpec to specify the controller code that
+# was generated by Rails when you ran the scaffold generator.
+#
+# It assumes that the implementation code is generated by the rails scaffold
+# generator. If you are using any extension libraries to generate different
+# controller code, this generated spec may or may not pass.
+#
+# It only uses APIs available in rails and/or rspec-rails. There are a number
+# of tools you can use to make these specs even more expressive, but we're
+# sticking to rails and rspec-rails APIs to keep things simple and stable.
+#
+# Compared to earlier versions of this generator, there is very limited use of
+# stubs and message expectations in this spec. Stubs are only used when there
+# is no simpler way to get a handle on the object needed for the example.
+# Message expectations are only used when there is no simpler way to specify
+# that an instance is receiving a specific message.
+
+describe RailsBlogEngine::CommentsController do
+end
View
20 spec/dummy/db/schema.rb
@@ -11,7 +11,25 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20110913190319) do
+ActiveRecord::Schema.define(:version => 20111125111958) do
+
+ create_table "rails_blog_engine_comments", :force => true do |t|
+ t.integer "post_id"
+ t.string "author_byline"
+ t.string "author_email"
+ t.string "author_url"
+ t.string "author_ip"
+ t.string "author_user_agent"
+ t.boolean "author_can_post"
+ t.string "referrer"
+ t.string "state"
+ t.text "body"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+ add_index "rails_blog_engine_comments", ["author_email"], :name => "index_rails_blog_engine_comments_on_author_email"
+ add_index "rails_blog_engine_comments", ["post_id"], :name => "index_rails_blog_engine_comments_on_post_id"
create_table "rails_blog_engine_posts", :force => true do |t|
t.string "title"
View
14 spec/helpers/rails_blog_engine/comments_helper_spec.rb
@@ -0,0 +1,14 @@
+require 'spec_helper'
+
+# Specs in this file have access to a helper object that includes
+# the CommentsHelper. For example:
+#
+# describe CommentsHelper do
+# describe "string concat" do
+# it "concats two strings with spaces" do
+# helper.concat_strings("this","that").should == "this that"
+# end
+# end
+# end
+describe RailsBlogEngine::CommentsHelper do
+end
View
8 spec/models/rails_blog_engine/comment_spec.rb
@@ -0,0 +1,8 @@
+require 'spec_helper'
+
+describe RailsBlogEngine::Comment do
+ it { should belong_to(:post) }
+
+ it { should_not allow_value('').for(:author_byline) }
+ it { should_not allow_value('').for(:body) }
+end
View
2  spec/models/rails_blog_engine/post_spec.rb
@@ -3,6 +3,8 @@
describe RailsBlogEngine::Post do
Post = RailsBlogEngine::Post
+ it { should have_many(:comments) }
+
describe "validations" do
before { Post.make! }
View
4 spec/support/blueprints.rb
@@ -18,3 +18,7 @@
password { "password" }
password_confirmation { "password" }
end
+
+RailsBlogEngine::Comment.blueprint do
+ # Attributes here
+end
Please sign in to comment.
Something went wrong with that request. Please try again.