Skip to content

Commit

Permalink
Allow basic posting of comments
Browse files Browse the repository at this point in the history
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
emk committed Nov 25, 2011
1 parent bdf0773 commit 547eda0
Show file tree
Hide file tree
Showing 23 changed files with 195 additions and 22 deletions.
4 changes: 2 additions & 2 deletions TODO.txt
Expand Up @@ -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
2 changes: 2 additions & 0 deletions 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.
4 changes: 4 additions & 0 deletions 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.
*/
@@ -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,
Expand Down
19 changes: 19 additions & 0 deletions app/controllers/rails_blog_engine/application_controller.rb
Expand Up @@ -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
16 changes: 16 additions & 0 deletions 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
18 changes: 1 addition & 17 deletions 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

Expand Down Expand Up @@ -30,6 +27,7 @@ def create
end

def show
@comment = Comment.new {|c| c.post = @post }
end

def edit
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions app/helpers/rails_blog_engine/comments_helper.rb
@@ -0,0 +1,4 @@
module RailsBlogEngine
module CommentsHelper
end
end
8 changes: 8 additions & 0 deletions 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
1 change: 1 addition & 0 deletions 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
Expand Down
7 changes: 7 additions & 0 deletions 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)
6 changes: 6 additions & 0 deletions 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"
8 changes: 8 additions & 0 deletions app/views/rails_blog_engine/posts/_post.html.haml
Expand Up @@ -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))
11 changes: 11 additions & 0 deletions config/locales/rails_blog_engine.en.yml
Expand Up @@ -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"
4 changes: 3 additions & 1 deletion config/routes.rb
Expand Up @@ -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
21 changes: 21 additions & 0 deletions 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
12 changes: 12 additions & 0 deletions spec/acceptance/rails_blog_engine/posts_spec.rb
Expand Up @@ -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
22 changes: 22 additions & 0 deletions 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
20 changes: 19 additions & 1 deletion spec/dummy/db/schema.rb
Expand Up @@ -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"
Expand Down
14 changes: 14 additions & 0 deletions 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
8 changes: 8 additions & 0 deletions 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
2 changes: 2 additions & 0 deletions spec/models/rails_blog_engine/post_spec.rb
Expand Up @@ -3,6 +3,8 @@
describe RailsBlogEngine::Post do
Post = RailsBlogEngine::Post

it { should have_many(:comments) }

describe "validations" do
before { Post.make! }

Expand Down
4 changes: 4 additions & 0 deletions spec/support/blueprints.rb
Expand Up @@ -18,3 +18,7 @@
password { "password" }
password_confirmation { "password" }
end

RailsBlogEngine::Comment.blueprint do
# Attributes here
end

0 comments on commit 547eda0

Please sign in to comment.