Commit f6328c600dac62dc5b2120ca73fd6fe1ce326b34

Authored by AntonioTerceiro
1 parent d7d70f16

ActionItem21: checkpoint



git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@986 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/my_profile/cms/text_html.rb
1 1 class CmsController
2 2  
3   - def text_html_new
4   - @article = Article.new(params[:article])
5   - if params[:parent_id]
6   - @article.parent = profile.articles.find(params[:parent_id])
7   - end
8   - @article.profile = profile
9   - @article.last_changed_by = user
10   - if request.post?
11   - if @article.save
12   - redirect_to :action => 'view', :id => @article.id
13   - end
14   - end
15   - end
16   -
17   - def text_html_edit
18   - @article = Article.find(params[:id])
19   - if request.post?
20   - @article.last_changed_by = user
21   - if @article.update_attributes(params[:article])
22   - redirect_to :action => 'view', :id => @article.id
23   - end
24   - end
25   - end
  3 + # add eventual helper methods you need for editing your type of article.
26 4  
27 5 end
... ...
app/controllers/my_profile/cms_controller.rb
... ... @@ -19,8 +19,34 @@ class CmsController < MyProfileController
19 19 end
20 20  
21 21 def edit
22   - article = profile.articles.find(params[:id])
23   - redirect_to(url_for_edit_article(article))
  22 + @article = Article.find(params[:id])
  23 + if request.post?
  24 + @article.last_changed_by = user
  25 + if @article.update_attributes(params[:article])
  26 + redirect_to :action => 'view', :id => @article.id
  27 + end
  28 + end
  29 +
  30 + render :action => "#{mime_type_to_action_name(@article.mime_type)}_edit"
  31 + end
  32 +
  33 + def new
  34 + # FIXME until now, use text/html by default
  35 + type = params[:type] || 'text/html'
  36 +
  37 + @article = Article.new(params[:article])
  38 + if params[:parent_id]
  39 + @article.parent = profile.articles.find(params[:parent_id])
  40 + end
  41 + @article.profile = profile
  42 + @article.last_changed_by = user
  43 + if request.post?
  44 + if @article.save
  45 + redirect_to :action => 'view', :id => @article.id
  46 + end
  47 + end
  48 +
  49 + render :action => "#{mime_type_to_action_name(type)}_new"
24 50 end
25 51  
26 52 post_only :set_home_page
... ...
app/helpers/cms_helper.rb
... ... @@ -5,13 +5,17 @@ module CmsHelper
5 5 end
6 6  
7 7 def url_for_edit_article(article)
8   - action = article.mime_type.gsub('/', '_') + '_edit'
  8 + action = mime_type_to_action_name(article.mime_type) + '_edit'
9 9 url_for(:action => action, :id => article.id)
10 10 end
11 11  
12 12 def link_to_new_article(mime_type)
13   - action = mime_type.gsub('/', '_') + '_new'
  13 + action = mime_type_to_action_name(mime_type) + '_new'
14 14 button('new', _("New %s") % mime_type, :action => action, :parent_id => params[:parent_id])
15 15 end
16 16  
  17 + def mime_type_to_action_name(mime_type)
  18 + mime_type.gsub('/', '_').gsub('-', '')
  19 + end
  20 +
17 21 end
... ...
app/models/article.rb
... ... @@ -31,6 +31,8 @@ class Article < ActiveRecord::Base
31 31 # provides the icon name to be used for this article. In this class this
32 32 # method just returns 'text-html', but subclasses may (and should) override
33 33 # to return their specific icons.
  34 + #
  35 + # FIXME use mime_type and generate this name dinamically
34 36 def icon_name
35 37 'text-html'
36 38 end
... ...
test/functional/cms_controller_test.rb
... ... @@ -42,6 +42,19 @@ class CmsControllerTest < Test::Unit::TestCase
42 42 end
43 43  
44 44 should 'be able to edit a document' do
  45 + a = profile.articles.build(:name => 'test')
  46 + a.save!
  47 +
  48 + get :edit, :profile => profile.identifier, :id => a.id
  49 + assert_template 'text_html_edit'
  50 + end
  51 +
  52 + should 'be able to type a new document' do
  53 + get :new, :profile => profile.identifier
  54 + assert_template 'text_html_new'
  55 + end
  56 +
  57 + should 'be able to create a new document' do
45 58 flunk 'pending'
46 59 end
47 60  
... ... @@ -73,19 +86,23 @@ class CmsControllerTest < Test::Unit::TestCase
73 86 assert_equal [ 'text/html', 'image' ], CmsController.available_types
74 87 end
75 88  
76   - should 'made the editor actions available' do
77   - # ASSUMING that 'text/html' is always available and has 'new' and 'edit'
78   - assert CmsController.instance_methods.include?('text_html_new')
79   - assert CmsController.instance_methods.include?('text_html_edit')
80   - end
81   -
82   - should 'edit by redirecting to the correct editor depending on the mime-type' do
  89 + should 'edit by using the correct template to display the editor depending on the mime-type' do
83 90 a = profile.articles.build(:name => 'test document')
84 91 a.save!
85 92 assert_equal 'text/html', a.mime_type
86 93  
87 94 get :edit, :profile => profile.identifier, :id => a.id
88   - assert_redirected_to :action => 'text_html_edit', :id => a.id
  95 + assert_response :success
  96 + assert_template 'text_html_edit'
  97 + end
  98 +
  99 + should 'convert mime-types to action names' do
  100 + obj = mock
  101 + obj.extend(CmsHelper)
  102 +
  103 + assert_equal 'text_html', obj.mime_type_to_action_name('text/html')
  104 + assert_equal 'image', obj.mime_type_to_action_name('image')
  105 + assert_equal 'application_xnoosferosomething', obj.mime_type_to_action_name('application/x-noosfero-something')
89 106 end
90 107  
91 108 end
... ...