Module: CrudComponents::Admin::ViewHelpers

Defined in:
lib/crud_components/admin/view_helpers.rb

Overview

View helpers for the engine's own templates. Included explicitly by the engine's controllers, so they never reach the host app's views.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, **options, &block) ⇒ Object

A route helper the engine does not have falls through to the host application's routes — the admin renders the host's partials and render blocks, and those call the host's routes.



118
119
120
121
122
# File 'lib/crud_components/admin/view_helpers.rb', line 118

def method_missing(name, *args, **options, &block)
  return super unless forwardable_route_helper?(name)

  main_app.public_send(name, *args, **options, &block)
end

Instance Method Details

#admin_current_entry?(entry) ⇒ Boolean

Whether this entry is the one being looked at.

Returns:

  • (Boolean)


59
60
61
# File 'lib/crud_components/admin/view_helpers.rb', line 59

def admin_current_entry?(entry)
  params[:crud_model].to_s == entry.name
end

#admin_delete_action(entry) ⇒ Object

The delete button: a link to the confirmation page rather than a DELETE behind a browser dialog. Keeps the derived action's icon and label.



84
85
86
87
88
89
90
# File 'lib/crud_components/admin/view_helpers.rb', line 84

def admin_delete_action(entry)
  return nil unless entry.allows?(:destroy)

  CrudComponents::Action.new(:destroy, on: :row, method: :get, confirm: false) do |record|
    public_send("delete_#{entry.singular_route_key}_path", record)
  end
end

One record a delete would take, named: an attachment names its file and opens it, anything else links to its own admin page.



37
38
39
40
41
42
# File 'lib/crud_components/admin/view_helpers.rb', line 37

def admin_dependent_link(record, owner: nil)
  return admin_attachment_link(record) if admin_attachment?(record)

  path = crud_record_path(record, owner: owner)
  path ? link_to(crud_label(record), path, data: { turbo_action: 'advance' }) : crud_label(record)
end

#admin_dependents_path(owner, item) ⇒ Object

The index holding the rest of a dependent item: nested under the owner, else the target's index filtered by it, else nil.



46
47
48
49
50
# File 'lib/crud_components/admin/view_helpers.rb', line 46

def admin_dependents_path(owner, item)
  return nil unless item.model

  CrudComponents::RouteResolver.collection_index_path(self, item.model, owner, item.name)
end

#admin_destroy_selected_action(entry) ⇒ Object

The bulk action, or nil for a model with no destroy route. Like the row's delete it leads to the confirmation page, not straight to a DELETE.



73
74
75
76
77
78
79
80
# File 'lib/crud_components/admin/view_helpers.rb', line 73

def admin_destroy_selected_action(entry)
  return nil unless entry.allows?(:destroy)

  CrudComponents::Action.new(
    :destroy_selected, on: :selection, method: :get, confirm: false, icon: 'trash',
    if: :destroy, title: t('crud_components.admin.destroy_selected', default: 'Delete selected')
  ) { public_send("delete_#{entry.route_key}_path") }
end

#admin_icon(name, css_class: nil) ⇒ Object



52
53
54
55
56
# File 'lib/crud_components/admin/view_helpers.rb', line 52

def admin_icon(name, css_class: nil)
  return nil unless name

  tag.i(nil, class: ["#{CrudComponents.config.css.icon_prefix}#{name}", css_class].compact.join(' '))
end

#admin_index_path(entry) ⇒ Object

The engine's index path for a registered model. Built from the engine's own route set rather than a bare helper name, which a host helper of the same name would shadow.



9
10
11
# File 'lib/crud_components/admin/view_helpers.rb', line 9

def admin_index_path(entry)
  CrudComponents::Admin.path_for(entry.model, :index)
end

#admin_may_destroy?(model) ⇒ Boolean

Whether these could be deleted on their own. A cascade takes them either way; the confirmation page says so. No ability means no opinion.

Returns:

  • (Boolean)


15
16
17
18
19
# File 'lib/crud_components/admin/view_helpers.rb', line 15

def admin_may_destroy?(model)
  return true unless model && respond_to?(:can?)

  can?(:destroy, model)
end

#admin_selected_values(records) ⇒ Object

What the ticked rows are identified by, for the form that deletes them.



31
32
33
# File 'lib/crud_components/admin/view_helpers.rb', line 31

def admin_selected_values(records)
  records.map { |record| record.public_send(CrudComponents::Structure.for(record.class).identify_by).to_s }
end

#admin_selection_item(model, records) ⇒ Object

The ticked rows as one dependent item, so the confirmation page names them the same way it names everything else that goes.



23
24
25
26
27
28
# File 'lib/crud_components/admin/view_helpers.rb', line 23

def admin_selection_item(model, records)
  CrudComponents::Admin::Dependents::Item.new(
    name: nil, model: model, count: records.size, behavior: :destroy, cascades: false,
    records: records.first(CrudComponents::Admin::Dependents::PREVIEW)
  )
end

#admin_show_in_app_actionObject

The row action linking a record to the host app's own page for it. Resolves through crud_app_path, so it disappears when there is none.



94
95
96
97
98
99
# File 'lib/crud_components/admin/view_helpers.rb', line 94

def admin_show_in_app_action
  CrudComponents::Action.new(
    :show_in_app, on: :row, icon: 'box-arrow-up-right',
    title: t('crud_components.admin.show_in_app', default: 'Show in app')
  ) { |record| crud_app_path(record) }
end

#admin_stylesObject

The admin layout's stylesheet, inlined (CSP-nonce aware).



66
67
68
69
# File 'lib/crud_components/admin/view_helpers.rb', line 66

def admin_styles
  nonce = content_security_policy_nonce if respond_to?(:content_security_policy_nonce)
  tag.style(CrudComponents::Admin.bundled_css.html_safe, type: 'text/css', nonce: nonce)
end

#admin_titleObject



63
# File 'lib/crud_components/admin/view_helpers.rb', line 63

def admin_title = CrudComponents::Admin.config.resolved_title

#polymorphic_path(record, options = {}) ⇒ Object



109
110
111
112
113
# File 'lib/crud_components/admin/view_helpers.rb', line 109

def polymorphic_path(record, options = {})
  return main_app.polymorphic_path(record, options) if active_storage_object?(record)

  super
end

#polymorphic_url(record, options = {}) ⇒ Object

Active Storage routes live in the application's route set, not in an isolated engine's; attachment cells reach them through these two.



103
104
105
106
107
# File 'lib/crud_components/admin/view_helpers.rb', line 103

def polymorphic_url(record, options = {})
  return main_app.polymorphic_url(record, options) if active_storage_object?(record)

  super
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/crud_components/admin/view_helpers.rb', line 124

def respond_to_missing?(name, include_private = false)
  forwardable_route_helper?(name) || super
end