From f6328c600dac62dc5b2120ca73fd6fe1ce326b34 Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Fri, 30 Nov 2007 21:13:07 +0000 Subject: [PATCH] ActionItem21: checkpoint --- app/controllers/my_profile/cms/text_html.rb | 24 +----------------------- app/controllers/my_profile/cms_controller.rb | 30 ++++++++++++++++++++++++++++-- app/helpers/cms_helper.rb | 8 ++++++-- app/models/article.rb | 2 ++ test/functional/cms_controller_test.rb | 33 +++++++++++++++++++++++++-------- 5 files changed, 62 insertions(+), 35 deletions(-) diff --git a/app/controllers/my_profile/cms/text_html.rb b/app/controllers/my_profile/cms/text_html.rb index 67b3d82..03c460a 100644 --- a/app/controllers/my_profile/cms/text_html.rb +++ b/app/controllers/my_profile/cms/text_html.rb @@ -1,27 +1,5 @@ class CmsController - def text_html_new - @article = Article.new(params[:article]) - if params[:parent_id] - @article.parent = profile.articles.find(params[:parent_id]) - end - @article.profile = profile - @article.last_changed_by = user - if request.post? - if @article.save - redirect_to :action => 'view', :id => @article.id - end - end - end - - def text_html_edit - @article = Article.find(params[:id]) - if request.post? - @article.last_changed_by = user - if @article.update_attributes(params[:article]) - redirect_to :action => 'view', :id => @article.id - end - end - end + # add eventual helper methods you need for editing your type of article. end diff --git a/app/controllers/my_profile/cms_controller.rb b/app/controllers/my_profile/cms_controller.rb index e42d050..bd4d39f 100644 --- a/app/controllers/my_profile/cms_controller.rb +++ b/app/controllers/my_profile/cms_controller.rb @@ -19,8 +19,34 @@ class CmsController < MyProfileController end def edit - article = profile.articles.find(params[:id]) - redirect_to(url_for_edit_article(article)) + @article = Article.find(params[:id]) + if request.post? + @article.last_changed_by = user + if @article.update_attributes(params[:article]) + redirect_to :action => 'view', :id => @article.id + end + end + + render :action => "#{mime_type_to_action_name(@article.mime_type)}_edit" + end + + def new + # FIXME until now, use text/html by default + type = params[:type] || 'text/html' + + @article = Article.new(params[:article]) + if params[:parent_id] + @article.parent = profile.articles.find(params[:parent_id]) + end + @article.profile = profile + @article.last_changed_by = user + if request.post? + if @article.save + redirect_to :action => 'view', :id => @article.id + end + end + + render :action => "#{mime_type_to_action_name(type)}_new" end post_only :set_home_page diff --git a/app/helpers/cms_helper.rb b/app/helpers/cms_helper.rb index 2cbb02e..fb3a795 100644 --- a/app/helpers/cms_helper.rb +++ b/app/helpers/cms_helper.rb @@ -5,13 +5,17 @@ module CmsHelper end def url_for_edit_article(article) - action = article.mime_type.gsub('/', '_') + '_edit' + action = mime_type_to_action_name(article.mime_type) + '_edit' url_for(:action => action, :id => article.id) end def link_to_new_article(mime_type) - action = mime_type.gsub('/', '_') + '_new' + action = mime_type_to_action_name(mime_type) + '_new' button('new', _("New %s") % mime_type, :action => action, :parent_id => params[:parent_id]) end + def mime_type_to_action_name(mime_type) + mime_type.gsub('/', '_').gsub('-', '') + end + end diff --git a/app/models/article.rb b/app/models/article.rb index d60bb0f..f94799c 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -31,6 +31,8 @@ class Article < ActiveRecord::Base # provides the icon name to be used for this article. In this class this # method just returns 'text-html', but subclasses may (and should) override # to return their specific icons. + # + # FIXME use mime_type and generate this name dinamically def icon_name 'text-html' end diff --git a/test/functional/cms_controller_test.rb b/test/functional/cms_controller_test.rb index d6752b3..c22e738 100644 --- a/test/functional/cms_controller_test.rb +++ b/test/functional/cms_controller_test.rb @@ -42,6 +42,19 @@ class CmsControllerTest < Test::Unit::TestCase end should 'be able to edit a document' do + a = profile.articles.build(:name => 'test') + a.save! + + get :edit, :profile => profile.identifier, :id => a.id + assert_template 'text_html_edit' + end + + should 'be able to type a new document' do + get :new, :profile => profile.identifier + assert_template 'text_html_new' + end + + should 'be able to create a new document' do flunk 'pending' end @@ -73,19 +86,23 @@ class CmsControllerTest < Test::Unit::TestCase assert_equal [ 'text/html', 'image' ], CmsController.available_types end - should 'made the editor actions available' do - # ASSUMING that 'text/html' is always available and has 'new' and 'edit' - assert CmsController.instance_methods.include?('text_html_new') - assert CmsController.instance_methods.include?('text_html_edit') - end - - should 'edit by redirecting to the correct editor depending on the mime-type' do + should 'edit by using the correct template to display the editor depending on the mime-type' do a = profile.articles.build(:name => 'test document') a.save! assert_equal 'text/html', a.mime_type get :edit, :profile => profile.identifier, :id => a.id - assert_redirected_to :action => 'text_html_edit', :id => a.id + assert_response :success + assert_template 'text_html_edit' + end + + should 'convert mime-types to action names' do + obj = mock + obj.extend(CmsHelper) + + assert_equal 'text_html', obj.mime_type_to_action_name('text/html') + assert_equal 'image', obj.mime_type_to_action_name('image') + assert_equal 'application_xnoosferosomething', obj.mime_type_to_action_name('application/x-noosfero-something') end end -- libgit2 0.21.2