From 7925133a1fbf549c085046601965d16c5b11b844 Mon Sep 17 00:00:00 2001 From: Oleg Date: Tue, 5 Dec 2017 14:54:30 -0800 Subject: [PATCH] revisions are in --- Gemfile | 2 + .../admin/blog/revisions/post_controller.rb | 22 ++++++ app/models/comfy/blog/post.rb | 2 + .../comfy/admin/blog/posts/edit.html.haml | 4 ++ config/environments/development.rb | 2 + config/environments/test.rb | 2 + lib/comfy_blog/routes/blog_admin.rb | 6 +- .../blog/revisions/post_controller_test.rb | 67 +++++++++++++++++++ test/fixtures/comfy/cms/revisions.yml | 7 ++ 9 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 app/controllers/comfy/admin/blog/revisions/post_controller.rb create mode 100644 test/controllers/comfy/admin/blog/revisions/post_controller_test.rb create mode 100644 test/fixtures/comfy/cms/revisions.yml diff --git a/Gemfile b/Gemfile index 2c2a521..a1ad97f 100644 --- a/Gemfile +++ b/Gemfile @@ -2,6 +2,8 @@ source 'http://rubygems.org' gemspec +gem "comfortable_mexican_sofa", github: "comfy/comfortable-mexican-sofa" + gem 'kaminari' group :development do diff --git a/app/controllers/comfy/admin/blog/revisions/post_controller.rb b/app/controllers/comfy/admin/blog/revisions/post_controller.rb new file mode 100644 index 0000000..0655511 --- /dev/null +++ b/app/controllers/comfy/admin/blog/revisions/post_controller.rb @@ -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 diff --git a/app/models/comfy/blog/post.rb b/app/models/comfy/blog/post.rb index fed1463..fff520a 100644 --- a/app/models/comfy/blog/post.rb +++ b/app/models/comfy/blog/post.rb @@ -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' diff --git a/app/views/comfy/admin/blog/posts/edit.html.haml b/app/views/comfy/admin/blog/posts/edit.html.haml index 66e944b..fe0aaf1 100644 --- a/app/views/comfy/admin/blog/posts/edit.html.haml +++ b/app/views/comfy/admin/blog/posts/edit.html.haml @@ -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') diff --git a/config/environments/development.rb b/config/environments/development.rb index 3479289..8311d11 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -57,4 +57,6 @@ config.file_watcher = ActiveSupport::EventedFileUpdateChecker config.active_job.queue_adapter = :inline + + config.action_view.raise_on_missing_translations = true end diff --git a/config/environments/test.rb b/config/environments/test.rb index 9c534e9..9a4503a 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -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 diff --git a/lib/comfy_blog/routes/blog_admin.rb b/lib/comfy_blog/routes/blog_admin.rb index ffe729e..6dc2a06 100644 --- a/lib/comfy_blog/routes/blog_admin.rb +++ b/lib/comfy_blog/routes/blog_admin.rb @@ -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 diff --git a/test/controllers/comfy/admin/blog/revisions/post_controller_test.rb b/test/controllers/comfy/admin/blog/revisions/post_controller_test.rb new file mode 100644 index 0000000..5061b1a --- /dev/null +++ b/test/controllers/comfy/admin/blog/revisions/post_controller_test.rb @@ -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 diff --git a/test/fixtures/comfy/cms/revisions.yml b/test/fixtures/comfy/cms/revisions.yml new file mode 100644 index 0000000..07bf9fb --- /dev/null +++ b/test/fixtures/comfy/cms/revisions.yml @@ -0,0 +1,7 @@ +post: + record: default (Comfy::Blog::Post) + data: <%= { + "fragments_attributes" => [ + { "identifier" => "content", + "content" => "old content" } + ]}.to_yaml.inspect %> \ No newline at end of file