rails-settings-cached 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +71 -33
- data/lib/rails-settings/settings.rb +27 -26
- metadata +10 -4
data/README.md
CHANGED
@@ -15,85 +15,123 @@ arrays, or any object. Ported to Rails 3!
|
|
15
15
|
|
16
16
|
Edit your Gemfile:
|
17
17
|
|
18
|
-
|
18
|
+
```ruby
|
19
|
+
gem "rails-settings-cached"
|
20
|
+
```
|
19
21
|
|
20
22
|
Generate your settings:
|
21
23
|
|
22
|
-
|
24
|
+
```bash
|
25
|
+
$ rails g settings <settings_name>
|
26
|
+
```
|
23
27
|
|
24
28
|
Now just put that migration in the database with:
|
25
29
|
|
26
|
-
|
30
|
+
```bash
|
31
|
+
rake db:migrate
|
32
|
+
```
|
27
33
|
|
28
34
|
## Usage
|
29
35
|
|
30
36
|
The syntax is easy. First, lets create some settings to keep track of:
|
31
37
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
38
|
+
```ruby
|
39
|
+
Setting.admin_password = 'supersecret'
|
40
|
+
Setting.date_format = '%m %d, %Y'
|
41
|
+
Setting.cocktails = ['Martini', 'Screwdriver', 'White Russian']
|
42
|
+
Setting.foo = 123
|
43
|
+
Setting.credentials = { :username => 'tom', :password => 'secret' }
|
44
|
+
```
|
37
45
|
|
38
46
|
Now lets read them back:
|
39
47
|
|
40
|
-
|
48
|
+
```ruby
|
49
|
+
Setting.foo # returns 123
|
50
|
+
```
|
41
51
|
|
42
52
|
Changing an existing setting is the same as creating a new setting:
|
43
53
|
|
44
|
-
|
54
|
+
```ruby
|
55
|
+
Setting.foo = 'super duper bar'
|
56
|
+
```
|
45
57
|
|
46
58
|
For changing an existing setting which is a Hash, you can merge new values with existing ones:
|
47
59
|
|
48
|
-
|
49
|
-
|
60
|
+
```ruby
|
61
|
+
Setting.merge!(:credentials, :password => 'topsecret')
|
62
|
+
Setting.credentials # returns { :username => 'tom', :password => 'topsecret' }
|
63
|
+
```
|
50
64
|
|
51
65
|
Decide you dont want to track a particular setting anymore?
|
52
66
|
|
53
|
-
|
54
|
-
|
67
|
+
```ruby
|
68
|
+
Setting.destroy :foo
|
69
|
+
Setting.foo # returns nil
|
70
|
+
```
|
55
71
|
|
56
72
|
Want a list of all the settings?
|
57
73
|
|
58
|
-
|
74
|
+
```ruby
|
75
|
+
Setting.all
|
76
|
+
# returns {'admin_password' => 'super_secret', 'date_format' => '%m %d, %Y'}
|
77
|
+
```
|
59
78
|
|
60
79
|
You need name spaces and want a list of settings for a give name space? Just choose your prefered named space delimiter and use Setting.all like this:
|
61
80
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
81
|
+
```ruby
|
82
|
+
Setting['preferences.color'] = :blue
|
83
|
+
Setting['preferences.size'] = :large
|
84
|
+
Setting['license.key'] = 'ABC-DEF'
|
85
|
+
Setting.all('preferences.')
|
86
|
+
# returns { 'preferences.color' => :blue, 'preferences.size' => :large }
|
87
|
+
```
|
66
88
|
|
67
89
|
Set defaults for certain settings of your app. This will cause the defined settings to return with the
|
68
90
|
Specified value even if they are not in the database. Make a new file in `config/initializers/default_settings.rb`
|
69
91
|
with the following:
|
70
92
|
|
71
|
-
|
93
|
+
```ruby
|
94
|
+
Setting.defaults[:some_setting] = 'footastic'
|
95
|
+
```
|
72
96
|
|
73
97
|
Now even if the database is completely empty, you app will have some intelligent defaults:
|
74
98
|
|
75
|
-
|
99
|
+
```ruby
|
100
|
+
Setting.some_setting # returns 'footastic'
|
101
|
+
```
|
76
102
|
|
77
103
|
Settings may be bound to any existing ActiveRecord object. Define this association like this:
|
78
104
|
Notice! is not do caching in this version.
|
79
105
|
|
80
|
-
|
81
|
-
|
82
|
-
|
106
|
+
```ruby
|
107
|
+
class User < ActiveRecord::Base
|
108
|
+
include RailsSettings::Extend
|
109
|
+
end
|
110
|
+
```
|
83
111
|
|
84
112
|
Then you can set/get a setting for a given user instance just by doing this:
|
85
113
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
114
|
+
```ruby
|
115
|
+
user = User.find(123)
|
116
|
+
user.settings.color = :red
|
117
|
+
user.settings.color # returns :red
|
118
|
+
user.settings.all # { "color" => :red }
|
119
|
+
```
|
90
120
|
|
91
121
|
I you want to find users having or not having some settings, there are named scopes for this:
|
92
122
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
123
|
+
```ruby
|
124
|
+
User.with_settings
|
125
|
+
# => returns a scope of users having any setting
|
126
|
+
|
127
|
+
User.with_settings_for('color')
|
128
|
+
# => returns a scope of users having a 'color' setting
|
129
|
+
|
130
|
+
User.without_settings
|
131
|
+
# returns a scope of users having no setting at all (means user.settings.all == {})
|
132
|
+
|
133
|
+
User.without_settings('color')
|
134
|
+
# returns a scope of users having no 'color' setting (means user.settings.color == nil)
|
135
|
+
```
|
98
136
|
|
99
137
|
That's all there is to it! Enjoy!
|
@@ -1,37 +1,38 @@
|
|
1
1
|
module RailsSettings
|
2
2
|
class Settings < ActiveRecord::Base
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
|
4
|
+
self.table_name = 'settings'
|
5
|
+
attr_accessible :var
|
6
|
+
|
6
7
|
class SettingNotFound < RuntimeError; end
|
7
|
-
|
8
|
+
|
8
9
|
cattr_accessor :defaults
|
9
10
|
@@defaults = {}.with_indifferent_access
|
10
|
-
|
11
|
+
|
11
12
|
# Support old plugin
|
12
13
|
if defined?(SettingsDefaults::DEFAULTS)
|
13
14
|
@@defaults = SettingsDefaults::DEFAULTS.with_indifferent_access
|
14
15
|
end
|
15
|
-
|
16
|
+
|
16
17
|
#get or set a variable with the variable as the called method
|
17
18
|
def self.method_missing(method, *args)
|
18
19
|
method_name = method.to_s
|
19
20
|
super(method, *args)
|
20
|
-
|
21
|
+
|
21
22
|
rescue NoMethodError
|
22
23
|
#set a value for a variable
|
23
24
|
if method_name =~ /=$/
|
24
25
|
var_name = method_name.gsub('=', '')
|
25
26
|
value = args.first
|
26
27
|
self[var_name] = value
|
27
|
-
|
28
|
+
|
28
29
|
#retrieve a value
|
29
30
|
else
|
30
31
|
self[method_name]
|
31
|
-
|
32
|
+
|
32
33
|
end
|
33
34
|
end
|
34
|
-
|
35
|
+
|
35
36
|
#destroy the specified settings record
|
36
37
|
def self.destroy(var_name)
|
37
38
|
var_name = var_name.to_s
|
@@ -42,19 +43,19 @@ module RailsSettings
|
|
42
43
|
raise SettingNotFound, "Setting variable \"#{var_name}\" not found"
|
43
44
|
end
|
44
45
|
end
|
45
|
-
|
46
|
+
|
46
47
|
#retrieve all settings as a hash (optionally starting with a given namespace)
|
47
48
|
def self.all(starting_with=nil)
|
48
49
|
options = starting_with ? { :conditions => "var LIKE '#{starting_with}%'"} : {}
|
49
50
|
vars = thing_scoped.find(:all, {:select => 'var, value'}.merge(options))
|
50
|
-
|
51
|
+
|
51
52
|
result = {}
|
52
53
|
vars.each do |record|
|
53
54
|
result[record.var] = record.value
|
54
55
|
end
|
55
56
|
result.with_indifferent_access
|
56
57
|
end
|
57
|
-
|
58
|
+
|
58
59
|
#get a setting value by [] notation
|
59
60
|
def self.[](var_name)
|
60
61
|
if var = object(var_name)
|
@@ -65,48 +66,48 @@ module RailsSettings
|
|
65
66
|
nil
|
66
67
|
end
|
67
68
|
end
|
68
|
-
|
69
|
+
|
69
70
|
#set a setting value by [] notation
|
70
71
|
def self.[]=(var_name, value)
|
71
72
|
var_name = var_name.to_s
|
72
|
-
|
73
|
+
|
73
74
|
record = object(var_name) || thing_scoped.new(:var => var_name)
|
74
75
|
record.value = value
|
75
76
|
record.save!
|
76
|
-
|
77
|
+
|
77
78
|
value
|
78
79
|
end
|
79
|
-
|
80
|
+
|
80
81
|
def self.merge!(var_name, hash_value)
|
81
82
|
raise ArgumentError unless hash_value.is_a?(Hash)
|
82
|
-
|
83
|
+
|
83
84
|
old_value = self[var_name] || {}
|
84
85
|
raise TypeError, "Existing value is not a hash, can't merge!" unless old_value.is_a?(Hash)
|
85
|
-
|
86
|
+
|
86
87
|
new_value = old_value.merge(hash_value)
|
87
88
|
self[var_name] = new_value if new_value != old_value
|
88
|
-
|
89
|
+
|
89
90
|
new_value
|
90
91
|
end
|
91
|
-
|
92
|
+
|
92
93
|
def self.object(var_name)
|
93
94
|
thing_scoped.find_by_var(var_name.to_s)
|
94
95
|
end
|
95
|
-
|
96
|
+
|
96
97
|
#get the value field, YAML decoded
|
97
98
|
def value
|
98
99
|
YAML::load(self[:value])
|
99
100
|
end
|
100
|
-
|
101
|
+
|
101
102
|
#set the value field, YAML encoded
|
102
103
|
def value=(new_value)
|
103
104
|
self[:value] = new_value.to_yaml
|
104
105
|
end
|
105
|
-
|
106
|
+
|
106
107
|
def self.thing_scoped
|
107
108
|
self.scoped_by_thing_type_and_thing_id(nil, nil)
|
108
109
|
end
|
109
|
-
|
110
|
-
|
110
|
+
|
111
|
+
|
111
112
|
end
|
112
113
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-settings-cached
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -16,7 +16,7 @@ date: 2011-01-14 00:00:00.000000000 Z
|
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|
18
18
|
name: rails
|
19
|
-
requirement:
|
19
|
+
requirement: !ruby/object:Gem::Requirement
|
20
20
|
none: false
|
21
21
|
requirements:
|
22
22
|
- - ! '>='
|
@@ -24,7 +24,12 @@ dependencies:
|
|
24
24
|
version: 3.0.0
|
25
25
|
type: :runtime
|
26
26
|
prerelease: false
|
27
|
-
version_requirements:
|
27
|
+
version_requirements: !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 3.0.0
|
28
33
|
description:
|
29
34
|
email: huacnlee@gmail.com
|
30
35
|
executables: []
|
@@ -60,7 +65,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
60
65
|
version: '0'
|
61
66
|
requirements: []
|
62
67
|
rubyforge_project:
|
63
|
-
rubygems_version: 1.8.
|
68
|
+
rubygems_version: 1.8.24
|
64
69
|
signing_key:
|
65
70
|
specification_version: 3
|
66
71
|
summary: This is imporved from rails-settings, added caching. Settings is a plugin
|
@@ -70,3 +75,4 @@ summary: This is imporved from rails-settings, added caching. Settings is a plug
|
|
70
75
|
into your rails app. You can store any kind of object. Strings, numbers, arrays,
|
71
76
|
or any object. Ported to Rails 3!
|
72
77
|
test_files: []
|
78
|
+
has_rdoc:
|