Class: Radiant::Config

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

Overview

The Radiant::Config object emulates a hash with simple bracket methods which allow you to get and set values in the configuration table:

Radiant::Config['setting.name'] = 'value'
Radiant::Config['setting.name'] #=> "value"

Currently, there is not a way to edit configuration through the admin system so it must be done manually. The console script is probably the easiest way to this:

% script/console production
Loading production environment.
>> Radiant::Config['setting.name'] = 'value'
=> "value"
>>

Radiant currently uses the following settings:

admin.title

the title of the admin system

admin.subtitle

the subtitle of the admin system

defaults.page.parts

a comma separated list of default page parts

defaults.page.status

a string representation of the default page status

defaults.page.filter

the default filter to use on new page parts

dev.host

the hostname where draft pages are viewable

local.timezone

the timezone name (‘rake -D time` for full list) used to correct displayed times

page.edit.published_date?

when true, shows the datetime selector for published date on the page edit screen

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](key) ⇒ Object



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

def [](key)
  if table_exists?
    unless Radiant::Config.cache_file_exists?
      Radiant::Config.ensure_cache_file
      Radiant::Config.initialize_cache
    end
    Radiant::Config.initialize_cache if Radiant::Config.stale_cache?
    Rails.cache.read('Radiant::Config')[key]
  end
end

.[]=(key, value) ⇒ Object



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

def []=(key, value)
  if table_exists?
    pair = find_or_initialize_by_key(key)
    pair.update_attributes(:value => value)
    value
  end
end

.cache_file_exists?Boolean

Returns:

  • (Boolean)


65
66
67
68
# File 'app/models/radiant/config.rb', line 65

def cache_file_exists?
  return false if Rails.cache.read('Radiant.cache_file').nil?
  File.file?(Rails.cache.read('Radiant.cache_file'))
end

.ensure_cache_fileObject



75
76
77
78
79
80
81
# File 'app/models/radiant/config.rb', line 75

def ensure_cache_file
  cache_path = "#{Rails.root}/tmp"
  cache_file = File.join(cache_path,'radiant_config_cache.txt')
  Rails.cache.write('Radiant.cache_file', cache_file)
  FileUtils.mkpath(cache_path)
  FileUtils.touch(cache_file)
end

.initialize_cacheObject



59
60
61
62
63
# File 'app/models/radiant/config.rb', line 59

def initialize_cache
  Radiant::Config.ensure_cache_file
  Rails.cache.write('Radiant::Config',Radiant::Config.to_hash)
  Rails.cache.write('Radiant.cache_mtime', File.mtime(Rails.cache.read('Radiant.cache_file')))
end

.stale_cache?Boolean

Returns:

  • (Boolean)


70
71
72
73
# File 'app/models/radiant/config.rb', line 70

def stale_cache?
  return true unless Radiant::Config.cache_file_exists?
  Rails.cache.read('Radiant.cache_mtime') != File.mtime(Rails.cache.read('Radiant.cache_file'))
end

.to_hashObject



55
56
57
# File 'app/models/radiant/config.rb', line 55

def to_hash
  Hash[ *find(:all).map { |pair| [pair.key, pair.value] }.flatten ]
end

Instance Method Details

#update_cacheObject



96
97
98
# File 'app/models/radiant/config.rb', line 96

def update_cache
  Radiant::Config.initialize_cache
end

#valueObject



88
89
90
91
92
93
94
# File 'app/models/radiant/config.rb', line 88

def value
  if key.ends_with? "?"
    self[:value] == "true"
  else
    self[:value]
  end
end

#value=(param) ⇒ Object



84
85
86
# File 'app/models/radiant/config.rb', line 84

def value=(param)
  self[:value] = param.to_s
end