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:



32
33
34
# File 'lib/compony/view_helpers.rb', line 32

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)

    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
# File 'lib/compony/view_helpers.rb', line 22

def compony_link(comp_name_or_cst, model_or_family_name_or_cst, *link_args, label_opts: {}, **link_kwargs)
  model = model_or_family_name_or_cst.respond_to?(:model_name) ? model_or_family_name_or_cst : nil
  comp = Compony.comp_class_for!(comp_name_or_cst, model_or_family_name_or_cst).new(data: model)
  return unless comp.standalone_access_permitted_for?(self)
  return helpers.link_to(comp.label(model, **label_opts), Compony.path(comp.comp_name, comp.family_name, model), *link_args, **link_kwargs)
end