Module: BootstrapConcerns::ComponentsHelper

Defined in:
lib/bootstrap_concerns/components_helper.rb

Constant Summary collapse

MARGIN =
"mb-3".freeze

Instance Method Summary collapse

Instance Method Details

#assign_icon ⇒ Object



5
6
7
# File 'lib/bootstrap_concerns/components_helper.rb', line 5

def assign_icon
  icon("box-arrow-in-left", "Assign")
end

#boolean_false_icon ⇒ Object



9
10
11
# File 'lib/bootstrap_concerns/components_helper.rb', line 9

def boolean_false_icon
  icon("x-circle-fill")
end

#boolean_true_icon ⇒ Object



13
14
15
# File 'lib/bootstrap_concerns/components_helper.rb', line 13

def boolean_true_icon
  icon("check-circle-fill")
end

#bs_anchor_to(name, anchor, **html_options) ⇒ Object

A link to a section of the current page. A bare fragment resolves against the address bar, so the query string survives the click, and the link opts out of Turbo Drive, which would fetch the document again where the browser simply scrolls. Everything else is bs_link_to's: a button unless told button: false.



21
22
23
# File 'lib/bootstrap_concerns/components_helper.rb', line 21

def bs_anchor_to(name, anchor, **html_options)
  bs_link_to(name, "##{anchor}", html_options.deep_merge(data: {turbo: false}))
end

#bs_button_tag(content_or_options = nil, options = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bootstrap_concerns/components_helper.rb', line 25

def bs_button_tag(content_or_options = nil, options = nil, &)
  normalized_options =
    if content_or_options.is_a?(Hash)
      content_or_options
    else
      options ||= {}
    end

  normalized_options.merge!(Option.options_with_button_class(normalized_options))

  button_tag(content_or_options, options, &)
end

#bs_button_to(name = nil, options = nil, html_options = nil) ⇒ Object



38
39
40
# File 'lib/bootstrap_concerns/components_helper.rb', line 38

def bs_button_to(name = nil, options = nil, html_options = nil, &)
  bs_link_or_button_to(:button_to, name, options, html_options, &)
end

#bs_errors(record) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/bootstrap_concerns/components_helper.rb', line 42

def bs_errors(record)
  return if record.errors.empty?

  (:div, class: "alert alert-danger alert-dismissible fade show #{MARGIN}") do
    concat (:h5, "Errors")
    concat(
      (:ul, class: "mb-0") do
        record.errors.full_messages.each do |message|
          concat (:li, message)
        end
      end
    )
    concat (:button, nil, type: "button", class: "btn-close", data: {bs_dismiss: "alert"})
  end
end

#bs_form_with(model: false, **options) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/bootstrap_concerns/components_helper.rb', line 58

def bs_form_with(model: false, **options, &)
  form_with(model: model, **options) do |form|
    concat(bs_errors(model)) if model
    concat(
      (:div, class: "row") do
        (:div, class: "col-md-6") do
          yield(form)
        end
      end
    )
  end
end


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/bootstrap_concerns/components_helper.rb', line 71

def bs_link_or_button_to(method_name, name, options, html_options, &)
  normalized_html_options =
    if block_given?
      options ||= {}
    else
      html_options ||= {}
    end

  button = normalized_html_options.delete(:button) { true }
  normalized_html_options.merge!(Option.options_with_button_class(normalized_html_options)) if button

  public_send(
    method_name,
    name,
    options,
    html_options,
    &
  )
end


91
92
93
# File 'lib/bootstrap_concerns/components_helper.rb', line 91

def bs_link_to(name = nil, options = nil, html_options = nil, &)
  bs_link_or_button_to(:link_to, name, options, html_options, &)
end

#bs_search_col ⇒ Object



95
96
97
# File 'lib/bootstrap_concerns/components_helper.rb', line 95

def bs_search_col(&)
  (:div, class: "col-12 col-sm #{MARGIN}", &)
end

#bs_search_submit_col ⇒ Object



99
100
101
102
103
104
105
# File 'lib/bootstrap_concerns/components_helper.rb', line 99

def bs_search_submit_col
  (:div, class: "col-auto #{MARGIN}") do
    (:button, BootstrapConcerns::Option.options_with_button_class) do
      search_icon
    end
  end
end

#bs_table ⇒ Object



155
156
157
158
159
# File 'lib/bootstrap_concerns/components_helper.rb', line 155

def bs_table(&)
   :div, class: "table-responsive" do
    (:table, class: "table table-striped table-sm align-middle", &)
  end
end

#copy_icon ⇒ Object



107
108
109
# File 'lib/bootstrap_concerns/components_helper.rb', line 107

def copy_icon
  icon("files", "Copy")
end

#delete_icon(text = "Delete") ⇒ Object



111
112
113
# File 'lib/bootstrap_concerns/components_helper.rb', line 111

def delete_icon(text = "Delete")
  icon("trash", text)
end

#download_icon ⇒ Object



115
116
117
# File 'lib/bootstrap_concerns/components_helper.rb', line 115

def download_icon
  icon("download", "Download")
end

#edit_icon ⇒ Object



119
120
121
# File 'lib/bootstrap_concerns/components_helper.rb', line 119

def edit_icon
  icon("pencil-square", "Edit")
end

#export_icon(label = "Export") ⇒ Object



123
124
125
# File 'lib/bootstrap_concerns/components_helper.rb', line 123

def export_icon(label = "Export")
  icon("filetype-xlsx", label)
end

#flash_alert_color(key) ⇒ Object



127
128
129
130
131
132
133
134
135
136
# File 'lib/bootstrap_concerns/components_helper.rb', line 127

def flash_alert_color(key)
  case key.to_s
  when "notice"
    "success"
  when "alert"
    "danger"
  else
    key
  end
end

#icon(name, text = nil) ⇒ Object



138
139
140
141
# File 'lib/bootstrap_concerns/components_helper.rb', line 138

def icon(name, text = nil)
  i = ("i", nil, class: "bi bi-#{name}")
  text ? i + (:span, text, class: "ms-1 d-none d-sm-inline") : i
end

#import_icon ⇒ Object



143
144
145
# File 'lib/bootstrap_concerns/components_helper.rb', line 143

def import_icon
  icon("box-arrow-in-down", "Import")
end

#new_icon(label = "New") ⇒ Object



147
148
149
# File 'lib/bootstrap_concerns/components_helper.rb', line 147

def new_icon(label = "New")
  icon("file-earmark-plus", label)
end

#rename_icon ⇒ Object



151
152
153
# File 'lib/bootstrap_concerns/components_helper.rb', line 151

def rename_icon
  icon("input-cursor-text", "Rename")
end

#search_icon ⇒ Object



161
162
163
# File 'lib/bootstrap_concerns/components_helper.rb', line 161

def search_icon
  icon("search", "Search")
end