Skip to content

Commit 76cf04c

Browse files
committed
Rely on ActiveModel's :default option
1 parent 6a0485a commit 76cf04c

File tree

22 files changed

+34
-79
lines changed

22 files changed

+34
-79
lines changed

lib/block_kit/blocks.rb

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
module BlockKit
66
class Blocks < Base
7-
attribute :blocks, Types::Array.of(Types::Blocks.new(*Layout.all))
7+
attribute :blocks, Types::Array.of(Types::Blocks.new(*Layout.all)), default: []
88
validates :blocks, "block_kit/validators/associated": true
99
fixes :blocks, associated: true
1010

@@ -22,14 +22,7 @@ class Blocks < Base
2222
delegate_missing_to :blocks
2323

2424
def initialize(attributes = {})
25-
attributes = case attributes
26-
when Array
27-
{blocks: attributes}
28-
when Hash
29-
attributes.with_indifferent_access.tap { |attrs| attrs[:blocks] ||= [] }
30-
else
31-
raise ArgumentError, "Expected a Hash of attributes or Array of blocks, instead got #{attributes_or_blocks.class}"
32-
end
25+
attributes = {blocks: attributes} if attributes.is_a?(Array)
3326

3427
super
3528
end

lib/block_kit/composition/dispatch_action_config.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class DispatchActionConfig < Base
1010
ON_CHARACTER_ENTERED = "on_character_entered"
1111
].freeze
1212

13-
attribute :trigger_actions_on, Types::Set.of(:string)
13+
attribute :trigger_actions_on, Types::Set.of(:string), default: []
1414
validates :trigger_actions_on, presence: true, "block_kit/validators/array_inclusion": {in: VALID_TRIGGERS}
1515
fixes :trigger_actions_on, null_value: {error_types: [:inclusion]}
1616

@@ -22,8 +22,7 @@ class DispatchActionConfig < Base
2222
end
2323

2424
define_method(:"trigger_actions_on_#{value}!") do
25-
self.trigger_actions_on ||= []
26-
self.trigger_actions_on.add(value)
25+
trigger_actions_on.add(value)
2726
end
2827
end
2928

lib/block_kit/concerns/has_rich_text_elements.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module HasRichTextElements
66
extend ActiveSupport::Concern
77

88
included do
9-
attribute :elements, Types::Array.of(Types::Blocks.new(*Layout::RichText::Elements.all))
9+
attribute :elements, Types::Array.of(Types::Blocks.new(*Layout::RichText::Elements.all)), default: []
1010
validates :elements, presence: true, "block_kit/validators/associated": true
1111
fixes :elements, associated: true
1212

@@ -16,13 +16,6 @@ module HasRichTextElements
1616
dsl_method :elements, as: :emoji, type: Layout::RichText::Elements::Emoji, required_fields: [:name], yields: false
1717
end
1818

19-
def initialize(attributes = {})
20-
attributes = attributes.with_indifferent_access
21-
attributes[:elements] ||= []
22-
23-
super
24-
end
25-
2619
def channel(channel_id:, styles: [])
2720
style = if styles.present?
2821
styles = Array(styles).map { |s| [s.to_s, true] }.to_h

lib/block_kit/elements/overflow.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Overflow < Base
99

1010
include Concerns::Confirmable
1111

12-
attribute :options, Types::Array.of(Composition::OverflowOption)
12+
attribute :options, Types::Array.of(Composition::OverflowOption), default: []
1313
validates :options, presence: true, length: {maximum: MAX_OPTIONS, message: "is too long (maximum is %{count} options)"}, "block_kit/validators/associated": true
1414
fixes :options, truncate: {maximum: MAX_OPTIONS, dangerous: true}, associated: true
1515

lib/block_kit/layout/actions.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,7 @@ class Actions < Base
2828
Elements::WorkflowButton
2929
]
3030

31-
def initialize(attributes = {})
32-
attributes = attributes.with_indifferent_access
33-
attributes[:elements] ||= []
34-
35-
super
36-
end
37-
38-
attribute :elements, Types::Array.of(Types::Blocks.new(*SUPPORTED_ELEMENTS))
31+
attribute :elements, Types::Array.of(Types::Blocks.new(*SUPPORTED_ELEMENTS)), default: []
3932
validates :elements, presence: true, length: {maximum: MAX_ELEMENTS, message: "is too long (maximum is %{count} elements)"}, "block_kit/validators/associated": true
4033
fixes :elements, truncate: {maximum: MAX_ELEMENTS, dangerous: true}, associated: true
4134

lib/block_kit/layout/context.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,13 @@ class Context < Base
1212
Composition::PlainText
1313
].freeze
1414

15-
attribute :elements, Types::Array.of(Types::Blocks.new(*SUPPORTED_ELEMENTS))
15+
attribute :elements, Types::Array.of(Types::Blocks.new(*SUPPORTED_ELEMENTS)), default: []
1616
validates :elements, presence: true, length: {maximum: MAX_ELEMENTS, message: "is too long (maximum is %{count} elements)"}, "block_kit/validators/associated": true
1717
fixes :elements, truncate: {maximum: MAX_ELEMENTS, dangerous: true}, associated: true
1818

1919
dsl_method :elements, as: :mrkdwn, type: Composition::Mrkdwn, required_fields: [:text], yields: false
2020
dsl_method :elements, as: :plain_text, type: Composition::PlainText, required_fields: [:text], yields: false
2121

22-
def initialize(attributes = {})
23-
attributes = attributes.with_indifferent_access
24-
attributes[:elements] ||= []
25-
26-
super
27-
end
28-
2922
def image(alt_text:, image_url: nil, slack_file: nil)
3023
if (image_url.nil? && slack_file.nil?) || (image_url && slack_file)
3124
raise ArgumentError, "Must provide either image_url or slack_file, but not both."

lib/block_kit/layout/rich_text.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class RichText < Base
1818
RichText::Section
1919
].freeze
2020

21-
attribute :elements, Types::Array.of(Types::Blocks.new(*SUPPORTED_ELEMENTS))
21+
attribute :elements, Types::Array.of(Types::Blocks.new(*SUPPORTED_ELEMENTS)), default: []
2222
validates :elements, presence: true, "block_kit/validators/associated": true
2323
fixes :elements, associated: true
2424

@@ -32,13 +32,6 @@ class RichText < Base
3232
alias_method :quote, :rich_text_quote
3333
alias_method :section, :rich_text_section
3434

35-
def initialize(attributes = {})
36-
attributes = attributes.with_indifferent_access
37-
attributes[:elements] ||= []
38-
39-
super
40-
end
41-
4235
def append(element)
4336
elements << element
4437

lib/block_kit/surfaces/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Base < BlockKit::Base
2020
Layout::Video
2121
]
2222

23-
attribute :blocks, Types::Array.of(Types::Blocks.new(*SUPPORTED_BLOCKS))
23+
attribute :blocks, Types::Array.of(Types::Blocks.new(*SUPPORTED_BLOCKS)), default: []
2424
attribute :private_metadata, :string
2525
attribute :callback_id, :string
2626
attribute :external_id, :string

lib/block_kit/surfaces/home.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,6 @@ class Home < Base
2828
Elements::TimePicker,
2929
Elements::UsersSelect
3030
].freeze
31-
32-
def initialize(attributes = {})
33-
attributes = attributes.with_indifferent_access
34-
attributes[:blocks] ||= []
35-
36-
super
37-
end
3831
end
3932
end
4033
end

lib/block_kit/surfaces/message.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Message < BlockKit::Base
3232
].freeze
3333

3434
attribute :text, :string
35-
attribute :blocks, Types::Array.of(Types::Blocks.new(*Layout.all))
35+
attribute :blocks, Types::Array.of(Types::Blocks.new(*Layout.all)), default: []
3636
attribute :thread_ts, :string
3737
attribute :mrkdwn, :boolean
3838

@@ -53,13 +53,6 @@ class Message < BlockKit::Base
5353
dsl_method :blocks, as: :section, type: Layout::Section
5454
dsl_method :blocks, as: :video, type: Layout::Video, required_fields: [:alt_text, :title, :thumbnail_url, :video_url], yields: false
5555

56-
def initialize(attributes = {})
57-
attributes = attributes.with_indifferent_access
58-
attributes[:blocks] ||= []
59-
60-
super
61-
end
62-
6356
def image(alt_text:, image_url: nil, slack_file: nil, title: nil, emoji: nil, block_id: nil)
6457
if (image_url.nil? && slack_file.nil?) || (image_url && slack_file)
6558
raise ArgumentError, "Must provide either image_url or slack_file, but not both."

0 commit comments

Comments
 (0)