added CRUD for snippets
Hesham E
committed Sep 01, 2010
commit 32e99321d5e2092744f0697f9a9947a857d107f9
Showing 6
changed files with
126 additions
and 3 deletions
app/controllers/cms_admin/snippets_controller.rb
+46
-1
| @@ | @@ -1,5 +1,50 @@ |
| class CmsAdmin::SnippetsController < CmsAdmin::BaseController | |
| + | before_filter :build_cms_snippet, |
| + | :only => [:new, :create] |
| + | before_filter :load_cms_snippet, |
| + | :only => [:edit, :update, :destroy] |
| + | |
| + | def index |
| + | @cms_snippets = CmsSnippet.all(:order => 'label') |
| + | end |
| - | # TODO |
| + | def new |
| + | end |
| + | def create |
| + | @cms_snippet.save! |
| + | flash[:notice] = 'Snippet saved' |
| + | redirect_to :action => :edit, :id => @cms_snippet |
| + | rescue ActiveRecord::RecordInvalid |
| + | render :action => :new |
| + | end |
| + | |
| + | def edit |
| + | end |
| + | |
| + | def update |
| + | @cms_snippet.update_attributes!(params[:cms_snippet]) |
| + | flash[:notice] = 'Snippet saved' |
| + | redirect_to :action => :edit, :id => @cms_snippet |
| + | rescue ActiveRecord::RecordInvalid |
| + | render :action => :edit |
| + | end |
| + | |
| + | def destroy |
| + | @cms_snippet.destroy |
| + | flash[:notice] = 'Snippet deleted' |
| + | redirect_to :action => :index |
| + | end |
| + | |
| + | protected |
| + | def build_cms_snippet |
| + | @cms_snippet = CmsSnippet.new(params[:cms_snippet]) |
| + | end |
| + | |
| + | def load_cms_snippet |
| + | @cms_snippet = CmsSnippet.find(params[:id]) |
| + | rescue ActiveRecord::RecordNotFound |
| + | flash[:error] = 'Snippet not found' |
| + | redirect_to :action => :index |
| + | end |
| end | |
app/views/cms_admin/snippets/_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/snippets/edit.html.erb
+6
-0
| @@ | @@ -0,0 +1,6 @@ |
| + | <h1> Editing Snippet </h1> |
| + | |
| + | <%= cms_form_for @cms_snippet, :url => {:action => :update} do |form| %> |
| + | <%= render :partial => 'form', :object => form %> |
| + | <%= form.submit 'Update Snippet' %> |
| + | <% end %> |
| \ No newline at end of file | |
app/views/cms_admin/snippets/index.html.erb
+3
-0
| @@ | @@ -0,0 +1,3 @@ |
| + | <h1>Snippets</h1> |
| + | |
| + | <%= debug @cms_snippets %> |
| \ No newline at end of file | |
app/views/cms_admin/snippets/new.html.erb
+6
-0
| @@ | @@ -0,0 +1,6 @@ |
| + | <h1> New Snippet </h1> |
| + | |
| + | <%= cms_form_for @cms_snippet, :url => {:action => :create} do |form| %> |
| + | <%= render :partial => 'form', :object => form %> |
| + | <%= form.submit 'Create Snippet' %> |
| + | <% end %> |
| \ No newline at end of file | |
test/functional/cms_admin/snippets_controller_test.rb
+63
-2
| @@ | @@ -1,9 +1,70 @@ |
| require File.dirname(__FILE__) + '/../../test_helper' | |
| class CmsAdmin::SnippetsControllerTest < ActionController::TestCase | |
| + | |
| + | def test_index |
| + | get :index |
| + | assert_response :success |
| + | assert_template 'index' |
| + | end |
| + | |
| + | def test_new |
| + | get :new |
| + | assert_response :success |
| + | assert_template 'new' |
| + | end |
| + | |
| + | def test_create |
| + | assert_difference 'CmsSnippet.count', 1 do |
| + | post :create, :cms_snippet => cms_snippet_params |
| + | end |
| + | assert_equal 'Snippet saved', flash[:notice] |
| + | assert_redirected_to edit_cms_admin_snippet_path(assigns(:cms_snippet)) |
| + | end |
| + | |
| + | def test_create_fails |
| + | assert_no_difference 'CmsSnippet.count' do |
| + | post :create, :cms_snippet => cms_snippet_params(:label => '') |
| + | end |
| + | assert_response :success |
| + | assert_template 'new' |
| + | end |
| - | def test_something |
| - | flunk |
| + | def test_edit |
| + | get :edit, :id => cms_snippets(:default) |
| + | assert_response :success |
| + | assert_template 'edit' |
| end | |
| + | def test_update |
| + | snippet = cms_snippets(:default) |
| + | put :update, :id => snippet, :cms_snippet => {:label => 'new-label'} |
| + | assert_equal 'Snippet saved', flash[:notice] |
| + | assert_redirected_to edit_cms_admin_snippet_path(assigns(:cms_snippet)) |
| + | assert_equal 'new-label', assigns(:cms_snippet).label |
| + | end |
| + | |
| + | def test_update_fails |
| + | snippet = cms_snippets(:default) |
| + | put :update, :id => snippet, :cms_snippet => {:label => ''} |
| + | assert_response :success |
| + | assert_template 'edit' |
| + | assert_equal 'default_snippet', snippet.label |
| + | end |
| + | |
| + | def test_destroy |
| + | assert_difference 'CmsSnippet.count', -1 do |
| + | delete :destroy, :id => cms_snippets(:default) |
| + | end |
| + | assert_equal 'Snippet deleted', flash[:notice] |
| + | assert_redirected_to cms_admin_snippets_path |
| + | end |
| + | |
| + | protected |
| + | def cms_snippet_params(options = {}) |
| + | { |
| + | :label => 'snippet-label', |
| + | :content => 'Lorem ipsum dolor sit amet, consectetur adipisicing elit' |
| + | }.merge(options) |
| + | end |
| end | |
| \ No newline at end of file | |