Commit e17f85d613cdb25d840b812740eee9e8a88c1d25

Authored by Rodrigo Souto
Committed by Daniela Feitosa
1 parent 69b22fc8

Creating generic settings for plugins (moving part of it from AntiSpam Plugin)

config/initializers/plugins.rb
@@ -3,4 +3,5 @@ require 'noosfero/plugin/hot_spot' @@ -3,4 +3,5 @@ require 'noosfero/plugin/hot_spot'
3 require 'noosfero/plugin/manager' 3 require 'noosfero/plugin/manager'
4 require 'noosfero/plugin/active_record' 4 require 'noosfero/plugin/active_record'
5 require 'noosfero/plugin/mailer_base' 5 require 'noosfero/plugin/mailer_base'
  6 +require 'noosfero/plugin/settings'
6 Noosfero::Plugin.init_system if $NOOSFERO_LOAD_PLUGINS 7 Noosfero::Plugin.init_system if $NOOSFERO_LOAD_PLUGINS
lib/noosfero/plugin/settings.rb 0 → 100644
@@ -0,0 +1,45 @@ @@ -0,0 +1,45 @@
  1 +class Noosfero::Plugin::Settings
  2 +
  3 + def initialize(environment, plugin, attributes = nil)
  4 + @environment = environment
  5 + @plugin = plugin
  6 + attributes ||= {}
  7 + attributes.each do |k,v|
  8 + self.send("#{k}=", v)
  9 + end
  10 + end
  11 +
  12 + def settings
  13 + @environment.settings["#{@plugin.public_name}_plugin".to_sym] ||= {}
  14 + end
  15 +
  16 + def method_missing(method, *args, &block)
  17 + if method.to_s =~ /^(.+)=$/
  18 + set_setting($1, args.first)
  19 + elsif method.to_s =~ /^(.+)$/
  20 + get_setting($1)
  21 + end
  22 + end
  23 +
  24 + def get_setting(name)
  25 + if settings[name.to_sym].nil?
  26 + if @plugin.respond_to?("#{name}_default_setting")
  27 + @plugin.send("#{name}_default_setting")
  28 + else
  29 + nil
  30 + end
  31 + else
  32 + settings[name.to_sym]
  33 + end
  34 + end
  35 +
  36 + def set_setting(name, value)
  37 + settings[name.to_sym] = value
  38 + end
  39 +
  40 + def save!
  41 + @environment.save!
  42 + end
  43 +
  44 +end
  45 +
plugins/anti_spam/controllers/anti_spam_plugin_admin_controller.rb
@@ -2,7 +2,7 @@ class AntiSpamPluginAdminController < AdminController @@ -2,7 +2,7 @@ class AntiSpamPluginAdminController < AdminController
2 append_view_path File.join(File.dirname(__FILE__) + '/../views') 2 append_view_path File.join(File.dirname(__FILE__) + '/../views')
3 3
4 def index 4 def index
5 - @settings = AntiSpamPlugin::Settings.new(environment, params[:settings]) 5 + @settings = Noosfero::Plugin::Settings.new(environment, AntiSpamPlugin, params[:settings])
6 if request.post? 6 if request.post?
7 @settings.save! 7 @settings.save!
8 redirect_to :action => 'index' 8 redirect_to :action => 'index'
plugins/anti_spam/lib/anti_spam_plugin.rb
@@ -8,6 +8,10 @@ class AntiSpamPlugin < Noosfero::Plugin @@ -8,6 +8,10 @@ class AntiSpamPlugin < Noosfero::Plugin
8 _("Checks comments against a spam checking service compatible with the Akismet API") 8 _("Checks comments against a spam checking service compatible with the Akismet API")
9 end 9 end
10 10
  11 + def self.host_default_setting
  12 + 'api.antispam.typepad.com'
  13 + end
  14 +
11 def check_comment_for_spam(comment) 15 def check_comment_for_spam(comment)
12 if rakismet_call(comment, :spam?) 16 if rakismet_call(comment, :spam?)
13 comment.spam = true 17 comment.spam = true
@@ -26,7 +30,7 @@ class AntiSpamPlugin < Noosfero::Plugin @@ -26,7 +30,7 @@ class AntiSpamPlugin < Noosfero::Plugin
26 protected 30 protected
27 31
28 def rakismet_call(comment, op) 32 def rakismet_call(comment, op)
29 - settings = AntiSpamPlugin::Settings.new(comment.environment) 33 + settings = Noosfero::Plugin::Settings.new(comment.environment, self.class)
30 34
31 Rakismet.host = settings.host 35 Rakismet.host = settings.host
32 Rakismet.key = settings.api_key 36 Rakismet.key = settings.api_key
plugins/anti_spam/lib/anti_spam_plugin/settings.rb
@@ -1,35 +0,0 @@ @@ -1,35 +0,0 @@
1 -class AntiSpamPlugin::Settings  
2 -  
3 - def initialize(environment, attributes = nil)  
4 - @environment = environment  
5 - attributes ||= {}  
6 - attributes.each do |k,v|  
7 - self.send("#{k}=", v)  
8 - end  
9 - end  
10 -  
11 - def settings  
12 - @environment.settings[:anti_spam_plugin] ||= {}  
13 - end  
14 -  
15 - def host  
16 - settings[:host] ||= 'api.antispam.typepad.com'  
17 - end  
18 -  
19 - def host=(value)  
20 - settings[:host] = value  
21 - end  
22 -  
23 - def api_key  
24 - settings[:api_key]  
25 - end  
26 -  
27 - def api_key=(value)  
28 - settings[:api_key] = value  
29 - end  
30 -  
31 - def save!  
32 - @environment.save!  
33 - end  
34 -  
35 -end  
plugins/anti_spam/test/unit/anti_spam_plugin/settings_test.rb
@@ -1,29 +0,0 @@ @@ -1,29 +0,0 @@
1 -require 'test_helper'  
2 -  
3 -class AntiSpamSettingsTest < ActiveSupport::TestCase  
4 -  
5 - def setup  
6 - @environment = Environment.new  
7 - @settings = AntiSpamPlugin::Settings.new(@environment)  
8 - end  
9 -  
10 - should 'store setttings in environment' do  
11 - @settings.host = 'foo.com'  
12 - @settings.api_key = '1234567890'  
13 - assert_equal 'foo.com', @environment.settings[:anti_spam_plugin][:host]  
14 - assert_equal '1234567890', @environment.settings[:anti_spam_plugin][:api_key]  
15 - assert_equal 'foo.com', @settings.host  
16 - assert_equal '1234567890', @settings.api_key  
17 - end  
18 -  
19 - should 'save environment on save' do  
20 - @environment.expects(:save!)  
21 - @settings.save!  
22 - end  
23 -  
24 - should 'use TypePad AntiSpam by default' do  
25 - assert_equal 'api.antispam.typepad.com', @settings.host  
26 - end  
27 -  
28 -  
29 -end  
plugins/anti_spam/test/unit/anti_spam_plugin_test.rb
@@ -7,7 +7,7 @@ class AntiSpamPluginTest &lt; ActiveSupport::TestCase @@ -7,7 +7,7 @@ class AntiSpamPluginTest &lt; ActiveSupport::TestCase
7 article = fast_create(TextileArticle, :profile_id => profile.id) 7 article = fast_create(TextileArticle, :profile_id => profile.id)
8 @comment = fast_create(Comment, :source_id => article.id, :source_type => 'Article') 8 @comment = fast_create(Comment, :source_id => article.id, :source_type => 'Article')
9 9
10 - @settings = AntiSpamPlugin::Settings.new(@comment.environment) 10 + @settings = Noosfero::Plugin::Settings.new(@comment.environment, AntiSpamPlugin)
11 @settings.api_key = 'b8b80ddb8084062d0c9119c945ce3bc3' 11 @settings.api_key = 'b8b80ddb8084062d0c9119c945ce3bc3'
12 @settings.save! 12 @settings.save!
13 13
test/unit/plugin_settings_test.rb 0 → 100644
@@ -0,0 +1,38 @@ @@ -0,0 +1,38 @@
  1 +require 'test_helper'
  2 +
  3 +class SolarSystemPlugin < Noosfero::Plugin
  4 + def self.secret_default_setting
  5 + 42
  6 + end
  7 +end
  8 +
  9 +class PluginSettingsTest < ActiveSupport::TestCase
  10 +
  11 + def setup
  12 + @environment = Environment.new
  13 + @plugin = SolarSystemPlugin
  14 + @settings = Noosfero::Plugin::Settings.new(@environment, @plugin)
  15 + end
  16 +
  17 + attr_accessor :environment, :plugin, :settings
  18 +
  19 + should 'store setttings in environment' do
  20 + settings.star = 'sun'
  21 + settings.planets = 8
  22 + assert_equal 'sun', environment.settings[:solar_system_plugin][:star]
  23 + assert_equal 8, environment.settings[:solar_system_plugin][:planets]
  24 + assert_equal 'sun', settings.star
  25 + assert_equal 8, settings.planets
  26 + end
  27 +
  28 + should 'save environment on save' do
  29 + environment.expects(:save!)
  30 + settings.save!
  31 + end
  32 +
  33 + should 'use default value defined on the plugin class' do
  34 + assert_equal 42, settings.secret
  35 + end
  36 +
  37 +end
  38 +