Module: Avo::ApplicationHelper

Instance Method Summary collapse

Methods included from SummaryChartHelper

#summary_chart_params_for

Methods included from ResourcesHelper

#field_wrapper, #filter_wrapper, #index_field_wrapper, #item_selector_data_attributes, #record_path, #record_title, #resource_for_record, #resource_grid, #resource_show_path, #resource_table

Instance Method Details

#a_button(**args, &block) ⇒ Object



89
90
91
92
93
# File 'app/helpers/avo/application_helper.rb', line 89

def a_button(**args, &block)
  render Avo::ButtonComponent.new(is_link: false, **args) do
    capture(&block) if block.present?
  end
end


95
96
97
98
99
# File 'app/helpers/avo/application_helper.rb', line 95

def a_link(path = nil, **args, &block)
  render Avo::ButtonComponent.new(path, is_link: true, **args) do
    capture(&block) if block.present?
  end
end

#body_classesObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'app/helpers/avo/application_helper.rb', line 195

def body_classes
  os_class = request.user_agent.to_s.include?("Mac") ? "os-mac" : "os-pc"

  view_class = if controller.class.superclass.to_s == "Avo::ResourcesController"
    case action_name.to_sym
    when :index
      "resource-index-view"
    when :show
      "resource-show-view"
    when :edit, :update
      "resource-edit-view"
    when :new, :create
      "resource-new-view"
    end
  end

  [os_class, view_class, *Array.wrap(Avo.configuration.body_classes)]
end

#button_classes(extra_classes = nil, color: nil, variant: nil, size: :md, active: false) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/helpers/avo/application_helper.rb', line 101

def button_classes(extra_classes = nil, color: nil, variant: nil, size: :md, active: false)
  classes = "inline-flex grow-0 items-center text-sm font-semibold leading-6 fill-current whitespace-nowrap transition duration-100 rounded-sm transform transition duration-100 active:translate-x-px active:translate-y-px cursor-pointer disabled:cursor-not-allowed #{extra_classes}"

  if color.present?
    if variant.present? && (variant.to_sym == :outlined)
      classes += " bg-white border"

      classes += " hover:border-#{color}-700 border-#{color}-500 text-#{color}-600 hover:text-#{color}-700 disabled:border-gray-300 disabled:text-gray-600"
    else
      classes += " text-white bg-#{color}-500 hover:bg-#{color}-600 disabled:bg-#{color}-300"
    end
  else
    classes += " text-gray-700 bg-white hover:bg-gray-100 disabled:bg-gray-300"
  end

  size = size.present? ? size.to_sym : :md
  classes += case size
  when :xs
    " p-2 py-1"
  when :sm
    " py-1 px-4"
  when :md
    " py-2 px-4"
  when :xl
    " py-3 px-4"
  else
    " p-4"
  end

  classes
end

#chart_color(index) ⇒ Object



186
187
188
189
# File 'app/helpers/avo/application_helper.rb', line 186

def chart_color(index)
  colors = Avo.configuration.appearance.chart_colors
  colors[index % colors.length]
end

#container_classesObject



224
225
226
227
228
229
# File 'app/helpers/avo/application_helper.rb', line 224

def container_classes
  return "container-#{container_width_css_suffix(@container_size.to_sym)}" if @container_size.present?

  width = Avo.configuration.container_width.fetch(@view&.to_sym, :large)
  "container-#{container_width_css_suffix(width)}"
end

#d(value) ⇒ Object

decrypt & decode params



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

def d(value)
  decoded = Base64.urlsafe_decode64(value.to_s)
  Avo::Services::EncryptionService.decrypt(message: decoded, purpose: :return_to, serializer: Marshal)
rescue
  nil
end

#decode_filter_params(encoded_params) ⇒ Object



159
160
161
# File 'app/helpers/avo/application_helper.rb', line 159

def decode_filter_params(encoded_params)
  Avo::Filters::BaseFilter.decode_filters(encoded_params)
end

#e(value) ⇒ Object

encode & encrypt params



232
233
234
235
# File 'app/helpers/avo/application_helper.rb', line 232

def e(value)
  encrypted = Avo::Services::EncryptionService.encrypt(message: value, purpose: :return_to, serializer: Marshal)
  Base64.urlsafe_encode64(encrypted, padding: false)
end

#editor_file_path(path) ⇒ Object



253
254
255
# File 'app/helpers/avo/application_helper.rb', line 253

def editor_file_path(path)
  editor_url(Object.const_source_location(path.class.to_s)&.first)
end

#editor_url(path) ⇒ Object



257
258
259
# File 'app/helpers/avo/application_helper.rb', line 257

def editor_url(path)
  Avo.configuration.default_editor_url.gsub("%{path}", path)
end

#empty_state(**args) ⇒ Object



85
86
87
# File 'app/helpers/avo/application_helper.rb', line 85

def empty_state(**args)
  render Avo::EmptyStateComponent.new(**args)
end

#encode_filter_params(filter_params) ⇒ Object



163
164
165
# File 'app/helpers/avo/application_helper.rb', line 163

def encode_filter_params(filter_params)
  Avo::Filters::BaseFilter.encode_filters(filter_params)
end

#frame_id(resource) ⇒ Object



182
183
184
# File 'app/helpers/avo/application_helper.rb', line 182

def frame_id(resource)
  ["frame", resource.model_name.singular, resource.record_param].compact.join("-")
end

#get_model_class(model) ⇒ Object



141
142
143
144
145
146
147
# File 'app/helpers/avo/application_helper.rb', line 141

def get_model_class(model)
  if model.instance_of?(Class)
    model
  else
    model.class
  end
end

#input_classes(extra_classes = "", has_error: false, size: :md) ⇒ Object



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

def input_classes(extra_classes = "", has_error: false, size: :md)
  classes = ""
  classes += "input--size-#{size}" if [:sm, :md, :lg].include?(size)
  classes += " input-field--error" if has_error
  classes += " #{extra_classes}"
  classes.strip
end

The cookie name that remembers an opened manual frame. Derived from the frame’s deferred URL (which already encodes resource + record + frame), so the memory is scoped per record + association/tab. Hashed to keep the name short and cookie-safe. The ‘manual-frame` Stimulus controller writes this same name client-side on a successful load (see manual_frame_controller.js).



13
14
15
# File 'app/helpers/avo/application_helper.rb', line 13

def manual_frame_cookie_name(url)
  "amf_#{Digest::MD5.hexdigest(url.to_s)}"
end

#manual_frame_remembered?(url, auto_load_for) ⇒ Boolean

Whether this manual frame was opened recently enough to skip the placeholder and render a real auto-loading ‘<turbo-frame src>` (so the Load button never appears). Presence-based: the cookie carries `max-age`, so the browser drops it when the window lapses — a present cookie means “still remembered”.

Sliding: every render that finds it remembered refreshes the cookie, so the window keeps moving forward as long as the user keeps coming back.

Returns false (and touches nothing) for plain ‘loading: :manual` frames, which carry no `auto_load_for` window.

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
# File 'app/helpers/avo/application_helper.rb', line 27

def manual_frame_remembered?(url, auto_load_for)
  return false if auto_load_for.blank?

  name = manual_frame_cookie_name(url)
  return false if request.cookies[name].blank?

  cookies[name] = {value: "1", path: "/", max_age: auto_load_for.to_i, same_site: :lax}
  true
end

#mount_pathObject



155
156
157
# File 'app/helpers/avo/application_helper.rb', line 155

def mount_path
  Avo::Engine.routes.find_script_name(params.permit!.to_h.symbolize_keys)
end

#number_to_social(number, start_at: 10_000) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'app/helpers/avo/application_helper.rb', line 167

def number_to_social(number, start_at: 10_000)
  return number_with_delimiter(number) if number < start_at

  number_to_human(number,
    precision: 1,
    significant: false,
    round_mode: :down,
    format: "%n%u",
    units: {
      thousand: "K",
      million: "M",
      billion: "B"
    })
end

#possibly_rails_authentication?Boolean

Returns:

  • (Boolean)


191
192
193
# File 'app/helpers/avo/application_helper.rb', line 191

def possibly_rails_authentication?
  defined?(Authentication) && Authentication.private_instance_methods.include?(:require_authentication) && Authentication.private_instance_methods.include?(:authenticated?)
end

#render_header_menu_itemsObject



261
262
263
264
265
266
267
268
269
270
271
# File 'app/helpers/avo/application_helper.rb', line 261

def render_header_menu_items
  items = Avo.configuration.header_menu_items
  return link_to(Avo.configuration.app_name, "/", target: :_blank) if items.blank?

  safe_join(items.map { |item|
    link_to(item.name, item.to_path,
      target: item.target,
      title: item.title,
      data: item.method ? {turbo_method: item.method} : nil)
  })
end

#render_license_warning(title: "", message: "", icon: "exclamation") ⇒ Object



77
78
79
80
81
82
83
# File 'app/helpers/avo/application_helper.rb', line 77

def render_license_warning(title: "", message: "", icon: "exclamation")
  render partial: "avo/sidebar/license_warning", locals: {
    title: title,
    message: message,
    icon: icon
  }
end

#root_path_without_urlObject



149
150
151
152
153
# File 'app/helpers/avo/application_helper.rb', line 149

def root_path_without_url
  "#{Avo.configuration.prefix_path}#{mount_path}"
rescue
  Avo.configuration.root_path
end

#rtl?Boolean

Check if the current locale is RTL (Right-to-Left)

Returns:

  • (Boolean)


215
216
217
# File 'app/helpers/avo/application_helper.rb', line 215

def rtl?
  Avo.configuration.rtl?
end

#safe_blob_path(blob) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'app/helpers/avo/application_helper.rb', line 51

def safe_blob_path(blob)
  if blob.filename.to_s.blank?
    main_app.rails_service_blob_path(blob.signed_id, routable_blob_filename(blob))
  else
    main_app.rails_blob_path(blob)
  end
rescue ActionController::UrlGenerationError
  nil
end

#safe_blob_representation_url(representation) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/helpers/avo/application_helper.rb', line 61

def safe_blob_representation_url(representation)
  blob = representation.blob

  if blob.filename.to_s.blank?
    main_app.rails_blob_representation_url(
      blob.signed_id,
      representation.variation.key,
      routable_blob_filename(blob)
    )
  else
    main_app.url_for(representation)
  end
rescue ActionController::UrlGenerationError
  nil
end

#safe_blob_url(blob) ⇒ Object

Active Storage URL helpers raise UrlGenerationError when a blob’s filename is blank (the route requires a :filename segment). Use a synthetic filename so attach mode and other callers still get a routable URL; return nil only when generation still fails for another reason.



41
42
43
44
45
46
47
48
49
# File 'app/helpers/avo/application_helper.rb', line 41

def safe_blob_url(blob)
  if blob.filename.to_s.blank?
    main_app.rails_service_blob_url(blob.signed_id, routable_blob_filename(blob))
  else
    main_app.url_for(blob)
  end
rescue ActionController::UrlGenerationError
  nil
end

#text_directionObject

Returns “rtl” or “ltr” based on current locale



220
221
222
# File 'app/helpers/avo/application_helper.rb', line 220

def text_direction
  Avo.configuration.text_direction
end

#uiObject



6
# File 'app/helpers/avo/application_helper.rb', line 6

def ui = Avo::UIInstance

#wrap_in_modal(content) ⇒ Object



245
246
247
248
249
250
251
# File 'app/helpers/avo/application_helper.rb', line 245

def wrap_in_modal(content)
  turbo_frame_tag Avo::MODAL_FRAME_ID do
    render(Avo::ModalComponent.new(width: :xl, body_class: "bg-application")) do |c|
      content
    end
  end
end