Class: Setting

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/setting.rb

Direct Known Subclasses

Auth, General, Provisioning, Puppet

Defined Under Namespace

Classes: Auth, General, Provisioning, Puppet

Constant Summary collapse

TYPES =
%w{ integer boolean hash array string }
FROZEN_ATTRS =
%w{ name default description category }
NONZERO_ATTRS =
%w{ puppet_interval idle_timeout entries_per_page max_trend }
BLANK_ATTRS =
%w{ trusted_puppetmaster_hosts }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](name) ⇒ Object


36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/setting.rb', line 36

def self.[](name)
  name = name.to_s
  cache_value = Setting.cache.read(name)
  if cache_value.nil?
    value = where(:name => name).first.try(:value)
    Setting.cache.write(name, value)
    return value
  else
    return cache_value
  end
end

.[]=(name, value) ⇒ Object


48
49
50
51
52
53
# File 'app/models/setting.rb', line 48

def self.[]=(name, value)
  name   = name.to_s
  record = find_or_create_by_name name
  record.value = value
  record.save!
end

.create(opts) ⇒ Object


140
141
142
143
144
145
146
147
# File 'app/models/setting.rb', line 140

def self.create opts
  if (s = Setting.find_by_name(opts[:name].to_s)).nil?
    super opts
  else
    s.update_attribute(:default, opts[:default])
    s
  end
end

.create!(opts) ⇒ Object


149
150
151
152
153
154
155
156
# File 'app/models/setting.rb', line 149

def self.create! opts
  if (s = Setting.find_by_name(opts[:name].to_s)).nil?
    super opts
  else
    s.update_attribute(:default, opts[:default])
    s
  end
end

.method_missing(method, *args) ⇒ Object


55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/setting.rb', line 55

def self.method_missing(method, *args)
  super(method, *args)
rescue NoMethodError
  method_name = method.to_s

  #setter method
  if method_name =~ /=$/
    self[method_name.chomp("=")] = args.first
  #getter
  else
    self[method_name]
  end
end

.per_pageObject

can't use our own settings


34
# File 'app/models/setting.rb', line 34

def self.per_page; 20 end

Instance Method Details

#defaultObject Also known as: default_before_type_cast


80
81
82
83
# File 'app/models/setting.rb', line 80

def default
  d = read_attribute(:default)
  d.nil? ? nil : YAML.load(d)
end

#default=(v) ⇒ Object


85
86
87
# File 'app/models/setting.rb', line 85

def default=(v)
  write_attribute :default, v.to_yaml
end

#parse_string_value(val) ⇒ Object


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/models/setting.rb', line 91

def parse_string_value val

  case settings_type
  when "boolean"
    val = val.downcase
    if val == "true"
      self.value = true
    elsif val == "false"
      self.value = false
    else
      invalid_value_error _("must be boolean")
      return false
    end

  when "integer"
    if val =~ /\d+/
      self.value = val.to_i
    else
      invalid_value_error _("must be integer")
      return false
    end

  when "array"
    if val =~ /^\[.*\]$/
      begin
        self.value = YAML.load(val.gsub(/(\,)(\S)/, "\\1 \\2"))
      rescue => e
        invalid_value_error e.to_s
        return false
      end
    else
      invalid_value_error _("must be an array")
      return false
    end

  when "string", nil
    #string is taken as default setting type for parsing
    self.value = val.to_s.strip

  when "hash"
    raise Foreman::Exception, "parsing hash from string is not supported"

  else
    raise Foreman::Exception.new(N_("parsing settings type '%s' from string is not defined"), settings_type)

  end
  return true
end

#valueObject Also known as: value_before_type_cast


74
75
76
77
# File 'app/models/setting.rb', line 74

def value
  v = read_attribute(:value)
  v.nil? ? default : YAML.load(v)
end

#value=(v) ⇒ Object


69
70
71
72
# File 'app/models/setting.rb', line 69

def value=(v)
  v = v.to_yaml unless v.nil?
  write_attribute :value, v
end