settings_on_rails 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +50 -21
- data/lib/settings_on_rails/settings.rb +4 -0
- data/lib/settings_on_rails/version.rb +1 -1
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d01e255a35c506e47539e153ae23cabb903e9c52
|
4
|
+
data.tar.gz: 6e573d6f5554b495193174535a6998f027124eb0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fc44c017dd98bcb5b3d91a7c58fd4b948efc4a2f0da732da57c610150e26d87a230277fbc7a5c7e7bf28a96cedc482249759f24e70c59ca4d2e961cf5bcd2eb9
|
7
|
+
data.tar.gz: 845f2459c7dd562f200fd1a3c4935d0c8980b856284ce37233bab13bbd46b5752344493f7a8590716a0cc1aa8b10b129dc5d0a072ae59026ded05a6dd1e4816f
|
data/README.md
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
|
-
|
7
|
+
If you are using [Bundler](http://bundler.io/), add this line to your application's Gemfile:
|
8
8
|
|
9
9
|
```ruby
|
10
10
|
gem 'settings_on_rails'
|
@@ -14,26 +14,30 @@ And then execute:
|
|
14
14
|
|
15
15
|
$ bundle
|
16
16
|
|
17
|
-
|
17
|
+
Alternatively, install it by running:
|
18
18
|
|
19
19
|
$ gem install settings_on_rails
|
20
20
|
|
21
21
|
## Getting Started
|
22
22
|
|
23
|
-
|
23
|
+
### Add database column
|
24
|
+
|
25
|
+
Start by adding a text field to the model on which you want settings:
|
26
|
+
|
24
27
|
```ruby
|
25
28
|
rails g migration add_settings_column_to_blogs settings_column:text
|
26
|
-
|
27
29
|
```
|
28
30
|
|
29
|
-
Declare in model
|
31
|
+
### Declare in model
|
32
|
+
|
30
33
|
```ruby
|
31
34
|
class Blog < ActiveRecord::Base
|
32
35
|
has_settings_on :settings_column
|
33
36
|
end
|
34
37
|
```
|
35
38
|
|
36
|
-
Set settings
|
39
|
+
### Set settings
|
40
|
+
|
37
41
|
```ruby
|
38
42
|
@blog.settings.title = 'My Space'
|
39
43
|
@blog.settings(:theme).background_color = 'blue'
|
@@ -41,14 +45,18 @@ Set settings
|
|
41
45
|
@blog.save
|
42
46
|
```
|
43
47
|
|
44
|
-
Get settings
|
48
|
+
### Get settings
|
49
|
+
|
45
50
|
```ruby
|
46
|
-
@blog.settings(:theme).background_color
|
51
|
+
@blog.settings(:theme).background_color
|
52
|
+
=> 'blue'
|
47
53
|
|
48
|
-
|
54
|
+
# returns nil if not set
|
55
|
+
@blog.settings(:post).pagination
|
56
|
+
=> nil
|
49
57
|
```
|
50
58
|
|
51
|
-
## Default Values
|
59
|
+
## Defining Default Values
|
52
60
|
|
53
61
|
```ruby
|
54
62
|
class Blog < ActiveRecord::Base
|
@@ -60,50 +68,71 @@ class Blog < ActiveRecord::Base
|
|
60
68
|
end
|
61
69
|
end
|
62
70
|
```
|
71
|
+
|
63
72
|
OR
|
73
|
+
|
64
74
|
```ruby
|
65
75
|
class Blog < ActiveRecord::Base
|
66
76
|
has_settings_on :column do |s|
|
67
|
-
s.key :theme, defaults:{ background_color: 'red', text_size: 50 }
|
77
|
+
s.key :theme, defaults: { background_color: 'red', text_size: 50 }
|
68
78
|
s.attr :title, default: 'My Space'
|
69
79
|
end
|
70
80
|
end
|
71
81
|
```
|
72
82
|
|
73
83
|
You can get these defaults by:
|
84
|
+
|
74
85
|
```ruby
|
75
|
-
@blog.settings(:theme).background_color
|
76
|
-
|
77
|
-
|
86
|
+
@blog.settings(:theme).background_color
|
87
|
+
=> 'red'
|
88
|
+
|
89
|
+
@blog.settings(:theme).text_size
|
90
|
+
=> 50
|
91
|
+
|
92
|
+
@blog.settings.title
|
93
|
+
=> 'My Space'
|
78
94
|
```
|
79
95
|
|
80
96
|
## Nested Keys
|
81
|
-
|
97
|
+
|
98
|
+
Settings on Rails supports nested keys by chaining calls to the `settings` method:
|
99
|
+
|
82
100
|
```ruby
|
83
101
|
# Set
|
84
102
|
@blog.settings(:theme).settings(:homepage).background_color = 'white'
|
103
|
+
|
85
104
|
# Get
|
86
|
-
@blog.settings(:theme).settings(:homepage).background_color
|
105
|
+
@blog.settings(:theme).settings(:homepage).background_color
|
106
|
+
=> 'white'
|
87
107
|
```
|
88
108
|
|
89
|
-
|
90
|
-
|
109
|
+
## Multiple Keys
|
110
|
+
|
111
|
+
You can also define multiple keys in the following way, this is equivalent to nested keys:
|
112
|
+
|
91
113
|
```ruby
|
92
114
|
# Set
|
93
115
|
@blog.settings(:theme, :homepage).background_color = 'white'
|
116
|
+
|
94
117
|
# Get
|
95
|
-
@blog.settings(:theme, :homepage).background_color
|
118
|
+
@blog.settings(:theme, :homepage).background_color
|
119
|
+
=> 'white'
|
96
120
|
```
|
97
121
|
|
98
122
|
|
99
123
|
## Method Name Customization
|
100
|
-
|
124
|
+
|
125
|
+
You can customize the name of the `settings` method:
|
126
|
+
|
101
127
|
```ruby
|
102
128
|
class Blog < ActiveRecord::Base
|
103
129
|
has_settings_on :settings_column, method: :preferences
|
104
130
|
end
|
131
|
+
```
|
105
132
|
|
106
|
-
|
133
|
+
Which allows you to do:
|
134
|
+
|
135
|
+
```ruby
|
107
136
|
@blog.preferences(:theme).background_color
|
108
137
|
```
|
109
138
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: settings_on_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ALLEN WANG QIANG
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-10-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -125,9 +125,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
125
125
|
version: '0'
|
126
126
|
requirements: []
|
127
127
|
rubyforge_project:
|
128
|
-
rubygems_version: 2.4.5
|
128
|
+
rubygems_version: 2.4.5.1
|
129
129
|
signing_key:
|
130
130
|
specification_version: 4
|
131
131
|
summary: Model specific Hash Preferences/Settings for Rails.
|
132
132
|
test_files: []
|
133
|
-
has_rdoc:
|