Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add categories to blog posts. #91

Open
wants to merge 16 commits into
base: 1.12
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@ before_script:
script:
- rake ci
rvm:
- 1.9.3
- 2.0.0
- 2.1.5
- 2.2.0
- 2.5.9
- 2.6.7
- 2.7.3
gemfile:
- test/gemfiles/Gemfile.rails.4.0
- test/gemfiles/Gemfile.rails.4.1
- test/gemfiles/Gemfile.rails.4.2
- test/gemfiles/Gemfile.rails.6.0
- test/gemfiles/Gemfile.rails.6.1
branches:
only:
- master
matrix:
fast_finish: true
include:
- rvm: 2.2.0
- rvm: 2.7.3
gemfile: test/gemfiles/Gemfile.rails.master
allow_failures:
- gemfile: test/gemfiles/Gemfile.rails.master
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ source 'http://rubygems.org'
gemspec

gem 'kaminari'
gem 'comfortable_mexican_sofa', path: '/Users/ath0/dev/github/comfortable-mexican-sofa'

group :development do
gem 'awesome_print'
gem 'better_errors'
gem 'binding_of_caller'
gem 'quiet_assets'
end

group :test do
gem 'sqlite3'
gem 'coveralls', :require => false
gem 'rails-controller-testing'
end
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ comfy_route :blog, :path => 'blog'

You should also find view templates in `/app/views/blog` folder. Feel free to adjust them as you see fit.


## Upgrading

### Upgrading to v 1.15 from earlier versions

If you're upgrading to v 1.15, you should review the upgrade procedure for ComfortableMexicanSofa

---

Copyright 2009-2014 Oleg Khabarov
21 changes: 0 additions & 21 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,3 @@ end

require_relative 'config/application'
ComfyBlog::Application.load_tasks

namespace :test do

Rake::TestTask.new(:lib) do |t|
t.libs << 'test'
t.pattern = 'test/lib/**/*_test.rb'
t.verbose = true
end

Rake::TestTask.new(:generators) do |t|
t.libs << 'test'
t.pattern = 'test/generators/**/*_test.rb'
t.verbose = true
end

end

Rake::Task[:test].enhance do
Rake::Task['test:lib'].invoke
Rake::Task['test:generators'].invoke
end
3 changes: 3 additions & 0 deletions app/assets/config/manifest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// app/assets/config/manifest.js
//= link comfy/admin/cms/application.css
//= link comfy/admin/cms/application.js
2 changes: 1 addition & 1 deletion app/controllers/comfy/admin/blog/blogs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def create
end

def update
@blog.update_attributes!(blog_params)
@blog.update!(blog_params)
flash[:success] = 'Blog updated'
redirect_to :action => :edit, :id => @blog
rescue ActiveRecord::RecordInvalid
Expand Down
8 changes: 6 additions & 2 deletions app/controllers/comfy/admin/blog/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ class Comfy::Admin::Blog::PostsController < Comfy::Admin::Blog::BaseController
before_action :load_post, :only => [:edit, :update, :destroy]

def index
@posts = comfy_paginate(@blog.posts.order(:published_at))
if params[:category].present?
@posts = comfy_paginate(@blog.posts.for_category(params[:category]))
else
@posts = comfy_paginate(@blog.posts.order(:published_at))
end
end

def new
Expand All @@ -28,7 +32,7 @@ def edit
end

def update
@post.update_attributes!(post_params)
@post.update!(post_params)
flash[:success] = 'Blog Post updated'
redirect_to :action => :edit, :id => @post

Expand Down
17 changes: 17 additions & 0 deletions app/controllers/comfy/blog/categories_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Comfy::Blog::CategoriesController < Comfy::Blog::BaseController
before_action :load_blog

def index
scope = @blog.site.categories.of_type('Comfy::Blog::Post')
respond_to do |format|
format.html do
@categories = comfy_paginate(scope, 100)
end
end
end

def show
@category = Comfy::Cms::Category.find_by_slug!(params[:slug])
@posts = @blog.posts.for_category(@category.label)
end
end
2 changes: 1 addition & 1 deletion app/models/comfy/blog/blog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Comfy::Blog::Blog < ActiveRecord::Base
:uniqueness => { :scope => :site_id },
:format => { :with => /\A\w[a-z0-9_-]*\z/i },
:presence => true,
:if => 'restricted_path?'
:if => :restricted_path?

protected

Expand Down
2 changes: 2 additions & 0 deletions app/models/comfy/blog/post.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class Comfy::Blog::Post < ActiveRecord::Base

self.table_name = 'comfy_blog_posts'

cms_is_categorized

# -- Relationships --------------------------------------------------------
belongs_to :blog
Expand Down
2 changes: 1 addition & 1 deletion app/views/comfy/admin/blog/blogs/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
= form.text_field :identifier, :data => {:slug => true}
= form.text_field :path

- if (options = Comfy::Cms::Layout.app_layouts_for_select).present?
- if (options = Comfy::Cms::Layout.app_layouts_for_select(lookup_context.view_paths)).present?
= form.select :app_layout, options

= form.text_area :description, :class => 'short'
Expand Down
1 change: 1 addition & 0 deletions app/views/comfy/admin/blog/posts/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
= form.text_field :author
= form.text_area :content, :data => {'cms-rich-text' => true}
= form.text_area :excerpt, :class => 'short'
= render 'comfy/admin/cms/categories/form', :form => form
= 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
Expand Down
2 changes: 2 additions & 0 deletions app/views/comfy/admin/blog/posts/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

= comfy_paginate @posts

= render :partial => 'comfy/admin/cms/categories/index', :object => 'Comfy::Blog::Post'

%table.table.table-hover
%tr
%th.main Title
Expand Down
8 changes: 8 additions & 0 deletions app/views/comfy/blog/categories/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
%h1 Blog Post Categories

- if @categories.any?
%ul
- @categories.each do |category|
%li= link_to category.label, comfy_blog_category_path(@cms_site.path, @blog.path, category.slug)

= comfy_paginate @categories
9 changes: 9 additions & 0 deletions app/views/comfy/blog/categories/show.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
%h1 All Blog Posts in category "#{@category.label}"

- if @posts.any?
%ul
- @posts.each do |post|
%li= link_to post.title, comfy_blog_post_path(@cms_site.path, @blog.path, post.slug)

- else
%p No Blog posts were filed in this category
5 changes: 5 additions & 0 deletions app/views/comfy/blog/posts/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
.content
= @post.content.html_safe

.categories
%ul
= @post.categories.each do |category|
%li= category.label

.comments
- @post.comments.published.each do |comment|
.comment
Expand Down
2 changes: 1 addition & 1 deletion comfy_blog.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
s.require_paths = ['lib']

s.add_dependency 'comfortable_mexican_sofa', '>= 1.12.7'
s.add_dependency 'comfortable_mexican_sofa', '>= 1.15.0', '< 2.0'
end
4 changes: 2 additions & 2 deletions config/boot.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)

require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
File.exists?(ENV['BUNDLE_GEMFILE'])
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
File.exist?(ENV['BUNDLE_GEMFILE'])
3 changes: 2 additions & 1 deletion db/migrate/00_create_cms.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class CreateCms < ActiveRecord::Migration
class CreateCms < ActiveRecord::Migration[4.2]

def self.up

Expand Down Expand Up @@ -112,6 +112,7 @@ def self.up
t.integer :site_id, :null => false
t.string :label, :null => false
t.string :categorized_type, :null => false
t.string :slug, :null => false
end
add_index :comfy_cms_categories, [:site_id, :categorized_type, :label], :unique => true,
:name => 'index_cms_categories_on_site_id_and_cat_type_and_label'
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/01_create_blog.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class CreateBlog < ActiveRecord::Migration
class CreateBlog < ActiveRecord::Migration[4.2]

def self.up
create_table :comfy_blogs do |t|
Expand Down
3 changes: 3 additions & 0 deletions lib/comfy_blog/routes/blog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ def comfy_route_blog(options = {})
o.get ':year/:month' => 'posts#index', :as => :posts_of_month
o.get ':year/:month/:slug' => 'posts#show', :as => :posts_dated
end
get 'categories' => 'categories#index', :as => :categories
get 'categories/:slug' => 'categories#show', :as => :category

post ':slug/comments' => 'comments#create', :as => :comments
get ':slug' => 'posts#serve', :as => :post
get '/' => 'posts#serve', :as => :posts
Expand Down
2 changes: 1 addition & 1 deletion lib/comfy_blog/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ComfyBlog
VERSION = "1.12.3"
VERSION = "1.15.0"
end
2 changes: 1 addition & 1 deletion lib/generators/comfy/blog/blog_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def generate_initialization

def generate_routing
route_string = " comfy_route :blog_admin, :path => '/admin'\n"
route_string << " comfy_route :blog, :path => '/blog'\n"
route_string << "comfy_route :blog, :path => '/blog'\n\n"
route route_string[2..-1]
end

Expand Down
24 changes: 12 additions & 12 deletions test/controllers/comfy/admin/blog/blogs_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,43 @@ def setup
end

def test_get_index
get :index, :site_id => @site
get :index, params: { :site_id => @site }
assert_response :success
assert assigns(:blogs)
assert_template :index
end

def test_get_new
get :new, :site_id => @site
get :new, params: { :site_id => @site }
assert_response :success
assert assigns(:blog)
assert_template :new
assert_select "form[action='/admin/sites/#{@site.id}/blogs']"
end

def test_get_edit
get :edit, :site_id => @site, :id => @blog
get :edit, params: { :site_id => @site, :id => @blog }
assert_response :success
assert assigns(:blog)
assert_template :edit
assert_select "form[action='/admin/sites/#{@site.id}/blogs/#{@blog.id}']"
end

def test_get_edit_failure
get :edit, :site_id => @site, :id => 'invalid'
get :edit, params: { :site_id => @site, :id => 'invalid' }
assert_response :redirect
assert_redirected_to :action => :index
assert_equal 'Blog not found', flash[:error]
end

def test_creation
assert_difference 'Comfy::Blog::Blog.count' do
post :create, :site_id => @site, :blog => {
post :create, params: { :site_id => @site, :blog => {
:label => 'Test Blog',
:identifier => 'test-blog',
:path => 'test-blog',
:description => 'Test Description'
}
}}
blog = Comfy::Blog::Blog.last
assert_response :redirect
assert_redirected_to :action => :edit, :id => blog
Expand All @@ -54,17 +54,17 @@ def test_creation

def test_creation_failure
assert_no_difference 'Comfy::Blog::Blog.count' do
post :create, :site_id => @site, :blog => { }
post :create, params: { :site_id => @site, :blog => { } }
assert_response :success
assert_template :new
assert_equal 'Failed to create Blog', flash[:error]
end
end

def test_update
put :update, :site_id => @site, :id => @blog, blog: {
put :update, params: { :site_id => @site, :id => @blog, blog: {
:label => 'Updated'
}
}}
assert_response :redirect
assert_redirected_to :action => :edit, :id => @blog
assert_equal 'Blog updated', flash[:success]
Expand All @@ -73,9 +73,9 @@ def test_update
end

def test_update_failure
put :update, :site_id => @site, :id => @blog, :blog => {
put :update, params: { :site_id => @site, :id => @blog, :blog => {
:label => ''
}
}}
assert_response :success
assert_template :edit
assert_equal 'Failed to update Blog', flash[:error]
Expand All @@ -85,7 +85,7 @@ def test_update_failure

def test_destroy
assert_difference 'Comfy::Blog::Blog.count', -1 do
delete :destroy, :site_id => @site, :id => @blog
delete :destroy, params: { :site_id => @site, :id => @blog }
assert_response :redirect
assert_redirected_to :action => :index
assert_equal 'Blog deleted', flash[:success]
Expand Down
Loading