Module: CrudComponents::Helpers
- Defined in:
- lib/crud_components/helpers.rb
Overview
The everyday view API, included into ActionView by the engine. Every helper builds a presenter and renders a partial you can override via the host app’s view path (‘app/views/crud_components/…`).
Instance Method Summary collapse
-
#crud_actions(subject, fieldset: nil) ⇒ ActiveSupport::SafeBuffer
The action buttons for a record (row actions) or a model class (collection actions) — for manual placement when you render with ‘actions: false`.
-
#crud_association_index_path(owner, field) ⇒ String?
The index a has_many cell links to: nested under the owner, else the target’s filtered index, else nil.
-
#crud_association_label(field, record) ⇒ String
The label for an associated record in an association column: a per-column ‘label:` callable (`attribute :order, label: ->(o) { o.full_title(short: true) }`) when given, else the target’s default #crud_label.
-
#crud_collection(records, fieldset: nil, layout: :table, query: :auto, param_prefix: nil, actions: true, group_by: nil, extra_columns: nil, picker: false, picked_columns: :auto) ⇒ ActiveSupport::SafeBuffer
A set of records as a table (or any layout partial you point ‘layout:` at).
-
#crud_column_picker(subject, fieldset: nil, extra_columns: nil, picked_columns: :auto, url: nil, param_prefix: nil) ⇒ ActiveSupport::SafeBuffer
A standalone column picker — the same gear-and-checklist the table renders in its header, but placed wherever you like (e.g. above a ‘crud_record` detail view).
-
#crud_components_styles ⇒ Object
Inline the gem’s stylesheet (the column-picker float styles) as a <style> tag — drop ‘<%= crud_components_styles %>` once in your layout <head>.
-
#crud_file_icon(filename) ⇒ String
A Bootstrap-icon name (no library prefix — pair with css.icon_prefix) for a filename, by extension: config.file_icons, else config.file_fallback_icon.
-
#crud_filter(model, fieldset: nil, query: nil, param_prefix: nil, layout: :filter) ⇒ ActiveSupport::SafeBuffer
A standalone labelled filter form (modal / sidebar) — separate from the inline filter row a table renders.
-
#crud_form(record, fieldset: nil, action: nil, url: nil, method: nil, layout: :form) ⇒ ActiveSupport::SafeBuffer
A derived create/edit form.
-
#crud_label(record) ⇒ String
The display label for a record — its declared ‘label`, else a humanized guess.
-
#crud_model_icon(subject, **html_options) ⇒ ActiveSupport::SafeBuffer?
The icon markup badging a model — an ‘<i>` tag (paired with `css.icon_prefix`) for the model’s Structure#icon, or nil when the model has no icon (undeclared and unmapped, with no ‘model_fallback_icon`).
-
#crud_model_icon_name(subject) ⇒ String?
The bare icon name (no library prefix) for a model — Structure#icon for the model behind a record / class / relation, or nil.
-
#crud_record(record, fieldset: nil, actions: true, layout: :record, picked_columns: :auto, param_prefix: nil, extra_columns: nil) ⇒ ActiveSupport::SafeBuffer
One record as a definition list (or any layout partial you point ‘layout:` at).
-
#crud_record_path(record, owner: nil) ⇒ String?
The canonical path to a record (its ‘show`, resolved by RouteResolver).
Instance Method Details
#crud_actions(subject, fieldset: nil) ⇒ ActiveSupport::SafeBuffer
The action buttons for a record (row actions) or a model class (collection actions) — for manual placement when you render with ‘actions: false`.
150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/crud_components/helpers.rb', line 150 def crud_actions(subject, fieldset: nil) if subject.is_a?(ActiveRecord::Relation) raise ArgumentError, 'crud_actions takes a record (row actions) or a model class (collection ' \ "actions), not a relation — pass `#{subject.klass}`, not a scope." end model = subject.is_a?(Class) ? subject : subject.class structure = Structure.for(model) kind = subject.is_a?(Class) ? :collection : :row resolved_fieldset = structure.fieldset(fieldset || (kind == :collection ? :index : :show)) presenter = Presenters::Actions.new(view: self, subject: subject, structure: structure, actions: structure.fieldset_actions(resolved_fieldset, on: kind)) render 'crud_components/actions', actions: presenter end |
#crud_association_index_path(owner, field) ⇒ String?
The index a has_many cell links to: nested under the owner, else the target’s filtered index, else nil.
244 245 246 |
# File 'lib/crud_components/helpers.rb', line 244 def crud_association_index_path(owner, field) RouteResolver.collection_index_path(self, field.target, owner, field.name) end |
#crud_association_label(field, record) ⇒ String
The label for an associated record in an association column: a per-column ‘label:` callable (`attribute :order, label: ->(o) { o.full_title(short: true) }`) when given, else the target’s default #crud_label. Used by the association / association_list renderers so a column can re-title the associated record for its context while keeping the nil-safe link.
182 183 184 185 |
# File 'lib/crud_components/helpers.rb', line 182 def crud_association_label(field, record) callable = field.[:label] callable.respond_to?(:call) ? callable.call(record) : crud_label(record) end |
#crud_collection(records, fieldset: nil, layout: :table, query: :auto, param_prefix: nil, actions: true, group_by: nil, extra_columns: nil, picker: false, picked_columns: :auto) ⇒ ActiveSupport::SafeBuffer
A set of records as a table (or any layout partial you point ‘layout:` at).
40 41 42 43 44 45 46 47 |
# File 'lib/crud_components/helpers.rb', line 40 def crud_collection(records, fieldset: nil, layout: :table, query: :auto, param_prefix: nil, actions: true, group_by: nil, extra_columns: nil, picker: false, picked_columns: :auto) presenter = Presenters::Collection.new(view: self, records: records, fieldset: fieldset, query: query, layout: layout, param_prefix: param_prefix, actions: actions, group_by: group_by, extra_columns: extra_columns, picker: picker, picked_columns: picked_columns) render "crud_components/layouts/#{presenter.layout}", collection: presenter end |
#crud_column_picker(subject, fieldset: nil, extra_columns: nil, picked_columns: :auto, url: nil, param_prefix: nil) ⇒ ActiveSupport::SafeBuffer
A standalone column picker — the same gear-and-checklist the table renders in its header, but placed wherever you like (e.g. above a ‘crud_record` detail view). It submits `?cols[]=` to `url` (the current page by default), so a `crud_collection`/`crud_record` on the target page picks it up via `picked_columns:` or the param directly. Persist the choice with CrudComponents.selected_columns.
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/crud_components/helpers.rb', line 95 def crud_column_picker(subject, fieldset: nil, extra_columns: nil, picked_columns: :auto, url: nil, param_prefix: nil) relation = if subject.respond_to?(:klass) then subject elsif subject.is_a?(Class) then subject.all else subject.class.all end presenter = Presenters::Collection.new(view: self, records: relation, fieldset: fieldset, query: :static, extra_columns: extra_columns, picker: true, picked_columns: picked_columns, param_prefix: param_prefix, actions: false) render 'crud_components/column_picker', collection: presenter, url: (url || request.path) end |
#crud_components_styles ⇒ Object
Inline the gem’s stylesheet (the column-picker float styles) as a <style> tag — drop ‘<%= crud_components_styles %>` once in your layout <head>. This is the pipeline-agnostic way to load it: it needs no asset compilation, so it works the same under cssbundling/sass, importmap, sprockets or propshaft. Hosts whose pipeline serves engine assets can instead link the same file with `stylesheet_link_tag “crud_components”`.
254 255 256 257 |
# File 'lib/crud_components/helpers.rb', line 254 def crud_components_styles nonce = content_security_policy_nonce if respond_to?(:content_security_policy_nonce) tag.style(CrudComponents.bundled_css.html_safe, type: 'text/css', nonce: nonce) end |
#crud_file_icon(filename) ⇒ String
A Bootstrap-icon name (no library prefix — pair with css.icon_prefix) for a filename, by extension: config.file_icons, else config.file_fallback_icon. Used by the attachment renderer’s icon fallback; override that partial to customize.
224 225 226 227 228 |
# File 'lib/crud_components/helpers.rb', line 224 def crud_file_icon(filename) config = CrudComponents.config ext = File.extname(filename.to_s).delete('.').downcase config.file_icons.fetch(ext, config.file_fallback_icon) end |
#crud_filter(model, fieldset: nil, query: nil, param_prefix: nil, layout: :filter) ⇒ ActiveSupport::SafeBuffer
A standalone labelled filter form (modal / sidebar) — separate from the inline filter row a table renders.
116 117 118 119 120 |
# File 'lib/crud_components/helpers.rb', line 116 def crud_filter(model, fieldset: nil, query: nil, param_prefix: nil, layout: :filter) presenter = Presenters::Filter.new(view: self, model: model, fieldset: fieldset, query: query, param_prefix: param_prefix) render "crud_components/#{layout}", filter: presenter end |
#crud_form(record, fieldset: nil, action: nil, url: nil, method: nil, layout: :form) ⇒ ActiveSupport::SafeBuffer
A derived create/edit form. The gem renders; your controller saves using the matching permit list (CrudComponents.permitted_attributes), so the form and strong-params can’t drift.
134 135 136 137 138 |
# File 'lib/crud_components/helpers.rb', line 134 def crud_form(record, fieldset: nil, action: nil, url: nil, method: nil, layout: :form) presenter = Presenters::Form.new(view: self, record: record, fieldset: fieldset, action: action, url: url, method: method) render "crud_components/#{layout}", form: presenter end |
#crud_label(record) ⇒ String
The display label for a record — its declared ‘label`, else a humanized guess.
170 171 172 |
# File 'lib/crud_components/helpers.rb', line 170 def crud_label(record) Structure.for(record.class).label_for(record, self) end |
#crud_model_icon(subject, **html_options) ⇒ ActiveSupport::SafeBuffer?
The icon markup badging a model — an ‘<i>` tag (paired with `css.icon_prefix`) for the model’s Structure#icon, or nil when the model has no icon (undeclared and unmapped, with no ‘model_fallback_icon`). Pass a record, a model class or a relation. Extra html options merge onto the tag; `class:` adds to the icon classes.
<%= crud_model_icon(@book) %> # <i class="bi bi-book" aria-hidden="true">
<%= crud_model_icon(Publisher, class: 'me-1') %>
198 199 200 201 202 203 204 205 |
# File 'lib/crud_components/helpers.rb', line 198 def crud_model_icon(subject, **) name = crud_model_icon_name(subject) return nil unless name extra = Array(.delete(:class)) classes = ["#{CrudComponents.config.css.icon_prefix}#{name}", *extra].join(' ') tag.i('', class: classes, aria: { hidden: true }, **) end |
#crud_model_icon_name(subject) ⇒ String?
The bare icon name (no library prefix) for a model — Structure#icon for the model behind a record / class / relation, or nil. Use when you need the name rather than the ‘<i>` tag #crud_model_icon builds.
212 213 214 215 |
# File 'lib/crud_components/helpers.rb', line 212 def crud_model_icon_name(subject) model = subject.respond_to?(:klass) ? subject.klass : subject.is_a?(Class) ? subject : subject.class Structure.for(model).icon end |
#crud_record(record, fieldset: nil, actions: true, layout: :record, picked_columns: :auto, param_prefix: nil, extra_columns: nil) ⇒ ActiveSupport::SafeBuffer
One record as a definition list (or any layout partial you point ‘layout:` at). Extend by creating your own partial — e.g. `crud_components/_card` — and passing `layout: :card`.
69 70 71 72 73 74 75 |
# File 'lib/crud_components/helpers.rb', line 69 def crud_record(record, fieldset: nil, actions: true, layout: :record, picked_columns: :auto, param_prefix: nil, extra_columns: nil) presenter = Presenters::Record.new(view: self, record: record, fieldset: fieldset, actions: actions, picked_columns: picked_columns, param_prefix: param_prefix, extra_columns: extra_columns) render "crud_components/#{layout}", record_presenter: presenter end |
#crud_record_path(record, owner: nil) ⇒ String?
The canonical path to a record (its ‘show`, resolved by RouteResolver).
234 235 236 237 |
# File 'lib/crud_components/helpers.rb', line 234 def crud_record_path(record, owner: nil) found = RouteResolver.record_path(self, record, owner: owner) found&.first end |