Module: ToolbarHelper

Included in:
ScavengerHunt::ApplicationHelper
Defined in:
app/helpers/toolbar_helper.rb

Instance Method Summary collapse

Instance Method Details

#form_toolbar(form, options = {}) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'app/helpers/toolbar_helper.rb', line 2

def form_toolbar(form, options = {})
  options = combine_options(options, class: 'toolbar--footer')

  model_name = form.object.class.model_name.human.titleize
  navigate_back = toolbar_item do
    (form.object.persisted? ? link_to("View this #{model_name}", { action: :show }, class: 'button button--outline') : "".html_safe) +
      link_to("View all #{model_name.pluralize}", { action: :index }, class: 'button button--outline')
  end

  toolbar(options) do
    (
      submit_toolbar_item(form) + navigate_back + (block_given? ? yield : "")
    ).html_safe
  end
end

#show_toolbar(instance, options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/helpers/toolbar_helper.rb', line 18

def show_toolbar(instance, options = {})
  delete_title = options.delete(:delete_title) || "Delete"
  edit_or_delete = toolbar_item(tag: :div) do
    item = "".html_safe

    can_edit = options.fetch(:edit) { policy(instance).edit? }
    item << link_to('Edit', { action: :edit }, class: 'button button--outline') if can_edit

    can_delete = options.fetch(:delete) { policy(instance).destroy? }
    item << button_to(delete_title, { action: :show }, title: "#{delete_title} #{instance}", class: 'button button--outline', data: { confirm: "Are you sure you want to #{delete_title.downcase} #{instance}?" }, method: :delete) if can_delete

    item
  end

  object_name = instance.class.model_name.human.titleize
  view_all = link_to("View all #{object_name.pluralize}", { action: :index }, class: 'button button--outline')

  options = combine_options(options, { class: 'toolbar--footer', tag: :nav, title: 'Actions' })
  toolbar(options) do
    (edit_or_delete + view_all).html_safe
  end
end

#submit_toolbar_item(form) ⇒ Object



41
42
43
44
45
# File 'app/helpers/toolbar_helper.rb', line 41

def submit_toolbar_item(form)
  toolbar_item do
    form.button(:submit, class: 'toolbar-item') + link_to('Cancel', :back, class: 'button button--outline')
  end
end

#toolbar(options = {}) ⇒ Object



47
48
49
# File 'app/helpers/toolbar_helper.rb', line 47

def toolbar(options = {})
  component(defaults: { class: 'toolbar' }, options: options) { yield if block_given? }
end

#toolbar_item(options = {}) ⇒ Object



51
52
53
# File 'app/helpers/toolbar_helper.rb', line 51

def toolbar_item(options = {})
  component(defaults: { class: 'toolbar-item' }, options: options, tag: :li) { yield if block_given? }
end