2012-09-20 12:08:02 +00:00
|
|
|
class Setting < ApplicationModel
|
2012-04-13 13:51:10 +00:00
|
|
|
store :options
|
|
|
|
store :state
|
|
|
|
store :state_initial
|
2013-01-24 00:37:27 +00:00
|
|
|
before_create :state_check, :set_initial
|
|
|
|
before_update :state_check
|
2013-01-07 08:33:07 +00:00
|
|
|
after_create :delete_cache
|
|
|
|
after_update :delete_cache
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2013-03-19 00:46:49 +00:00
|
|
|
@@current = {}
|
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
def self.load
|
2012-07-28 16:37:18 +00:00
|
|
|
|
2012-04-13 13:51:10 +00:00
|
|
|
# check if config is already generated
|
2013-03-19 00:46:49 +00:00
|
|
|
return @@current[:settings_config] if @@current[:settings_config]
|
2012-07-28 16:37:18 +00:00
|
|
|
|
2012-04-13 13:51:10 +00:00
|
|
|
# read all config settings
|
2012-04-10 14:06:46 +00:00
|
|
|
config = {}
|
|
|
|
Setting.select('name, state').order(:id).each { |setting|
|
|
|
|
config[setting.name] = setting.state[:value]
|
|
|
|
}
|
2012-07-28 16:37:18 +00:00
|
|
|
|
2012-04-13 13:51:10 +00:00
|
|
|
# config lookups
|
|
|
|
config.each { |key, value|
|
|
|
|
next if value.class.to_s != 'String'
|
|
|
|
config[key].gsub!( /\#\{config\.(.+?)\}/ ) { |s|
|
|
|
|
s = config[$1].to_s
|
|
|
|
}
|
|
|
|
}
|
2012-07-28 16:37:18 +00:00
|
|
|
|
2012-04-13 13:51:10 +00:00
|
|
|
# store for class requests
|
2013-03-19 00:46:49 +00:00
|
|
|
@@current[:settings_config] = config
|
2012-04-10 14:06:46 +00:00
|
|
|
return config
|
|
|
|
end
|
|
|
|
|
2012-12-24 13:55:43 +00:00
|
|
|
def self.set(name, value)
|
|
|
|
setting = Setting.where( :name => name ).first
|
|
|
|
if !setting
|
|
|
|
raise "Can't find config setting '#{name}'"
|
|
|
|
end
|
|
|
|
setting.state = { :value => value }
|
|
|
|
setting.save
|
|
|
|
end
|
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
def self.get(name)
|
|
|
|
self.load
|
2013-03-19 00:46:49 +00:00
|
|
|
return @@current[:settings_config][name]
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2012-07-28 16:37:18 +00:00
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
private
|
2012-04-14 09:57:01 +00:00
|
|
|
def delete_cache
|
2013-03-19 00:46:49 +00:00
|
|
|
@@current[:settings_config] = nil
|
2012-04-14 09:57:01 +00:00
|
|
|
end
|
2012-04-10 14:06:46 +00:00
|
|
|
def set_initial
|
|
|
|
self.state_initial = self.state
|
|
|
|
end
|
2013-01-24 00:37:27 +00:00
|
|
|
def state_check
|
2013-01-25 10:57:44 +00:00
|
|
|
if self.state || self.state == false
|
2013-01-24 00:46:37 +00:00
|
|
|
if !self.state.respond_to?('has_key?') || !self.state.has_key?(:value)
|
2013-01-24 00:37:27 +00:00
|
|
|
self.state = { :value => self.state }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|