Skip to content

Commit 8dc786f

Browse files
committed
version 2.9.0
1 parent bbb7a67 commit 8dc786f

File tree

4 files changed

+54
-9
lines changed

4 files changed

+54
-9
lines changed

Gemfile.lock

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
rails-settings-cached (2.8.3)
4+
rails-settings-cached (2.9.0)
55
activerecord (>= 5.0.0)
66
railties (>= 5.0.0)
77

@@ -48,8 +48,6 @@ GEM
4848
nokogiri (1.13.10)
4949
mini_portile2 (~> 2.8.0)
5050
racc (~> 1.4)
51-
nokogiri (1.13.10-x86_64-darwin)
52-
racc (~> 1.4)
5351
parallel (1.22.1)
5452
parser (3.1.3.0)
5553
ast (~> 2.4.1)
@@ -89,7 +87,6 @@ GEM
8987
ruby-progressbar (1.11.0)
9088
sqlite3 (1.5.4)
9189
mini_portile2 (~> 2.8.0)
92-
sqlite3 (1.5.4-x86_64-darwin)
9390
thor (1.2.1)
9491
tzinfo (2.0.5)
9592
concurrent-ruby (~> 1.0)

README.md

+49
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,55 @@ Setting.get_field("default_locale")[:options]
161161
=> { option_values: %w[en zh-CN jp], help_text: "Bla bla ..." }
162162
```
163163

164+
### Custom type for setting
165+
166+
> Since: 2.9.0
167+
168+
You can write your custom field type by under `RailsSettings::Fields` module.
169+
170+
#### For example
171+
172+
```rb
173+
module RailsSettings
174+
module Fields
175+
class YesNo < ::RailsSettings::Fields::Base
176+
def serialize(value)
177+
case value
178+
when true then "YES"
179+
when false then "NO"
180+
else raise StandardError, 'invalid value'
181+
end
182+
end
183+
184+
def deserialize(value)
185+
case value
186+
when "YES" then true
187+
when "NO" then false
188+
else nil
189+
end
190+
end
191+
end
192+
end
193+
end
194+
```
195+
196+
Now you can use `yes_no` type in you setting:
197+
198+
```rb
199+
class Setting
200+
field :custom_item, type: :yes_no, default: 'YES'
201+
end
202+
```
203+
204+
```rb
205+
irb> Setting.custom_item = 'YES'
206+
irb> Setting.custom_item
207+
true
208+
irb> Setting.custom_item = 'NO'
209+
irb> Setting.custom_item
210+
false
211+
```
212+
164213
#### Get All defined fields
165214

166215
> version 2.7.0+

lib/rails-settings/base.rb

+3-4
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,9 @@ def _define_field(key, default: nil, type: :string, readonly: false, separator:
9696
@defined_fields ||= []
9797
@defined_fields << field
9898

99-
if readonly
100-
define_singleton_method(key) { field.read }
101-
else
102-
define_singleton_method(key) { field.read }
99+
define_singleton_method(key) { field.read }
100+
101+
unless readonly
103102
define_singleton_method("#{key}=") { |value| field.save!(value: value) }
104103

105104
if validates

lib/rails-settings/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module RailsSettings
44
class << self
55
def version
6-
"2.8.3"
6+
"2.9.0"
77
end
88
end
99
end

0 commit comments

Comments
 (0)