Module: Avo::ApplicationHelper

Instance Method Summary collapse

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



19
20
21
22
23
# File 'app/helpers/avo/application_helper.rb', line 19

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


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

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



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/helpers/avo/application_helper.rb', line 124

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/helpers/avo/application_helper.rb', line 31

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



116
117
118
# File 'app/helpers/avo/application_helper.rb', line 116

def chart_color(index)
  Avo.configuration.branding.chart_colors[index % Avo.configuration.branding.chart_colors.length]
end

#container_classesObject



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

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



167
168
169
170
171
172
# File 'app/helpers/avo/application_helper.rb', line 167

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



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

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

#e(value) ⇒ Object

encode & encrypt params



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

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



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

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

#editor_url(path) ⇒ Object



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

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

#empty_state(**args) ⇒ Object



15
16
17
# File 'app/helpers/avo/application_helper.rb', line 15

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

#encode_filter_params(filter_params) ⇒ Object



93
94
95
# File 'app/helpers/avo/application_helper.rb', line 93

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

#frame_id(resource) ⇒ Object



112
113
114
# File 'app/helpers/avo/application_helper.rb', line 112

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

#get_model_class(model) ⇒ Object



71
72
73
74
75
76
77
# File 'app/helpers/avo/application_helper.rb', line 71

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



63
64
65
66
67
68
69
# File 'app/helpers/avo/application_helper.rb', line 63

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

#mount_pathObject



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

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



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/helpers/avo/application_helper.rb', line 97

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)


120
121
122
# File 'app/helpers/avo/application_helper.rb', line 120

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

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



7
8
9
10
11
12
13
# File 'app/helpers/avo/application_helper.rb', line 7

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



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

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)


144
145
146
# File 'app/helpers/avo/application_helper.rb', line 144

def rtl?
  Avo.configuration.rtl?
end

#text_directionObject

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



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

def text_direction
  Avo.configuration.text_direction
end

#uiObject



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

def ui = Avo::UIInstance

#wrap_in_modal(content) ⇒ Object



174
175
176
177
178
179
180
# File 'app/helpers/avo/application_helper.rb', line 174

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