controllers are ready
Oleg
committed May 09, 2011
commit 8cec19a1d3e94e69719b86bb8caf3f0ef4e7f48b
Showing 10
changed files with
224 additions
and 13 deletions
Gemfile
+1
-4
| @@ | @@ -1,12 +1,9 @@ |
| source 'http://rubygems.org' | |
| - | gem 'rails', '>=3.0.3' |
| + | gem 'rails', '>=3.0.0' |
| gem 'active_link_to', '>=0.0.6' | |
| gem 'paperclip', '>=2.3.8' | |
| - | # If you wish to use sqlite in production |
| - | # gem 'sqlite3' |
| - | |
| group :development do | |
| gem 'sqlite3' | |
| end | |
Gemfile.lock
+1
-1
| @@ | @@ -81,5 +81,5 @@ DEPENDENCIES |
| active_link_to (>= 0.0.6) | |
| jeweler (>= 1.4.0) | |
| paperclip (>= 2.3.8) | |
| - | rails (>= 3.0.3) |
| + | rails (>= 3.0.0) |
| sqlite3 | |
app/controllers/cms_admin/revisions_controller.rb
+50
-0
| @@ | @@ -0,0 +1,50 @@ |
| + | class CmsAdmin::RevisionsController < CmsAdmin::BaseController |
| + | |
| + | before_filter :load_record |
| + | before_filter :load_revision, :except => :index |
| + | |
| + | def index |
| + | redirect_to :action => :show, :id => @record.revisions.first.try(:id) || 0 |
| + | end |
| + | |
| + | def show |
| + | render |
| + | end |
| + | |
| + | def revert |
| + | @record.restore_from_revision(@revision) |
| + | flash[:notice] = 'Content Reverted' |
| + | redirect_to_record |
| + | end |
| + | |
| + | protected |
| + | |
| + | def load_record |
| + | @record = if params[:layout_id] |
| + | Cms::Layout.find(params[:layout_id]) |
| + | elsif params[:page_id] |
| + | Cms::Page.find(params[:page_id]) |
| + | elsif params[:snippet_id] |
| + | Cms::Snippet.find(params[:snippet_id]) |
| + | end |
| + | rescue ActiveRecord::RecordNotFound |
| + | flash[:error] = 'Record Not Found' |
| + | redirect_to cms_admin_path |
| + | end |
| + | |
| + | def load_revision |
| + | @revision = @record.revisions.find(params[:id]) |
| + | rescue ActiveRecord::RecordNotFound |
| + | flash[:error] = 'Revision Not Found' |
| + | redirect_to_record |
| + | end |
| + | |
| + | def redirect_to_record |
| + | redirect_to case @record |
| + | when Cms::Layout then edit_cms_admin_layout_path(@record) |
| + | when Cms::Page then edit_cms_admin_page_path(@record) |
| + | when Cms::Snippet then edit_cms_admin_snippet_path(@record) |
| + | end |
| + | end |
| + | |
| + | end |
| \ No newline at end of file | |
app/models/cms/revision.rb
+3
-0
| @@ | @@ -7,4 +7,7 @@ class Cms::Revision < ActiveRecord::Base |
| # -- Relationships -------------------------------------------------------- | |
| belongs_to :record, :polymorphic => true | |
| + | # -- Scopes --------------------------------------------------------------- |
| + | default_scope order('created_at DESC') |
| + | |
| end | |
| \ No newline at end of file | |
app/views/cms_admin/revisions/show.html.erb
+2
-0
| @@ | @@ -0,0 +1,2 @@ |
| + | <%= debug @record %> |
| + | <%= debug @revision %> |
| \ No newline at end of file | |
app/views/cms_admin/snippets/edit.html.erb
+1
-1
| @@ | @@ -1,4 +1,4 @@ |
| - | <%= link_to span_tag(pluralize(@cms_snippet.revisions.count, 'revision')), new_cms_admin_snippet_path, :class => 'big button' %> |
| + | <%= link_to span_tag(pluralize(@cms_snippet.revisions.count, 'revision')), cms_admin_snippet_revisions_path(@cms_snippet), :class => 'big button' %> |
| <h1> Editing Snippet </h1> | |
| <%= cms_form_for @cms_snippet, :url => {:action => :update} do |form| %> | |
config/routes.rb
+13
-2
| @@ | @@ -10,11 +10,22 @@ Rails.application.routes.draw do |
| collection do | |
| match :reorder | |
| end | |
| + | resources :revisions, :only => [:index, :show, :revert] do |
| + | post :revert, :on => :member |
| + | end |
| end | |
| resources :sites | |
| - | resources :layouts |
| - | resources :snippets |
| resources :uploads, :only => [:create, :destroy] | |
| + | resources :layouts do |
| + | resources :revisions, :only => [:index, :show, :revert] do |
| + | post :revert, :on => :member |
| + | end |
| + | end |
| + | resources :snippets do |
| + | resources :revisions, :only => [:index, :show, :revert] do |
| + | post :revert, :on => :member |
| + | end |
| + | end |
| end | |
| scope :controller => :cms_content do | |
comfortable_mexican_sofa/has_revisions.rb b/lib/comfortable_mexican_sofa/has_revisions.rb
+2
-2
| @@ | @@ -29,6 +29,7 @@ module ComfortableMexicanSofa::HasRevisions |
| # Preparing revision data. A bit of a special thing to grab page blocks | |
| def prepare_revision | |
| + | return if self.new_record? |
| if (self.respond_to?(:blocks_attributes_changed) && self.blocks_attributes_changed) || | |
| !(self.changed & revision_fields).empty? | |
| self.revision_data = revision_fields.inject({}) do |c, field| | |
| @@ | @@ -48,8 +49,7 @@ module ComfortableMexicanSofa::HasRevisions |
| end | |
| # blowing away old revisions | |
| - | ids = [0] + self.revisions.order('created_at DESC'). |
| - | limit(ComfortableMexicanSofa.config.revisions_limit.to_i).collect(&:id) |
| + | ids = [0] + self.revisions.limit(ComfortableMexicanSofa.config.revisions_limit.to_i).collect(&:id) |
| self.revisions.where('id NOT IN (?)', ids).destroy_all | |
| end | |
test/functional/cms_admin/revisions_controller_test.rb
+135
-0
| @@ | @@ -0,0 +1,135 @@ |
| + | require File.expand_path('../../test_helper', File.dirname(__FILE__)) |
| + | |
| + | class CmsAdmin::RevisionsControllerTest < ActionController::TestCase |
| + | |
| + | def test_get_index_for_layouts |
| + | get :index, :layout_id => cms_layouts(:default) |
| + | assert_response :redirect |
| + | assert_redirected_to :action => :show, :id => cms_revisions(:layout) |
| + | end |
| + | |
| + | def test_get_index_for_pages |
| + | get :index, :page_id => cms_pages(:default) |
| + | assert_response :redirect |
| + | assert_redirected_to :action => :show, :id => cms_revisions(:page) |
| + | end |
| + | |
| + | def test_get_index_for_snippets |
| + | get :index, :snippet_id => cms_snippets(:default) |
| + | assert_response :redirect |
| + | assert_redirected_to :action => :show, :id => cms_revisions(:snippet) |
| + | end |
| + | |
| + | def test_get_index_for_snippets_with_no_revisions |
| + | Cms::Revision.delete_all |
| + | get :index, :snippet_id => cms_snippets(:default) |
| + | assert_response :redirect |
| + | assert_redirected_to :action => :show, :id => 0 |
| + | end |
| + | |
| + | def test_get_show_for_layout |
| + | get :show, :layout_id => cms_layouts(:default), :id => cms_revisions(:layout) |
| + | assert_response :success |
| + | assert assigns(:record) |
| + | assert assigns(:revision) |
| + | assert assigns(:record).is_a?(Cms::Layout) |
| + | assert_template :show |
| + | end |
| + | |
| + | def test_get_show_for_page |
| + | get :show, :page_id => cms_pages(:default), :id => cms_revisions(:page) |
| + | assert_response :success |
| + | assert assigns(:record) |
| + | assert assigns(:revision) |
| + | assert assigns(:record).is_a?(Cms::Page) |
| + | assert_template :show |
| + | end |
| + | |
| + | def test_get_show_for_snippet |
| + | get :show, :snippet_id => cms_snippets(:default), :id => cms_revisions(:snippet) |
| + | assert_response :success |
| + | assert assigns(:record) |
| + | assert assigns(:revision) |
| + | assert assigns(:record).is_a?(Cms::Snippet) |
| + | assert_template :show |
| + | end |
| + | |
| + | def test_get_show_for_bad_type |
| + | get :show, :snippet_id => 'invalid', :id => cms_revisions(:snippet) |
| + | assert_response :redirect |
| + | assert_redirected_to cms_admin_path |
| + | assert_equal 'Record Not Found', flash[:error] |
| + | end |
| + | |
| + | def test_get_show_for_layout_failure |
| + | get :show, :layout_id => cms_layouts(:default), :id => 'invalid' |
| + | assert_response :redirect |
| + | assert assigns(:record) |
| + | assert_redirected_to edit_cms_admin_layout_path(assigns(:record)) |
| + | assert_equal 'Revision Not Found', flash[:error] |
| + | end |
| + | |
| + | def test_get_show_for_page_failure |
| + | get :show, :page_id => cms_pages(:default), :id => 'invalid' |
| + | assert_response :redirect |
| + | assert assigns(:record) |
| + | assert_redirected_to edit_cms_admin_page_path(assigns(:record)) |
| + | assert_equal 'Revision Not Found', flash[:error] |
| + | end |
| + | |
| + | def test_get_show_for_snippet_failure |
| + | get :show, :snippet_id => cms_snippets(:default), :id => 'invalid' |
| + | assert_response :redirect |
| + | assert assigns(:record) |
| + | assert_redirected_to edit_cms_admin_snippet_path(assigns(:record)) |
| + | assert_equal 'Revision Not Found', flash[:error] |
| + | end |
| + | |
| + | def test_revert_for_layout |
| + | layout = cms_layouts(:default) |
| + | |
| + | assert_difference 'layout.revisions.count' do |
| + | post :revert, :layout_id => layout, :id => cms_revisions(:layout) |
| + | assert_response :redirect |
| + | assert_redirected_to edit_cms_admin_layout_path(layout) |
| + | assert_equal 'Content Reverted', flash[:notice] |
| + | |
| + | layout.reload |
| + | assert_equal 'revision {{cms:page:default_page_text}}', layout.content |
| + | assert_equal 'revision css', layout.css |
| + | assert_equal 'revision js', layout.js |
| + | end |
| + | end |
| + | |
| + | def test_revert_for_page |
| + | page = cms_pages(:default) |
| + | |
| + | assert_difference 'page.revisions.count' do |
| + | post :revert, :page_id => page, :id => cms_revisions(:page) |
| + | assert_response :redirect |
| + | assert_redirected_to edit_cms_admin_page_path(page) |
| + | assert_equal 'Content Reverted', flash[:notice] |
| + | |
| + | page.reload |
| + | assert_equal [ |
| + | { :label => 'default_field_text', :content => 'revision field content' }, |
| + | { :label => 'default_page_text', :content => 'revision page content' } |
| + | ], page.blocks_attributes |
| + | end |
| + | end |
| + | |
| + | def test_revert_for_snippet |
| + | snippet = cms_snippets(:default) |
| + | |
| + | assert_difference 'snippet.revisions.count' do |
| + | post :revert, :snippet_id => snippet, :id => cms_revisions(:snippet) |
| + | assert_response :redirect |
| + | assert_redirected_to edit_cms_admin_snippet_path(snippet) |
| + | assert_equal 'Content Reverted', flash[:notice] |
| + | |
| + | snippet.reload |
| + | assert_equal 'revision content', snippet.content |
| + | end |
| + | end |
| + | |
| + | end |
| \ No newline at end of file | |
test/unit/revisions_test.rb
+16
-3
| @@ | @@ -41,7 +41,7 @@ class RevisionsTest < ActiveSupport::TestCase |
| ) | |
| layout.reload | |
| assert_equal 2, layout.revisions.count | |
| - | revision = layout.revisions.last |
| + | revision = layout.revisions.first |
| assert_equal old_attributes, revision.data | |
| end | |
| end | |
| @@ | @@ -65,7 +65,7 @@ class RevisionsTest < ActiveSupport::TestCase |
| ) | |
| page.reload | |
| assert_equal 2, page.revisions.count | |
| - | revision = page.revisions.last |
| + | revision = page.revisions.first |
| assert_equal ({ | |
| 'blocks_attributes' => [ | |
| { :label => 'default_field_text', | |
| @@ | @@ -91,7 +91,7 @@ class RevisionsTest < ActiveSupport::TestCase |
| snippet.update_attribute(:content, 'new content') | |
| snippet.reload | |
| assert_equal 2, snippet.revisions.count | |
| - | revision = snippet.revisions.last |
| + | revision = snippet.revisions.first |
| assert_equal old_attributes, revision.data | |
| end | |
| end | |
| @@ | @@ -103,6 +103,19 @@ class RevisionsTest < ActiveSupport::TestCase |
| end | |
| end | |
| + | def test_creation_for_new_record |
| + | assert_difference 'Cms::Snippet.count' do |
| + | assert_no_difference 'Cms::Revision.count' do |
| + | snippet = cms_sites(:default).snippets.create!( |
| + | :label => 'test snippet', |
| + | :slug => 'test_snippet', |
| + | :content => 'test content' |
| + | ) |
| + | assert_equal 0, snippet.revisions.count |
| + | end |
| + | end |
| + | end |
| + | |
| def test_restore_from_revision_for_layout | |
| layout = cms_layouts(:default) | |
| revision = cms_revisions(:layout) | |