Module: Avo::ApplicationHelper

Instance Method Summary collapse

Instance Method Details

#a_button(label = nil, **args, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/helpers/avo/application_helper.rb', line 29

def a_button(label = nil, **args, &block)
  args[:class] = button_classes(args[:class], color: args[:color], variant: args[:variant], size: args[:size])
  if args[:spinner]
    args[:"data-controller"] = "loading-button"
  end

  locals = {
    label: label,
    args: args
  }

  if block
    render layout: "avo/partials/a_button", locals: locals do
      capture(&block)
    end
  else
    render partial: "avo/partials/a_button", locals: locals
  end
end


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/helpers/avo/application_helper.rb', line 49

def a_link(label, url = nil, **args, &block)
  args[:class] = button_classes(args[:class], color: args[:color], variant: args[:variant], size: args[:size])

  if block
    url = label
  end

  locals = {
    label: label,
    url: url,
    args: args
  }

  if block
    render layout: "avo/partials/a_link", locals: locals do
      capture(&block)
    end
  else
    render partial: "avo/partials/a_link", locals: locals
  end
end

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



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
# File 'app/helpers/avo/application_helper.rb', line 71

def button_classes(extra_classes = nil, color: nil, variant: nil, size: :md)
  classes = "inline-flex flex-grow-0 items-center text-sm font-bold leading-none fill-current whitespace-nowrap transition duration-100 rounded-lg shadow-xl 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}-600 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
    " p-3"
  when :md
    " p-4"
  else
    " p-4"
  end

  classes
end

#empty_state(resource_name) ⇒ Object



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

def empty_state(resource_name)
  render partial: "avo/partials/empty_state", locals: {resource_name: resource_name}
end

#get_model_class(model) ⇒ Object



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

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

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



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/helpers/avo/application_helper.rb', line 136

def input_classes(extra_classes = "", has_error: false)
  classes = "appearance-none inline-flex bg-slate-100 disabled:bg-slate-300 disabled:cursor-not-allowed focus:bg-white text-slate-700 disabled:text-slate-700 rounded-md py-2 px-3 leading-tight border outline-none outline"

  classes += if has_error
    " border-red-600"
  else
    " border-slate-300"
  end

  classes += " #{extra_classes}"

  classes
end

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



11
12
13
14
15
16
17
# File 'app/helpers/avo/application_helper.rb', line 11

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

#render_license_warningsObject



5
6
7
8
9
# File 'app/helpers/avo/application_helper.rb', line 5

def render_license_warnings
  render partial: "avo/sidebar/license_warnings", locals: {
    license: Avo::App.license.properties
  }
end

#svg(file_name, **args) ⇒ 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
132
133
134
# File 'app/helpers/avo/application_helper.rb', line 101

def svg(file_name, **args)
  options = {}
  options[:class] = args[:class].present? ? args[:class] : "h-4 mr-1"
  options[:class] += args[:extra_class].present? ? " #{args[:extra_class]}" : ""

  # Create the path to the svgs directory
  file_path = "#{Avo::Engine.root}/app/assets/svgs/#{file_name}"
  file_path = "#{file_path}.svg" unless file_path.end_with? ".svg"

  # Create a cache hash
  hash = Digest::MD5.hexdigest "#{file_path.underscore}_#{options}"

  svg_content = Avo::App.cache_store.fetch "svg_file_#{hash}", expires_in: 1.year, cache_nils: false do
    if File.exist?(file_path)
      file = File.read(file_path)

      # parse svg
      doc = Nokogiri::HTML::DocumentFragment.parse file
      svg = doc.at_css "svg"

      # attach options
      options.each do |attr, value|
        svg[attr.to_s] = value
      end

      # cast to html
      doc.to_html.html_safe
    end
  end

  return "(not found)" if svg_content.to_s.blank?

  svg_content
end

#turbo_frame_wrap(name, &block) ⇒ Object



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

def turbo_frame_wrap(name, &block)
  render layout: "avo/partials/turbo_frame_wrap", locals: {name: name} do
    capture(&block)
  end
end