Class: CrudComponents::Presenters::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/crud_components/presenters/base.rb

Overview

Presenters are the single local each partial receives — they hold the logic so the templates stay dumb markup.

Direct Known Subclasses

Actions, Collection, Filter, Form, Record

Defined Under Namespace

Classes: ViewAbility

Constant Summary collapse

GEM_VIEW_ROOT =

Where the gem’s own field partials live — used to tell a host override apart from the shipped partial (so the fast path only skips the latter).

File.expand_path('../../../app/views', __dir__).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view:) ⇒ Base

Returns a new instance of Base.



8
9
10
# File 'lib/crud_components/presenters/base.rb', line 8

def initialize(view:)
  @view = view
end

Instance Attribute Details

#viewObject (readonly)

Returns the value of attribute view.



6
7
8
# File 'lib/crud_components/presenters/base.rb', line 6

def view
  @view
end

Instance Method Details

#abilityObject

An ability object for Query (auto mode).



22
23
24
25
26
27
28
# File 'lib/crud_components/presenters/base.rb', line 22

def ability
  if view.respond_to?(:current_ability)
    view.current_ability
  elsif view.respond_to?(:can?)
    ViewAbility.new(view)
  end
end

#configObject



12
# File 'lib/crud_components/presenters/base.rb', line 12

def config = CrudComponents.config

#cssObject



13
# File 'lib/crud_components/presenters/base.rb', line 13

def css = config.css

#permission_contextObject

can?-shaped context for ‘if:` checks; the view itself when CanCanCan (or anything can?-shaped) is around.



17
18
19
# File 'lib/crud_components/presenters/base.rb', line 17

def permission_context
  @permission_context ||= view.respond_to?(:can?) ? view : PermissionContext.new(nil)
end

#render_cell(field, record, surface:, cell_context: nil) ⇒ Object

Renders one field value: the render block (in view context, record as argument), a fast inline renderer (built-in types, no host override), or the renderer partial ‘crud_components/fields/_<name>`. `cell_context` (a CellContext or nil) lets value renderers build click-to-filter links; it is nil on surfaces without a query.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/crud_components/presenters/base.rb', line 45

def render_cell(field, record, surface:, cell_context: nil)
  # The render block gets the record *and* the field's value — so a block on
  # a DynamicColumn can read its `preload:`-ed value without an `as:` partial.
  # Extra arg is harmless for one-arg blocks/procs (Proc ignores surplus args).
  return view.instance_exec(record, field.value(record), &field.render_block) if field.render_block

  renderer = field.renderer(record) || :string
  locals = { value: field.value(record), record: record, field: field,
             surface: surface, cell_context: cell_context }
  if fast_cell?(renderer)
    cells.render(renderer, **locals)
  else
    view.render("crud_components/fields/#{renderer}", **locals)
  end
end

#render_filter_control(field, query, form_id: nil, compact: false, autosubmit: false) ⇒ Object

Renders one filter control partial ‘crud_components/filters/_<control>`.



62
63
64
65
66
67
# File 'lib/crud_components/presenters/base.rb', line 62

def render_filter_control(field, query, form_id: nil, compact: false, autosubmit: false)
  view.render("crud_components/filters/#{field.filter_control}",
              field: field, query: query, form_id: form_id, compact: compact,
              autosubmit: autosubmit, param_name: query.param_name(field.name.to_s),
              css: css)
end