From 62ccc88c42d7e76119290c82a0a9acc60318def8 Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Wed, 26 Dec 2007 20:00:09 +0000 Subject: [PATCH] ActionItem24: preparing CMS controller to cope with types of articles --- app/controllers/my_profile/cms_controller.rb | 41 +++++++++++++++++++++++------------------ app/views/cms/edit.rhtml | 2 ++ app/views/cms/select_article_type.rhtml | 9 ++++++--- test/functional/cms_controller_test.rb | 29 ++++++++++++----------------- test/integration/manage_documents_test.rb | 5 ++++- 5 files changed, 47 insertions(+), 39 deletions(-) diff --git a/app/controllers/my_profile/cms_controller.rb b/app/controllers/my_profile/cms_controller.rb index ec7f5fc..0c07255 100644 --- a/app/controllers/my_profile/cms_controller.rb +++ b/app/controllers/my_profile/cms_controller.rb @@ -6,6 +6,10 @@ class CmsController < MyProfileController include CmsHelper + ARTICLE_TYPES = [ + TinyMceArticle + ] + def view @article = profile.articles.find(params[:id]) @subitems = @article.children @@ -29,10 +33,26 @@ class CmsController < MyProfileController end def new - # FIXME need to display a page and let the user choose a type of article. - type = params[:type] || 'text/html' + # user must choose an article type first + type = params[:type] + if type.blank? + @article_types = [] + ARTICLE_TYPES.each do |type| + @article_types.push({ + :name => type.name, + :short_description => type.short_description, + :description => type.description + }) + end + render :action => 'select_article_type' + return + end + + raise "Invalid article type #{type}" unless ARTICLE_TYPES.map {|item| item.name}.include?(type) + klass = type.constantize + @article = klass.new(params[:article]) + - @article = Article.new(params[:article]) if params[:parent_id] @article.parent = profile.articles.find(params[:parent_id]) end @@ -63,20 +83,5 @@ class CmsController < MyProfileController redirect_to :action => (@article.parent ? 'view' : 'index'), :id => @article.parent end - protected - - class << self - def available_editors - Dir.glob(File.join(File.dirname(__FILE__), 'cms', '*.rb')) - end - - def available_types - available_editors.map {|item| File.basename(item).gsub(/\.rb$/, '').gsub('_', '/') } - end - end - end -CmsController.available_editors.each do |item| - load item -end diff --git a/app/views/cms/edit.rhtml b/app/views/cms/edit.rhtml index 4dccdb8..b016899 100644 --- a/app/views/cms/edit.rhtml +++ b/app/views/cms/edit.rhtml @@ -6,6 +6,8 @@ <% labelled_form_for 'article', @article do |f| %> + <%= hidden_field_tag("type", params[:type]) if params[:type] %> + <%= hidden_field_tag('parent_id', params[:parent_id]) if params[:parent_id] %> <%= render :partial => partial_for_class(@article.class), :locals => { :f => f } %> diff --git a/app/views/cms/select_article_type.rhtml b/app/views/cms/select_article_type.rhtml index 7d7445f..a641680 100644 --- a/app/views/cms/select_article_type.rhtml +++ b/app/views/cms/select_article_type.rhtml @@ -3,7 +3,10 @@

diff --git a/test/functional/cms_controller_test.rb b/test/functional/cms_controller_test.rb index 0c479b3..077c5ff 100644 --- a/test/functional/cms_controller_test.rb +++ b/test/functional/cms_controller_test.rb @@ -52,18 +52,25 @@ class CmsControllerTest < Test::Unit::TestCase should 'be able to create a new document' do get :new, :profile => profile.identifier + assert_response :success assert_template 'select_article_type' - assert_tag :tag => 'form', :attributes => { :action => "/myprofile/#{profile.identifier}/cms/new", :method => /post/i } + + # TODO add more types here !! + [ 'TinyMceArticle' ].each do |item| + assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/cms/new?type=#{item}" } + end end should 'present edit screen after choosing article type' do - get :new, :profile => profile.identifier, :article_type => 'Article' + get :new, :profile => profile.identifier, :type => 'TinyMceArticle' assert_template 'edit' + + assert_tag :tag => 'form', :attributes => { :action => "/myprofile/#{profile.identifier}/cms/new", :method => /post/i }, :descendant => { :tag => "input", :attributes => { :type => 'hidden', :value => 'TinyMceArticle' }} end - should 'be able to save a save a document' do + should 'be able to save a document' do assert_difference Article, :count do - post :new, :profile => profile.identifier, :article => { :name => 'a test article', :body => 'the text of the article ...' } + post :new, :type => 'TinyMceArticle', :profile => profile.identifier, :article => { :name => 'a test article', :body => 'the text of the article ...' } end end @@ -84,7 +91,7 @@ class CmsControllerTest < Test::Unit::TestCase should 'set last_changed_by when creating article' do login_as(profile.identifier) - post :new, :profile => profile.identifier, :article => { :name => 'changed by me', :body => 'content ...' } + post :new, :type => 'TinyMceArticle', :profile => profile.identifier, :article => { :name => 'changed by me', :body => 'content ...' } a = profile.articles.find_by_path('changed-by-me') assert_not_nil a @@ -106,18 +113,6 @@ class CmsControllerTest < Test::Unit::TestCase assert_equal profile, a.last_changed_by end - should 'list available editors' do - editors = [ "#{RAILS_ROOT}/app/controllers/my_profile/cms/bli.rb", "#{RAILS_ROOT}/app/controllers/my_profile/cms/blo.rb" ] - Dir.expects(:glob).with("#{RAILS_ROOT}/app/controllers/my_profile/cms/*.rb").returns(editors) - assert_equal editors, CmsController.available_editors - end - - should 'list available types' do - editors = [ "#{RAILS_ROOT}/app/controllers/my_profile/cms/text_html.rb", "#{RAILS_ROOT}/app/controllers/my_profile/cms/image.rb" ] - CmsController.expects(:available_editors).returns(editors) - assert_equal [ 'text/html', 'image' ], CmsController.available_types - end - 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! diff --git a/test/integration/manage_documents_test.rb b/test/integration/manage_documents_test.rb index 01bfc75..8b63d84 100644 --- a/test/integration/manage_documents_test.rb +++ b/test/integration/manage_documents_test.rb @@ -15,10 +15,13 @@ class ManageDocumentsTest < ActionController::IntegrationTest assert_tag :tag => 'a', :attributes => { :href => '/myprofile/myuser/cms' } get '/myprofile/myuser/cms/new' + assert_tag :tag => 'a', :attributes => { :href => '/myprofile/myuser/cms/new?type=TinyMceArticle' } + get '/myprofile/myuser/cms/new?type=TinyMceArticle' + assert_tag :tag => 'form', :attributes => { :action => '/myprofile/myuser/cms/new', :method => /post/i } assert_difference Article, :count do - post '/myprofile/myuser/cms/new', :article => { :name => 'my article', :body => 'this is the body of ther article'} + post '/myprofile/myuser/cms/new', :type => 'TinyMceArticle', :article => { :name => 'my article', :body => 'this is the body of ther article'} end assert_response :redirect -- libgit2 0.21.2