Commit 8951ae049ebac949a42096e80c157d34a266acdc
1 parent
a2b4c459
Exists in
ratings_minor_fixes
and in
4 other branches
Refactor EventBlock#human_time_left into helper
This method fits bteer into a helper since it is formatting data for the view.
Showing
7 changed files
with
50 additions
and
48 deletions
Show diff stats
plugins/event/lib/event_block_helper.rb
| @@ -1,10 +0,0 @@ | @@ -1,10 +0,0 @@ | ||
| 1 | -module 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 | -end |
plugins/event/lib/event_plugin/event_block.rb
| @@ -47,21 +47,6 @@ class EventPlugin::EventBlock < Block | @@ -47,21 +47,6 @@ class EventPlugin::EventBlock < Block | ||
| 47 | event_list | 47 | event_list |
| 48 | end | 48 | end |
| 49 | 49 | ||
| 50 | - def human_time_left(days_left) | ||
| 51 | - months_left = (days_left/30.0).round | ||
| 52 | - if days_left <= -60 | ||
| 53 | - n_('One month ago', '%d months ago', -months_left) % -months_left | ||
| 54 | - elsif days_left < 0 | ||
| 55 | - n_('Yesterday', '%d days ago', -days_left) % -days_left | ||
| 56 | - elsif days_left == 0 | ||
| 57 | - _("Today") | ||
| 58 | - elsif days_left < 60 | ||
| 59 | - n_('Tomorrow', '%d days left to start', days_left) % days_left | ||
| 60 | - else | ||
| 61 | - n_('One month left to start', '%d months left to start', months_left) % months_left | ||
| 62 | - end | ||
| 63 | - end | ||
| 64 | - | ||
| 65 | def self.expire_on | 50 | def self.expire_on |
| 66 | { :profile => [:article], :environment => [:article] } | 51 | { :profile => [:article], :environment => [:article] } |
| 67 | end | 52 | end |
| @@ -0,0 +1,25 @@ | @@ -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
| 1 | require 'test_helper' | 1 | require 'test_helper' |
| 2 | 2 | ||
| 3 | class EventBlockHelperTest < ActionView::TestCase | 3 | class EventBlockHelperTest < ActionView::TestCase |
| 4 | - include EventBlockHelper | 4 | + include EventPlugin::EventBlockHelper |
| 5 | 5 | ||
| 6 | should 'write formatable data in html' do | 6 | should 'write formatable data in html' do |
| 7 | html = '<span class="week-day">Tue</span>'+ | 7 | html = '<span class="week-day">Tue</span>'+ |
| @@ -11,4 +11,24 @@ class EventBlockHelperTest < ActionView::TestCase | @@ -11,4 +11,24 @@ class EventBlockHelperTest < ActionView::TestCase | ||
| 11 | 11 | ||
| 12 | assert_equal html, date_to_html(Date.new 1983, 9, 27) | 12 | assert_equal html, date_to_html(Date.new 1983, 9, 27) |
| 13 | end | 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 | ||
| 14 | end | 34 | end |
plugins/event/test/unit/event_block_test.rb
| @@ -77,26 +77,6 @@ class EventBlockTest < ActiveSupport::TestCase | @@ -77,26 +77,6 @@ class EventBlockTest < ActiveSupport::TestCase | ||
| 77 | assert_equal 2, @block.events.length | 77 | assert_equal 2, @block.events.length |
| 78 | end | 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 'show unlimited time distance events' do | 80 | should 'show unlimited time distance events' do |
| 101 | @block.box.owner = @env | 81 | @block.box.owner = @env |
| 102 | @block.all_env_events = true | 82 | @block.all_env_events = true |
plugins/event/views/blocks/event.html.erb
| 1 | +<% extend EventPlugin::EventBlockHelper %> | ||
| 2 | + | ||
| 1 | <%= block_title(block.title, block.subtitle) %> | 3 | <%= block_title(block.title, block.subtitle) %> |
| 2 | 4 | ||
| 3 | <ul class="events"> | 5 | <ul class="events"> |
| @@ -10,7 +12,7 @@ | @@ -10,7 +12,7 @@ | ||
| 10 | :event => event, | 12 | :event => event, |
| 11 | :block => block, | 13 | :block => block, |
| 12 | :time_class => days_left < 0 ? 'past' : 'future', | 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 | %> |