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
-
#render(context, params = {}) ⇒ nil
Renders activity from views.
-
#text(params = {}) ⇒ Object
Virtual attribute returning text description of the activity using the activity’s key to translate using i18n.
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]
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 |