Skip to content

Commit

Permalink
version 2.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed May 29, 2023
1 parent bbb7a67 commit 8dc786f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
5 changes: 1 addition & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
rails-settings-cached (2.8.3)
rails-settings-cached (2.9.0)
activerecord (>= 5.0.0)
railties (>= 5.0.0)

Expand Down Expand Up @@ -48,8 +48,6 @@ GEM
nokogiri (1.13.10)
mini_portile2 (~> 2.8.0)
racc (~> 1.4)
nokogiri (1.13.10-x86_64-darwin)
racc (~> 1.4)
parallel (1.22.1)
parser (3.1.3.0)
ast (~> 2.4.1)
Expand Down Expand Up @@ -89,7 +87,6 @@ GEM
ruby-progressbar (1.11.0)
sqlite3 (1.5.4)
mini_portile2 (~> 2.8.0)
sqlite3 (1.5.4-x86_64-darwin)
thor (1.2.1)
tzinfo (2.0.5)
concurrent-ruby (~> 1.0)
Expand Down
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,55 @@ Setting.get_field("default_locale")[:options]
=> { option_values: %w[en zh-CN jp], help_text: "Bla bla ..." }
```

### Custom type for setting

> Since: 2.9.0
You can write your custom field type by under `RailsSettings::Fields` module.

#### For example

```rb
module RailsSettings
module Fields
class YesNo < ::RailsSettings::Fields::Base
def serialize(value)
case value
when true then "YES"
when false then "NO"
else raise StandardError, 'invalid value'
end
end

def deserialize(value)
case value
when "YES" then true
when "NO" then false
else nil
end
end
end
end
end
```

Now you can use `yes_no` type in you setting:

```rb
class Setting
field :custom_item, type: :yes_no, default: 'YES'
end
```

```rb
irb> Setting.custom_item = 'YES'
irb> Setting.custom_item
true
irb> Setting.custom_item = 'NO'
irb> Setting.custom_item
false
```

#### Get All defined fields

> version 2.7.0+
Expand Down
7 changes: 3 additions & 4 deletions lib/rails-settings/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,9 @@ def _define_field(key, default: nil, type: :string, readonly: false, separator:
@defined_fields ||= []
@defined_fields << field

if readonly
define_singleton_method(key) { field.read }
else
define_singleton_method(key) { field.read }
define_singleton_method(key) { field.read }

unless readonly
define_singleton_method("#{key}=") { |value| field.save!(value: value) }

if validates
Expand Down
2 changes: 1 addition & 1 deletion lib/rails-settings/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module RailsSettings
class << self
def version
"2.8.3"
"2.9.0"
end
end
end

0 comments on commit 8dc786f

Please sign in to comment.