Module: Compony::ViewHelpers

Included in:
ControllerMixin
Defined in:
lib/compony/view_helpers.rb

Overview

Methods in this module are available in content blocks and Rails views. Rule of thumb: this holds methods that require a view context and results are rendered immediately.

Instance Method Summary collapse

Instance Method Details

#compony_actionsObject

Use this in your application layout to render all actions of the current root component.



8
9
10
11
# File 'lib/compony/view_helpers.rb', line 8

def compony_actions
  return nil unless Compony.root_comp
  Compony.root_comp.render_actions(self, wrapper_class: 'root-actions', action_class: 'root-action')
end

#compony_buttonObject

Given a component and a family/model, this instanciates and renders a button component.

See Also:



40
41
42
# File 'lib/compony/view_helpers.rb', line 40

def compony_button(...)
  Compony.button(...).render(helpers.controller)
end

Renders a link to a component given a comp and model or family. If authentication is configured and the current user has insufficient permissions to access the target object, the link is not displayed.

Parameters:

  • comp_name_or_cst (String, Symbol)

    The component that should be loaded, for instance ShowForAll, 'ShowForAll' or :show_for_all

  • model_or_family_name_or_cst (String, Symbol, ApplicationRecord) (defaults to: nil)

    Either the family that contains the requested component, or an instance implementing model_name from which the family name is auto-generated. Examples: Users, 'Users', :users, User.first

  • link_args (Array)

    Positional arguments that will be passed to the Rails link_to helper

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

    Options hash that will be passed to the label method (see ComponentMixins::Default::Labelling#label)

  • link_kwargs (Hash)

    Named arguments that will be passed to the Rails link_to helper



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/compony/view_helpers.rb', line 22

def compony_link(comp_name_or_cst_or_class, model_or_family_name_or_cst = nil, *link_args, label_opts: {}, params: {}, standalone_name: nil, **link_kwargs)
  model = model_or_family_name_or_cst.respond_to?(:model_name) ? model_or_family_name_or_cst : nil
  if comp_name_or_cst_or_class.is_a?(Class) && (comp_name_or_cst_or_class <= Compony::Component)
    target_comp_instance = comp_name_or_cst_or_class.new(data: model)
  else
    target_comp_instance = Compony.comp_class_for!(comp_name_or_cst_or_class, model_or_family_name_or_cst).new(data: model)
  end
  return unless target_comp_instance.standalone_access_permitted_for?(self, standalone_name:)
  return helpers.link_to(
    target_comp_instance.label(model, **label_opts),
    Compony.path(target_comp_instance.comp_name, target_comp_instance.family_name, model, standalone_name:, **params),
    *link_args, **link_kwargs
  )
end