Skip to content

Commit

Permalink
Merge 972a938 into 59cd0bb
Browse files Browse the repository at this point in the history
  • Loading branch information
pbrudny committed Jan 17, 2015
2 parents 59cd0bb + 972a938 commit f4b66b7
Show file tree
Hide file tree
Showing 17 changed files with 174 additions and 100 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ Gemfile.lock
.ruby-version
/.sass-cache
coverage/
# Ignore Rubymine files
.idea
9 changes: 4 additions & 5 deletions app/controllers/comfy/blog/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
class Comfy::Blog::CommentsController < Comfy::Blog::BaseController

before_action :load_post,
:build_comment

def create
@comment.save!

flash[:success] = 'Comment created'
redirect_to comfy_blog_post_path(@cms_site.path, @blog.path, @post.slug)

rescue ActiveRecord::RecordInvalid
flash[:error] = 'Failed to create Comment'
render 'comfy/blog/posts/show'
Expand All @@ -22,7 +21,7 @@ def load_post
flash[:error] = 'Blog Post not found'
redirect_to comfy_blog_posts_path(@cms_site.path, @blog.path)
end

def build_comment
@comment = @post.comments.new(comment_params)
end
Expand Down
23 changes: 14 additions & 9 deletions app/models/comfy/blog/comment.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
class Comfy::Blog::Comment < ActiveRecord::Base

self.table_name = 'comfy_blog_comments'

# -- Relationships --------------------------------------------------------
belongs_to :post

# -- Callbacks ------------------------------------------------------------
before_create :set_is_published

# -- Validations ----------------------------------------------------------
validates :post_id, :content, :author, :email,
validates :post_id, :content, :author, :email,
:presence => true
validates :email,
:format => { :with => /\A([\w.%-+]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i }


validate :check_commentable

# -- Scopes ---------------------------------------------------------------
scope :published, -> {
where(:is_published => true)
}

protected

def set_is_published
self.is_published = ComfyBlog.config.auto_publish_comments
return
end


def check_commentable
errors.add(:base, 'comments are not allowed') if post.try(:comments_disabled?)
end
end
26 changes: 15 additions & 11 deletions app/models/comfy/blog/post.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
class Comfy::Blog::Post < ActiveRecord::Base

self.table_name = 'comfy_blog_posts'

# -- Relationships --------------------------------------------------------
belongs_to :blog

has_many :comments,
:dependent => :destroy

# -- Validations ----------------------------------------------------------
validates :blog_id, :title, :slug, :year, :month, :content,
:presence => true
validates :slug,
:uniqueness => { :scope => [:blog_id, :year, :month] },
:format => { :with => /\A\w[a-z0-9_-]*\z/i }

# -- Scopes ---------------------------------------------------------------
default_scope -> {
order('published_at DESC')
Expand All @@ -28,25 +28,29 @@ class Comfy::Blog::Post < ActiveRecord::Base
scope :for_month, -> month {
where(:month => month)
}

# -- Callbacks ------------------------------------------------------------
before_validation :set_slug,
:set_published_at,
:set_date


def comments_disabled?
!ComfyBlog.config.allow_comments || !is_commentable
end

protected

def set_slug
self.slug ||= self.title.to_s.downcase.slugify
end

def set_date
self.year = self.published_at.year
self.month = self.published_at.month
end

def set_published_at
self.published_at ||= Time.zone.now
end

end
3 changes: 3 additions & 0 deletions app/views/comfy/admin/blog/posts/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
= form.text_field :published_at, :value => @post.published_at.try(:to_s, :db), :data => {'cms-datetime' => true}
= form.form_group :is_published do
= form.check_box :is_published
- if ComfyBlog.config.allow_comments
= form.form_group :is_commentable do
= form.check_box :is_commentable

= form.form_group :class => 'form-actions' do
= form.submit :class => 'btn btn-primary'
4 changes: 4 additions & 0 deletions app/views/comfy/blog/comments/_index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- @post.comments.published.each do |comment|
.comment
.author= comment.author
.content= comment.content
11 changes: 4 additions & 7 deletions app/views/comfy/blog/posts/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
.content
= @post.content.html_safe

.comments
- @post.comments.published.each do |comment|
.comment
.author= comment.author
.content= comment.content

= render :partial => 'comfy/blog/comments/form'
- unless @post.comments_disabled?
.comments
= render :partial => 'comfy/blog/comments/index'
= render :partial => 'comfy/blog/comments/form'
4 changes: 4 additions & 0 deletions config/initializers/comfy_blog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
# Number of posts per page. Default is 10
# config.posts_per_page = 10

# Enable/disable comments on posts
# Default is true
# config.allow_comments = false

# Comments can be automatically approved/published by changing this setting
# Default is false.
# config.auto_publish_comments = false
Expand Down
27 changes: 14 additions & 13 deletions db/migrate/01_create_blog.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class CreateBlog < ActiveRecord::Migration

def self.up
create_table :comfy_blogs do |t|
t.integer :site_id, :null => false
Expand All @@ -11,25 +11,26 @@ def self.up
end
add_index :comfy_blogs, [:site_id, :path]
add_index :comfy_blogs, :identifier

create_table :comfy_blog_posts do |t|
t.integer :blog_id, :null => false
t.string :title, :null => false
t.string :slug, :null => false
t.integer :blog_id, :null => false
t.string :title, :null => false
t.string :slug, :null => false
t.text :content
t.string :excerpt, :limit => 1024
t.string :excerpt, :limit => 1024
t.string :author
t.integer :year, :null => false, :limit => 4
t.integer :month, :null => false, :limit => 2
t.boolean :is_published, :null => false, :default => true
t.datetime :published_at, :null => false
t.integer :year, :null => false, :limit => 4
t.integer :month, :null => false, :limit => 2
t.boolean :is_published, :null => false, :default => true
t.boolean :is_commentable, :null => false, :default => true
t.datetime :published_at, :null => false
t.timestamps
end
add_index :comfy_blog_posts, [:is_published, :year, :month, :slug],
:name => 'index_blog_posts_on_published_year_month_slug'
add_index :comfy_blog_posts, [:is_published, :created_at]
add_index :comfy_blog_posts, :created_at

create_table :comfy_blog_comments do |t|
t.integer :post_id, :null => false
t.string :author, :null => false
Expand All @@ -42,11 +43,11 @@ def self.up
add_index :comfy_blog_comments, [:post_id, :is_published, :created_at],
:name => 'index_blog_comments_on_post_published_created'
end

def self.down
drop_table :comfy_blogs
drop_table :comfy_posts
drop_table :comfy_comments
end

end
7 changes: 6 additions & 1 deletion lib/comfy_blog/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@ class Configuration
# Number of posts per page. Default is 10
attr_accessor :posts_per_page

# Enable/disable comments on posts
# Default is true
attr_accessor :allow_comments

# Comments can be automatically approved/published by changing this setting
# Default is false.
attr_accessor :auto_publish_comments

# A default author can be specified for posts
attr_accessor :default_author

# Configuration defaults
def initialize
@posts_per_page = 10
@allow_comments = true
@auto_publish_comments = false
@default_author = nil
end
Expand Down
6 changes: 3 additions & 3 deletions test/controllers/comfy/blog/comments_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require_relative '../../../test_helper'

class Comfy::Blog::CommentsControllerTest < ActionController::TestCase

def setup
@blog = comfy_blog_blogs(:default)
@post = comfy_blog_posts(:default)
Expand All @@ -17,13 +17,13 @@ def test_creation
assert_response :redirect
assert_redirected_to comfy_blog_post_path
assert_equal 'Comment created', flash[:success]

comment = Comfy::Blog::Comment.last
assert_equal 'Test', comment.author
assert_equal @post, comment.post
end
end

def test_creation_failure
assert_no_difference 'Comfy::Blog::Comment.count' do
post :create, :slug => @post.slug, :comment => { }
Expand Down
38 changes: 20 additions & 18 deletions test/controllers/comfy/blog/posts_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,80 +1,82 @@
require_relative '../../../test_helper'

class Comfy::Blog::PostsControllerTest < ActionController::TestCase

def setup
@blog = comfy_blog_blogs(:default)
@post = comfy_blog_posts(:default)
@second_post = comfy_blog_posts(:not_commentable)
end

def test_get_index
get :serve
assert_response :success
assert_template :index
assert assigns(:posts)
assert_equal 1, assigns(:posts).size
assert_equal 2, assigns(:posts).size
end

def test_get_index_as_rss
get :serve, :format => :rss
assert_response :success
assert_template :index
assert assigns(:posts)
assert_equal 1, assigns(:posts).size
assert_equal 2, assigns(:posts).size
end

def test_get_index_with_unpublished
comfy_blog_posts(:default).update_column(:is_published, false)
get :serve
assert_response :success
assert_equal 0, assigns(:posts).size
assert_equal 1, assigns(:posts).size
end

def test_get_index_for_year_archive
get :index, :year => 2012
assert_response :success
assert_equal 1, assigns(:posts).size
assert_equal 2, assigns(:posts).size

get :index, :year => 1999
assert_response :success
assert_equal 0, assigns(:posts).size
end

def test_get_index_for_month_archive
get :index, :year => 2012, :month => 1
assert_response :success
assert_equal 1, assigns(:posts).size
assert_equal 2, assigns(:posts).size

get :index, :year => 2012, :month => 12
assert_response :success
assert_equal 0, assigns(:posts).size
end

def test_get_show
get :serve, :slug => @post.slug
assert_response :success
assert_template :show
assert assigns(:post)
end

def test_get_show_unpublished
@post.update_attribute(:is_published, false)
@second_post.update_attribute(:is_published, false)
assert_exception_raised ComfortableMexicanSofa::MissingPage do
get :serve, :slug => @post.slug
end
end

def test_get_show_with_date
get :show, :year => @post.year, :month => @post.month, :slug => @post.slug
assert_response :success
assert_template :show
assert assigns(:post)
end

def test_get_show_with_date_invalid
assert_exception_raised ComfortableMexicanSofa::MissingPage do
get :show, :year => '1999', :month => '99', :slug => 'invalid'
end
end

end

0 comments on commit f4b66b7

Please sign in to comment.