refactoring of site settings

This commit is contained in:
Sam 2013-06-13 12:41:27 +10:00
parent 28853177fd
commit 384d743e7b
2 changed files with 41 additions and 29 deletions

View file

@ -90,8 +90,6 @@ module SiteSettingExtension
mutex.synchronize do mutex.synchronize do
ensure_listen_for_changes ensure_listen_for_changes
old = current old = current
changes = []
deletions = []
all_settings = SiteSetting.select([:name,:value,:data_type]) all_settings = SiteSetting.select([:name,:value,:data_type])
new_hash = Hash[*(all_settings.map{|s| [s.name.intern, convert(s.value,s.data_type)]}.to_a.flatten)] new_hash = Hash[*(all_settings.map{|s| [s.name.intern, convert(s.value,s.data_type)]}.to_a.flatten)]
@ -99,13 +97,7 @@ module SiteSettingExtension
# add defaults # add defaults
new_hash = defaults.merge(new_hash) new_hash = defaults.merge(new_hash)
new_hash.each do |name, value| changes,deletions = diff_hash(new_hash, old)
changes << [name,value] if !old.has_key?(name) || old[name] != value
end
old.each do |name,value|
deletions << [name,value] unless new_hash.has_key?(name)
end
if deletions.length > 0 || changes.length > 0 if deletions.length > 0 || changes.length > 0
@current = new_hash @current = new_hash
@ -121,26 +113,29 @@ module SiteSettingExtension
end end
end end
def ensure_listen_for_changes def ensure_listen_for_changes
unless @subscribed unless @subscribed
pid = process_id MessageBus.subscribe("/site_settings") do |message|
MessageBus.subscribe("/site_settings") do |msg| process_message(message)
message = msg.data
if message["process"] != pid
begin
@last_message_processed = msg.global_id
# picks a db
MessageBus.on_connect.call(msg.site_id)
SiteSetting.refresh!
ensure
MessageBus.on_disconnect.call(msg.site_id)
end
end
end end
@subscribed = true @subscribed = true
end end
end end
def process_message(message)
data = message.data
if data["process"] != process_id
begin
@last_message_processed = message.global_id
MessageBus.on_connect.call(message.site_id)
SiteSetting.refresh!
ensure
MessageBus.on_disconnect.call(message.site_id)
end
end
end
def diags def diags
{ {
last_message_processed: @last_message_processed last_message_processed: @last_message_processed
@ -192,16 +187,31 @@ module SiteSettingExtension
protected protected
def diff_hash(new_hash, old)
changes = []
deletions = []
new_hash.each do |name, value|
changes << [name,value] if !old.has_key?(name) || old[name] != value
end
old.each do |name,value|
deletions << [name,value] unless new_hash.has_key?(name)
end
[changes,deletions]
end
def get_data_type(name,val) def get_data_type(name,val)
return types[:null] if val.nil? return types[:null] if val.nil?
return types[:enum] if enums[name]
if enums[name] case val
types[:enum] when String
elsif String === val
types[:string] types[:string]
elsif Fixnum === val when Fixnum
types[:fixnum] types[:fixnum]
elsif TrueClass === val || FalseClass === val when TrueClass, FalseClass
types[:bool] types[:bool]
else else
raise ArgumentError.new :val raise ArgumentError.new :val

View file

@ -1,4 +1,6 @@
require 'spec_helper' require 'spec_helper'
require_dependency 'site_setting'
require_dependency 'site_setting_extension'
describe SiteSetting do describe SiteSetting do