Skip to content

v2.7.0

Compare
Choose a tag to compare
@huacnlee huacnlee released this 08 Jul 07:30
· 62 commits to main since this release
  • Allows define field for assignment more options, and then you can get option by get_field.
  • Export Setting.defined_fields method for get all fields.
  • Add optional scope method to allows us grouping fields on if we need.

Use case:

class Setting < RailsSettings::Base
  # cache_prefix { "v1" }

  scope :application do
    field :app_name, default: "Rails Settings", validates: { presence: true, length: { in: 2..20 } }
    field :host, default: "http://example.com", readonly: true
    field :default_locale, default: "zh-CN", validates: { presence: true, inclusion: { in: %w[zh-CN en jp] } }, option_values: %w[en jp zh-CN], foo: 123
    field :admin_emails, type: :array, default: %w[[email protected]]

    # lambda default value
    field :welcome_message, type: :string, default: -> { "welcome to #{self.app_name}" }, validates: { length: { maximum: 255 } }
    # Override array separator, default: /[\n,]/ split with \n or comma.
    field :tips, type: :array, separator: /[\n]+/
  end

  scope :limits do
    field :user_limits, type: :integer, default: 20
    field :exchange_rate, type: :float, default: 0.123
    field :captcha_enable, type: :boolean, default: true
  end

  field :notification_options, type: :hash, default: {
    send_all: true,
    logging: true,
    sender_email: "[email protected]"
  }

  field :readonly_item, type: :integer, default: 100, readonly: true
end

Setting.get_field(:default_locale)[:options]
=> { option_values: %w[en zh-CN], foo: 123}

field_groups = Setting.defined_fields.select { |field|  }.group_by { |field| field[:scope] || :other }
field_groups.keys 
=> [:application, :limit, :other]