Commit fde9abb333ee2c430f75454d6b0de57ce035efd5

Authored by Braulio Bhavamitra
2 parents fa17838c 8951ae04

Merge branch 'refactor_event_plugin' into 'master'

Refactor event plugin

This removes HTML generation from the model improving the MVC compliance.

See merge request !915
plugins/event/lib/event_plugin/event_block.rb
1 1 class EventPlugin::EventBlock < Block
2   - include DatesHelper
3   -
4 2 attr_accessible :all_env_events, :limit, :future_only, :date_distance_limit
5 3  
6 4 settings_items :all_env_events, :type => :boolean, :default => false
... ... @@ -49,38 +47,6 @@ class EventPlugin::EventBlock &lt; Block
49 47 event_list
50 48 end
51 49  
52   - def content(args={})
53   - block = self
54   - proc do
55   - render(
56   - :file => 'blocks/event',
57   - :locals => { :block => block }
58   - )
59   - end
60   - end
61   -
62   - def human_time_left(days_left)
63   - months_left = (days_left/30.0).round
64   - if days_left <= -60
65   - n_('One month ago', '%d months ago', -months_left) % -months_left
66   - elsif days_left < 0
67   - n_('Yesterday', '%d days ago', -days_left) % -days_left
68   - elsif days_left == 0
69   - _("Today")
70   - elsif days_left < 60
71   - n_('Tomorrow', '%d days left to start', days_left) % days_left
72   - else
73   - n_('One month left to start', '%d months left to start', months_left) % months_left
74   - end
75   - end
76   -
77   - def date_to_html(date)
78   - content_tag(:span, show_day_of_week(date, true), :class => 'week-day') +
79   - content_tag(:span, month_name(date.month, true), :class => 'month') +
80   - content_tag(:span, date.day.to_s, :class => 'day') +
81   - content_tag(:span, date.year.to_s, :class => 'year')
82   - end
83   -
84 50 def self.expire_on
85 51 { :profile => [:article], :environment => [:article] }
86 52 end
... ...
plugins/event/lib/event_plugin/event_block_helper.rb 0 → 100644
... ... @@ -0,0 +1,25 @@
  1 +module EventPlugin::EventBlockHelper
  2 + include DatesHelper
  3 +
  4 + def date_to_html(date)
  5 + content_tag(:span, show_day_of_week(date, true), :class => 'week-day') +
  6 + content_tag(:span, month_name(date.month, true), :class => 'month') +
  7 + content_tag(:span, date.day.to_s, :class => 'day') +
  8 + content_tag(:span, date.year.to_s, :class => 'year')
  9 + end
  10 +
  11 + def human_time_left(days_left)
  12 + months_left = (days_left/30.0).round
  13 + if days_left <= -60
  14 + n_('One month ago', '%d months ago', -months_left) % -months_left
  15 + elsif days_left < 0
  16 + n_('Yesterday', '%d days ago', -days_left) % -days_left
  17 + elsif days_left == 0
  18 + _("Today")
  19 + elsif days_left < 60
  20 + n_('Tomorrow', '%d days left to start', days_left) % days_left
  21 + else
  22 + n_('One month left to start', '%d months left to start', months_left) % months_left
  23 + end
  24 + end
  25 +end
... ...
plugins/event/test/unit/event_block_helper_test.rb 0 → 100644
... ... @@ -0,0 +1,34 @@
  1 +require 'test_helper'
  2 +
  3 +class EventBlockHelperTest < ActionView::TestCase
  4 + include EventPlugin::EventBlockHelper
  5 +
  6 + should 'write formatable data in html' do
  7 + html = '<span class="week-day">Tue</span>'+
  8 + '<span class="month">Sep</span>'+
  9 + '<span class="day">27</span>'+
  10 + '<span class="year">1983</span>'
  11 +
  12 + assert_equal html, date_to_html(Date.new 1983, 9, 27)
  13 + end
  14 +
  15 + should 'say human left time for an event' do
  16 + assert_match /Tomorrow/, human_time_left(1)
  17 + assert_match /5 days left/, human_time_left(5)
  18 + assert_match /30 days left/, human_time_left(30)
  19 + assert_match /2 months left/, human_time_left(60)
  20 + assert_match /3 months left/, human_time_left(85)
  21 + end
  22 +
  23 + should 'say human past time for an event' do
  24 + assert_match /Yesterday/, human_time_left(-1)
  25 + assert_match /5 days ago/, human_time_left(-5)
  26 + assert_match /30 days ago/, human_time_left(-30)
  27 + assert_match /2 months ago/, human_time_left(-60)
  28 + assert_match /3 months ago/, human_time_left(-85)
  29 + end
  30 +
  31 + should 'say human present time for an event' do
  32 + assert_match /Today/, human_time_left(0)
  33 + end
  34 +end
... ...
plugins/event/test/unit/event_block_test.rb
... ... @@ -77,35 +77,6 @@ class EventBlockTest &lt; ActiveSupport::TestCase
77 77 assert_equal 2, @block.events.length
78 78 end
79 79  
80   - should 'say human left time for an event' do
81   - assert_match /Tomorrow/, @block.human_time_left(1)
82   - assert_match /5 days left/, @block.human_time_left(5)
83   - assert_match /30 days left/, @block.human_time_left(30)
84   - assert_match /2 months left/, @block.human_time_left(60)
85   - assert_match /3 months left/, @block.human_time_left(85)
86   - end
87   -
88   - should 'say human past time for an event' do
89   - assert_match /Yesterday/, @block.human_time_left(-1)
90   - assert_match /5 days ago/, @block.human_time_left(-5)
91   - assert_match /30 days ago/, @block.human_time_left(-30)
92   - assert_match /2 months ago/, @block.human_time_left(-60)
93   - assert_match /3 months ago/, @block.human_time_left(-85)
94   - end
95   -
96   - should 'say human present time for an event' do
97   - assert_match /Today/, @block.human_time_left(0)
98   - end
99   -
100   - should 'write formatable data in html' do
101   - html = '<span class="week-day">Tue</span>'+
102   - '<span class="month">Sep</span>'+
103   - '<span class="day">27</span>'+
104   - '<span class="year">1983</span>'
105   -
106   - assert_equal html, @block.date_to_html(Date.new 1983, 9, 27)
107   - end
108   -
109 80 should 'show unlimited time distance events' do
110 81 @block.box.owner = @env
111 82 @block.all_env_events = true
... ...
plugins/event/views/blocks/event.html.erb
  1 +<% extend EventPlugin::EventBlockHelper %>
  2 +
1 3 <%= block_title(block.title, block.subtitle) %>
2 4  
3 5 <ul class="events">
... ... @@ -10,7 +12,7 @@
10 12 :event => event,
11 13 :block => block,
12 14 :time_class => days_left < 0 ? 'past' : 'future',
13   - :time_left_str => block.human_time_left(days_left)
  15 + :time_left_str => human_time_left(days_left)
14 16 }
15 17 )
16 18 %>
... ...
plugins/event/views/event_plugin/event_block_item.html.erb
1 1 <%
  2 + extend EventPlugin::EventBlockHelper
  3 +
2 4 # compute layout values
3 5 ev_days_tag = ''
4 6 if event.duration > 1
... ... @@ -18,7 +20,7 @@
18 20 <%=
19 21 link_to(safe_join([
20 22 content_tag('time',
21   - block.date_to_html(event.start_date),
  23 + date_to_html(event.start_date),
22 24 :itemprop => 'startDate',
23 25 :datetime => show_date(event.start_date),
24 26 :class => 'date ' + img_class, :style => bg,
... ...