-
Notifications
You must be signed in to change notification settings - Fork 7
/
README
115 lines (85 loc) · 3.44 KB
/
README
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
31
32
33
34
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
= ActsAsConfigurable
This plugin provides the ability to specify settings on a model class
similarly to the way columns are defined in migrations, including
default values. It does this by saving the settings to a TEXT column
as a Hash object, serialized by YAML.
While you could get the same effect by creating a column for each
individual setting, this becomes cumbersome when there are more than a
handful of settings, or when the settings need to be changed
frequently.
== Getting Started
First, install the plugin:
$ ./script/plugin install git://github.com/omghax/acts_as_configurable.git
Next, declare acts_as_configurable in your model(s):
class Blog < ActiveRecord::Base
acts_as_configurable do |c|
c.string :title, :default => "My Blog"
c.integer :number_of_frontpage_articles, :default => 10
c.boolean :enable_akismet, :default => false
end
end
This will define instance methods on Blog for each setting, with the
specified default values. These attributes can be used in
mass-assignment, validations, and pretty much anywhere else you'd use
a normal database column.
The available types of settings are :string, :integer, :boolean, and
:object.
By default, these settings will be stored inside a database column
named "settings". If you'd prefer to store them elsewhere, use the
:using option.
class Blog < ActiveRecord::Base
acts_as_configurable :using => "some_other_column" do |c|
...
end
end
This column should be a TEXT column.
=== Strings
First let's take a look at string settings. These behave as you would
expect them to.
Example:
>> b = Blog.new
>> b.title
# => "My Blog"
>> b.title = "My Custom Blog"
>> b.title
# => "My Custom Blog"
=== Integers
Integer settings work similarly to string settings, but are coerced
into integers when their values are set.
=== Booleans
Here's how you deal with boolean settings. Note that these settings
are flexible with what they accept during assignment.
[false]
nil, 0, "0", "f", "false", false
[true]
anything else
Example:
>> b.enable_akismet?
# => false
>> b.enable_akismet = true
>> b.enable_akismet?
# => true
=== Objects
Object settings allow you to use any serializable ruby object to
represent a setting's value. They should be used with care though, as
changes to your code can potentially break the records in your
database which reference earlier versions of classes that have
changed.
== License
Copyright (c) 2008 Dray Lacy
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.