adding cms_layout crud

Oleg committed Aug 27, 2010
commit 53f43f50e090912580798244cb824a4c312dcb52
Showing 8 changed files with 160 additions and 5 deletions
app/controllers/cms_admin/layouts_controller.rb +49 -1
@@ @@ -1,5 +1,53 @@
class CmsAdmin::LayoutsController < CmsAdmin::BaseController
- #TODO
+ before_filter :build_cms_layout, :only => [:new, :create]
+ before_filter :load_cms_layout, :only => [:edit, :update, :destroy]
+
+ def index
+ @cms_layouts = CmsLayout.all
+ end
+
+ def new
+ render
+ end
+
+ def edit
+ render
+ end
+
+ def create
+ @cms_layout.save!
+ flash[:notice] = 'Layout successfully created'
+ redirect_to :action => :edit, :id => @cms_layout
+ rescue ActiveRecord::RecordInvalid
+ render :action => :new
+ end
+
+ def update
+ @cms_layout.update_attributes!(params[:cms_layout])
+ flash[:notice] = 'Layout successfully updated'
+ redirect_to :action => :edit, :id => @cms_layout
+ rescue ActiveRecord::RecordInvalid
+ render :action => :edit
+ end
+
+ def destroy
+ @cms_layout.destroy
+ flash[:notice] = 'Layout deleted'
+ redirect_to :action => :index
+ end
+
+ protected
+
+ def build_cms_layout
+ @cms_layout = CmsLayout.new(params[:cms_layout])
+ end
+
+ def load_cms_layout
+ @cms_layout = CmsLayout.find(params[:id])
+ rescue ActiveRecord::RecordNotFound
+ flash[:error] = 'Layout not found'
+ redirect_to :action => :index
+ end
end
app/views/cms_admin/layouts/_form.html.erb +2 -0
@@ @@ -0,0 +1,2 @@
+ <%= form.text_field :label %>
+ <%= form.text_area :content %>
\ No newline at end of file
app/views/cms_admin/layouts/edit.html.erb +6 -0
@@ @@ -0,0 +1,6 @@
+ <h1> Editing Layout </h1>
+
+ <%= form_for @cms_layout, :url => {:action => :update} do |form| %>
+ <%= render :partial => form %>
+ <%= form.submit 'Update Layout' %>
+ <% end %>
\ No newline at end of file
app/views/cms_admin/layouts/index.html.erb +3 -0
@@ @@ -0,0 +1,3 @@
+ <h1>Layouts</h1>
+
+ <%= debug @cms_layouts %>
\ No newline at end of file
app/views/cms_admin/layouts/new.html.erb +6 -0
@@ @@ -0,0 +1,6 @@
+ <h1> New Layout </h1>
+
+ <%= form_for @cms_layout, :url => {:action => :create} do |form| %>
+ <%= render :partial => form %>
+ <%= form.submit 'Create Layout' %>
+ <% end %>
\ No newline at end of file
app/views/layouts/cms_admin.html.erb +11 -1
@@ @@ -6,6 +6,16 @@
<%= yield :head %>
</head>
<body>
- <%= yield %>
+ <div class='body_wrapper'>
+ <div class='left_column'>
+ <%= yield :left_column %>
+ </div>
+ <div class='center_column'>
+ <%= yield %>
+ </div>
+ <div class='right_column'>
+ <%= yield :right_column %>
+ </div>
+ </div>
</body>
</html>
\ No newline at end of file
config/routes.rb +1 -1
@@ @@ -1,6 +1,6 @@
Rails.application.routes.draw do
- namespace :cms_admin do
+ namespace :cms_admin, :except => :show do
resources :layouts
resources :pages
resources :snippets
test/functional/cms_admin/layouts_controller_test.rb +82 -2
@@ @@ -2,8 +2,88 @@ require File.dirname(__FILE__) + '/../../test_helper'
class CmsAdmin::LayoutsControllerTest < ActionController::TestCase
- def test_something
- flunk
+ def test_get_index
+ get :index
+ assert_response :success
+ assert assigns(:cms_layouts)
+ assert_template :index
+ end
+
+ def test_get_new
+ get :new
+ assert_response :success
+ assert assigns(:cms_layout)
+ assert_template :new
+ assert_select 'form[action=/cms_admin/layouts]'
+ end
+
+ def test_get_edit
+ get :edit, :id => cms_layouts(:default)
+ assert_response :success
+ assert assigns(:cms_layout)
+ assert_template :edit
+ assert_select "form[action=/cms_admin/layouts/#{cms_layouts(:default).id}]"
+ end
+
+ def test_get_edit_failure
+ get :edit, :id => 'not_found'
+ assert_response :redirect
+ assert_redirected_to :action => :index
+ assert_equal 'Layout not found', flash[:error]
+ end
+
+ def test_creation
+ assert_difference 'CmsLayout.count' do
+ post :create, :cms_layout => {
+ :label => 'Test Layout',
+ :content => 'Test Content'
+ }
+ assert_response :redirect
+ assert_redirected_to :action => :edit, :id => CmsLayout.last
+ assert_equal 'Layout successfully created', flash[:notice]
+ end
+ end
+
+ def test_creation_failure
+ assert_no_difference 'CmsLayout.count' do
+ post :create, :cms_layout => { }
+ assert_response :success
+ assert_template :new
+ end
+ end
+
+ def test_update
+ layout = cms_layouts(:default)
+ put :update, :id => layout, :cms_layout => {
+ :label => 'New Label',
+ :content => 'New Content'
+ }
+ assert_response :redirect
+ assert_redirected_to :action => :edit, :id => layout
+ assert_equal 'Layout successfully updated', flash[:notice]
+ layout.reload
+ assert_equal 'New Label', layout.label
+ assert_equal 'New Content', layout.content
+ end
+
+ def test_update_failure
+ layout = cms_layouts(:default)
+ put :update, :id => layout, :cms_layout => {
+ :label => ''
+ }
+ assert_response :success
+ assert_template :edit
+ layout.reload
+ assert_not_equal '', layout.label
+ end
+
+ def test_destroy
+ assert_difference 'CmsLayout.count', -1 do
+ delete :destroy, :id => cms_layouts(:default)
+ assert_response :redirect
+ assert_redirected_to :action => :index
+ assert_equal 'Layout deleted', flash[:notice]
+ end
end
end
\ No newline at end of file