Skip to content

Commit

Permalink
revisions are in
Browse files Browse the repository at this point in the history
  • Loading branch information
GBH committed Dec 5, 2017
1 parent d28d956 commit 7925133
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ source 'http://rubygems.org'

gemspec

gem "comfortable_mexican_sofa", github: "comfy/comfortable-mexican-sofa"

gem 'kaminari'

group :development do
Expand Down
22 changes: 22 additions & 0 deletions app/controllers/comfy/admin/blog/revisions/post_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Comfy::Admin::Blog::Revisions::PostController < Comfy::Admin::Cms::Revisions::BaseController

def show
@current_content = @record.fragments.inject({}){|c, b| c[b.identifier] = b.content; c }
@versioned_content = @record.fragments.inject({}){|c, b| c[b.identifier] = @revision.data['fragments_attributes'].detect{|r| r[:identifier] == b.identifier}.try(:[], :content); c }

render "comfy/admin/cms/revisions/show"
end

private

def load_record
@record = @site.blog_posts.find(params[:blog_post_id])
rescue ActiveRecord::RecordNotFound
flash[:danger] = I18n.t('comfy.admin.cms.revisions.record_not_found')
redirect_to comfy_admin_blog_posts_path(@site)
end

def record_path
edit_comfy_admin_blog_post_path(@site, @record)
end
end
2 changes: 2 additions & 0 deletions app/models/comfy/blog/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class Comfy::Blog::Post < ActiveRecord::Base
include Comfy::Cms::WithFragments
include Comfy::Cms::WithCategories

cms_has_revisions_for :fragments_attributes

# -- Relationships -----------------------------------------------------------
belongs_to :site,
class_name: 'Comfy::Cms::Site'
Expand Down
4 changes: 4 additions & 0 deletions app/views/comfy/admin/blog/posts/edit.html.haml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
- content_for :right_column do
- link = comfy_admin_blog_post_revisions_path(@site, @post)
= render "comfy/admin/cms/revisions/sidebar", record: @post, link: link

.page-header
%h2= t('.title')

Expand Down
2 changes: 2 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,6 @@
config.file_watcher = ActiveSupport::EventedFileUpdateChecker

config.active_job.queue_adapter = :inline

config.action_view.raise_on_missing_translations = true
end
2 changes: 2 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@
# config.action_view.raise_on_missing_translations = true

config.active_job.queue_adapter = :inline

config.action_view.raise_on_missing_translations = true
end
6 changes: 5 additions & 1 deletion lib/comfy_blog/routes/blog_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ def comfy_route_blog_admin(options = {})
scope module: :comfy, as: :comfy do
scope module: :admin do
namespace :blog, as: :admin, path: path, except: [:show] do
resources :posts, as: :blog_posts, path: "blog-posts"
resources :posts, as: :blog_posts, path: "blog-posts" do
resources :revisions, only: [:index, :show], controller: "revisions/post" do
patch :revert, on: :member
end
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require_relative '../../../../../test_helper'

class Comfy::Admin::Cms::Revisions::PageControllerTest < ActionDispatch::IntegrationTest

setup do
@site = comfy_cms_sites(:default)
@post = comfy_blog_posts(:default)
@revision = comfy_cms_revisions(:post)
end

def test_get_index
r :get, comfy_admin_blog_post_revisions_path(@site, @post)
assert_response :redirect
assert_redirected_to action: :show, id: @revision
end

def test_get_index_with_no_revisions
Comfy::Cms::Revision.delete_all
r :get, comfy_admin_blog_post_revisions_path(@site, @post)
assert_response :redirect
assert_redirected_to edit_comfy_admin_blog_post_path(@site, @post)
end

def test_get_show
r :get, comfy_admin_blog_post_revision_path(@site, @post, @revision)
assert_response :success
assert assigns(:record)
assert assigns(:revision)
assert assigns(:record).is_a?(Comfy::Blog::Post)
assert_template :show
end

def test_get_show_for_invalid_record
r :get, comfy_admin_blog_post_revision_path(@site, "invalid", @revision)
assert_response :redirect
assert_redirected_to comfy_admin_blog_posts_path(@site)
assert_equal 'Record Not Found', flash[:danger]
end


def test_get_show_failure
r :get, comfy_admin_blog_post_revision_path(@site, @post, "invalid")
assert_response :redirect
assert assigns(:record)
assert_redirected_to edit_comfy_admin_blog_post_path(@site, assigns(:record))
assert_equal 'Revision Not Found', flash[:danger]
end

def test_revert
assert_difference -> {@post.revisions.count} do
r :patch, revert_comfy_admin_blog_post_revision_path(@site, @post, @revision)
assert_response :redirect
assert_redirected_to edit_comfy_admin_blog_post_path(@site, @post)
assert_equal 'Content Reverted', flash[:success]

@post.reload

assert_equal [{
identifier: "content",
tag: "text",
content: "old content",
datetime: nil,
boolean: false
}], @post.fragments_attributes
end
end
end
7 changes: 7 additions & 0 deletions test/fixtures/comfy/cms/revisions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
post:
record: default (Comfy::Blog::Post)
data: <%= {
"fragments_attributes" => [
{ "identifier" => "content",
"content" => "old content" }
]}.to_yaml.inspect %>

0 comments on commit 7925133

Please sign in to comment.