Commit 62ccc88c42d7e76119290c82a0a9acc60318def8

Authored by AntonioTerceiro
1 parent 4c09e30e

ActionItem24: preparing CMS controller to cope with types of articles



git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1126 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/my_profile/cms_controller.rb
@@ -6,6 +6,10 @@ class CmsController < MyProfileController @@ -6,6 +6,10 @@ class CmsController < MyProfileController
6 6
7 include CmsHelper 7 include CmsHelper
8 8
  9 + ARTICLE_TYPES = [
  10 + TinyMceArticle
  11 + ]
  12 +
9 def view 13 def view
10 @article = profile.articles.find(params[:id]) 14 @article = profile.articles.find(params[:id])
11 @subitems = @article.children 15 @subitems = @article.children
@@ -29,10 +33,26 @@ class CmsController < MyProfileController @@ -29,10 +33,26 @@ class CmsController < MyProfileController
29 end 33 end
30 34
31 def new 35 def new
32 - # FIXME need to display a page and let the user choose a type of article.  
33 - type = params[:type] || 'text/html' 36 + # user must choose an article type first
  37 + type = params[:type]
  38 + if type.blank?
  39 + @article_types = []
  40 + ARTICLE_TYPES.each do |type|
  41 + @article_types.push({
  42 + :name => type.name,
  43 + :short_description => type.short_description,
  44 + :description => type.description
  45 + })
  46 + end
  47 + render :action => 'select_article_type'
  48 + return
  49 + end
  50 +
  51 + raise "Invalid article type #{type}" unless ARTICLE_TYPES.map {|item| item.name}.include?(type)
  52 + klass = type.constantize
  53 + @article = klass.new(params[:article])
  54 +
34 55
35 - @article = Article.new(params[:article])  
36 if params[:parent_id] 56 if params[:parent_id]
37 @article.parent = profile.articles.find(params[:parent_id]) 57 @article.parent = profile.articles.find(params[:parent_id])
38 end 58 end
@@ -63,20 +83,5 @@ class CmsController < MyProfileController @@ -63,20 +83,5 @@ class CmsController < MyProfileController
63 redirect_to :action => (@article.parent ? 'view' : 'index'), :id => @article.parent 83 redirect_to :action => (@article.parent ? 'view' : 'index'), :id => @article.parent
64 end 84 end
65 85
66 - protected  
67 -  
68 - class << self  
69 - def available_editors  
70 - Dir.glob(File.join(File.dirname(__FILE__), 'cms', '*.rb'))  
71 - end  
72 -  
73 - def available_types  
74 - available_editors.map {|item| File.basename(item).gsub(/\.rb$/, '').gsub('_', '/') }  
75 - end  
76 - end  
77 -  
78 end 86 end
79 87
80 -CmsController.available_editors.each do |item|  
81 - load item  
82 -end  
app/views/cms/edit.rhtml
@@ -6,6 +6,8 @@ @@ -6,6 +6,8 @@
6 6
7 <% labelled_form_for 'article', @article do |f| %> 7 <% labelled_form_for 'article', @article do |f| %>
8 8
  9 + <%= hidden_field_tag("type", params[:type]) if params[:type] %>
  10 +
9 <%= hidden_field_tag('parent_id', params[:parent_id]) if params[:parent_id] %> 11 <%= hidden_field_tag('parent_id', params[:parent_id]) if params[:parent_id] %>
10 12
11 <%= render :partial => partial_for_class(@article.class), :locals => { :f => f } %> 13 <%= render :partial => partial_for_class(@article.class), :locals => { :f => f } %>
app/views/cms/select_article_type.rhtml
@@ -3,7 +3,10 @@ @@ -3,7 +3,10 @@
3 </p> 3 </p>
4 4
5 <ul> 5 <ul>
6 -<% CmsController.available_types.each do |item| %>  
7 - <li><%= link_to_new_article(item) %></li>  
8 -<% end %> 6 + <% for type in @article_types %>
  7 + <li>
  8 + <%= link_to type[:short_description], :action => 'new', :type => type[:name] %>
  9 + <p><%= type[:description] %></p>
  10 + </li>
  11 + <% end %>
9 </ul> 12 </ul>
test/functional/cms_controller_test.rb
@@ -52,18 +52,25 @@ class CmsControllerTest &lt; Test::Unit::TestCase @@ -52,18 +52,25 @@ class CmsControllerTest &lt; Test::Unit::TestCase
52 52
53 should 'be able to create a new document' do 53 should 'be able to create a new document' do
54 get :new, :profile => profile.identifier 54 get :new, :profile => profile.identifier
  55 + assert_response :success
55 assert_template 'select_article_type' 56 assert_template 'select_article_type'
56 - assert_tag :tag => 'form', :attributes => { :action => "/myprofile/#{profile.identifier}/cms/new", :method => /post/i } 57 +
  58 + # TODO add more types here !!
  59 + [ 'TinyMceArticle' ].each do |item|
  60 + assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/cms/new?type=#{item}" }
  61 + end
57 end 62 end
58 63
59 should 'present edit screen after choosing article type' do 64 should 'present edit screen after choosing article type' do
60 - get :new, :profile => profile.identifier, :article_type => 'Article' 65 + get :new, :profile => profile.identifier, :type => 'TinyMceArticle'
61 assert_template 'edit' 66 assert_template 'edit'
  67 +
  68 + assert_tag :tag => 'form', :attributes => { :action => "/myprofile/#{profile.identifier}/cms/new", :method => /post/i }, :descendant => { :tag => "input", :attributes => { :type => 'hidden', :value => 'TinyMceArticle' }}
62 end 69 end
63 70
64 - should 'be able to save a save a document' do 71 + should 'be able to save a document' do
65 assert_difference Article, :count do 72 assert_difference Article, :count do
66 - post :new, :profile => profile.identifier, :article => { :name => 'a test article', :body => 'the text of the article ...' } 73 + post :new, :type => 'TinyMceArticle', :profile => profile.identifier, :article => { :name => 'a test article', :body => 'the text of the article ...' }
67 end 74 end
68 end 75 end
69 76
@@ -84,7 +91,7 @@ class CmsControllerTest &lt; Test::Unit::TestCase @@ -84,7 +91,7 @@ class CmsControllerTest &lt; Test::Unit::TestCase
84 should 'set last_changed_by when creating article' do 91 should 'set last_changed_by when creating article' do
85 login_as(profile.identifier) 92 login_as(profile.identifier)
86 93
87 - post :new, :profile => profile.identifier, :article => { :name => 'changed by me', :body => 'content ...' } 94 + post :new, :type => 'TinyMceArticle', :profile => profile.identifier, :article => { :name => 'changed by me', :body => 'content ...' }
88 95
89 a = profile.articles.find_by_path('changed-by-me') 96 a = profile.articles.find_by_path('changed-by-me')
90 assert_not_nil a 97 assert_not_nil a
@@ -106,18 +113,6 @@ class CmsControllerTest &lt; Test::Unit::TestCase @@ -106,18 +113,6 @@ class CmsControllerTest &lt; Test::Unit::TestCase
106 assert_equal profile, a.last_changed_by 113 assert_equal profile, a.last_changed_by
107 end 114 end
108 115
109 - should 'list available editors' do  
110 - editors = [ "#{RAILS_ROOT}/app/controllers/my_profile/cms/bli.rb", "#{RAILS_ROOT}/app/controllers/my_profile/cms/blo.rb" ]  
111 - Dir.expects(:glob).with("#{RAILS_ROOT}/app/controllers/my_profile/cms/*.rb").returns(editors)  
112 - assert_equal editors, CmsController.available_editors  
113 - end  
114 -  
115 - should 'list available types' do  
116 - editors = [ "#{RAILS_ROOT}/app/controllers/my_profile/cms/text_html.rb", "#{RAILS_ROOT}/app/controllers/my_profile/cms/image.rb" ]  
117 - CmsController.expects(:available_editors).returns(editors)  
118 - assert_equal [ 'text/html', 'image' ], CmsController.available_types  
119 - end  
120 -  
121 should 'edit by using the correct template to display the editor depending on the mime-type' do 116 should 'edit by using the correct template to display the editor depending on the mime-type' do
122 a = profile.articles.build(:name => 'test document') 117 a = profile.articles.build(:name => 'test document')
123 a.save! 118 a.save!
test/integration/manage_documents_test.rb
@@ -15,10 +15,13 @@ class ManageDocumentsTest &lt; ActionController::IntegrationTest @@ -15,10 +15,13 @@ class ManageDocumentsTest &lt; ActionController::IntegrationTest
15 assert_tag :tag => 'a', :attributes => { :href => '/myprofile/myuser/cms' } 15 assert_tag :tag => 'a', :attributes => { :href => '/myprofile/myuser/cms' }
16 get '/myprofile/myuser/cms/new' 16 get '/myprofile/myuser/cms/new'
17 17
  18 + assert_tag :tag => 'a', :attributes => { :href => '/myprofile/myuser/cms/new?type=TinyMceArticle' }
  19 + get '/myprofile/myuser/cms/new?type=TinyMceArticle'
  20 +
18 assert_tag :tag => 'form', :attributes => { :action => '/myprofile/myuser/cms/new', :method => /post/i } 21 assert_tag :tag => 'form', :attributes => { :action => '/myprofile/myuser/cms/new', :method => /post/i }
19 22
20 assert_difference Article, :count do 23 assert_difference Article, :count do
21 - post '/myprofile/myuser/cms/new', :article => { :name => 'my article', :body => 'this is the body of ther article'} 24 + post '/myprofile/myuser/cms/new', :type => 'TinyMceArticle', :article => { :name => 'my article', :body => 'this is the body of ther article'}
22 end 25 end
23 26
24 assert_response :redirect 27 assert_response :redirect