Class: Slocks::BlocksBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/slocks/blocks_builder.rb

Direct Known Subclasses

ModalBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ BlocksBuilder

Returns a new instance of BlocksBuilder.



7
8
9
10
# File 'lib/slocks/blocks_builder.rb', line 7

def initialize(context)
  @context = context
  @blocks = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, **kwargs, &block) ⇒ Object



504
505
506
507
508
509
510
# File 'lib/slocks/blocks_builder.rb', line 504

def method_missing(method, *args, **kwargs, &block)
  if @context.respond_to?(method)
    @context.public_send(method, *args, **kwargs, &block)
  else
    super
  end
end

Instance Attribute Details

#blocks ⇒ Object (readonly)

Returns the value of attribute blocks.



5
6
7
# File 'lib/slocks/blocks_builder.rb', line 5

def blocks
  @blocks
end

Instance Method Details

#actions(elements) ⇒ Object



61
62
63
64
65
66
# File 'lib/slocks/blocks_builder.rb', line 61

def actions(elements)
  @blocks << {
    type: "actions",
    elements: elements
  }
end

#button(text, action_id, value: nil, url: nil, style: nil, confirm: nil, accessibility_label: nil) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/slocks/blocks_builder.rb', line 151

def button(text, action_id, value: nil, url: nil, style: nil, confirm: nil, accessibility_label: nil)
  {
    type: "button",
    text: { type: "plain_text", text: text },
    action_id:,
    value:,
    url:,
    style:,
    confirm:,
    accessibility_label:
  }.compact
end

#channels_select(action_id, placeholder:, initial_channel: nil, confirm: nil, focus_on_load: nil, response_url_enabled: nil) ⇒ Object



354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/slocks/blocks_builder.rb', line 354

def channels_select(action_id, placeholder:, initial_channel: nil, confirm: nil, focus_on_load: nil,
                    response_url_enabled: nil)
  {
    type: "channels_select",
    action_id:,
    placeholder: { type: "plain_text", text: placeholder },
    initial_channel:,
    confirm:,
    focus_on_load:,
    response_url_enabled:
  }.compact
end

#checkboxes(action_id, options, initial_options: nil, confirm: nil, focus_on_load: nil) ⇒ Object



164
165
166
167
168
169
170
171
172
173
# File 'lib/slocks/blocks_builder.rb', line 164

def checkboxes(action_id, options, initial_options: nil, confirm: nil, focus_on_load: nil)
  {
    type: "checkboxes",
    action_id:,
    options:,
    initial_options:,
    confirm:,
    focus_on_load:
  }.compact
end

#confirm_dialog(title:, text:, confirm:, deny:, style: nil) ⇒ Object



469
470
471
472
473
474
475
476
477
478
# File 'lib/slocks/blocks_builder.rb', line 469

def confirm_dialog(title:, text:, confirm:, deny:, style: nil)
  dialog = {
    title: { type: "plain_text", text: title },
    text: { type: "mrkdwn", text: text },
    confirm: { type: "plain_text", text: confirm },
    deny: { type: "plain_text", text: deny }
  }
  dialog[:style] = style if style
  dialog
end

#context(elements) ⇒ Object



46
47
48
49
50
51
# File 'lib/slocks/blocks_builder.rb', line 46

def context(elements)
  @blocks << {
    type: "context",
    elements: elements
  }
end

#context_actions(elements, block_id: nil) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/slocks/blocks_builder.rb', line 53

def context_actions(elements, block_id: nil)
  @blocks << {
    type: "context_actions",
    elements:,
    block_id:
  }.compact
end

#conversations_select(action_id, placeholder:, initial_conversation: nil, default_to_current_conversation: nil, filter: nil, confirm: nil, focus_on_load: nil, response_url_enabled: nil) ⇒ Object



324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/slocks/blocks_builder.rb', line 324

def conversations_select(action_id, placeholder:, initial_conversation: nil, default_to_current_conversation: nil,
                         filter: nil, confirm: nil, focus_on_load: nil, response_url_enabled: nil)
  {
    type: "conversations_select",
    action_id:,
    placeholder: { type: "plain_text", text: placeholder },
    initial_conversation:,
    default_to_current_conversation:,
    filter:,
    confirm:,
    focus_on_load:,
    response_url_enabled:
  }.compact
end

#date_picker(action_id, placeholder: nil, initial_date: nil, confirm: nil, focus_on_load: nil) ⇒ Object



175
176
177
178
179
180
181
182
183
184
# File 'lib/slocks/blocks_builder.rb', line 175

def date_picker(action_id, placeholder: nil, initial_date: nil, confirm: nil, focus_on_load: nil)
  {
    type: "datepicker",
    action_id:,
    placeholder: placeholder ? { type: "plain_text", text: placeholder } : nil,
    initial_date:,
    confirm:,
    focus_on_load:
  }.compact
end

#datetime_picker(action_id, initial_date_time: nil, confirm: nil, focus_on_load: nil) ⇒ Object



186
187
188
189
190
191
192
193
194
# File 'lib/slocks/blocks_builder.rb', line 186

def datetime_picker(action_id, initial_date_time: nil, confirm: nil, focus_on_load: nil)
  {
    type: "datetimepicker",
    action_id:,
    initial_date_time:,
    confirm:,
    focus_on_load:
  }.compact
end

#dispatch_action_config(*trigger_actions_on) ⇒ Object



480
481
482
483
484
# File 'lib/slocks/blocks_builder.rb', line 480

def dispatch_action_config(*trigger_actions_on)
  {
    trigger_actions_on: trigger_actions_on
  }
end

#divider ⇒ Object



42
43
44
# File 'lib/slocks/blocks_builder.rb', line 42

def divider
  @blocks << { type: "divider" }
end

#email_input(action_id, placeholder: nil, initial_value: nil, dispatch_action_config: nil, focus_on_load: nil) ⇒ Object



223
224
225
226
227
228
229
230
231
232
# File 'lib/slocks/blocks_builder.rb', line 223

def email_input(action_id, placeholder: nil, initial_value: nil, dispatch_action_config: nil, focus_on_load: nil)
  {
    type: "email_text_input",
    action_id:,
    placeholder: placeholder ? { type: "plain_text", text: placeholder } : nil,
    initial_value:,
    dispatch_action_config:,
    focus_on_load:
  }.compact
end

#feedback_buttons(positive_text, positive_value, negative_text, negative_value, action_id: nil, positive_accessibility_label: nil, negative_accessibility_label: nil) ⇒ Object



420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/slocks/blocks_builder.rb', line 420

def feedback_buttons(positive_text, positive_value, negative_text, negative_value, action_id: nil,
                     positive_accessibility_label: nil, negative_accessibility_label: nil)
  {
    type: "feedback_buttons",
    action_id:,
    positive_button: {
      text: { type: "plain_text", text: positive_text },
      value: positive_value,
      accessibility_label: positive_accessibility_label
    }.compact,
    negative_button: {
      text: { type: "plain_text", text: negative_text },
      value: negative_value,
      accessibility_label: negative_accessibility_label
    }.compact
  }.compact
end

#file(external_id, source: "remote", block_id: nil) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/slocks/blocks_builder.rb', line 91

def file(external_id, source: "remote", block_id: nil)
  block = {
    type: "file",
    external_id: external_id,
    source: source
  }
  block[:block_id] = block_id if block_id
  @blocks << block
end

#file_input(action_id, filetypes: nil, max_files: nil) ⇒ Object



389
390
391
392
393
394
395
396
# File 'lib/slocks/blocks_builder.rb', line 389

def file_input(action_id, filetypes: nil, max_files: nil)
  {
    type: "file_input",
    action_id:,
    filetypes:,
    max_files:
  }.compact
end

#filter(include: nil, exclude_external_shared_channels: nil, exclude_bot_users: nil) ⇒ Object



486
487
488
489
490
491
492
# File 'lib/slocks/blocks_builder.rb', line 486

def filter(include: nil, exclude_external_shared_channels: nil, exclude_bot_users: nil)
  f = {}
  f[:include] = include if include
  f[:exclude_external_shared_channels] = exclude_external_shared_channels if exclude_external_shared_channels
  f[:exclude_bot_users] = exclude_bot_users if exclude_bot_users
  f
end

#header(text) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/slocks/blocks_builder.rb', line 12

def header(text)
  @blocks << {
    type: "header",
    text: {
      type: "plain_text",
      text: text
    }
  }
end

#icon_button(icon, text, action_id, value: nil, confirm: nil, accessibility_label: nil, visible_to_user_ids: nil) ⇒ Object



438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/slocks/blocks_builder.rb', line 438

def icon_button(icon, text, action_id, value: nil, confirm: nil, accessibility_label: nil,
                visible_to_user_ids: nil)
  {
    type: "icon_button",
    icon:,
    text: { type: "plain_text", text: text },
    action_id:,
    value:,
    confirm:,
    accessibility_label:,
    visible_to_user_ids:
  }.compact
end

#image(image_url, alt_text, title: nil) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/slocks/blocks_builder.rb', line 68

def image(image_url, alt_text, title: nil)
  block = {
    type: "image",
    image_url: image_url,
    alt_text: alt_text
  }
  block[:title] = { type: "plain_text", text: title } if title
  @blocks << block
end

#image_element(image_url, alt_text) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/slocks/blocks_builder.rb', line 128

def image_element(image_url, alt_text)
  {
    type: "image",
    image_url: image_url,
    alt_text: alt_text
  }
end

#input(label, element, optional: false, hint: nil, block_id: nil, dispatch_action: nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/slocks/blocks_builder.rb', line 78

def input(label, element, optional: false, hint: nil, block_id: nil, dispatch_action: nil)
  block = {
    type: "input",
    label: { type: "plain_text", text: label },
    element: element
  }
  block[:block_id] = block_id if block_id
  block[:optional] = optional if optional
  block[:hint] = { type: "plain_text", text: hint } if hint
  block[:dispatch_action] = dispatch_action if dispatch_action
  @blocks << block
end

#mrkdwn_text(text) ⇒ Object



136
137
138
139
140
141
# File 'lib/slocks/blocks_builder.rb', line 136

def mrkdwn_text(text)
  {
    type: "mrkdwn",
    text: text
  }
end

#multi_channels_select(action_id, placeholder:, initial_channels: nil, max_selected_items: nil, confirm: nil, focus_on_load: nil) ⇒ Object



367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/slocks/blocks_builder.rb', line 367

def multi_channels_select(action_id, placeholder:, initial_channels: nil, max_selected_items: nil, confirm: nil,
                          focus_on_load: nil)
  {
    type: "multi_channels_select",
    action_id:,
    placeholder: { type: "plain_text", text: placeholder },
    initial_channels:,
    max_selected_items:,
    confirm:,
    focus_on_load:
  }.compact
end

#multi_conversations_select(action_id, placeholder:, initial_conversations: nil, max_selected_items: nil, default_to_current_conversation: nil, filter: nil, confirm: nil, focus_on_load: nil) ⇒ Object



339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/slocks/blocks_builder.rb', line 339

def multi_conversations_select(action_id, placeholder:, initial_conversations: nil, max_selected_items: nil,
                               default_to_current_conversation: nil, filter: nil, confirm: nil, focus_on_load: nil)
  {
    type: "multi_conversations_select",
    action_id:,
    placeholder: { type: "plain_text", text: placeholder },
    initial_conversations:,
    max_selected_items:,
    default_to_current_conversation:,
    filter:,
    confirm:,
    focus_on_load:
  }.compact
end

#multi_select_menu(action_id, placeholder:, options: nil, option_groups: nil, initial_options: nil, max_selected_items: nil, confirm: nil, focus_on_load: nil) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/slocks/blocks_builder.rb', line 285

def multi_select_menu(action_id, placeholder:, options: nil, option_groups: nil, initial_options: nil,
                      max_selected_items: nil, confirm: nil, focus_on_load: nil)
  {
    type: "multi_static_select",
    action_id:,
    placeholder: { type: "plain_text", text: placeholder },
    options:,
    option_groups:,
    initial_options:,
    max_selected_items:,
    confirm:,
    focus_on_load:
  }.compact
end

#multi_users_select(action_id, placeholder:, initial_users: nil, max_selected_items: nil, confirm: nil, focus_on_load: nil) ⇒ Object



311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/slocks/blocks_builder.rb', line 311

def multi_users_select(action_id, placeholder:, initial_users: nil, max_selected_items: nil, confirm: nil,
                       focus_on_load: nil)
  {
    type: "multi_users_select",
    action_id:,
    placeholder: { type: "plain_text", text: placeholder },
    initial_users:,
    max_selected_items:,
    confirm:,
    focus_on_load:
  }.compact
end

#number_input(action_id, is_decimal_allowed:, placeholder: nil, initial_value: nil, min_value: nil, max_value: nil, dispatch_action_config: nil, focus_on_load: nil) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/slocks/blocks_builder.rb', line 245

def number_input(action_id, is_decimal_allowed:, placeholder: nil, initial_value: nil, min_value: nil,
                 max_value: nil, dispatch_action_config: nil, focus_on_load: nil)
  {
    type: "number_input",
    action_id:,
    is_decimal_allowed:,
    placeholder: placeholder ? { type: "plain_text", text: placeholder } : nil,
    initial_value:,
    min_value:,
    max_value:,
    dispatch_action_config:,
    focus_on_load:
  }.compact
end

#option(text, value, description: nil, url: nil) ⇒ Object



452
453
454
455
456
457
458
459
460
# File 'lib/slocks/blocks_builder.rb', line 452

def option(text, value, description: nil, url: nil)
  opt = {
    text: { type: "plain_text", text: text },
    value: value
  }
  opt[:description] = { type: "plain_text", text: description } if description
  opt[:url] = url if url
  opt
end

#option_group(label, options) ⇒ Object



462
463
464
465
466
467
# File 'lib/slocks/blocks_builder.rb', line 462

def option_group(label, options)
  {
    label: { type: "plain_text", text: label },
    options: options
  }
end

#overflow(action_id, options, confirm: nil) ⇒ Object



380
381
382
383
384
385
386
387
# File 'lib/slocks/blocks_builder.rb', line 380

def overflow(action_id, options, confirm: nil)
  {
    type: "overflow",
    action_id:,
    options:,
    confirm:
  }.compact
end

#plain_text(text, emoji: true) ⇒ Object



143
144
145
146
147
148
149
# File 'lib/slocks/blocks_builder.rb', line 143

def plain_text(text, emoji: true)
  {
    type: "plain_text",
    text: text,
    emoji: emoji
  }
end

#plain_text_input(action_id, placeholder: nil, initial_value: nil, multiline: nil, min_length: nil, max_length: nil, dispatch_action_config: nil, focus_on_load: nil) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/slocks/blocks_builder.rb', line 208

def plain_text_input(action_id, placeholder: nil, initial_value: nil, multiline: nil, min_length: nil,
                     max_length: nil, dispatch_action_config: nil, focus_on_load: nil)
  {
    type: "plain_text_input",
    action_id:,
    placeholder: placeholder ? { type: "plain_text", text: placeholder } : nil,
    initial_value:,
    multiline:,
    min_length:,
    max_length:,
    dispatch_action_config:,
    focus_on_load:
  }.compact
end

#radio_buttons(action_id, options, initial_option: nil, confirm: nil, focus_on_load: nil) ⇒ Object



260
261
262
263
264
265
266
267
268
269
# File 'lib/slocks/blocks_builder.rb', line 260

def radio_buttons(action_id, options, initial_option: nil, confirm: nil, focus_on_load: nil)
  {
    type: "radio_buttons",
    action_id:,
    options:,
    initial_option:,
    confirm:,
    focus_on_load:
  }.compact
end

#render(partial, locals: {}) ⇒ Object



494
495
496
497
498
499
500
501
502
# File 'lib/slocks/blocks_builder.rb', line 494

def render(partial, locals: {})
  if partial.respond_to?(:to_ary)
    partial.flat_map do |item|
      render_partial_for_item(item, locals)
    end
  else
    render_partial_for_item(partial, locals)
  end
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


512
513
514
# File 'lib/slocks/blocks_builder.rb', line 512

def respond_to_missing?(method, include_private = false)
  @context.respond_to?(method, include_private) || super
end

#rich_text(&block) ⇒ Object



119
120
121
122
123
124
125
126
# File 'lib/slocks/blocks_builder.rb', line 119

def rich_text(&block)
  builder = RichTextBuilder.new
  builder.instance_eval(&block) if block_given?
  @blocks << {
    type: "rich_text",
    elements: builder.elements
  }
end

#rich_text_input(action_id, placeholder: nil, initial_value: nil, dispatch_action_config: nil, focus_on_load: nil) ⇒ Object



398
399
400
401
402
403
404
405
406
407
408
# File 'lib/slocks/blocks_builder.rb', line 398

def rich_text_input(action_id, placeholder: nil, initial_value: nil, dispatch_action_config: nil,
                    focus_on_load: nil)
  {
    type: "rich_text_input",
    action_id:,
    placeholder: placeholder ? { type: "plain_text", text: placeholder } : nil,
    initial_value:,
    dispatch_action_config:,
    focus_on_load:
  }.compact
end

#section(text = nil, accessory: nil, fields: nil, markdown: false) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/slocks/blocks_builder.rb', line 22

def section(text = nil, accessory: nil, fields: nil, markdown: false)
  block = { type: "section" }

  if text
    block[:text] = {
      type: markdown ? "mrkdwn" : "plain_text",
      text: text
    }
  end

  block[:accessory] = accessory if accessory
  block[:fields] = fields if fields

  @blocks << block
end

#select_menu(action_id, placeholder:, options: nil, option_groups: nil, initial_option: nil, confirm: nil, focus_on_load: nil) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/slocks/blocks_builder.rb', line 271

def select_menu(action_id, placeholder:, options: nil, option_groups: nil, initial_option: nil, confirm: nil,
                focus_on_load: nil)
  {
    type: "static_select",
    action_id:,
    placeholder: { type: "plain_text", text: placeholder },
    options:,
    option_groups:,
    initial_option:,
    confirm:,
    focus_on_load:
  }.compact
end

#simple_section(text) ⇒ Object



38
39
40
# File 'lib/slocks/blocks_builder.rb', line 38

def simple_section(text)
  section(text, markdown: true)
end

#time_picker(action_id, placeholder: nil, initial_time: nil, confirm: nil, focus_on_load: nil, timezone: nil) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
# File 'lib/slocks/blocks_builder.rb', line 196

def time_picker(action_id, placeholder: nil, initial_time: nil, confirm: nil, focus_on_load: nil, timezone: nil)
  {
    type: "timepicker",
    action_id:,
    placeholder: placeholder ? { type: "plain_text", text: placeholder } : nil,
    initial_time:,
    confirm:,
    focus_on_load:,
    timezone:
  }.compact
end

#url_input(action_id, placeholder: nil, initial_value: nil, dispatch_action_config: nil, focus_on_load: nil) ⇒ Object



234
235
236
237
238
239
240
241
242
243
# File 'lib/slocks/blocks_builder.rb', line 234

def url_input(action_id, placeholder: nil, initial_value: nil, dispatch_action_config: nil, focus_on_load: nil)
  {
    type: "url_text_input",
    action_id:,
    placeholder: placeholder ? { type: "plain_text", text: placeholder } : nil,
    initial_value:,
    dispatch_action_config:,
    focus_on_load:
  }.compact
end

#users_select(action_id, placeholder:, initial_user: nil, confirm: nil, focus_on_load: nil) ⇒ Object



300
301
302
303
304
305
306
307
308
309
# File 'lib/slocks/blocks_builder.rb', line 300

def users_select(action_id, placeholder:, initial_user: nil, confirm: nil, focus_on_load: nil)
  {
    type: "users_select",
    action_id:,
    placeholder: { type: "plain_text", text: placeholder },
    initial_user:,
    confirm:,
    focus_on_load:
  }.compact
end

#video(title, title_url, thumbnail_url, video_url, alt_text:, description: nil, author_name: nil, provider_name: nil, provider_icon_url: nil, block_id: nil) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/slocks/blocks_builder.rb', line 101

def video(title, title_url, thumbnail_url, video_url, alt_text:, description: nil, author_name: nil,
          provider_name: nil, provider_icon_url: nil, block_id: nil)
  block = {
    type: "video",
    title: { type: "plain_text", text: title },
    title_url:,
    thumbnail_url:,
    video_url:,
    alt_text:,
    description: description ? { type: "plain_text", text: description } : nil,
    author_name:,
    provider_name:,
    provider_icon_url:,
    block_id:
  }.compact
  @blocks << block
end

#workflow_button(text, workflow:, style: nil, accessibility_label: nil) ⇒ Object



410
411
412
413
414
415
416
417
418
# File 'lib/slocks/blocks_builder.rb', line 410

def workflow_button(text, workflow:, style: nil, accessibility_label: nil)
  {
    type: "workflow_button",
    text: { type: "plain_text", text: text },
    workflow:,
    style:,
    accessibility_label:
  }.compact
end