Module: BlockKit::Concerns::HasOptionGroups

Defined in:
lib/block_kit/concerns/has_option_groups.rb

Class Method Summary collapse

Class Method Details

.new(limit:, options_limit:) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/block_kit/concerns/has_option_groups.rb', line 6

def self.new(limit:, options_limit:)
  Module.new do
    extend ActiveSupport::Concern

    included do
      include HasOptions.new(limit: options_limit)

      attribute :option_groups, Types::Array.of(Composition::OptionGroup)
      validates :option_groups, length: {maximum: limit, message: "is too long (maximum is %{count} groups)"}, "block_kit/validators/associated": true
      fixes :option_groups, truncate: {maximum: limit, dangerous: true}, associated: true

      validate :options_or_option_groups

      dsl_method :option_groups, as: :option_group, required_fields: [:label]
    end

    def as_json(*)
      super.merge(option_groups: option_groups&.map(&:as_json)).compact
    end

    private

    def options_or_option_groups
      if (options.blank? && option_groups.blank?) || (options.present? && option_groups.present?)
        errors.add(:base, "must have either options or option groups, not both")
      end
    end
  end
end