From e5160e47a2cecf07fdf331be8674ab1d33b81188 Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Tue, 27 Nov 2007 19:50:52 +0000 Subject: [PATCH] ActionItem21: checkpoint, several small stuff --- app/controllers/my_profile/cms/README | 46 ++++++++++++++++++++++++++++++++++++++++++++++ app/controllers/my_profile/cms/text_html.rb | 23 +++++++++++++++++++++++ app/controllers/my_profile/cms_controller.rb | 21 +++++++++++++++++++++ app/controllers/public/content_viewer_controller.rb | 1 + app/helpers/cms_helper.rb | 14 ++++++++++++++ app/models/article.rb | 8 ++++++++ app/views/cms/new.rhtml | 3 +++ app/views/cms/text_html_edit.rhtml | 1 + app/views/cms/text_html_new.rhtml | 13 +++++++++++++ app/views/cms/view.rhtml | 1 + test/functional/cms_controller_test.rb | 31 +++++++++++++++++++++++++++++-- test/functional/content_viewer_controller_test.rb | 22 ++++++++++++++++------ 12 files changed, 176 insertions(+), 8 deletions(-) create mode 100644 app/controllers/my_profile/cms/README create mode 100644 app/controllers/my_profile/cms/text_html.rb create mode 100644 app/views/cms/new.rhtml create mode 120000 app/views/cms/text_html_edit.rhtml create mode 100644 app/views/cms/text_html_new.rhtml diff --git a/app/controllers/my_profile/cms/README b/app/controllers/my_profile/cms/README new file mode 100644 index 0000000..6bd4cf2 --- /dev/null +++ b/app/controllers/my_profile/cms/README @@ -0,0 +1,46 @@ +CMS editors for Noosfero +======================== + +This directory contains code for custom content editors in Noosfero. + +Creating a new editor +===================== + +:: File structure + +Let's say that you are implementingn and editor for type, say, "foo/bar". Then +you have to create: + + * app/controllers/my_profile/cms/foo_bar.html + * app/views/cms/foo_bar_*.rhtml (one view for each action you define in the + controller class, if applicable). + +:: Coding conventions + +You file must add methods to CmsController class. They are going to be loaded +after the cms_controller.rb file itself, to you don't need to care about +declaring the superclass: + + class CmsController + def foo_bar_edit + # code for preparing the "edit" view + end + + def foo_bar_new + # code for preparing the "new" view + end + + # etc ... + end + +Note that *all* of your actions must be prefixed with the content type (e.g. +"foor_bar_" for "foo/bar"), in order to avoid conflicts. + +The views for those actions can be thrown in app/views/cms/, just like other +views for this controller. + +Limitations +=========== + + + diff --git a/app/controllers/my_profile/cms/text_html.rb b/app/controllers/my_profile/cms/text_html.rb new file mode 100644 index 0000000..a96a105 --- /dev/null +++ b/app/controllers/my_profile/cms/text_html.rb @@ -0,0 +1,23 @@ +class CmsController + + def text_html_new + @article = Article.new(params[:article]) + @article.parent = profile.articles.find(params[:parent_id]) + @article.profile = profile + 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? + if @article.update_attributes(params[:article]) + redirect_to :action => 'view', :id => @article.id + end + end + end + +end diff --git a/app/controllers/my_profile/cms_controller.rb b/app/controllers/my_profile/cms_controller.rb index 6847684..bf1f603 100644 --- a/app/controllers/my_profile/cms_controller.rb +++ b/app/controllers/my_profile/cms_controller.rb @@ -3,6 +3,8 @@ class CmsController < MyProfileController # FIXME add the access control again # protect 'post_content', :profile, :only => [:edit, :new, :reorder, :delete] + include CmsHelper + def view @article = profile.articles.find(params[:id]) @subitems = @article.children @@ -14,6 +16,11 @@ class CmsController < MyProfileController render :action => 'view' end + def edit + article = profile.articles.find(params[:id]) + redirect_to(url_for_edit_article(article)) + end + post_only :set_home_page def set_home_page @article = profile.articles.find(params[:id]) @@ -24,4 +31,18 @@ class CmsController < MyProfileController 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/controllers/public/content_viewer_controller.rb b/app/controllers/public/content_viewer_controller.rb index f4436f8..85c4c63 100644 --- a/app/controllers/public/content_viewer_controller.rb +++ b/app/controllers/public/content_viewer_controller.rb @@ -7,6 +7,7 @@ class ContentViewerController < PublicController if path.blank? @page = profile.home_page + # FIXME need to do something when the user didn't set a homepage else @page = profile.articles.find_by_path(path) end diff --git a/app/helpers/cms_helper.rb b/app/helpers/cms_helper.rb index e476188..ee88335 100644 --- a/app/helpers/cms_helper.rb +++ b/app/helpers/cms_helper.rb @@ -1,3 +1,17 @@ module CmsHelper + def link_to_edit_article(article) + link_to(_("Edit"), url_for_edit_article(article)) + end + + def url_for_edit_article(article) + action = article.mime_type.gsub('/', '_') + '_edit' + url_for(:action => action, :id => article.id) + end + + def link_to_new_article(mime_type) + action = mime_type.gsub('/', '_') + '_new' + link_to(_("New %s") % mime_type, :action => action, :parent_id => params[:parent_id]) + end + end diff --git a/app/models/article.rb b/app/models/article.rb index e7ff79c..6140e41 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -31,4 +31,12 @@ class Article < ActiveRecord::Base 'text-html' end + def mime_type + 'text/html' + end + + def title + name + end + end diff --git a/app/views/cms/new.rhtml b/app/views/cms/new.rhtml new file mode 100644 index 0000000..f5ed00b --- /dev/null +++ b/app/views/cms/new.rhtml @@ -0,0 +1,3 @@ +<% CmsController.available_types.each do |item| %> + <%= link_to_new_article(item) %> +<% end %> diff --git a/app/views/cms/text_html_edit.rhtml b/app/views/cms/text_html_edit.rhtml new file mode 120000 index 0000000..ee9726e --- /dev/null +++ b/app/views/cms/text_html_edit.rhtml @@ -0,0 +1 @@ +text_html_new.rhtml \ No newline at end of file diff --git a/app/views/cms/text_html_new.rhtml b/app/views/cms/text_html_new.rhtml new file mode 100644 index 0000000..6203abf --- /dev/null +++ b/app/views/cms/text_html_new.rhtml @@ -0,0 +1,13 @@ +<%= error_messages_for 'article' %> + +<% labelled_form_for 'article', @article do |f| %> + + <%= hidden_field_tag 'parent_id', params[:parent_id] %> + + <%= f.text_field 'name' %> + + <%= f.text_area('body') %> + + <%= submit_tag _('Save') %> + +<% end %> diff --git a/app/views/cms/view.rhtml b/app/views/cms/view.rhtml index b676991..743905e 100644 --- a/app/views/cms/view.rhtml +++ b/app/views/cms/view.rhtml @@ -30,6 +30,7 @@ <%# display the article content %> <% if @article %> + <%= link_to_edit_article(@article) %>

<%= @article.name %>

<%= @article.to_html %> <% end %> diff --git a/test/functional/cms_controller_test.rb b/test/functional/cms_controller_test.rb index 31a2402..0389140 100644 --- a/test/functional/cms_controller_test.rb +++ b/test/functional/cms_controller_test.rb @@ -42,15 +42,42 @@ class CmsControllerTest < Test::Unit::TestCase end should 'be able to edit a document' do - flunk 'not yet' + flunk 'pending' end should 'be able to save a save a document' do - flunk 'not yet' + flunk 'pending' end should 'be able to set home page' do flunk 'pending' 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 '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 + 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 + end + end diff --git a/test/functional/content_viewer_controller_test.rb b/test/functional/content_viewer_controller_test.rb index 67131cb..19690e8 100644 --- a/test/functional/content_viewer_controller_test.rb +++ b/test/functional/content_viewer_controller_test.rb @@ -12,17 +12,27 @@ class ContentViewerControllerTest < Test::Unit::TestCase @controller = ContentViewerController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new + + @profile = create_user('testinguser').person end + attr_reader :profile + + def test_should_display_page + page = profile.articles.build(:name => 'test') + page.save! - def test_should_display_homepage uses_host 'anhetegua.net' + get :view_page, :profile => profile.identifier, :page => [ 'test' ] + assert_response :success + assert_equal page, assigns(:page) + end - a = Article.new - Article.expects(:find_by_path).with('ze').returns(a) + def test_should_display_homepage + flunk 'pending' + end - get :view_page, :profile => 'ze', :page => [] - assert_response :success - assert_equal a, assigns(:page) + def test_should_display_something_else_for_empty_homepage + flunk 'pending' end def test_should_get_not_found_error_for_unexisting_page -- libgit2 0.21.2