Class GlobalConfig
In: app/models/global_config.rb
Parent: ActiveRecord::Base

This class is used for storing the global configuration. A choice has been made here to store the configuration in the database rather than in a file.

This has two advantages:

  1. It allows one to change configuration only in one place (when multiple servers use one db) and during runtime.
  2. Rails objects can be stored as values in the configuration.

This global configuration class actually does a little more than just ‘storing’: It also initializes/creates and sets some things. There are a number of steps that are undertaken by this class:

The database is filled with (from config/config.rb):# :

  1. basic settings (just filling db)
  * main_page_tag, title line, header keywords and description, minima and
    maximums and some other things (minor/detailed)
  1. indirect settings (calculated based on basic)
  * sets the daily fraction based on the half-life...
  1. user settings (depend on basics)
  * creates anonymous and administrative users
  1. group settings (depend on users)
  * creates the public user- and peer-group

After each step the GlobalConfig is saved so it can be used by the next step. At the end some finishing touches are applied like creating prefs-profiles for the system users…

Methods

Attributes

available_subdomains  [RW] 

Public Class methods

For use in controllers, etc…

[Source]

     # File app/models/global_config.rb, line 148
148:   def self.available?
149:     begin
150:       g_c = GlobalConfig.find(:first)
151:       if !g_c.nil? and g_c.done?
152:         return true
153:       else
154:         return false
155:       end
156:     rescue
157:       return false
158:     end
159:   end

Fills the database with the initial settings

[Source]

     # File app/models/global_config.rb, line 90
 90:   def self.initial_to_db
 91:     g_c = GlobalConfig.new
 92: 
 93:     # the basic settings that can be changed
 94:     g_c.set_updatable_settings(InitialGlobalConfig::HASH)
 95: 
 96:     # the basic settings that cannot; tag strings
 97:     InitialGlobalConfig::HASH.each_pair do |key, value|
 98:       if key.to_s =~ /^.*tag_string$/
 99:         g_c.send(key.to_s + '=', value)
100:       end
101:     end
102: 
103:     # save the basic settings
104:     g_c.progress = Const::GlobalConfig::DONE_WITH_BASICS
105:     g_c.save
106:     LogLogMessage.initialize_global_config
107: 
108:     # resolve indirect settings
109:     g_c.resolve_indirect_settings
110: 
111:     g_c.progress = Const::GlobalConfig::DONE_WITH_INDIRECTS
112:     g_c.save
113: 
114:     # the role-settings that have their functions below
115: 
116:     # start with user-settings
117:     g_c.send(:users.to_s + '=', InitialGlobalConfig::HASH[:users])
118: 
119:     g_c.progress = Const::GlobalConfig::DONE_WITH_USERS
120:     g_c.save
121: 
122:     # the usergroup-settings
123:     g_c.send(:user_groups.to_s + '=', InitialGlobalConfig::HASH[:user_groups])
124: 
125:     # and then the peergroup-settings
126:     g_c.send(:peer_groups.to_s + '=', InitialGlobalConfig::HASH[:peer_groups])
127: 
128:     g_c.progress = Const::GlobalConfig::DONE_WITH_GROUPS
129:     g_c.save
130: 
131:     # needed because the next_logi_nr is otherwise ignored
132:     User.admin.save!
133:     g_c.admin_user.reload
134: 
135:     # and give the admin_user it's system_user_group membership.
136:     g_c.postconfigure_admin_user
137: 
138:     # and give the anonymous_user it's public_user_group membership
139:     # and a power per vote of 0.1.
140:     g_c.postconfigure_anonymous_user
141: 
142:     g_c.progress = Const::GlobalConfig::DONE
143:     g_c.save
144:   end

Passes requests on to the object below

[Source]

     # File app/models/global_config.rb, line 173
173:   def self.method_missing(method, *args)
174:     g_c = GlobalConfig.find(:first)
175:     if g_c.nil?
176:       return nil
177:     else
178:       return g_c.send(method, *args)
179:     end
180:   end

Administrative methods

[Source]

    # File app/models/global_config.rb, line 78
78:   def self.update_to_db
79:     g_c = GlobalConfig.find(:first)
80: 
81:     g_c.set_updatable_settings(InitialGlobalConfig::HASH)
82: 
83:     g_c.resolve_indirect_settings
84: 
85:     g_c.save
86:   end

[Source]

     # File app/models/global_config.rb, line 164
164:     def self.use_log_log
165:       return false
166:     end

Public Instance methods

Returns true if initialization is done.

[Source]

     # File app/models/global_config.rb, line 321
321:   def done?
322:     return self.progress == Const::GlobalConfig::DONE
323:   end

Getters

[Source]

     # File app/models/global_config.rb, line 303
303:   def done_with_basics?
304:     return self.progress >= Const::GlobalConfig::DONE_WITH_BASICS
305:   end

[Source]

     # File app/models/global_config.rb, line 315
315:   def done_with_groups?
316:     return self.progress >= Const::GlobalConfig::DONE_WITH_GROUPS
317:   end

[Source]

     # File app/models/global_config.rb, line 307
307:   def done_with_indirects?
308:     return self.progress >= Const::GlobalConfig::DONE_WITH_INDIRECTS
309:   end

[Source]

     # File app/models/global_config.rb, line 311
311:   def done_with_users?
312:     return self.progress >= Const::GlobalConfig::DONE_WITH_USERS
313:   end

[Source]

     # File app/models/global_config.rb, line 350
350:   def noreply_email
351:     return 'noreply@' + self.domain
352:   end

Creates the public peer_group.

[Source]

     # File app/models/global_config.rb, line 250
250:   def peer_groups=(peer_groups_hash)
251:     p_g = PeerGroup.new(peer_groups_hash[:public])
252:     p_g.memberships.build(:user => User.admin, :admin_settings => true)
253:     p_g.save
254:     self.public_peer_group = p_g
255:     self.save
256:   end

Makes the admin_user member of the system_user_group, and configures his prefs-profile.

[Source]

     # File app/models/global_config.rb, line 275
275:   def postconfigure_admin_user
276:     admin = self.admin_user
277:     admin.prefs_profiles.create(
278:         :name => self.system_user_group.name,
279:         :user_group => self.system_user_group,
280:         :default_peer_group => self.public_peer_group)
281:     admin.default_prefs_profile = admin.prefs_profiles.last
282:     admin.save
283:   end

Makes the anonymous_user member of the public_user_group, and configures his prefs-profile.

[Source]

     # File app/models/global_config.rb, line 288
288:   def postconfigure_anonymous_user
289:     anonymous = self.anonymous_user
290:     anonymous.prefs_profiles.create(
291:         :name => self.public_user_group.name,
292:         :user_group => self.public_user_group,
293:         :default_peer_group => self.public_peer_group)
294:     anonymous.default_prefs_profile = anonymous.prefs_profiles.last
295:     anonymous.save
296:     public_membership = self.public_peer_group.membership_for(anonymous)
297:     public_membership.power = PeerGroupMembership.anonymous_power
298:     public_membership.save
299:   end

Sets variables that need to be resolved after the db is filled or that are indirect.

[Source]

     # File app/models/global_config.rb, line 263
263:   def resolve_indirect_settings
264:     if !self.done_with_basics?
265:       raise StandardError.new
266:     end
267: 
268:     # daily inflation fraction
269:     self.daily_fraction = 0.5 ** (1.0 / self.half_life)
270:   end

Administrative methods

[Source]

     # File app/models/global_config.rb, line 186
186:   def set_updatable_settings(hash)
187:     hash.each_pair do |key, value|
188:       if key.to_s !~ /^admin_settings|users|user_groups|peer_groups|.*tag_string$/
189:         self.send(key.to_s + '=', value)
190:       end
191:     end
192:   end

[Source]

     # File app/models/global_config.rb, line 207
207:   def site_domain=(string)
208:     self.domain = string
209: 
210:     # domain_levels derived from domain
211:     self.domain_levels = self.domain.split('.').size
212:   end

[Source]

     # File app/models/global_config.rb, line 340
340:   def site_host(options = {})
341:     if options[:subdomain]
342:       return options[:subdomain] + '.' + self.domain
343:     elsif self.use_language_subdomains?
344:       return self.site_subdomain + '.' + self.domain
345:     else
346:       return self.domain
347:     end
348:   end

[Source]

     # File app/models/global_config.rb, line 325
325:   def site_subdomain
326:     if InitialGlobalConfig::DIFFERENT_SITE_SUBDOMAIN
327:       return InitialGlobalConfig::DIFFERENT_SITE_SUBDOMAIN
328:     else
329:       return self.language_code
330:     end
331:   end

Composites

[Source]

     # File app/models/global_config.rb, line 335
335:   def site_url(options = {})
336:     return self.protocol + '://' + self.site_host(options) + 
337:         (RAILS_ENV == "development" ? ":3000" : "") + '/'
338:   end

Creates the public user_group.

[Source]

     # File app/models/global_config.rb, line 236
236:   def user_groups=(user_groups_hash)
237:     s_u_g = UserGroup.new(user_groups_hash[:system])
238:     s_u_g.memberships.build(:user => User.admin, :admin_settings => true)
239:     s_u_g.save
240:     p_u_g = UserGroup.new(user_groups_hash[:public])
241:     p_u_g.memberships.build(:user => User.admin, :admin_settings => true)
242:     p_u_g.save
243:     self.system_user_group = s_u_g
244:     self.public_user_group = p_u_g
245:     self.save
246:   end

Creates anonymous & admin users.

[Source]

     # File app/models/global_config.rb, line 216
216:   def users=(users_hash)
217:     users_hash.each_pair do |key, hash|
218:       if key == :admin
219:         hash.merge!(InitialGlobalConfig::HASH[:admin_settings])
220:       elsif key == :anonymous
221:         hash = InitialGlobalConfig::HASH[:admin_settings].merge(hash)
222:       end
223:       user = User.new(hash)
224:       user.save
225:       if key == :admin
226:         self.admin_user = user
227:       elsif key == :anonymous
228:         self.anonymous_user = user
229:       end
230:     end
231:     self.save
232:   end

Protected Instance methods

Loads the available_subdomains

[Source]

     # File app/models/global_config.rb, line 360
360:   def after_initialize
361:     if !self.new_record?
362:       @available_subdomains = Array.from_s(self.available_subdomains_string)
363:     end
364:     return true
365:   end

Sets the available_subdomains_string and the default_language_prefs_string

[Source]

     # File app/models/global_config.rb, line 370
370:   def prepare_for_saving
371:     self.available_subdomains_string = @available_subdomains.to_s
372:     return true
373:   end

[Validate]