cirandas.net

ref: master

lib/noosfero/plugin/settings.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Noosfero::Plugin::Settings

  def initialize(base, plugin, attributes = nil)
    @base = base
    @plugin = plugin
    attributes ||= {}
    attributes.each do |k,v|
      self.send("#{k}=", v)
    end
  end

  def settings
    settings_field = @base.class.settings_field
    @base.send(settings_field)["#{@plugin.public_name}_plugin".to_sym] ||= {}
  end

  def method_missing(method, *args, &block)
    if method.to_s =~ /^(.+)=$/
      set_setting($1, args.first)
    elsif method.to_s =~ /^(.+)$/
      get_setting($1)
    end
  end

  def get_setting(name)
    if settings[name.to_sym].nil?
      if @plugin.respond_to?("#{name}_default_setting")
        @plugin.send("#{name}_default_setting")
      else
        nil
      end
    else
      settings[name.to_sym]
    end
  end

  def set_setting(name, value)
    settings[name.to_sym] = value
  end

  def save!
    @base.save!
  end

end