public
Description: Settings is a plugin that makes managing a table of global key, value pairs easy. Think of it like a global Hash stored in your database, that uses simple ActiveRecord like methods for manipulation. Keep track of any global setting that you don't want to hard code into your rails app. You can store any kind of object. Strings, numbers, arrays, or any serializable object.
Clone URL: git://github.com/Squeegy/rails-settings.git
Modernized code a bit, and got tests passing.  Implemented easier and 
simpler defaults.
Squeegy (author)
Sat Apr 12 15:03:08 -0700 2008
commit  c409585a39d89378f10c081d1b739c22384565ad
tree    94f2e6a225f9500b2fd30a067f2c6735c1e18e46
parent  cd5c446f9cff667d3f611d38d8b0dd11c01b5fa7
  • README.rdoc
  • doc/classes/Settings.html
  • doc/classes/Settings.src/M000001.html
  • doc/classes/Settings.src/M000002.html
  • doc/classes/Settings.src/M000003.html
  • doc/classes/Settings.src/M000004.html
  • doc/classes/Settings.src/M000005.html
  • doc/classes/Settings.src/M000006.html
  • doc/created.rid
  • doc/files/README.html
  • doc/files/lib/settings_rb.html
  • doc/fr_class_index.html
  • doc/fr_file_index.html
  • doc/fr_method_index.html
  • doc/index.html
  • doc/rdoc-style.css
  • generators/settings_migration/templates/migration.rb
  • lib/settings.rb
  • test/settings_test.rb
...
6
7
8
 
9
10
11
12
13
14
15
 
16
17
18
...
35
36
37
38
 
39
40
41
42
43
44
45
46
47
48
49
50
51
52
 
 
 
 
53
54
55
 
 
 
56
57
58
59
60
 
61
...
6
7
8
9
10
11
12
13
14
15
 
16
17
18
19
...
36
37
38
 
39
40
41
42
43
44
45
 
 
 
 
 
 
 
 
46
47
48
49
50
 
 
51
52
53
54
 
55
 
56
57
58
0
@@ -6,13 +6,14 @@ like methods for manipulation. Keep track of any global setting that you dont w
0
 to hard code into your rails app. You can store any kind of object. Strings, numbers,
0
 arrays, or any object.
0
 
0
+
0
 == Setup
0
 
0
 You must create the table used by the Settings model. Simply run this command:
0
   ruby script/generate settings_migration
0
 
0
 Now just put that migration in the database with:
0
- rake migrate
0
+ rake db:migrate
0
 
0
 
0
 == Usage
0
@@ -35,25 +36,21 @@ Changing an existing setting is the same as creating a new setting:
0
 Decide you dont want to track a particular setting anymore?
0
 
0
   Settings.destroy :foo
0
- Settings.foo # Now gives a setting variable not found error.
0
+ Settings.foo # returns nil
0
 
0
 Want a list of all the settings?
0
 
0
   Settings.all # returns {'admin_password' => 'super_secret', 'date_format' => '%m %d, %Y'}
0
 
0
 Set defaults for certain settings of your app. This will cause the defined settings to return with the
0
-Specified value even if they are not in the database. Here is what you insert into your environment.rb
0
-
0
- module SettingsDefaults
0
- DEFAULTS = {
0
- :setting_one => 'footastic',
0
- :setting_two => 123.321
0
- }
0
- end
0
+Specified value even if they are not in the database. Make a new file in config/initializers/settings.rb
0
+with the following:
0
+
0
+ Settings.defaults[:some_setting] = 'footastic'
0
   
0
- Settings.setting_one #=> returns "footastic" even though no record is in the databse for "some_setting"
0
- Settings.setting_one = 'bar' # Database record is now created and 'bar' will be used instead of the default.
0
+Now even if the database is completely empty, you app will have some intelligent defaults:
0
+
0
+ Settings.some_setting # returns 'footastic'
0
 
0
-NOTE: the server must be restarted in order to see new default settings.
0
 
0
-All there is to it!. Enjoy!
0
\ No newline at end of file
0
+That's all there is to it!. Enjoy!
0
\ No newline at end of file
...
1
2
3
4
5
6
7
 
 
 
8
9
10
...
1
2
3
 
 
 
 
4
5
6
7
8
9
0
@@ -1,10 +1,9 @@
0
 class <%= class_name %> < ActiveRecord::Migration
0
   def self.up
0
     create_table :settings, :force => true do |t|
0
- t.column :var, :string, :null => false
0
- t.column :value, :text, :null => true
0
- t.column :created_at, :datetime
0
- t.column :updated_at, :datetime
0
+ t.string :var, :null => false
0
+ t.text :value, :null => true
0
+ t.timestamps
0
     end
0
   end
0
 
...
1
2
3
4
5
 
 
 
 
 
 
 
 
6
7
8
9
10
11
12
13
 
 
14
15
16
 
 
17
18
19
 
20
21
22
...
44
45
46
47
48
49
50
51
52
53
54
55
56
 
 
57
58
59
...
1
 
 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
 
18
19
20
21
22
23
24
25
 
26
27
28
29
30
...
52
53
54
 
 
 
 
 
 
55
56
 
 
57
58
59
60
61
0
@@ -1,22 +1,30 @@
0
 class Settings < ActiveRecord::Base
0
- # @@defaults = (defined?(SettingsDefaults) ? SettingsDefaults::DEFAULTS : {}).with_indifferent_access
0
-
0
   class SettingNotFound < RuntimeError; end
0
   
0
+ cattr_accessor :defaults
0
+ @@defaults = {}.with_indifferent_access
0
+
0
+ # Support old plugin
0
+ if defined?(SettingsDefaults::DEFAULTS)
0
+ @@defaults = SettingsDefaults::DEFAULTS.with_indifferent_access
0
+ end
0
+
0
   #get or set a variable with the variable as the called method
0
   def self.method_missing(method, *args)
0
     method_name = method.to_s
0
     super(method, *args)
0
     
0
   rescue NoMethodError
0
- if method_name[-1..-1] == '='
0
- #set a value for a variable
0
+ #set a value for a variable
0
+ if method_name =~ /=$/
0
       var_name = method_name.gsub('=', '')
0
       value = args.first
0
       self[var_name] = value
0
+
0
+ #retrieve a value
0
     else
0
- #retrieve a value
0
       self[method_name]
0
+
0
     end
0
   end
0
   
0
@@ -44,16 +52,10 @@ class Settings < ActiveRecord::Base
0
   
0
   #retrieve a setting value by [] notation
0
   def self.[](var_name)
0
- #Ensure defaults are loaded
0
- @@defaults ||= (defined?(SettingsDefaults) ? SettingsDefaults::DEFAULTS : {}).with_indifferent_access
0
-
0
- #retrieve a setting
0
- var_name = var_name.to_s
0
-
0
     if var = object(var_name)
0
       var.value
0
- elsif @@defaults[var_name]
0
- @@defaults[var_name]
0
+ elsif @@defaults[var_name.to_s]
0
+ @@defaults[var_name.to_s]
0
     else
0
       nil
0
     end
...
1
2
3
4
5
6
7
8
9
10
11
12
 
 
13
14
15
16
17
18
19
20
21
22
 
 
 
 
 
 
 
 
 
 
23
24
25
26
 
27
28
29
...
35
36
37
38
39
40
 
 
 
41
42
43
...
51
52
53
54
 
55
56
57
...
1
2
3
 
 
 
 
4
5
6
 
 
7
8
9
10
 
 
 
 
 
 
 
 
11
12
13
14
15
16
17
18
19
20
21
22
23
 
24
25
26
27
...
33
34
35
 
 
 
36
37
38
39
40
41
...
49
50
51
 
52
53
54
55
0
@@ -1,29 +1,27 @@
0
 #!/usr/bin/env ruby
0
 require File.dirname(__FILE__) + '/../../../../test/test_helper'
0
 
0
-#module SettingsDefaults
0
-# DEFAULTS = {:some_setting => 'foo'}
0
-#end
0
-
0
 class SettingsTest < Test::Unit::TestCase
0
   
0
   def setup
0
- Settings.create(:var => 'test', :value => 'foo'.to_yaml)
0
- Settings.create(:var => 'secondary_test', :value => 'bar'.to_yaml)
0
+ Settings.create(:var => 'test', :value => 'foo')
0
+ Settings.create(:var => 'test2', :value => 'bar')
0
   end
0
   
0
-# def test_defaults
0
-# assert_equal 'foo', Settings.some_setting
0
-# assert_nil Settings.find(:first, :conditions => ['var = ?', 'some_setting'])
0
-#
0
-# Settings.some_setting = 'bar'
0
-# assert_equal 'bar', Settings.some_setting
0
-# assert_not_nil Settings.find(:first, :conditions => ['var = ?', 'some_setting'])
0
-# end
0
+ def test_defaults
0
+ Settings.defaults[:foo] = 'default foo'
0
+
0
+ assert_nil Settings.object(:foo)
0
+ assert_equal 'default foo', Settings.foo
0
+
0
+ Settings.foo = 'bar'
0
+ assert_equal 'bar', Settings.foo
0
+ assert_not_nil Settings.object(:foo)
0
+ end
0
   
0
   def test_get
0
     assert_setting 'foo', :test
0
- assert_setting 'bar', :secondary_test
0
+ assert_setting 'bar', :test2
0
   end
0
   
0
   def test_update
0
@@ -35,9 +33,9 @@ class SettingsTest < Test::Unit::TestCase
0
   end
0
   
0
   def test_complex_serialization
0
- object = [1, '2', {:three => true}]
0
- Settings.object = object
0
- assert_equal object, Settings.object
0
+ complex = [1, '2', {:three => true}]
0
+ Settings.complex = complex
0
+ assert_equal complex, Settings.complex
0
   end
0
   
0
   def test_serialization_of_float
0
@@ -51,7 +49,7 @@ class SettingsTest < Test::Unit::TestCase
0
     def assert_setting(value, key)
0
       key = key.to_sym
0
       assert_equal value, eval("Settings.#{key}")
0
- assert_equal value, Settings[key]
0
+ assert_equal value, Settings[key.to_sym]
0
       assert_equal value, Settings[key.to_s]
0
     end
0
     

Comments

    No one has commented yet.