Skip to content

Commit

Permalink
Fix #237 to allows setting to use nil value.
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Jun 2, 2023
1 parent 1375ab7 commit d0a51d9
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 41 deletions.
2 changes: 2 additions & 0 deletions lib/rails-settings/fields/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module RailsSettings
module Fields
class Array < ::RailsSettings::Fields::Base
def deserialize(value)
return nil if value.nil?

return value unless value.is_a?(::String)

value.split(separator).reject(&:empty?).map(&:strip)
Expand Down
2 changes: 2 additions & 0 deletions lib/rails-settings/fields/big_decimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module RailsSettings
module Fields
class BigDecimal < ::RailsSettings::Fields::Base
def deserialize(value)
return nil if value.nil?

value.to_d
end

Expand Down
2 changes: 2 additions & 0 deletions lib/rails-settings/fields/boolean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module RailsSettings
module Fields
class Boolean < ::RailsSettings::Fields::Base
def deserialize(value)
return nil if value.nil?

["true", "1", 1, true].include?(value)
end

Expand Down
2 changes: 2 additions & 0 deletions lib/rails-settings/fields/float.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module RailsSettings
module Fields
class Float < ::RailsSettings::Fields::Base
def deserialize(value)
return nil if value.nil?

value.to_f
end

Expand Down
2 changes: 2 additions & 0 deletions lib/rails-settings/fields/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module RailsSettings
module Fields
class Hash < ::RailsSettings::Fields::Base
def deserialize(value)
return nil if value.nil?

return value unless value.is_a?(::String)

load_value(value).deep_stringify_keys.with_indifferent_access
Expand Down
2 changes: 2 additions & 0 deletions lib/rails-settings/fields/integer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module RailsSettings
module Fields
class Integer < ::RailsSettings::Fields::Base
def deserialize(value)
return nil if value.nil?

value.to_i
end

Expand Down
2 changes: 2 additions & 0 deletions lib/rails-settings/fields/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module RailsSettings
module Fields
class String < ::RailsSettings::Fields::Base
def deserialize(value)
return nil if value.nil?

value
end

Expand Down
116 changes: 78 additions & 38 deletions test/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ def assert_record_value(var, val)
assert_equal val, record.value
end

def assert_record_value_nil(var)
record = find_value(var)
assert_not_nil record
assert_equal nil.to_yaml, record[:value]
assert_nil record.value
end

test "define setting with protected keys" do
assert_raise(RailsSettings::ProtectedKeyError, "Can't use var as setting key.") do
class NewSetting < RailsSettings::Base
Expand All @@ -51,13 +58,13 @@ class NewSetting < RailsSettings::Base
end

test "setting_keys" do
assert_equal 17, Setting.keys.size
assert_equal 22, Setting.keys.size
assert_includes(Setting.keys, "host")
assert_includes(Setting.keys, "readonly_item")
assert_includes(Setting.keys, "default_tags")
assert_includes(Setting.keys, "omniauth_google_options")

assert_equal 14, Setting.editable_keys.size
assert_equal 19, Setting.editable_keys.size
assert_includes(Setting.editable_keys, "host")
assert_includes(Setting.editable_keys, "default_tags")

Expand All @@ -82,9 +89,9 @@ class NewSetting < RailsSettings::Base
test "defined_fields and scope" do
scopes = Setting.defined_fields.select { |field| !field[:readonly] }.group_by { |field| field[:scope] || :none }
# assert_equal 2, groups.length
assert_equal %i[application contents mailer none], scopes.keys
assert_equal %i[application contents mailer test_types none], scopes.keys
assert_equal 4, scopes[:application].length
assert_equal 6, scopes[:contents].length
assert_equal 3, scopes[:contents].length
assert_equal 2, scopes[:mailer].length
end

Expand Down Expand Up @@ -126,32 +133,36 @@ class NewSetting < RailsSettings::Base
end

test "integer field" do
assert_equal 1, Setting.user_limits
assert_instance_of Integer, Setting.user_limits
assert_no_record :user_limits
assert_nil Setting.integer_item

Setting.user_limits = 12
assert_equal 12, Setting.user_limits
assert_instance_of Integer, Setting.user_limits
assert_record_value :user_limits, 12
Setting.integer_item = 12
assert_equal 12, Setting.integer_item
assert_instance_of Integer, Setting.integer_item
assert_record_value :integer_item, 12

Setting.user_limits = "27"
assert_equal 27, Setting.user_limits
assert_instance_of Integer, Setting.user_limits
assert_record_value :user_limits, 27
Setting.integer_item = "27"
assert_equal 27, Setting.integer_item
assert_instance_of Integer, Setting.integer_item
assert_record_value :integer_item, 27

Setting.user_limits = 2.7
assert_equal 2, Setting.user_limits
assert_instance_of Integer, Setting.user_limits
assert_record_value :user_limits, 2
Setting.integer_item = 2.7
assert_equal 2, Setting.integer_item
assert_instance_of Integer, Setting.integer_item
assert_record_value :integer_item, 2

Setting.integer_item = nil
assert_nil Setting.integer_item
assert_record_value_nil :integer_item

assert_equal 2, Setting.default_value_with_block
Setting.default_value_with_block = 100
assert_equal 100, Setting.default_value_with_block
end

test "float field" do
assert_equal 7, Setting.float_item
default_val = 7

assert_equal default_val, Setting.float_item
assert_instance_of Float, Setting.float_item
assert_no_record :float_item

Expand All @@ -169,10 +180,16 @@ class NewSetting < RailsSettings::Base
assert_equal 2.9, Setting.float_item
assert_instance_of Float, Setting.float_item
assert_record_value :float_item, "2.9".to_f

Setting.float_item = nil
assert_record_value_nil :float_item
assert_equal default_val, Setting.float_item
end

test "big decimal field" do
assert_equal 9, Setting.big_decimal_item
default_val = 9

assert_equal default_val, Setting.big_decimal_item
assert_instance_of BigDecimal, Setting.big_decimal_item
assert_no_record :big_decimal_item

Expand All @@ -190,35 +207,58 @@ class NewSetting < RailsSettings::Base
assert_equal 2.9, Setting.big_decimal_item
assert_instance_of BigDecimal, Setting.big_decimal_item
assert_record_value :big_decimal_item, "2.9".to_d

Setting.big_decimal_item = nil
assert_record_value_nil :big_decimal_item
assert_equal default_val, Setting.big_decimal_item
end

test "array field" do
assert_equal %w[[email protected]], Setting.admin_emails
assert_no_record :admin_emails
assert_nil Setting.array_item
assert_no_record :array_item

new_emails = %w[[email protected] [email protected]]
Setting.admin_emails = new_emails
assert_equal new_emails, Setting.admin_emails
assert_record_value :admin_emails, new_emails
Setting.array_item = new_emails
assert_equal new_emails, Setting.array_item
assert_record_value :array_item, new_emails

Setting.admin_emails = new_emails.join("\n")
assert_equal new_emails, Setting.admin_emails
assert_record_value :admin_emails, new_emails
Setting.array_item = new_emails.join("\n")
assert_equal new_emails, Setting.array_item
assert_record_value :array_item, new_emails

Setting.admin_emails = new_emails.join(",")
assert_equal new_emails, Setting.admin_emails
assert_record_value :admin_emails, new_emails
Setting.array_item = new_emails.join(",")
assert_equal new_emails, Setting.array_item
assert_record_value :array_item, new_emails

Setting.admin_emails = new_emails.join(";")
assert_equal new_emails, Setting.admin_emails
assert_record_value :admin_emails, new_emails
Setting.array_item = new_emails.join(";")
assert_equal new_emails, Setting.array_item
assert_record_value :array_item, new_emails

Setting.admin_emails = new_emails.join(" , ")
assert_equal new_emails, Setting.admin_emails
assert_record_value :admin_emails, new_emails
Setting.array_item = new_emails.join(" , ")
assert_equal new_emails, Setting.array_item
assert_record_value :array_item, new_emails

Setting.array_item = nil
assert_nil Setting.array_item
assert_record_value_nil :array_item
end

test "hash field" do
assert_nil Setting.hash_item

Setting.hash_item = { "foo" => "bar" }
assert_equal({ "foo" => "bar" }, Setting.hash_item)
assert_record_value :hash_item, { "foo" => "bar" }

Setting.hash_item = { foo: "bar" }
assert_equal({ "foo" => "bar" }, Setting.hash_item)

Setting.hash_item = nil
assert_nil Setting.hash_item
assert_record_value_nil :hash_item
end

test "hash field intergration" do
default_value = {
host: "foo.com",
username: "[email protected]",
Expand Down
14 changes: 11 additions & 3 deletions test/dummy/app/models/setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ def foo
scope :contents do
field :tips, type: :array, separator: /\n+/
field :default_tags, type: :array, separator: /[\s,]+/, default: []
field :float_item, type: :float, default: 7
field :big_decimal_item, type: :big_decimal, default: 9
field :default_value_with_block, type: :integer, default: -> { 1 + 1 }
field :custom_item, type: :custom, default: 1
end

scope :mailer do
Expand All @@ -47,6 +44,17 @@ def foo
}, type: :hash, readonly: true
end

scope :test_types do
field :string_item, type: :string
field :integer_item, type: :integer
field :float_item, type: :float, default: 7
field :big_decimal_item, type: :big_decimal, default: 9
field :boolean_item, type: :boolean
field :array_item, type: :array
field :hash_item, type: :hash
field :custom_item, type: :custom, default: 1
end

field :key_with_more_options, type: :array, validates: {presence: true}, default: %w[foo bar], foo: 1, section: :theme
field :fallback_field, type: :foo_bar, default: "hello"
end

0 comments on commit d0a51d9

Please sign in to comment.