Module: Plutonium::Helpers::DisplayHelper

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

Instance Method Summary collapse

Instance Method Details

#display_association_value(association) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/plutonium/helpers/display_helper.rb', line 66

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



111
112
113
# File 'lib/plutonium/helpers/display_helper.rb', line 111

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

#display_boolean_value(value) ⇒ Object



80
81
82
# File 'lib/plutonium/helpers/display_helper.rb', line 80

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

#display_clamped_quill(value) ⇒ Object



107
108
109
# File 'lib/plutonium/helpers/display_helper.rb', line 107

def display_clamped_quill(value)
  clamp_content quill(value)
end

#display_datetime_value(value) ⇒ Object



56
57
58
# File 'lib/plutonium/helpers/display_helper.rb', line 56

def display_datetime_value(value)
  timeago value
end

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/plutonium/helpers/display_helper.rb', line 35

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



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

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



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/plutonium/helpers/display_helper.rb', line 88

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



76
77
78
# File 'lib/plutonium/helpers/display_helper.rb', line 76

def display_numeric_value(value)
  number_with_delimiter value
end

#display_url_value(value) ⇒ Object



84
85
86
# File 'lib/plutonium/helpers/display_helper.rb', line 84

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

#nestable_resource_name_plural(resource_class) ⇒ Object

Returns a human-readable name for a nested collection using the association name. Falls back to resource_name_plural if not in a nested context. Uses I18n via human_attribute_name for proper localization. e.g., “Authored Comments” for has_many :authored_comments



27
28
29
30
31
32
33
# File 'lib/plutonium/helpers/display_helper.rb', line 27

def nestable_resource_name_plural(resource_class)
  if current_parent && current_nested_association
    current_parent.class.human_attribute_name(current_nested_association).titleize
  else
    resource_name_plural(resource_class)
  end
end

#resource_label(resource_class) ⇒ Object

Returns the appropriate label for a resource (singular for singular resources, plural otherwise)



18
19
20
21
# File 'lib/plutonium/helpers/display_helper.rb', line 18

def resource_label(resource_class)
  is_singular = current_engine.routes.singular_resource_route?(resource_class.model_name.plural)
  resource_name(resource_class, is_singular ? 1 : 2)
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