From a79d01ab7f327b07409c24efa5e0fba73626d076 Mon Sep 17 00:00:00 2001 From: Leandro Nunes dos Santos Date: Tue, 31 Mar 2015 13:32:40 -0300 Subject: [PATCH] hotspot: adding article_extra_toolbar_buttons hostspot --- app/helpers/application_helper.rb | 1 + app/views/content_viewer/_article_toolbar.html.erb | 5 ++++- lib/noosfero/plugin.rb | 15 +++++++++++---- test/functional/content_viewer_controller_test.rb | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/unit/plugin_test.rb | 9 +++++++++ 5 files changed, 97 insertions(+), 5 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 571d3f9..dc08a4c 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -212,6 +212,7 @@ module ApplicationHelper end def button(type, label, url, html_options = {}) + html_options ||= {} the_class = 'with-text' if html_options.has_key?(:class) the_class << ' ' << html_options[:class] diff --git a/app/views/content_viewer/_article_toolbar.html.erb b/app/views/content_viewer/_article_toolbar.html.erb index 87da0a9..e157fd4 100644 --- a/app/views/content_viewer/_article_toolbar.html.erb +++ b/app/views/content_viewer/_article_toolbar.html.erb @@ -46,7 +46,10 @@ <%= button(:clock, _('All versions'), {:controller => 'content_viewer', :profile => profile.identifier, :action => 'article_versions'}, :id => 'article-versions-link') %> <% end %> - <%= @plugins.dispatch(:article_toolbar_actions, @page).collect { |content| instance_exec(&content) }.join("")%> + <% @plugins.dispatch(:article_extra_toolbar_buttons, @page).each do |plugin_button| %> + <%= button plugin_button[:icon], plugin_button[:title], plugin_button[:url], plugin_button[:html_options] %> + <% end %> + <%= report_abuse(profile, :link, @page) %> diff --git a/lib/noosfero/plugin.rb b/lib/noosfero/plugin.rb index 0e65133..d8fa600 100644 --- a/lib/noosfero/plugin.rb +++ b/lib/noosfero/plugin.rb @@ -426,10 +426,17 @@ class Noosfero::Plugin [] end - # -> Adds aditional actions to article - # returns = lambda block that creates html code - def article_toolbar_actions article - nil + # -> Adds aditional action buttons to article + # returns = { :title => title, :icon => icon, :url => url, :html_options => {} } + # title = name that will be displayed. + # icon = css class name (for customized icons include them in a css file). + # url = url or route to which the button will redirect. + # html_options = Html options for customization + # + # Multiple values could be passed as parameter. + # returns = [{:title => title, :icon => icon}, {:title => title, :icon => icon}] + def article_extra_toolbar_buttons(article) + [] end # -> Adds adicional content to article diff --git a/test/functional/content_viewer_controller_test.rb b/test/functional/content_viewer_controller_test.rb index af40524..d291c90 100644 --- a/test/functional/content_viewer_controller_test.rb +++ b/test/functional/content_viewer_controller_test.rb @@ -1436,4 +1436,76 @@ class ContentViewerControllerTest < ActionController::TestCase assert_no_tag :tag => 'h1', :attributes => { :class => /title/ }, :content => article.name end + + should 'add extra toolbar actions on article from plugins' do + class Plugin1 < Noosfero::Plugin + def article_extra_toolbar_buttons(article) + {:title => 'some_title1', :icon => 'some_icon1', :url => {}} + end + end + Noosfero::Plugin.stubs(:all).returns([Plugin1.name]) + + Environment.default.enable_plugin(Plugin1.name) + + page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text') + + get :view_page, :profile => profile.identifier, :page => [ 'myarticle' ] + assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :title => "some_title1" }} + end + + should 'add more than one extra toolbar actions on article from plugins' do + class Plugin1 < Noosfero::Plugin + def article_extra_toolbar_buttons(article) + {:title => 'some_title1', :icon => 'some_icon1', :url => {}} + end + end + class Plugin2 < Noosfero::Plugin + def article_extra_toolbar_buttons(article) + {:title => 'some_title2', :icon => 'some_icon2', :url => {}} + end + end + Noosfero::Plugin.stubs(:all).returns([Plugin1.name, Plugin2.name]) + + Environment.default.enable_plugin(Plugin1.name) + Environment.default.enable_plugin(Plugin2.name) + + page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text') + + get :view_page, :profile => profile.identifier, :page => [ 'myarticle' ] + assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :title => "some_title1" }} + assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :title => "some_title2" }} + end + + should 'add icon attribute in extra toolbar actions on article from plugins' do + class Plugin1 < Noosfero::Plugin + def article_extra_toolbar_buttons(article) + {:title => 'some_title1', :icon => 'some_icon1', :url => {}} + end + end + Noosfero::Plugin.stubs(:all).returns([Plugin1.name]) + + Environment.default.enable_plugin(Plugin1.name) + + page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text') + + get :view_page, :profile => profile.identifier, :page => [ 'myarticle' ] + assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :class => /some_icon1/ }} + end + + should 'add url attribute in extra toolbar actions on article from plugins' do + class Plugin1 < Noosfero::Plugin + def article_extra_toolbar_buttons(article) + {:title => 'some_title1', :icon => 'some_icon1', :url => '/bli'} + end + end + Noosfero::Plugin.stubs(:all).returns([Plugin1.name]) + + Environment.default.enable_plugin(Plugin1.name) + + page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text') + + get :view_page, :profile => profile.identifier, :page => [ 'myarticle' ] + assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :href => "/bli" }} + end + end diff --git a/test/unit/plugin_test.rb b/test/unit/plugin_test.rb index a2ad18e..460ca26 100644 --- a/test/unit/plugin_test.rb +++ b/test/unit/plugin_test.rb @@ -560,4 +560,13 @@ class PluginTest < ActiveSupport::TestCase assert_equivalent [st4.term, st2.term], limited_suggestions end + should 'article_extra_toolbar_buttons return an empty array by default' do + class CustomBlock1 < Block; end; + + class Plugin1 < Noosfero::Plugin + end + p = Plugin1.new + assert_equal [], p.article_extra_toolbar_buttons(nil) + end + end -- libgit2 0.21.2