Behave like a controller
Because it inherits from ActionController::Base
, Coupdoeil::Popover
allows the use of familiar interfaces like:
- layout selection
- rendering API (
render
,render_to_string
) - callbacks (
before_action
,around_action
…) - helpers (and helper inclusion)
Layout
Layout system is the same as for ActionController::Base
. By default, it will be the "popover"
layout, located in app/popovers/layouts/popover.html.erb
, but you can change this behavior with the layout
method or the layout:
option passed to render
.
You can create new layouts for specific use-cases. For example, one main layout and one for tooltips, or action menus. Default directory is app/popovers/layouts/
. The layout template must call yield
to render the content.
<div class="main-popover-layout">
<%= yield %>
</div>
Note that the layout HTML element should not have a uniq CSS id as it can be rendered many times at once within the same document.
You can see different layout usage in examples.
Rendering
default
class ContactPopover < ApplicationPopover
def summary
# not specified, will render app/popovers/contact_popover/summary.html.erb
end
end
render another template
class ContactPopover < ApplicationPopover
def details
end
def summary
# will render app/popovers/contact_popover/details.html.erb
render :details
end
end
render partial
class ContactPopover < ApplicationPopover
def summary
# will render app/popovers/contact_popover/_business_card.html.erb, without layout
render partial: "business_card"
end
end
Remember that partials have their own layouts
Any renderable object, like a ViewComponent
class ContactPopover < ApplicationPopover
def details
end
def summary
render ContactSummaryComponent.new
end
end
Callbacks
If many popovers of the same class are relying on the same params, you can use before action callbacks to set up common state.
class ContactPopover < ApplicationPopover
before_actions :set_contact
def details
end
def summary
end
private
def set_contact
@contact = params[:contact]
end
end
Helpers
All application helpers are available when rendering a popover unless config.action_controller.include_all_helpers option is set to false
.
All helper_methods
already defined on ApplicationController
at boot time are delegated to the current context controller when rendering a popover. For example, this includes current_user
helper by Devise or allowed_to?
helper by ActionPolicy. This delegation aims to ensure consistency between regular views and popovers.
Both helpers inclusions can be disabled. See include_all_helpers and delegate_helper_methods configuration options.