Permalink
Browse files

Modernized code a bit, and got tests passing. Implemented easier and …

…simpler defaults.
  • Loading branch information...
1 parent cd5c446 commit c409585a39d89378f10c081d1b739c22384565ad @Squeegy Squeegy committed Apr 12, 2008
View
@@ -6,13 +6,14 @@ like methods for manipulation. Keep track of any global setting that you dont w
to hard code into your rails app. You can store any kind of object. Strings, numbers,
arrays, or any object.
+
== Setup
You must create the table used by the Settings model. Simply run this command:
ruby script/generate settings_migration
Now just put that migration in the database with:
- rake migrate
+ rake db:migrate
== Usage
@@ -35,25 +36,21 @@ Changing an existing setting is the same as creating a new setting:
Decide you dont want to track a particular setting anymore?
Settings.destroy :foo
- Settings.foo # Now gives a setting variable not found error.
+ Settings.foo # returns nil
Want a list of all the settings?
Settings.all # returns {'admin_password' => 'super_secret', 'date_format' => '%m %d, %Y'}
Set defaults for certain settings of your app. This will cause the defined settings to return with the
-Specified value even if they are not in the database. Here is what you insert into your environment.rb
-
- module SettingsDefaults
- DEFAULTS = {
- :setting_one => 'footastic',
- :setting_two => 123.321
- }
- end
+Specified value even if they are not in the database. Make a new file in config/initializers/settings.rb
+with the following:
+
+ Settings.defaults[:some_setting] = 'footastic'
- Settings.setting_one #=> returns "footastic" even though no record is in the databse for "some_setting"
- Settings.setting_one = 'bar' # Database record is now created and 'bar' will be used instead of the default.
+Now even if the database is completely empty, you app will have some intelligent defaults:
+
+ Settings.some_setting # returns 'footastic'
-NOTE: the server must be restarted in order to see new default settings.
-All there is to it!. Enjoy!
+That's all there is to it!. Enjoy!
View
@@ -1,230 +0,0 @@
-<?xml version="1.0" encoding="iso-8859-1"?>
-<!DOCTYPE html
- PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
-<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
-<head>
- <title>Class: Settings</title>
- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
- <meta http-equiv="Content-Script-Type" content="text/javascript" />
- <link rel="stylesheet" href=".././rdoc-style.css" type="text/css" media="screen" />
- <script type="text/javascript">
- // <![CDATA[
-
- function popupCode( url ) {
- window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
- }
-
- function toggleCode( id ) {
- if ( document.getElementById )
- elem = document.getElementById( id );
- else if ( document.all )
- elem = eval( "document.all." + id );
- else
- return false;
-
- elemStyle = elem.style;
-
- if ( elemStyle.display != "block" ) {
- elemStyle.display = "block"
- } else {
- elemStyle.display = "none"
- }
-
- return true;
- }
-
- // Make codeblocks hidden by default
- document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
-
- // ]]>
- </script>
-
-</head>
-<body>
-
-
-
- <div id="classHeader">
- <table class="header-table">
- <tr class="top-aligned-row">
- <td><strong>Class</strong></td>
- <td class="class-name-in-header">Settings</td>
- </tr>
- <tr class="top-aligned-row">
- <td><strong>In:</strong></td>
- <td>
- <a href="../files/lib/settings_rb.html">
- lib/settings.rb
- </a>
- <br />
- </td>
- </tr>
-
- <tr class="top-aligned-row">
- <td><strong>Parent:</strong></td>
- <td>
- ActiveRecord::Base
- </td>
- </tr>
- </table>
- </div>
- <!-- banner header -->
-
- <div id="bodyContent">
-
-
-
- <div id="contextContent">
-
-
-
- </div>
-
- <div id="method-list">
- <h3 class="section-bar">Methods</h3>
-
- <div class="name-list">
- <a href="#M000005">[]</a>&nbsp;&nbsp;
- <a href="#M000006">[]=</a>&nbsp;&nbsp;
- <a href="#M000003">all</a>&nbsp;&nbsp;
- <a href="#M000002">destroy</a>&nbsp;&nbsp;
- <a href="#M000001">method_missing</a>&nbsp;&nbsp;
- <a href="#M000004">reload</a>&nbsp;&nbsp;
- </div>
- </div>
-
- </div>
-
-
- <!-- if includes -->
-
- <div id="section">
-
-
-
-
-
-
-
-
- <!-- if method_list -->
- <div id="methods">
- <h3 class="section-bar">Public Class methods</h3>
-
- <div id="method-M000005" class="method-detail">
- <a name="M000005"></a>
-
- <div class="method-heading">
- <a href="Settings.src/M000005.html" target="Code" class="method-signature"
- onclick="popupCode('Settings.src/M000005.html');return false;">
- <span class="method-name">[]</span><span class="method-args">(var_name)</span>
- </a>
- </div>
-
- <div class="method-description">
- <p>
-retrieve a setting value bar [] notation
-</p>
- </div>
- </div>
-
- <div id="method-M000006" class="method-detail">
- <a name="M000006"></a>
-
- <div class="method-heading">
- <a href="Settings.src/M000006.html" target="Code" class="method-signature"
- onclick="popupCode('Settings.src/M000006.html');return false;">
- <span class="method-name">[]=</span><span class="method-args">(var_name, value)</span>
- </a>
- </div>
-
- <div class="method-description">
- <p>
-set a setting value by [] notation
-</p>
- </div>
- </div>
-
- <div id="method-M000003" class="method-detail">
- <a name="M000003"></a>
-
- <div class="method-heading">
- <a href="Settings.src/M000003.html" target="Code" class="method-signature"
- onclick="popupCode('Settings.src/M000003.html');return false;">
- <span class="method-name">all</span><span class="method-args">()</span>
- </a>
- </div>
-
- <div class="method-description">
- <p>
-retrieve all settings as a hash
-</p>
- </div>
- </div>
-
- <div id="method-M000002" class="method-detail">
- <a name="M000002"></a>
-
- <div class="method-heading">
- <a href="Settings.src/M000002.html" target="Code" class="method-signature"
- onclick="popupCode('Settings.src/M000002.html');return false;">
- <span class="method-name">destroy</span><span class="method-args">(var_name)</span>
- </a>
- </div>
-
- <div class="method-description">
- <p>
-destroy the specified settings record
-</p>
- </div>
- </div>
-
- <div id="method-M000001" class="method-detail">
- <a name="M000001"></a>
-
- <div class="method-heading">
- <a href="Settings.src/M000001.html" target="Code" class="method-signature"
- onclick="popupCode('Settings.src/M000001.html');return false;">
- <span class="method-name">method_missing</span><span class="method-args">(method, *args)</span>
- </a>
- </div>
-
- <div class="method-description">
- <p>
-get or set a variable with the variable as the called method
-</p>
- </div>
- </div>
-
- <div id="method-M000004" class="method-detail">
- <a name="M000004"></a>
-
- <div class="method-heading">
- <a href="Settings.src/M000004.html" target="Code" class="method-signature"
- onclick="popupCode('Settings.src/M000004.html');return false;">
- <span class="method-name">reload</span><span class="method-args">()</span>
- </a>
- </div>
-
- <div class="method-description">
- <p>
-reload all settings form the db
-</p>
- </div>
- </div>
-
-
- </div>
-
-
- </div>
-
-
-<div id="validator-badges">
- <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
-</div>
-
-</body>
-</html>
@@ -1,28 +0,0 @@
-<?xml version="1.0" encoding="iso-8859-1"?>
-<!DOCTYPE html
- PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
-<html>
-<head>
- <title>method_missing (Settings)</title>
- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
- <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
-</head>
-<body class="standalone-code">
- <pre><span class="ruby-comment cmt"># File lib/settings.rb, line 6</span>
- <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">method_missing</span>(<span class="ruby-identifier">method</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">args</span>)
- <span class="ruby-identifier">method_name</span> = <span class="ruby-identifier">method</span>.<span class="ruby-identifier">to_s</span>
-
- <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">method_name</span>.<span class="ruby-identifier">include?</span> <span class="ruby-value str">'='</span>
- <span class="ruby-comment cmt">#set a value for a variable</span>
- <span class="ruby-identifier">var_name</span> = <span class="ruby-identifier">method_name</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-value str">'='</span>, <span class="ruby-value str">''</span>)
- <span class="ruby-identifier">value</span> = <span class="ruby-identifier">args</span>.<span class="ruby-identifier">first</span>
- <span class="ruby-keyword kw">self</span>[<span class="ruby-identifier">var_name</span>] = <span class="ruby-identifier">value</span>
- <span class="ruby-keyword kw">else</span>
- <span class="ruby-comment cmt">#retrieve a value</span>
- <span class="ruby-keyword kw">self</span>[<span class="ruby-identifier">method_name</span>]
- <span class="ruby-keyword kw">end</span>
- <span class="ruby-keyword kw">end</span></pre>
-</body>
-</html>
@@ -1,19 +0,0 @@
-<?xml version="1.0" encoding="iso-8859-1"?>
-<!DOCTYPE html
- PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
-<html>
-<head>
- <title>destroy (Settings)</title>
- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
- <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
-</head>
-<body class="standalone-code">
- <pre><span class="ruby-comment cmt"># File lib/settings.rb, line 21</span>
- <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">destroy</span>(<span class="ruby-identifier">var_name</span>)
- <span class="ruby-keyword kw">return</span> <span class="ruby-ivar">@@cache</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">var_name</span>.<span class="ruby-identifier">to_s</span>) <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">delete_all</span>([<span class="ruby-value str">'var = ?'</span>, <span class="ruby-identifier">var_name</span>.<span class="ruby-identifier">to_s</span>]) <span class="ruby-comment cmt">#variable exists, destroy row and cache</span>
- <span class="ruby-identifier">raise</span> <span class="ruby-node">&quot;Setting variable \&quot;#{var_name}\&quot; not found&quot;</span>
- <span class="ruby-keyword kw">end</span></pre>
-</body>
-</html>
@@ -1,25 +0,0 @@
-<?xml version="1.0" encoding="iso-8859-1"?>
-<!DOCTYPE html
- PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
-<html>
-<head>
- <title>all (Settings)</title>
- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
- <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
-</head>
-<body class="standalone-code">
- <pre><span class="ruby-comment cmt"># File lib/settings.rb, line 27</span>
- <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">all</span>
- <span class="ruby-identifier">vars</span> = <span class="ruby-identifier">find</span>(<span class="ruby-identifier">:all</span>, <span class="ruby-identifier">:select</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">'var, value'</span>)
-
- <span class="ruby-identifier">result</span> = {}
- <span class="ruby-identifier">vars</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">record</span><span class="ruby-operator">|</span>
- <span class="ruby-identifier">result</span>[<span class="ruby-identifier">record</span>.<span class="ruby-identifier">var</span>] = <span class="ruby-constant">YAML</span><span class="ruby-operator">::</span><span class="ruby-identifier">load</span>(<span class="ruby-identifier">record</span>.<span class="ruby-identifier">value</span>)
- <span class="ruby-keyword kw">end</span>
- <span class="ruby-ivar">@@cache</span> = <span class="ruby-identifier">result</span>
- <span class="ruby-identifier">result</span>.<span class="ruby-identifier">with_indifferent_access</span>
- <span class="ruby-keyword kw">end</span></pre>
-</body>
-</html>
@@ -1,19 +0,0 @@
-<?xml version="1.0" encoding="iso-8859-1"?>
-<!DOCTYPE html
- PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
-<html>
-<head>
- <title>reload (Settings)</title>
- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
- <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
-</head>
-<body class="standalone-code">
- <pre><span class="ruby-comment cmt"># File lib/settings.rb, line 39</span>
- <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">reload</span>
- <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">all</span>
- <span class="ruby-keyword kw">self</span>
- <span class="ruby-keyword kw">end</span></pre>
-</body>
-</html>
Oops, something went wrong.

0 comments on commit c409585

Please sign in to comment.