Module: Plutonium::Helpers::DisplayHelper

Defined in:
lib/plutonium/helpers/display_helper.rb

Instance Method Summary collapse

Instance Method Details

#display_association_value(association) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/plutonium/helpers/display_helper.rb', line 48

def display_association_value(association)
  display_name = display_name_of(association)
  if registered_resources.include?(association.class)
    link_to display_name, resource_url_for(association, parent: nil),
      class: "font-medium text-primary-600 dark:text-primary-500"
  else
    display_name
  end
end

#display_attachment_value(value) ⇒ Object



93
94
95
# File 'lib/plutonium/helpers/display_helper.rb', line 93

def display_attachment_value(value, **, &)
  attachment_preview(value, **, &)
end

#display_boolean_value(value) ⇒ Object



62
63
64
# File 'lib/plutonium/helpers/display_helper.rb', line 62

def display_boolean_value(value)
  tag.input type: :checkbox, class: "form-check-input", checked: value, disabled: true
end

#display_clamped_quill(value) ⇒ Object



89
90
91
# File 'lib/plutonium/helpers/display_helper.rb', line 89

def display_clamped_quill(value)
  clamp_content quill(value)
end

#display_datetime_value(value) ⇒ Object



38
39
40
# File 'lib/plutonium/helpers/display_helper.rb', line 38

def display_datetime_value(value)
  timeago value
end

#display_field(value:, helper: nil, **options) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/plutonium/helpers/display_helper.rb', line 17

def display_field(value:, helper: nil, **options)
  return "-" unless value.present?

  stack_multiple = options.key?(:stack_multiple) ? options.delete(:stack_multiple) : helper != :display_name_of

  # clean options list
  options.select! { |k, _v| !k.starts_with? "pu_" }

  if value.respond_to?(:each) && stack_multiple
    tag.ul class: "list-unstyled m-0" do
      value.each do |val|
        rendered = display_field_value(value: val, helper:, **options)
        concat tag.li(rendered)
      end
    end
  else
    rendered = display_field_value(value:, helper:, **options)
    tag.span rendered
  end
end

#display_field_value(value:, helper: nil, title: nil) ⇒ Object



42
43
44
45
46
# File 'lib/plutonium/helpers/display_helper.rb', line 42

def display_field_value(value:, helper: nil, title: nil, **)
  title = (title != false) ? title || display_name_of(value) : nil
  rendered = helper.present? ? send(helper, value, **) : value
  tag.span rendered, title:
end

#display_name_of(obj, separator: ", ") ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/plutonium/helpers/display_helper.rb', line 70

def display_name_of(obj, separator: ", ")
  return unless obj.present?

  # If this is an array, display for each
  return obj.map { |i| display_name_of i, separator: }.join(separator) if obj.is_a? Array

  # Fallback to retrieving the value from a predefined list
  i[to_label name title].each do |method|
    name = obj.public_send(method) if obj.respond_to?(method)
    return name if name.present?
  end

  # Maybe this is a record?
  return "#{resource_name(obj.class)} ##{obj.id}" if obj.respond_to?(:id)

  # Oh well. Just convert it to a string.
  obj.to_s
end

#display_numeric_value(value) ⇒ Object



58
59
60
# File 'lib/plutonium/helpers/display_helper.rb', line 58

def display_numeric_value(value)
  number_with_delimiter value
end

#display_url_value(value) ⇒ Object



66
67
68
# File 'lib/plutonium/helpers/display_helper.rb', line 66

def display_url_value(value)
  link_to nil, value, class: "font-medium text-primary-600 dark:text-primary-500", target: :blank
end

#resource_name(resource_class, count = 1) ⇒ Object

def tooltip(text)

text = sanitize text
"title=\"#{text}\" data-controller=\"tooltip\" data-bs-title=\"#{text}\"".html_safe

end



9
10
11
# File 'lib/plutonium/helpers/display_helper.rb', line 9

def resource_name(resource_class, count = 1)
  resource_class.model_name.human.pluralize(count)
end

#resource_name_plural(resource_class) ⇒ Object



13
14
15
# File 'lib/plutonium/helpers/display_helper.rb', line 13

def resource_name_plural(resource_class)
  resource_name resource_class, 2
end