Skip to content

Commit

Permalink
Refactor advent calendar Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Altmann committed Dec 4, 2015
1 parent 1f32a15 commit 75dc01f
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 67 deletions.
20 changes: 10 additions & 10 deletions app/assets/stylesheets/controller/welcome/_advent_calendar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@

/*** Different images ***/

.advent-calendar-pre { background-image: image-url('welcome/advent_calendar_pre.jpg'); }
.advent-calendar-01 { background-image: image-url('welcome/advent_calendar_01.jpg'); }
.advent-calendar-02 { background-image: image-url('welcome/advent_calendar_02.jpg'); }
.advent-calendar-03 { background-image: image-url('welcome/advent_calendar_03.jpg'); }
.advent-calendar-04 { background-image: image-url('welcome/advent_calendar_04.jpg'); }
.advent-calendar-05 { background-image: image-url('welcome/advent_calendar_05.jpg'); }
.advent-calendar-06 { background-image: image-url('welcome/advent_calendar_06.jpg'); }
.advent-calendar-07 { background-image: image-url('welcome/advent_calendar_07.jpg'); }
.advent-calendar-08 { background-image: image-url('welcome/advent_calendar_08.jpg'); }
.advent-calendar-09 { background-image: image-url('welcome/advent_calendar_09.jpg'); }
.advent-calendar-0 { background-image: image-url('welcome/advent_calendar_pre.jpg'); }
.advent-calendar-1 { background-image: image-url('welcome/advent_calendar_01.jpg'); }
.advent-calendar-2 { background-image: image-url('welcome/advent_calendar_02.jpg'); }
.advent-calendar-3 { background-image: image-url('welcome/advent_calendar_03.jpg'); }
.advent-calendar-4 { background-image: image-url('welcome/advent_calendar_04.jpg'); }
.advent-calendar-5 { background-image: image-url('welcome/advent_calendar_05.jpg'); }
.advent-calendar-6 { background-image: image-url('welcome/advent_calendar_06.jpg'); }
.advent-calendar-7 { background-image: image-url('welcome/advent_calendar_07.jpg'); }
.advent-calendar-8 { background-image: image-url('welcome/advent_calendar_08.jpg'); }
.advent-calendar-9 { background-image: image-url('welcome/advent_calendar_09.jpg'); }
.advent-calendar-10 { background-image: image-url('welcome/advent_calendar_10.jpg'); }
.advent-calendar-11 { background-image: image-url('welcome/advent_calendar_11.jpg'); }
.advent-calendar-12 { background-image: image-url('welcome/advent_calendar_12.jpg'); }
Expand Down
18 changes: 13 additions & 5 deletions app/helpers/welcome_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# See the COPYRIGHT file for details.

module WelcomeHelper
CALENDAR_WINDOW_LINKS = %w[/adventskalender /advent01_naturkosmetik /advent02_ratespiele /advent03_tolino /advent04_holzspielzeug]

def rss_image_extractor content
if content.start_with? '<p><img'
Sanitize.clean(content[0..(content.index('/>') + 1)], elements: ['img'], attributes: { 'img' => %w(src alt) }).html_safe
Expand All @@ -24,15 +26,21 @@ def calendar_time?
end
end

def calendar_partial_name
# returns a number between 0 and 24
def calendar_window_num
now = Time.now
if now < Time.new(2015, 12, 1)
'welcome/advent_calendar/window_pre'
if now < Time.new(2015, 12, 1, 9)
0
elsif now >= Time.new(2015, 12, 25, 9)
24
else
day = now.day
day -= 1 if now.hour < 9
day_str = day.to_s.rjust(2, '0')
"welcome/advent_calendar/window_#{day_str}"
day
end
end

def calendar_window_link
CALENDAR_WINDOW_LINKS[calendar_window_num]
end
end
5 changes: 4 additions & 1 deletion app/views/welcome/_advent_calendar.html.slim
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.advent-calendar
.advent-calendar-inner
= render calendar_partial_name
article[class="advent-calendar-#{calendar_window_num}"]
div.advent-calendar-bow-div
h2= "#{calendar_window_num}. Advent"
p= link_to 'Jetzt gewinnen', calendar_window_link
5 changes: 0 additions & 5 deletions app/views/welcome/advent_calendar/_window_01.html.slim

This file was deleted.

5 changes: 0 additions & 5 deletions app/views/welcome/advent_calendar/_window_02.html.slim

This file was deleted.

5 changes: 0 additions & 5 deletions app/views/welcome/advent_calendar/_window_03.html.slim

This file was deleted.

5 changes: 0 additions & 5 deletions app/views/welcome/advent_calendar/_window_04.html.slim

This file was deleted.

1 change: 0 additions & 1 deletion app/views/welcome/advent_calendar/_window_05.html.slim

This file was deleted.

1 change: 0 additions & 1 deletion app/views/welcome/advent_calendar/_window_06.html.slim

This file was deleted.

5 changes: 0 additions & 5 deletions app/views/welcome/advent_calendar/_window_pre.html.slim

This file was deleted.

8 changes: 5 additions & 3 deletions test/features/advent_calendar_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
travel_to Time.new(2015, 11, 24) do
visit root_path
within '.advent-calendar' do
assert page.has_selector?('.advent-calendar-pre')
assert page.has_selector?('.advent-calendar-0')
end
end
end
Expand All @@ -25,7 +25,8 @@
travel_to Time.new(2015, 12, 5, 9) do
visit root_path
within '.advent-calendar' do
assert page.has_selector?('.advent-calendar-05')
assert page.has_selector?('.advent-calendar-5')
assert page.has_content?('5. Advent')
end
end
end
Expand All @@ -34,7 +35,8 @@
travel_to Time.new(2015, 12, 6, 9) do
visit root_path
within '.advent-calendar' do
assert page.has_selector?('.advent-calendar-06')
assert page.has_selector?('.advent-calendar-6')
assert page.has_content?('6. Advent')
end
end
end
Expand Down
71 changes: 50 additions & 21 deletions test/helpers/welcome_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,68 @@
end

describe 'advent calendar' do
it 'is calendar time between Nov 24th and Dec 25th, 9:00' do
travel_to Time.new(2015, 11, 24) do
assert(calendar_time?)
describe '#calendar_time?' do
it 'is calendar time between Nov 24th and Dec 25th, 9:00' do
travel_to Time.new(2015, 11, 24) do
assert(calendar_time?)
end
travel_to Time.new(2015, 12, 25, 9) do
assert(calendar_time?)
end
end
travel_to Time.new(2015, 12, 25, 9) do
assert(calendar_time?)

it 'is not calendar time outside of this time zone' do
travel_to Time.new(2015, 11, 23) do
refute(calendar_time?)
end
travel_to Time.new(2015, 12, 25, 9, 1) do
refute(calendar_time?)
end
end
end

it 'is not calendar time outside of this time zone' do
travel_to Time.new(2015, 11, 23) do
refute(calendar_time?)
describe '#calendar_window_num' do
it 'considers the calendar window number to be 4 on the 5th Dec at 8:59' do
travel_to Time.new(2015, 12, 05, 8, 59) do
assert_equal(4, calendar_window_num)
end
end
travel_to Time.new(2015, 12, 25, 9, 1) do
refute(calendar_time?)

it 'considers the calendar window number to be 5 on the 5th Dec at 9 o\' clock' do
travel_to Time.new(2015, 12, 05, 9) do
assert_equal(5, calendar_window_num)
end
end
end

it 'shows the calendar partial for the 4th on the 5th at 8:59' do
travel_to Time.new(2015, 12, 05, 8, 59) do
assert_equal('welcome/advent_calendar/window_04', calendar_partial_name)
it 'considers the calendar window number to be 0 on the 1st Dec at 8:59' do
travel_to Time.new(2015, 12, 01, 8, 59) do
assert_equal(0, calendar_window_num)
end
end

it 'considers the calendar window number to be 0 before the 1st Dec' do
travel_to Time.new(2015, 11, 27) do
assert_equal(0, calendar_window_num)
end
end

it 'considers the calendar window number to be 24 on the 25th Dec at 9 o\' clock' do
travel_to Time.new(2015, 12, 25, 9) do
assert_equal(24, calendar_window_num)
end
end
end

it 'shows the calendar partial for the 5th on the 5th at 9 o\' clock' do
travel_to Time.new(2015, 12, 05, 9) do
assert_equal('welcome/advent_calendar/window_05', calendar_partial_name)
it 'considers the calendar window number to be 24 after the 25th Dec' do
travel_to Time.new(2015, 12, 27) do
assert_equal(24, calendar_window_num)
end
end
end

it 'before December finds the pre-calendar partial' do
travel_to Time.new(2015, 11, 27) do
assert_equal('welcome/advent_calendar/window_pre', calendar_partial_name)
describe '#calendar_window_link' do
it 'returns a different link for two distinct calendar window numbers' do
helper.stubs(:calendar_window_num).returns(5, 6)
refute_equal(calendar_window_link, calendar_window_link)
end
end
end
Expand Down

0 comments on commit 75dc01f

Please sign in to comment.