Module: ApplicationHelper

Defined in:
app/helpers/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#ai_indexable_select_options(object) ⇒ Object



302
303
304
305
306
307
308
309
310
311
312
# File 'app/helpers/application_helper.rb', line 302

def ai_indexable_select_options(object)
  current = object.inherited_ai_indexable? ? translate("application_helper.indexable_select_options.yes") : translate("application_helper.indexable_select_options.no")
  options_for_select(
    [
      [translate("application_helper.ai_indexable_select_options.inherit", inherited: current), "inherit"],
      [translate("application_helper.ai_indexable_select_options.always_no"), "no"],
      [translate("application_helper.ai_indexable_select_options.always_yes"), "yes"]
    ],
    selected: object&.ai_indexable || "inherit"
  )
end

#card(style, title = nil, options = {}, &content) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/helpers/application_helper.rb', line 66

def card(style, title = nil, options = {}, &content)
  id = options[:id] || "card-#{SecureRandom.hex(4)}"
  card_class = ["card", "mb-4", options[:class]].join(" ")
  if options[:skip_link]
    skiplink = skip_link(options[:skip_link][:target], options[:skip_link][:text])
    card_class += " skip-link-container"
  end
  tag.div class: card_class, data: options[:data], id: id do
    safe_join([
      if title.present?
        tag.div(class: "card-header text-white bg-#{style}") do
          options[:collapse] ?
            safe_join([
              title,
              tag.span(Icon(icon: "arrows-expand", label: t("general.expand")), class: "float-end d-#{options[:collapse]}-none"),
              tag.a(
                nil,
                class: "link-unstyled stretched-link d-#{options[:collapse]}-none",
                "data-bs-toggle": "collapse",
                "data-bs-target": "##{id}-collapse",
                "aria-expanded": false,
                "aria-controls": "#{id}-collapse"
              )
            ]) :
            title
        end
      end,
      skiplink,
      tag.div(class: "card-body #{"collapse d-#{options[:collapse]}-block" if options[:collapse]}", id: "#{id}-collapse") do
        tag.div class: "card-text" do
          yield
        end
      end
    ].compact)
  end
end

#checkbox_input_row(form, attribute, options = {}) ⇒ Object



154
155
156
157
158
159
160
161
162
# File 'app/helpers/application_helper.rb', line 154

def checkbox_input_row(form, attribute, options = {})
  CheckBoxInputRow(
    form: form,
    attribute: attribute,
    label: options.delete(:label),
    help: options.delete(:help),
    options: options
  )
end

#checkmark(value) ⇒ Object



35
36
37
# File 'app/helpers/application_helper.rb', line 35

def checkmark(value)
  value ? "✅" : "❌"
end

#client_os ⇒ Object



2
3
4
# File 'app/helpers/application_helper.rb', line 2

def client_os
  UserAgentParser.parse(request&.user_agent)&.os
end

#collection_select_input_row(form, attribute, collection, value_method, text_method, options = {}) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
# File 'app/helpers/application_helper.rb', line 175

def collection_select_input_row(form, attribute, collection, value_method, text_method, options = {})
  CollectionSelectInputRow(
    form: form,
    attribute: attribute,
    collection: collection,
    value_method: value_method,
    text_method: text_method,
    label: options.delete(:label),
    help: options.delete(:help),
    options: options
  )
end

#errors_for(record, attribute) ⇒ Object



230
231
232
233
234
235
236
# File 'app/helpers/application_helper.rb', line 230

def errors_for(record, attribute)
  return if record.nil? || attribute.nil?
  return unless record.errors.include? attribute
  (:div,
    record.errors.full_messages_for(attribute).join("; "),
    class: "invalid-feedback d-block")
end

#file_input_row(form, name, options = {}) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'app/helpers/application_helper.rb', line 188

def file_input_row(form, name, options = {})
  safe_join([
    (:div) do
      form.label(name, options[:label], class: "col-form-label")
    end,
    (:div) do
      safe_join [
        (:div, class: "input-group") do
          safe_join [
            form.file_field(name, class: "form-control"),
            options[:remove] ? form.check_box(:"remove_#{name}", class: "btn-check", autocomplete: "off") : nil,
            options[:remove] ? form.label(:"remove_#{name}", Icon(icon: "trash", label: options[:remove_label]), class: "btn btn-outline-danger") : nil
          ].compact
        end,
        errors_for(form.object, name),
        (options[:help] ? (:span, class: "form-text") { options[:help] } : nil)
      ].compact
    end
  ])
end

#file_list_settings ⇒ Object



263
264
265
# File 'app/helpers/application_helper.rb', line 263

def file_list_settings
  current_user&.file_list_settings || SiteSettings::UserDefaults::FILE_LIST
end

#icon_for(klass) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/helpers/application_helper.rb', line 39

def icon_for(klass)
  case klass.name
  when "Creator"
    "person"
  when "Collection"
    "collection"
  when "Library"
    "boxes"
  when "Model"
    "box"
  when "ModelFile"
    "file"
  when "User"
    "person"
  when "Comment"
    "chat"
  end
end

#indexable_select_options(object) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
# File 'app/helpers/application_helper.rb', line 290

def indexable_select_options(object)
  current = object.inherited_indexable? ? translate("application_helper.indexable_select_options.yes") : translate("application_helper.indexable_select_options.no")
  options_for_select(
    [
      [translate("application_helper.indexable_select_options.inherit", inherited: current), "inherit"],
      [translate("application_helper.indexable_select_options.always_no"), "no"],
      [translate("application_helper.indexable_select_options.always_yes"), "yes"]
    ],
    selected: object&.indexable || "inherit"
  )
end

#landing_page_path ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/helpers/application_helper.rb', line 6

def landing_page_path
  SiteSettings.clear_cache
  preference = current_user&.landing_page || SiteSettings.default_landing_page
  case preference
  when "all_models"
    models_path
  when "all_collections"
    collections_path
  when "all_creators"
    creators_path
  when "my_models"
    models_path(owner: current_user&.to_param)
  else # also "dashboard"
    dashboard_path
  end
end

#markdownify(text) ⇒ Object



58
59
60
61
62
63
64
# File 'app/helpers/application_helper.rb', line 58

def markdownify(text)
  Kramdown::Document.new(
    sanitize(text),
    header_offset: 2,
    input: "GFM"
  ).to_html.html_safe # rubocop:disable Rails/OutputSafety
end


209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'app/helpers/application_helper.rb', line 209

def nav_link(ico, text, path, options = {})
  link_to(
    safe_join(
      [
        (:span, Icon(icon: ico, label: options[:title].presence || text), class: options[:icon_style]),
        (:span, text, class: options[:text_style])
      ],
      " "
    ),
    path,
    class: options[:style] || safe_join(["nav-link", (current_page?(path) ? "active" : "")], " "),
    method: options[:method],
    rel: options[:nofollow] ? "nofollow" : nil,
    id: options[:id],
    data: options[:data],
    aria: {
      label: options[:aria_label]
    }
  )
end

#numeric_input_row(form, attribute, options = {}) ⇒ Object



133
134
135
136
137
138
139
140
141
142
# File 'app/helpers/application_helper.rb', line 133

def numeric_input_row(form, attribute, options = {})
  NumericInputRow(
    form: form,
    attribute: attribute,
    unit: options.delete(:unit),
    label: options.delete(:label),
    help: options.delete(:help),
    options: options
  )
end

#oembed_params ⇒ Object



286
287
288
# File 'app/helpers/application_helper.rb', line 286

def oembed_params
  params.permit(:maxwidth, :maxheight)
end

#pagination_settings ⇒ Object



251
252
253
# File 'app/helpers/application_helper.rb', line 251

def pagination_settings
  current_user&.pagination_settings || SiteSettings::UserDefaults::PAGINATION
end

#password_input_row(form, attribute, options = {}) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'app/helpers/application_helper.rb', line 113

def password_input_row(form, attribute, options = {})
  PasswordInputRow(
    form: form,
    attribute: attribute,
    label: options.delete(:label),
    help: options.delete(:help),
    options: options
  )
end

#problem_settings ⇒ Object



267
268
269
# File 'app/helpers/application_helper.rb', line 267

def problem_settings
  current_user&.problem_settings || Problem::DEFAULT_SEVERITIES
end

#random_password ⇒ Object



271
272
273
# File 'app/helpers/application_helper.rb', line 271

def random_password
  (SecureRandom.base64(32) + "!0aB").chars.shuffle.join
end

#renderer_settings ⇒ Object



259
260
261
# File 'app/helpers/application_helper.rb', line 259

def renderer_settings
  current_user&.renderer_settings || SiteSettings::UserDefaults::RENDERER
end

#rich_text_input_row(form, attribute, options = {}) ⇒ Object



144
145
146
147
148
149
150
151
152
# File 'app/helpers/application_helper.rb', line 144

def rich_text_input_row(form, attribute, options = {})
  RichTextInputRow(
    form: form,
    attribute: attribute,
    label: options.delete(:label),
    help: options.delete(:help),
    options: options
  )
end

#select_input_row(form, attribute, select_options, options = {}) ⇒ Object



164
165
166
167
168
169
170
171
172
173
# File 'app/helpers/application_helper.rb', line 164

def select_input_row(form, attribute, select_options, options = {})
  SelectInputRow(
    form: form,
    attribute: attribute,
    select_options: select_options,
    label: options.delete(:label),
    help: options.delete(:help),
    options: options
  )
end

#server_indicator(object, full_address: false) ⇒ Object



275
276
277
278
279
280
281
282
283
284
# File 'app/helpers/application_helper.rb', line 275

def server_indicator(object, full_address: false)
  actor = object.respond_to?(:federails_actor) ? object.federails_actor : object
  return if !SiteSettings.federation_enabled? || actor.local?
  link_to sanitize(actor.profile_url), class: "link-primary link-underline-opacity-0 link-underline-opacity-100-hover" do
    safe_join([
      "⁂",
      sanitize(full_address ? actor.at_address : actor.server)
    ], " ")
  end
end

#site_icon ⇒ Object



31
32
33
# File 'app/helpers/application_helper.rb', line 31

def site_icon
  SiteSettings.site_icon.presence || vite_asset_path("images/roundel.svg")
end

#site_name(default: translate("application.title")) ⇒ Object



23
24
25
# File 'app/helpers/application_helper.rb', line 23

def site_name(default: translate("application.title"))
  SiteSettings.site_name.presence || default
end

#site_tagline ⇒ Object



27
28
29
# File 'app/helpers/application_helper.rb', line 27

def site_tagline
  SiteSettings.site_tagline.presence || t("application.tagline")
end


238
239
240
241
242
# File 'app/helpers/application_helper.rb', line 238

def skip_link(target, text)
   :div, class: "container-fluid skip-link text-bg-success p-2" do
    link_to text, "##{target}", class: "text-reset", tabindex: 0
  end
end

#tag_cloud_settings ⇒ Object



255
256
257
# File 'app/helpers/application_helper.rb', line 255

def tag_cloud_settings
  current_user&.tag_cloud_settings || SiteSettings::UserDefaults::TAG_CLOUD.merge(heatmap: false)
end

#text_input_row(form, attribute, options = {}) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'app/helpers/application_helper.rb', line 103

def text_input_row(form, attribute, options = {})
  TextInputRow(
    form: form,
    attribute: attribute,
    label: options.delete(:label),
    help: options.delete(:help),
    options: options
  )
end

#tour_attributes(id:, title:, description:) ⇒ Object



314
315
316
317
318
319
320
321
322
323
# File 'app/helpers/application_helper.rb', line 314

def tour_attributes(id:, title:, description:)
  return {} if current_user.nil? || current_user.first_use?
  tour_state = current_user.tour_state || User::DEFAULT_TOUR_STATE
  {
    "tour-id" => id,
    "tour-id-completed" => (tour_state.dig("completed")&.include?(id) == true).to_s,
    "tour-title" => title,
    "tour-description" => description
  }.compact
end

#translate_with_locale_wrapper(key, **options) ⇒ Object Also known as: t



244
245
246
247
248
# File 'app/helpers/application_helper.rb', line 244

def translate_with_locale_wrapper(key, **options)
  translate(key, **options) do |str, _key|
    str&.locale ? (:span, lang: str.locale) { sanitize str } : str
  end
end

#url_input_row(form, attribute, options = {}) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'app/helpers/application_helper.rb', line 123

def url_input_row(form, attribute, options = {})
  UrlInputRow(
    form: form,
    attribute: attribute,
    label: options.delete(:label),
    help: options.delete(:help),
    options: options
  )
end