Module: UserNotification::Renderable

Included in:
ORM::ActiveRecord::Notification, ORM::MongoMapper::Notification, ORM::Mongoid::Notification
Defined in:
lib/user_notification/renderable.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#render(context, params = {}) ⇒ nil

Renders notification from views.

Renders notification to the given ActionView context with included AV::Helpers::RenderingHelper (most commonly just ActionView::Base)

The preferred way of rendering notifications 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 Notification and notification_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 notification partials with :layout param. Keep in mind that layouts for partials are also partials.

Creating a template

To use templates for formatting how the notification should render, create a template based on notification key, for example:

Given a key notification.article.create, create directory tree app/views/user_notification/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:

notification.article.comments.destroy => app/views/user_notification/articles/comments/_destroy.html.erb

Variables in templates

From within a template there are two variables at your disposal:

  • notification (aliased as a for a shortcut)

  • params (aliased as p) [converted into a HashWithIndifferentAccess]

Examples:

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

<ul>
  <% for notification in UserNotification::Notification.all %>
   <li><%= render_notification(notification) %></li>
  <% end %>
</ul>

Supply a layout

# in views:
#   All examples look for a layout in app/views/layouts/_notification.erb
 render_notification @notification, :layout => "notification"
 render_notification @notification, :layout => "layouts/notification"
 render_notification @notification, :layout => :notification

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

Template for key: notification.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
104
# File 'lib/user_notification/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 = 'user_notification/'+params[:display].to_s
  end

  controller = UserNotification.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.parameters.with_indifferent_access
  params_indifferent.merge!(params)

  context.render :partial => (partial_path || self.template_path(self.key)),
    :layout => layout,
    :locals => locals.merge(:a => self, :notification => 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 notification using the notification’s key to translate using i18n.



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

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

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