From fe6897823ec70d42d6617e90bc69a58c98aa9302 Mon Sep 17 00:00:00 2001 From: Larissa Reis Date: Thu, 31 Jul 2014 05:44:35 -0300 Subject: [PATCH] social-share: plugin configuration page --- plugins/social_share_privacy/controllers/social_share_privacy_plugin_admin_controller.rb | 27 +++++++++++++++++++++++++++ plugins/social_share_privacy/lib/social_share_privacy_plugin.rb | 8 ++++++++ plugins/social_share_privacy/lib/social_share_privacy_plugin_helper.rb | 7 +++++++ plugins/social_share_privacy/public/style.css | 6 ++++++ plugins/social_share_privacy/test/functional/social_share_privacy_plugin_admin_controller_test.rb | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/social_share_privacy/test/unit/social_share_privacy_helper_test.rb | 11 +++++++++++ plugins/social_share_privacy/test/unit/social_share_privacy_test.rb | 5 +++++ plugins/social_share_privacy/views/social_share_privacy_plugin_admin/index.html.erb | 44 ++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 162 insertions(+), 0 deletions(-) create mode 100644 plugins/social_share_privacy/controllers/social_share_privacy_plugin_admin_controller.rb create mode 100644 plugins/social_share_privacy/lib/social_share_privacy_plugin_helper.rb create mode 100644 plugins/social_share_privacy/public/style.css create mode 100644 plugins/social_share_privacy/test/functional/social_share_privacy_plugin_admin_controller_test.rb create mode 100644 plugins/social_share_privacy/test/unit/social_share_privacy_helper_test.rb create mode 100644 plugins/social_share_privacy/views/social_share_privacy_plugin_admin/index.html.erb diff --git a/plugins/social_share_privacy/controllers/social_share_privacy_plugin_admin_controller.rb b/plugins/social_share_privacy/controllers/social_share_privacy_plugin_admin_controller.rb new file mode 100644 index 0000000..a64f6fa --- /dev/null +++ b/plugins/social_share_privacy/controllers/social_share_privacy_plugin_admin_controller.rb @@ -0,0 +1,27 @@ +class SocialSharePrivacyPluginAdminController < AdminController + append_view_path File.join(File.dirname(__FILE__) + '/../views') + + protect 'edit_environment_features', :environment + + include SocialSharePrivacyPluginHelper + + def index + @settings = Noosfero::Plugin::Settings.new(environment, SocialSharePrivacyPlugin, params[:settings]) + @settings.networks ||= [] + + @available_networks = social_share_privacy_networks.sort + @settings.networks &= @available_networks + @available_networks -= @settings.networks + + if request.post? + begin + @settings.save! + session[:notice] = _('Option updated successfully.') + rescue Exception => exception + session[:notice] = _('Option wasn\'t updated successfully.') + end + redirect_to :controller => 'plugins', :action => 'index' + end + end + +end diff --git a/plugins/social_share_privacy/lib/social_share_privacy_plugin.rb b/plugins/social_share_privacy/lib/social_share_privacy_plugin.rb index b69dbee..33a3248 100644 --- a/plugins/social_share_privacy/lib/social_share_privacy_plugin.rb +++ b/plugins/social_share_privacy/lib/social_share_privacy_plugin.rb @@ -8,4 +8,12 @@ class SocialSharePrivacyPlugin < Noosfero::Plugin _("A plugin that adds share buttons from other networks.") end + def self.networks_default_setting + [] + end + + def stylesheet? + true + end + end diff --git a/plugins/social_share_privacy/lib/social_share_privacy_plugin_helper.rb b/plugins/social_share_privacy/lib/social_share_privacy_plugin_helper.rb new file mode 100644 index 0000000..a5bd299 --- /dev/null +++ b/plugins/social_share_privacy/lib/social_share_privacy_plugin_helper.rb @@ -0,0 +1,7 @@ +module SocialSharePrivacyPluginHelper + + def social_share_privacy_networks + Dir[SocialSharePrivacyPlugin.root_path + 'public/socialshareprivacy/javascripts/modules/*.js'].map { |entry| entry.split('/').last.gsub(/\.js$/,'') } + end + +end diff --git a/plugins/social_share_privacy/public/style.css b/plugins/social_share_privacy/public/style.css new file mode 100644 index 0000000..e137426 --- /dev/null +++ b/plugins/social_share_privacy/public/style.css @@ -0,0 +1,6 @@ +#social-networks-list { + width: 50%; +} +#selected-social-networks { + width: 50%; +} diff --git a/plugins/social_share_privacy/test/functional/social_share_privacy_plugin_admin_controller_test.rb b/plugins/social_share_privacy/test/functional/social_share_privacy_plugin_admin_controller_test.rb new file mode 100644 index 0000000..c43a208 --- /dev/null +++ b/plugins/social_share_privacy/test/functional/social_share_privacy_plugin_admin_controller_test.rb @@ -0,0 +1,54 @@ +require File.dirname(__FILE__) + '/../../../../test/test_helper' +require File.dirname(__FILE__) + '/../../controllers/social_share_privacy_plugin_admin_controller' + +# Re-raise errors caught by the controller. +class SocialSharePrivacyPluginAdminController; def rescue_action(e) raise e end; end + +class SocialSharePrivacyPluginAdminControllerTest < ActionController::TestCase + + def setup + @environment = Environment.default + @profile = create_user('profile').person + @environment.add_admin(@profile) + login_as(@profile.identifier) + end + + attr_reader :environment + + should 'list networks not selected available in alphabetic order' do + Noosfero::Plugin::Settings.new(environment, SocialSharePrivacyPlugin, :networks => ['gplus']).save! + @controller.stubs(:social_share_privacy_networks).returns(['gplus', 'twitter', 'facebook']) + get :index + assert_equal ['facebook', 'twitter'], assigns(:available_networks) + end + + should 'list networks selected in order' do + Noosfero::Plugin::Settings.new(environment, SocialSharePrivacyPlugin, :networks => ['gplus', 'buffer']).save! + get :index + assert_equal ['gplus', 'buffer'], assigns(:settings).networks + end + + should 'save social networks buttons settings' do + post :index, :settings => {:networks => ['facebook', 'gplus']} + @settings = Noosfero::Plugin::Settings.new(environment.reload, SocialSharePrivacyPlugin) + assert_equal ['facebook', 'gplus'], @settings.settings[:networks] + end + + should 'remove all buttons if none selected' do + Noosfero::Plugin::Settings.new(environment, SocialSharePrivacyPlugin, :networks => ['twitter', 'gplus', 'buffer']).save! + post :index, :settings => {:networks => ['']} + @settings = Noosfero::Plugin::Settings.new(environment.reload, SocialSharePrivacyPlugin) + assert_equal [], @settings.settings[:networks] + end + + should 'ignore unknown networks' do + post :index, :settings => {:networks => ['orkut']} + @settings = Noosfero::Plugin::Settings.new(environment.reload, SocialSharePrivacyPlugin) + assert_equal [], @settings.settings[:networks] + end + + should 'redirect to index after save' do + post :index + assert_redirected_to :controller => 'plugins', :action => 'index' + end +end diff --git a/plugins/social_share_privacy/test/unit/social_share_privacy_helper_test.rb b/plugins/social_share_privacy/test/unit/social_share_privacy_helper_test.rb new file mode 100644 index 0000000..a98da8e --- /dev/null +++ b/plugins/social_share_privacy/test/unit/social_share_privacy_helper_test.rb @@ -0,0 +1,11 @@ +require File.dirname(__FILE__) + '/../../../../test/test_helper' + +class SocialSharePrivacyPluginHelperTest < ActiveSupport::TestCase + + include SocialSharePrivacyPluginHelper + + should 'list social networks provided' do + assert_equal ['buffer', 'facebook', 'gplus', 'mail', 'stumbleupon', 'xing', 'delicious', 'fbshare', 'hackernews', 'pinterest', 'tumblr', 'disqus', 'flattr', 'linkedin', 'reddit', 'twitter'].sort, social_share_privacy_networks.sort + end + +end diff --git a/plugins/social_share_privacy/test/unit/social_share_privacy_test.rb b/plugins/social_share_privacy/test/unit/social_share_privacy_test.rb index 6f7d5d8..470ec15 100644 --- a/plugins/social_share_privacy/test/unit/social_share_privacy_test.rb +++ b/plugins/social_share_privacy/test/unit/social_share_privacy_test.rb @@ -20,4 +20,9 @@ class SocialSharePrivacyPluginTest < ActiveSupport::TestCase assert_equal "A plugin that adds share buttons from other networks.", SocialSharePrivacyPlugin.plugin_description end + should 'have default value for networks setting' do + @settings = Noosfero::Plugin::Settings.new(Environment.default, SocialSharePrivacyPlugin) + assert_equal [], @settings.get_setting(:networks) + end + end diff --git a/plugins/social_share_privacy/views/social_share_privacy_plugin_admin/index.html.erb b/plugins/social_share_privacy/views/social_share_privacy_plugin_admin/index.html.erb new file mode 100644 index 0000000..a8174e5 --- /dev/null +++ b/plugins/social_share_privacy/views/social_share_privacy_plugin_admin/index.html.erb @@ -0,0 +1,44 @@ +

<%= _('Social Share Privacy Options')%>

+ +

<%= _('Choose the social networks you want to enable the share buttons.') %>

+ + + +
+ <%= labelled_form_field(_('Available networks'), select_tag('networks[]', options_for_select(@available_networks), {:id => 'social-networks-list', :multiple => true, :size => 6 }) ) %> +
+ +
+ <%= button :down, _('Add'), '#', { :id => 'add' } %> + <%= button :up, _('Remove'), '#', { :id => 'remove' } %> +
+ +<%= form_tag do %> +
+ <%= hidden_field_tag 'settings[networks][]' %> + <%= labelled_form_field(_('Selected networks'), select_tag('settings[networks]', options_for_select(@settings.networks), {:id => 'selected-social-networks', :multiple => true, :size => 6 })) %> +
+ +

+ <%= _('The same order in which you arrange the social networks here will be used for arranging the share buttons.') %> +

+ + <% button_bar do %> + <%= submit_button 'save', _('Save'), :cancel => {:controller => 'plugins', :action => 'index'} %> + <% end %> +<% end %> -- libgit2 0.21.2