Module: WiserTrails::Renderable

Included in:
ORM::ActiveRecord::Activity
Defined in:
lib/wiser_trails/renderable.rb

Overview

Provides logic for rendering activities. Handles both i18n strings support and smart partials rendering (different templates per activity key).

Instance Method Summary collapse

Instance Method Details

#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.

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/wiser_trails/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/wiser_trails/articles/comments/_destroy.html.erb

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]

Examples:

Render a list of all activities from a view (erb)

<ul>
  <% for activity in WiserTrails::Activity.all %>
   <li><%= render_activity(activity) %></li>
  <% end %>
</ul>

Supply a layout

# in views:
#   All examples look for a layout in app/views/layouts/_activity.erb
 render_activity @activity, :layout => "activity"
 render_activity @activity, :layout => "layouts/activity"
 render_activity @activity, :layout => :activity

# app/views/layouts/_activity.erb
<p><%= a.created_at %></p>
<%= yield %>

Template for key: activity.article.create (erb)

<p>
  Article <strong><%= p[:name] %></strong>
  was written by <em><%= p["author"] %></em>
  <%= distance_of_time_in_words_to_now(a.created_at) %>
</p>

Parameters:

  • context (ActionView::Base)

Returns:

  • (nil)

    nil



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/wiser_trails/renderable.rb', line 78

def render(context, params = {})
  partial_path = nil
  if params.has_key? :display
    # if i18n has been requested, let it render and bail
    return context.render :text => self.text(params) if params[:display].to_sym == :"i18n"
    partial_path = 'wiser_trails/'+params[:display].to_s
  end

  controller = WiserTrails.get_controller
  if layout = params.delete(:layout)
    layout = layout.to_s
    layout = layout[0,8] == "layouts/" ? layout : "layouts/#{layout}"
  end

  locals = params.delete(:locals) || Hash.new

  params_indifferent = self.new_value.with_indifferent_access
  params_indifferent.merge!(params)

  context.render params.merge(:partial => (partial_path || self.template_path(self.key)),
    :layout => layout,
    :locals => locals.merge(:a => self, :activity => self,
       :controller => controller,
       :current_user => controller.respond_to?(:current_user) ? controller.current_user : nil,
       :p => params_indifferent, :params => params_indifferent))
end

#text(params = {}) ⇒ Object

Virtual attribute returning text description of the activity using the activity’s key to translate using i18n.



7
8
9
10
11
12
13
14
# File 'lib/wiser_trails/renderable.rb', line 7

def text(params = {})
  # TODO: some helper for key transformation for two supported formats
  k = key.split('.')
  k.unshift('wiser_trails') if k.first != 'wiser_trails'
  k = k.join('.')

  I18n.t(k, params || {})
end