Module: K3Testing::PresentModel

Defined in:
lib/k3_testing/presenter.rb

Overview

PresentModel adds a helper method to assist with creating HTML with data-role attributes identifying it. This is especially useful for testing but can also help with Javascript as well.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.role_for(obj, attr = nil) ⇒ Object

Returns just the name of the role by itself as a string.



85
86
87
88
89
90
91
# File 'lib/k3_testing/presenter.rb', line 85

def self.role_for(obj, attr=nil)
  klass = obj.is_a?(Class) ? obj : obj.class
  klass = klass.name.underscore
  id    = ".#{obj.id}" if obj.respond_to?(:id)
  attr  = ".#{attr}" unless attr.blank?
  "#{klass}#{id}#{attr}"
end

Instance Method Details

#present_model(obj, *attr_and_options, &block) ⇒ Object

This method allows you to represent an ActiveRecord object as HTML and will add appropriate data-role attributes as needed.

Examples

The simplest way of calling, just represents the object and one of its attributes…

<%= present_model @user, :name %>

Will render something like the following:

<div data-role="user.42.name">Joe Blow</div>

You can also pass extra html attributes with your call by specifying :html_options as an extra parameter. If you don’t want a div element, you may specify :as as a parameter as well. For example:

<%= present_model @user, :name, :as => :span, :html_options => {:id => 'current_user'} %>

Will render something like the following:

<span data-role="user.42.name" id="current_user">Joe Blow</span>

You may also leave out the attribute and specify a block having a single parameter. That parameter may be used to render attribute elements. For example:

<%= present_model @user do |u| %>
  <%= u.attr :name %> is
  <%= u.attr :age %> years old.
<% end %>

Will render something like:

<div data-role="user.42">
  <span data-role="user.42.name">Joe Blow</span> is
  <span data-role="user.42.age">55</span> years old
</div>

Finally, the attr method can also take the options :as and :html_options, or even take a block. For example:

<%= present_model @user, :as => :tr do |u| %>
  <%= u.attr :name, :as => :td, :html_options => {:class => 'name'} %>
  <%= u.attr :age, :as => :td do %>
    <%= u.object.age %> years old
  <% end %>
<% end %>

Will render something like:

<tr data-role="user.42">
  <td data-role="user.42.name" class="name">Joe Blow</td>
  <td data-role="user.42.name">55 years old</td>
</tr>


63
64
65
66
67
68
69
70
71
# File 'lib/k3_testing/presenter.rb', line 63

def present_model(obj, *attr_and_options, &block)
  options = attr_and_options.last.is_a?(Hash) ? attr_and_options.pop : {}
  attr = attr_and_options.pop
  tag = options[:as] || :div
  presenter = Presenter.new(obj,self)
  output = block.nil? ? obj.send(attr).to_s : capture(presenter, &block)
  html_opts = (options[:html_options] || {}).merge(role_hash_for(obj, attr))
   tag, output, html_opts
end

#role_hash_for(obj, attr = nil) ⇒ Object

Returns just the data-role by itself in a hash for cases where you don’t need the full Presenter or can’t use it.

This can be used to give a data-role attribute to an element in HAML, for example:

%li{role_hash_for(record)}


79
80
81
# File 'lib/k3_testing/presenter.rb', line 79

def role_hash_for(obj, attr=nil)
  {'data-role' => PresentModel.role_for(obj, attr)}
end