Skip to content

Commit

Permalink
fixing posts controller again
Browse files Browse the repository at this point in the history
  • Loading branch information
GBH committed Nov 29, 2017
1 parent 2648778 commit b5df1c3
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 144 deletions.
8 changes: 1 addition & 7 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@ source 'http://rubygems.org'
gemspec

# 2.0.beta gems
gem 'rails',
github: "rails/rails"
gem "arel",
github: "rails/arel"
gem "bootstrap_form",
github: "bootstrap-ruby/rails-bootstrap-forms",
branch: "bootstrap-v4"
gem 'rails', ">= 5.2.beta1"
gem "comfortable_mexican_sofa",
github: "comfy/comfortable-mexican-sofa"

Expand Down
11 changes: 0 additions & 11 deletions app/controllers/comfy/admin/blog/base_controller.rb

This file was deleted.

15 changes: 7 additions & 8 deletions app/controllers/comfy/admin/blog/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
class Comfy::Admin::Blog::PostsController < Comfy::Admin::Blog::BaseController
class Comfy::Admin::Blog::PostsController < Comfy::Admin::Cms::BaseController

before_action :load_blog
before_action :build_post, only: [:new, :create]
before_action :load_post, only: [:edit, :update, :destroy]

def index
return redirect_to action: :new if @blog.posts.count == 0
return redirect_to action: :new if @site.blog_posts.count == 0

posts_scope = @blog.posts.
includes(:categories).for_category(params[:categories]).order(:published_at)
posts_scope = @site.blog_posts.
includes(:categories).for_category(params[:categories]).order(published_at: :desc)
@posts = comfy_paginate(posts_scope)
end

Expand Down Expand Up @@ -49,16 +48,16 @@ def destroy
protected

def load_post
@post = @blog.posts.find(params[:id])
@post = @site.blog_posts.find(params[:id])
rescue ActiveRecord::RecordNotFound
flash[:danger] = t('.not_found')
redirect_to action: :index
end

def build_post
@post = @blog.posts.new(post_params)
@post = @site.blog_posts.new(post_params)
@post.published_at ||= Time.zone.now
@post.layout ||= (@blog.posts.last.try(:layout) || @site.layouts.first)
@post.layout ||= (@site.blog_posts.last.try(:layout) || @site.layouts.first)
end

def post_params
Expand Down
12 changes: 3 additions & 9 deletions app/models/comfy/blog/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,9 @@ class Comfy::Blog::Post < ActiveRecord::Base
format: {with: /\A%*\w[a-z0-9_\-\%]*\z/i }

# -- Scopes ------------------------------------------------------------------
scope :published, -> {
where(is_published: true)
}
scope :for_year, -> year {
where(year: year)
}
scope :for_month, -> month {
where(month: month)
}
scope :published, -> {where(is_published: true)}
scope :for_year, -> year {where(year: year)}
scope :for_month, -> month {where(month: month)}

# -- Callbacks ---------------------------------------------------------------
before_validation :set_slug,
Expand Down
9 changes: 0 additions & 9 deletions app/views/comfy/admin/blog/blogs/_form.html.haml

This file was deleted.

5 changes: 0 additions & 5 deletions app/views/comfy/admin/blog/blogs/edit.html.haml

This file was deleted.

23 changes: 0 additions & 23 deletions app/views/comfy/admin/blog/blogs/index.html.haml

This file was deleted.

5 changes: 0 additions & 5 deletions app/views/comfy/admin/blog/blogs/new.html.haml

This file was deleted.

8 changes: 2 additions & 6 deletions app/views/comfy/admin/blog/partials/_navigation.html.haml
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
- if @site.present? && @site.persisted?
- if @site && @site.persisted?
%li.nav-item
= active_link_to comfy_admin_blogs_path(@site), active: [["comfy/admin/blog/blogs"]], class: "nav-link" do
= t('comfy.admin.cms.base.blogs')
- if @blog.present? && @blog.persisted?
%li.nav-item
= active_link_to comfy_admin_blog_posts_path(@site, @blog), class: "nav-link" do
= active_link_to comfy_admin_blog_posts_path(@site), class: "nav-link" do
= t('comfy.admin.cms.base.posts')
8 changes: 4 additions & 4 deletions app/views/comfy/admin/blog/posts/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
= form.text_field :title, data: {slugify: @post.new_record?}
= form.text_field :slug, data: {slug: true}

= form.text_field :published_at, value: @post.published_at.try(:to_s, :db), data: {'cms-datetime' => true}

= render 'comfy/admin/cms/categories/form', form: form

- if (options = ::Comfy::Cms::Layout.options_for_select(@site)).present?
= form.select :layout_id, options, {}, {data: {url: form_fragments_comfy_admin_cms_site_page_path(@site, @post.id.to_i)}, id: "fragments-toggle"}

= render "comfy/admin/cms/pages/form_fragments", form: form, record: @post

= 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}

.row
.col-sm-10.ml-auto
= form.check_box :is_published
Expand Down
14 changes: 7 additions & 7 deletions app/views/comfy/admin/blog/posts/index.html.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.page-header
= link_to t('.new_link'), new_comfy_admin_blog_post_path(@site, @blog), class: 'btn btn-secondary pull-right'
= link_to t('.new_link'), new_comfy_admin_blog_post_path(@site), class: 'btn btn-secondary pull-right'
%h2= t('.title')

= comfy_admin_partial "comfy/admin/blog/partials/posts_before"
Expand All @@ -15,20 +15,20 @@
.col-md-8.item
.item-content
.item-title
= link_to post.title, edit_comfy_admin_blog_post_path(@site, @blog, post)
= link_to post.title, edit_comfy_admin_blog_post_path(@site, post)
.item-meta
- link = comfy_blog_post_path(slug: post.slug)
= link_to link, link, target: "_blank"
%br
= post.published_at.try(:to_s, :db)

- link = comfy_blog_post_dated_path(@site.path, post.year, post.month, post.slug)
= link_to link, link, target: "_blank"

.item-categories.ml-auto.d-flex.align-items-center
= render "comfy/admin/cms/categories/categories", object: post

.col-md-4.d-flex.align-items-center.justify-content-md-end
.btn-group.btn-group-sm
= link_to t('.edit'), edit_comfy_admin_blog_post_path(@site, @blog, post), class: 'btn btn-outline-secondary'
= link_to t('.delete'), comfy_admin_blog_post_path(@site, @blog, post), method: :delete, data: {confirm: t('.are_you_sure')}, class: 'btn btn-danger'
= link_to t('.edit'), edit_comfy_admin_blog_post_path(@site, post), class: 'btn btn-outline-secondary'
= link_to t('.delete'), comfy_admin_blog_post_path(@site, post), method: :delete, data: {confirm: t('.are_you_sure')}, class: 'btn btn-danger'

= comfy_paginate @posts

Expand Down
36 changes: 1 addition & 35 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
en:
activerecord:
models:
comfy/blog/blog: Blog
comfy/blog/post: Post
attributes:
comfy/blog/blog:
identifier: Identifier
label: Label
path: Path
description: Description
comfy/blog/post:
title: Title
slug: Slug
Expand All @@ -19,36 +13,8 @@ en:
admin:
cms:
base:
blogs: Blogs
posts: Posts
posts: Blog Posts
blog:
blogs:
not_found: Blog not found
created: Blog created
create_failure: Failed to create Blog
updated: Blog updated
update_failure: Failed to update Blog
deleted: Blog deleted
index:
title: Blogs
new_link: New Blog
post_count:
zero: No Posts
one: '%{count} Post'
few: '%{count} Posts'
many: '%{count} Posts'
other: '%{count} Posts'
edit: Edit
delete: Delete
are_you_sure: Are you sure?
edit:
title: Edit blog
new:
title: New blog
form:
create: Create Blog
update: Update Blog
cancel: Cancel
posts:
created: Blog Post created
create_failure: Failed to create Blog Post
Expand Down
29 changes: 14 additions & 15 deletions test/controllers/comfy/admin/blog/posts_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@ class Comfy::Admin::Blog::PostsControllerTest < ActionDispatch::IntegrationTest
setup do
@site = comfy_cms_sites(:default)
@layout = comfy_cms_layouts(:default)
@blog = comfy_blog_blogs(:default)
@post = comfy_blog_posts(:default)
end

def test_get_index
r :get, comfy_admin_blog_posts_path(@site, @blog)
r :get, comfy_admin_blog_posts_path(@site)
assert_response :success
assert assigns(:posts)
assert_template :index
end

def test_get_index_with_no_posts
Comfy::Blog::Post.delete_all
r :get, comfy_admin_blog_posts_path(@site, @blog)
r :get, comfy_admin_blog_posts_path(@site)
assert_response :redirect
assert_redirected_to action: :new
end
Expand All @@ -30,33 +29,33 @@ def test_get_index_with_category
)
category.categorizations.create!(categorized: @post)

r :get, comfy_admin_blog_posts_path(@site, @blog), params: {categories: category.label}
r :get, comfy_admin_blog_posts_path(@site), params: {categories: category.label}
assert_response :success
assert assigns(:posts)
assert_equal 1, assigns(:posts).count
assert assigns(:posts).first.categories.member? category
end

def test_get_index_with_category_invalid
r :get, comfy_admin_blog_posts_path(@site, @blog), params: {categories: 'invalid'}
r :get, comfy_admin_blog_posts_path(@site), params: {categories: 'invalid'}
assert_response :success
assert assigns(:posts)
assert_equal 0, assigns(:posts).count
end

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

def test_creation
post_count = -> {Comfy::Blog::Post.count}
frag_count = -> {Comfy::Cms::Fragment.count}
assert_difference [post_count, frag_count] do
r :post, comfy_admin_blog_posts_path(@site, @blog), params: {post: {
r :post, comfy_admin_blog_posts_path(@site), params: {post: {
title: 'Test Post',
slug: 'test-post',
published_at: 2.days.ago.to_s(:db),
Expand All @@ -75,7 +74,7 @@ def test_creation

def test_creation_failure
assert_no_difference ->{Comfy::Blog::Post.count} do
r :post, comfy_admin_blog_posts_path(@site, @blog), params: {post: {}}
r :post, comfy_admin_blog_posts_path(@site), params: {post: {}}
assert_response :success
assert_template :new
assert assigns(:post)
Expand All @@ -84,22 +83,22 @@ def test_creation_failure
end

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

def test_get_edit_failure
r :get, edit_comfy_admin_blog_post_path(@site, @blog, 'invalid')
r :get, edit_comfy_admin_blog_post_path(@site, 'invalid')
assert_response :redirect
assert_redirected_to action: :index
assert_equal 'Blog Post not found', flash[:danger]
end

def test_update
r :put, comfy_admin_blog_post_path(@site, @blog, @post), params: {post: {
r :put, comfy_admin_blog_post_path(@site, @post), params: {post: {
title: 'Updated Post'
}}
assert_response :redirect
Expand All @@ -111,7 +110,7 @@ def test_update
end

def test_update_failure
r :put, comfy_admin_blog_post_path(@site, @blog, @post), params: {post: {
r :put, comfy_admin_blog_post_path(@site, @post), params: {post: {
title: ''
}}
assert_response :success
Expand All @@ -124,7 +123,7 @@ def test_update_failure

def test_destroy
assert_difference -> {Comfy::Blog::Post.count}, -1 do
r :delete, comfy_admin_blog_post_path(@site, @blog, @post)
r :delete, comfy_admin_blog_post_path(@site, @post)
assert_response :redirect
assert_redirected_to action: :index
assert_equal 'Blog Post removed', flash[:success]
Expand Down

0 comments on commit b5df1c3

Please sign in to comment.