public
Last active

  • Download Gist
events_helper.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
module EventsHelper
def events_list(events)
content_tag(:ul) do
events.inject('') do |list_content, event|
list_content << event_list_item(event)
end
end
end
 
private
 
def event_list_item(event)
content_tag(:li) do
item_content = content_tag(:h3, link_to(event.name, ''))
item_content << event_details(event)
end
end
 
def event_details(event)
content_tag(:div, :class => 'content') do
details_content = event.picture? ? image_tag(event.picture.url) : ''
details_content << simple_format(event.description)
end
end
end
events_helper_test.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
require 'test_helper'
 
class EventsHelperTest < ActionView::TestCase
context 'Given events' do
setup do
3.times { Factory(:event) }
@events = Event.all
end
 
context 'when displaying the events' do
setup { get_events_list }
 
should 'show all events' do
assert_select 'ul', :count => 1 do
assert_select 'li', :count => @events.size
end
end
 
should 'link each event title' do
@events.each do |event|
assert_select "li h3 a[href=]", :count => 1, :text => event.name
end
end
 
should 'show the event details' do
assert_select 'li div.content', :count => @events.size do
@events.each do |event|
assert_select 'p', :count => 1, :text => event.description
end
end
end
 
should 'not show a picture' do
assert_select 'img', :count => 0
end
end
end
 
context 'Given an event with line breaks in the description' do
setup do
Factory :event, :description => <<-EOS
This is some text.
 
This is more text.
Break.
EOS
 
@events = Event.all
get_events_list
end
 
should 'format the description' do
assert_select('div.content p', :count => 2) do
assert_select 'br', :count => 1
end
end
end
 
context 'Given an event with a picture' do
setup do
Factory :event_with_picture
@events = Event.all
end
 
context 'when displaying the events' do
setup { get_events_list }
 
should 'show the picture' do
event = @events.first
assert_select 'li div.content img[src=?] + p',
%r{/system/events/pictures/#{ event.id }/normal/#{ event.picture_file_name }\?\d+},
:count => 1
end
end
end
 
private
 
def get_events_list
set_response_body(events_list(@events))
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.