tunable 0.0.1 → 0.0.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.
- checksums.yaml +4 -4
- data/README.md +22 -22
- data/lib/tunable/model.rb +2 -2
- data/lib/tunable/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 429e9db65fc91c748fc526ef2b8b3a8241490e39
|
4
|
+
data.tar.gz: 21c3c99469bebdd04cfcf5e9d383a15878eb2520
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b12ebe2c3d3670c3d9eb3de9511d0e5e068d90806c315c1011ca91c74fe9579bdca681102dde7f1aa47ed298dde33064fc56fb2f14393f14b1921bb849cc7bbb
|
7
|
+
data.tar.gz: 44e64b53736a25c6c754dccb8f6a842ee3182c0aeaf2400ecc9fd5b860ae834fe920d22ea209840206c7ddb2515d1bd9e476cb2457fcd12edf053ab4c46991cf
|
data/README.md
CHANGED
@@ -1,19 +1,14 @@
|
|
1
1
|
Tunable
|
2
2
|
=======
|
3
3
|
|
4
|
-
|
4
|
+
A simple gem that provides scoped, pluggable settings for your AR 4+ models. Let's you keep things simple in your models by moving all customizable settings into a separate table, using a polymorphic Settings model.
|
5
5
|
|
6
6
|
The code
|
7
7
|
--------
|
8
8
|
|
9
|
-
Let's set up notification settings for our Users.
|
10
|
-
|
11
9
|
``` rb
|
12
10
|
class User < ActiveRecord::Base
|
13
11
|
include Tunable::Model
|
14
|
-
|
15
|
-
has_settings :notify => :activity, :new_messages, :weekly_report
|
16
|
-
|
17
12
|
end
|
18
13
|
```
|
19
14
|
|
@@ -21,16 +16,23 @@ Now we can do:
|
|
21
16
|
|
22
17
|
``` rb
|
23
18
|
user = User.create(:name => 'John Lennon')
|
24
|
-
user.get_setting(:
|
19
|
+
user.get_setting(:theme, :layout) # => nil
|
20
|
+
user.get_setting(:theme, :color) # => nil
|
25
21
|
|
26
|
-
user.settings = {
|
22
|
+
user.settings = { theme: { format: 'wide', color: 'red' } }
|
27
23
|
user.save
|
28
24
|
|
29
|
-
user.get_setting(:
|
25
|
+
user.get_setting(:theme, :layout) # => 'wide'
|
26
|
+
user.get_setting(:theme, :color) # => 'red'
|
30
27
|
```
|
31
28
|
|
32
|
-
|
29
|
+
You can also get a flat hash of your settings by calling the `settings_hash` method.
|
30
|
+
|
31
|
+
``` rb
|
32
|
+
user.settings_hash # => { :theme => { :layout => 'wide', :color => 'red' } }
|
33
|
+
```
|
33
34
|
|
35
|
+
Tunable also lets you set defaults for your settings. Let's set up default notification settings for our Users.
|
34
36
|
|
35
37
|
``` rb
|
36
38
|
class User < ActiveRecord::Base
|
@@ -39,9 +41,8 @@ class User < ActiveRecord::Base
|
|
39
41
|
has_settings :notify => {
|
40
42
|
activity: { default: false },
|
41
43
|
new_messages: { default: true },
|
42
|
-
weekly_report: { default:
|
44
|
+
weekly_report: { default: false }
|
43
45
|
}
|
44
|
-
|
45
46
|
end
|
46
47
|
```
|
47
48
|
|
@@ -57,7 +58,7 @@ Now we can do:
|
|
57
58
|
user.get_setting(:notify, :new_messages) # => false
|
58
59
|
```
|
59
60
|
|
60
|
-
Tunable also provides a `main_settings` helper that sets up main level settings.
|
61
|
+
Tunable also provides a `main_settings` helper that sets up main level settings. These automatically define setters and getters for your model instances.
|
61
62
|
|
62
63
|
``` rb
|
63
64
|
class User < ActiveRecord::Base
|
@@ -65,13 +66,11 @@ class User < ActiveRecord::Base
|
|
65
66
|
|
66
67
|
# in this case we're not setting a default value for the :no_cookies setting
|
67
68
|
main_settings :no_cookies, :language => { :default => 'en' }
|
68
|
-
|
69
69
|
end
|
70
70
|
```
|
71
71
|
|
72
72
|
Now let's see what happens.
|
73
73
|
|
74
|
-
|
75
74
|
``` rb
|
76
75
|
user = User.create(:name => 'Paul MacCartney')
|
77
76
|
user.no_cookies # => nil
|
@@ -85,14 +84,15 @@ Now let's see what happens.
|
|
85
84
|
user.language # => 'es'
|
86
85
|
```
|
87
86
|
|
88
|
-
You can also set a lambda to return the default setting for a model.
|
87
|
+
Beautiful. You can also set a lambda to return the default setting for a model.
|
89
88
|
|
90
89
|
|
91
|
-
```
|
90
|
+
``` rb
|
92
91
|
class User < ActiveRecord::Base
|
92
|
+
include Tunable::Model
|
93
93
|
|
94
|
-
main_settings :
|
95
|
-
:default => lambda { |user| user.is_admin? ? '
|
94
|
+
main_settings :layout_type => {
|
95
|
+
:default => lambda { |user| user.is_admin? ? 'advanced' : 'simple' }
|
96
96
|
}
|
97
97
|
|
98
98
|
end
|
@@ -102,12 +102,12 @@ Then:
|
|
102
102
|
|
103
103
|
``` rb
|
104
104
|
user = User.create(:name => 'Ringo Starr', :admin => false)
|
105
|
-
user.
|
105
|
+
user.layout_type # => 'simple'
|
106
106
|
user.admin = true
|
107
|
-
user.
|
107
|
+
user.layout_type # => 'advanced'
|
108
108
|
```
|
109
109
|
|
110
|
-
That's pretty much it.
|
110
|
+
That's pretty much it. Fork away and send a PR, but please add tests for it.
|
111
111
|
|
112
112
|
Boring stuff
|
113
113
|
------------
|
data/lib/tunable/model.rb
CHANGED
@@ -139,7 +139,6 @@ module Tunable
|
|
139
139
|
# instance methods below
|
140
140
|
|
141
141
|
def settings=(hash)
|
142
|
-
puts hash.inspect
|
143
142
|
Tunable::Setting.store_many(hash, self)
|
144
143
|
end
|
145
144
|
|
@@ -230,7 +229,8 @@ module Tunable
|
|
230
229
|
# main settings, so we need to make sure we normalize here again
|
231
230
|
normalized_value = Tunable.normalize_value(value)
|
232
231
|
|
233
|
-
|
232
|
+
# class.base_class returns name of parent class for STI models
|
233
|
+
new_settings << [context.to_s, key.to_s, normalized_value, self.id, self.class.base_class.name]
|
234
234
|
# remove_instance_variable("@setting_main_#{key}") if context == :main
|
235
235
|
end
|
236
236
|
end
|
data/lib/tunable/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tunable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomás Pollak
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-05-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|