Method: PublicActivity::Renderable#render
- Defined in:
- lib/public_activity/renderable.rb
permalink #render(context, params = {}) ⇒ nil
Renders activity from views.
Renders activity to the given ActionView context with included AV::Helpers::RenderingHelper (most commonly just ActionView::Base)
The preferred way of rendering activities is to provide a template specifying how the rendering should be happening. However, one may choose using I18n based approach when developing an application that supports plenty of languages.
If partial view exists that matches the key attribute renders that partial with local variables set to contain both Activity and activity_parameters (hash with indifferent access)
Otherwise, it outputs the I18n translation to the context
Layouts
You can supply a layout that will be used for activity partials with :layout param. Keep in mind that layouts for partials are also partials.
Custom Layout Location
You can customize the layout directory by supplying :layout_root or by using an absolute path.
Creating a template
To use templates for formatting how the activity should render, create a template based on activity key, for example:
Given a key activity.article.create, create directory tree app/views/public_activity/article/ and create the create partial there
Note that if a key consists of more than three parts splitted by commas, your directory structure will have to be deeper, for example:
activity.article.comments.destroy => app/views/public_activity/articles/comments/_destroy.html.erb
Custom Directory
You can override the default ‘public_directory` template root with the :root parameter
Variables in templates
From within a template there are two variables at your disposal:
-
activity (aliased as a for a shortcut)
-
params (aliased as p) [converted into a HashWithIndifferentAccess]
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/public_activity/renderable.rb', line 99 def render(context, params = {}) partial_root = params.delete(:root) || 'public_activity' partial_path = nil layout_root = params.delete(:layout_root) || 'layouts' if params.has_key? :display if params[:display].to_sym == :"i18n" text = self.text(params) return context.render :text => text, :plain => text else partial_path = File.join(partial_root, params[:display].to_s) end end context.render( params.merge({ :partial => prepare_partial(partial_root, partial_path), :layout => prepare_layout(layout_root, params.delete(:layout)), :locals => prepare_locals(params) }) ) end |