Commit 7843b7953899f98bfbea254b70a83999725245e5
1 parent
aeaed25f
Exists in
master
and in
29 other branches
Generic Settings for Plugins
This patch extends the infra developed on the spaminator branch to build generic settings for plugin by allowing the setting to be instatiated with any model that supports settings (and respond to the method settings). This way a plugin can have any number of settings over any model instance like a Profile or an Article and not only the Environment. !! This might conflict with the commit that introduces this idea !! !! The commit is 7afc68410de26905e442714857effa3e3a13ad1a !!
Showing
4 changed files
with
101 additions
and
0 deletions
Show diff stats
app/models/profile.rb
... | ... | @@ -140,6 +140,10 @@ class Profile < ActiveRecord::Base |
140 | 140 | |
141 | 141 | acts_as_having_settings :field => :data |
142 | 142 | |
143 | + def settings | |
144 | + data | |
145 | + end | |
146 | + | |
143 | 147 | settings_items :redirect_l10n, :type => :boolean, :default => false |
144 | 148 | settings_items :public_content, :type => :boolean, :default => true |
145 | 149 | settings_items :description | ... | ... |
config/initializers/plugins.rb
... | ... | @@ -0,0 +1,45 @@ |
1 | +class Noosfero::Plugin::Settings | |
2 | + | |
3 | + def initialize(base, plugin, attributes = nil) | |
4 | + @base = base | |
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 | + @base.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 | + @base.save! | |
42 | + end | |
43 | + | |
44 | +end | |
45 | + | ... | ... |
... | ... | @@ -0,0 +1,51 @@ |
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 | + @profile = Profile.new | |
14 | + @plugin = SolarSystemPlugin | |
15 | + end | |
16 | + | |
17 | + attr_accessor :environment, :profile, :plugin, :settings | |
18 | + | |
19 | + should 'store setttings on any model that offers settings' do | |
20 | + base = environment | |
21 | + settings = Noosfero::Plugin::Settings.new(base, plugin) | |
22 | + settings.star = 'sun' | |
23 | + settings.planets = 8 | |
24 | + assert_equal 'sun', base.settings[:solar_system_plugin][:star] | |
25 | + assert_equal 8, base.settings[:solar_system_plugin][:planets] | |
26 | + assert_equal 'sun', settings.star | |
27 | + assert_equal 8, settings.planets | |
28 | + | |
29 | + base = profile | |
30 | + settings = Noosfero::Plugin::Settings.new(base, plugin) | |
31 | + settings.star = 'sun' | |
32 | + settings.planets = 8 | |
33 | + assert_equal 'sun', base.settings[:solar_system_plugin][:star] | |
34 | + assert_equal 8, base.settings[:solar_system_plugin][:planets] | |
35 | + assert_equal 'sun', settings.star | |
36 | + assert_equal 8, settings.planets | |
37 | + end | |
38 | + | |
39 | + should 'save base on save' do | |
40 | + environment.expects(:save!) | |
41 | + settings = Noosfero::Plugin::Settings.new(environment, plugin) | |
42 | + settings.save! | |
43 | + end | |
44 | + | |
45 | + should 'use default value defined on the plugin class' do | |
46 | + settings = Noosfero::Plugin::Settings.new(profile, plugin) | |
47 | + assert_equal 42, settings.secret | |
48 | + end | |
49 | + | |
50 | +end | |
51 | + | ... | ... |