Module: Presenters::StandardOperationsForRecord

Extended by:
ActiveSupport::Concern
Included in:
CatalogItemPresenter, CatalogPresenter, DeliveryPresenter, ExportedCatalogItemPacketPresenter, OrderPresenter
Defined in:
app/concerns/presenters/standard_operations_for_record.rb

Overview

CRUD-link helpers for record presenters: destroy/edit buttons and links
(permission-checked, Bootstrap-styled, Turbo-aware), plus date and audit
button rendering.

Instance Method Summary collapse

Instance Method Details

#audit_buttonString?

Renders the audit-history button for the wrapped record.

Returns:

  • (String, nil)

    HTML button, or nil when the record is blank



103
104
105
106
107
# File 'app/concerns/presenters/standard_operations_for_record.rb', line 103

def audit_button
  return if r.blank?

  h.audit_button(r)
end

#created_atString, ...

Formatted created_at date of the wrapped record.

Returns:

  • (String, nil, false)

    rendered date, or nil/false when unavailable



79
80
81
82
83
84
85
86
# File 'app/concerns/presenters/standard_operations_for_record.rb', line 79

def created_at
  begin
    return unless r.created_at
  rescue StandardError
    false
  end
  h.render_date(r.created_at)
end

#destroy_record_button(record, options = {}) ⇒ String?

Renders a destroy button for a record assuming standard rails routing
practices and bootstrap styles

Parameters:

Options Hash (options):

  • class (String)

    CSS classes (default: 'btn btn-danger')

  • data (Hash)

    data attributes; :turbo_confirm defaults to a
    delete confirmation for the record

Returns:

  • (String, nil)

    HTML link, or nil when the user cannot destroy the record



23
24
25
26
27
28
29
30
31
32
33
# File 'app/concerns/presenters/standard_operations_for_record.rb', line 23

def destroy_record_button(record, options = {})
  return unless h.can?(:destroy, record)

  options.reverse_merge!({
    class: 'btn btn-danger',
    data: {
      turbo_confirm: "This will delete the #{record.class.name.humanize}, are you sure?"
    }
  })
  destroy_record_link record, options
end

Renders a 'Delete' link to the record with turbo delete method/confirm data.

Parameters:

  • record (ApplicationRecord)

    record to link to

  • options (Hash) (defaults to: {})

    html options forwarded to link_to

Options Hash (options):

  • class (String)

    CSS classes

  • data (Hash)

    data attributes; :turbo_method defaults to
    :delete, :turbo_confirm falls back to a legacy :confirm key

Returns:

  • (String)

    HTML link



43
44
45
46
47
48
49
50
# File 'app/concerns/presenters/standard_operations_for_record.rb', line 43

def destroy_record_link(record, options = {})
  options = options.dup
  data = (options[:data] || {}).dup
  data[:turbo_method] ||= :delete
  data[:turbo_confirm] ||= data.delete(:confirm) if data[:confirm]
  options[:data] = data
  h.link_to 'Delete', h.polymorphic_path(record), **options
end

#edit_record_button(record, options = {}) ⇒ String?

Renders an edit button for a record assuming standard rails routing
practices and bootstrap styles

Parameters:

Options Hash (options):

  • class (String)

    CSS classes (default: 'btn btn-outline-primary')

Returns:

  • (String, nil)

    HTML link, or nil when the user cannot update the record



59
60
61
62
63
64
# File 'app/concerns/presenters/standard_operations_for_record.rb', line 59

def edit_record_button(record, options = {})
  return unless h.can?(:update, record)

  options[:class] ||= 'btn btn-outline-primary'
  edit_record_link record, options
end

Renders an 'Edit' link to the record's edit page.

Parameters:

  • record (ApplicationRecord)

    record to link to

  • options (Hash) (defaults to: {})

    html options forwarded to link_to

Options Hash (options):

  • class (String)

    CSS classes

Returns:

  • (String)

    HTML link



72
73
74
# File 'app/concerns/presenters/standard_operations_for_record.rb', line 72

def edit_record_link(record, options = {})
  h.link_to 'Edit', h.polymorphic_path(record, action: :edit), **options
end

#updated_atString, ...

Formatted updated_at date of the wrapped record.

Returns:

  • (String, nil, false)

    rendered date, or nil/false when unavailable



91
92
93
94
95
96
97
98
# File 'app/concerns/presenters/standard_operations_for_record.rb', line 91

def updated_at
  begin
    return unless r.updated_at
  rescue StandardError
    false
  end
  h.render_date(r.updated_at)
end