[Git][noosfero/noosfero][master] 2 commits: Adds option to set type of a field when using Noosfero::Plugin::Settings

Rodrigo Souto gitlab at mg.gitlab.com
Tue May 30 09:48:54 BRT 2017


Rodrigo Souto pushed to branch master at Noosfero / noosfero


Commits:
a293f1d9 by Victor Navarro at 2017-05-23T15:04:17-03:00
Adds option to set type of a field when using Noosfero::Plugin::Settings

- - - - -
aa2d3925 by Rodrigo Souto at 2017-05-30T12:48:03+00:00
Merge branch 'add_type_to_attributes' into 'master'

Adds option to set type of a field when using Noosfero::Plugin::Settings

See merge request !1218
- - - - -


2 changed files:

- lib/noosfero/plugin/settings.rb
- test/unit/plugin_settings_test.rb


Changes:

=====================================
lib/noosfero/plugin/settings.rb
=====================================
--- a/lib/noosfero/plugin/settings.rb
+++ b/lib/noosfero/plugin/settings.rb
@@ -1,11 +1,11 @@
 class Noosfero::Plugin::Settings
 
-  def initialize(base, plugin, attributes = nil)
+  def initialize(base, plugin, attributes = nil, type = nil)
     @base = base
     @plugin = plugin
     attributes ||= {}
     attributes.each do |k,v|
-      self.send("#{k}=", v)
+      self.send("#{k}=", v, type.try("[]", k))
     end
   end
 
@@ -16,7 +16,7 @@ class Noosfero::Plugin::Settings
 
   def method_missing(method, *args, &block)
     if method.to_s =~ /^(.+)=$/
-      set_setting($1, args.first)
+      set_setting($1, args.first, args.second)
     elsif method.to_s =~ /^(.+)$/
       get_setting($1)
     end
@@ -34,7 +34,10 @@ class Noosfero::Plugin::Settings
     end
   end
 
-  def set_setting(name, value)
+  def set_setting(name, value, type=nil)
+    if type
+      value = Noosfero::Plugin::Settings::ActsAsHavingSettings.type_cast(value, ActiveRecord::Type.const_get(type.to_s.camelize.to_sym).new)
+    end
     settings[name.to_sym] = value
   end
 
@@ -43,4 +46,3 @@ class Noosfero::Plugin::Settings
   end
 
 end
-


=====================================
test/unit/plugin_settings_test.rb
=====================================
--- a/test/unit/plugin_settings_test.rb
+++ b/test/unit/plugin_settings_test.rb
@@ -47,4 +47,11 @@ class PluginSettingsTest < ActiveSupport::TestCase
     assert_equal 42, settings.secret
   end
 
+  should 'create attributes with types setted' do
+    base = environment
+    settings = Noosfero::Plugin::Settings.new(base, plugin, {:string_attribute => "test", :boolean_attribute => "0"}, {:boolean_attribute => "boolean"})
+
+    assert_equal "test", base.settings[:solar_system_plugin][:string_attribute]
+    assert_equal FalseClass, base.settings[:solar_system_plugin][:boolean_attribute].class
+  end
 end



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/5b4ff341d4fb666aa288a40a1b420ad23b00d510...aa2d39258e3d9e5ad0c3cae10ab93e5050f0b8a1

---
View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/5b4ff341d4fb666aa288a40a1b420ad23b00d510...aa2d39258e3d9e5ad0c3cae10ab93e5050f0b8a1
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20170530/0bbb1a49/attachment-0001.html>


More information about the Noosfero-dev mailing list