Removed duplicate methods date_range_string and date_string.
Added tests for date_string function corrections in the test
This commit is contained in:
parent
3bf8b2472d
commit
ec1adbada8
5 changed files with 56 additions and 56 deletions
|
|
@ -5,7 +5,6 @@ class ApplicationController < ActionController::Base
|
||||||
protect_from_forgery with: :exception
|
protect_from_forgery with: :exception
|
||||||
before_filter :get_conferences
|
before_filter :get_conferences
|
||||||
before_filter :store_location
|
before_filter :store_location
|
||||||
helper_method :date_string
|
|
||||||
# Ensure every controller authorizes resource or skips authorization (skip_authorization_check)
|
# Ensure every controller authorizes resource or skips authorization (skip_authorization_check)
|
||||||
check_authorization unless: :devise_controller?
|
check_authorization unless: :devise_controller?
|
||||||
|
|
||||||
|
|
@ -68,39 +67,4 @@ class ApplicationController < ActionController::Base
|
||||||
def not_found
|
def not_found
|
||||||
raise ActionController::RoutingError.new('Not Found')
|
raise ActionController::RoutingError.new('Not Found')
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
|
||||||
# Returns a string build from the start and end date of the given conference.
|
|
||||||
#
|
|
||||||
# If the conference is only one day long
|
|
||||||
# * %B %d %Y (January 17 2014)
|
|
||||||
# If the conference starts and ends in the same month and year
|
|
||||||
# * %B %d - %d, %Y (January 17 - 21 2014)
|
|
||||||
# If the conference ends in another month but in the same year
|
|
||||||
# * %B %d - %B %d, %Y (January 31 - February 02 2014)
|
|
||||||
# All other cases
|
|
||||||
# * %B %d, %Y - %B %d, %Y (December 30, 2013 - January 02, 2014)
|
|
||||||
def date_string(start_date, end_date)
|
|
||||||
startstr = 'Unknown - '
|
|
||||||
endstr = 'Unknown'
|
|
||||||
# When the conference in the same month
|
|
||||||
if start_date.month == end_date.month && start_date.year == end_date.year
|
|
||||||
if start_date.day == end_date.day
|
|
||||||
startstr = start_date.strftime('%B %d')
|
|
||||||
endstr = end_date.strftime(' %Y')
|
|
||||||
else
|
|
||||||
startstr = start_date.strftime('%B %d - ')
|
|
||||||
endstr = end_date.strftime('%d, %Y')
|
|
||||||
end
|
|
||||||
elsif start_date.month != end_date.month && start_date.year == end_date.year
|
|
||||||
startstr = start_date.strftime('%B %d - ')
|
|
||||||
endstr = end_date.strftime('%B %d, %Y')
|
|
||||||
else
|
|
||||||
startstr = start_date.strftime('%B %d, %Y - ')
|
|
||||||
endstr = end_date.strftime('%B %d, %Y')
|
|
||||||
end
|
|
||||||
|
|
||||||
result = startstr + endstr
|
|
||||||
result
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,39 @@
|
||||||
module ApplicationHelper
|
module ApplicationHelper
|
||||||
|
##
|
||||||
|
# Returns a string build from the start and end date of the given conference.
|
||||||
|
#
|
||||||
|
# If the conference is only one day long
|
||||||
|
# * %B %d %Y (January 17 2014)
|
||||||
|
# If the conference starts and ends in the same month and year
|
||||||
|
# * %B %d - %d, %Y (January 17 - 21 2014)
|
||||||
|
# If the conference ends in another month but in the same year
|
||||||
|
# * %B %d - %B %d, %Y (January 31 - February 02 2014)
|
||||||
|
# All other cases
|
||||||
|
# * %B %d, %Y - %B %d, %Y (December 30, 2013 - January 02, 2014)
|
||||||
|
def date_string(start_date, end_date)
|
||||||
|
startstr = 'Unknown - '
|
||||||
|
endstr = 'Unknown'
|
||||||
|
# When the conference is in the same month
|
||||||
|
if start_date.month == end_date.month && start_date.year == end_date.year
|
||||||
|
if start_date.day == end_date.day
|
||||||
|
startstr = start_date.strftime('%B %d')
|
||||||
|
endstr = end_date.strftime(' %Y')
|
||||||
|
else
|
||||||
|
startstr = start_date.strftime('%B %d - ')
|
||||||
|
endstr = end_date.strftime('%d, %Y')
|
||||||
|
end
|
||||||
|
elsif start_date.month != end_date.month && start_date.year == end_date.year
|
||||||
|
startstr = start_date.strftime('%B %d - ')
|
||||||
|
endstr = end_date.strftime('%B %d, %Y')
|
||||||
|
else
|
||||||
|
startstr = start_date.strftime('%B %d, %Y - ')
|
||||||
|
endstr = end_date.strftime('%B %d, %Y')
|
||||||
|
end
|
||||||
|
|
||||||
|
result = startstr + endstr
|
||||||
|
result
|
||||||
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Checks if the voting has already started, or if it has already ended
|
# Checks if the voting has already started, or if it has already ended
|
||||||
#
|
#
|
||||||
|
|
|
||||||
|
|
@ -68,24 +68,6 @@ class Conference < ActiveRecord::Base
|
||||||
|
|
||||||
after_create :create_free_ticket
|
after_create :create_free_ticket
|
||||||
|
|
||||||
def date_range_string
|
|
||||||
startstr = 'Unknown - '
|
|
||||||
endstr = 'Unknown'
|
|
||||||
if start_date.month == end_date.month && start_date.year == end_date.year
|
|
||||||
startstr = start_date.strftime('%B %d - ')
|
|
||||||
endstr = end_date.strftime('%d, %Y')
|
|
||||||
elsif start_date.month != end_date.month && start_date.year == end_date.year
|
|
||||||
startstr = start_date.strftime('%B %d - ')
|
|
||||||
endstr = end_date.strftime('%B %d, %Y')
|
|
||||||
else
|
|
||||||
startstr = start_date.strftime('%B %d, %Y - ')
|
|
||||||
endstr = end_date.strftime('%B %d, %Y')
|
|
||||||
end
|
|
||||||
|
|
||||||
result = startstr + endstr
|
|
||||||
result
|
|
||||||
end
|
|
||||||
|
|
||||||
##
|
##
|
||||||
# Checks if the user is registered to the conference
|
# Checks if the user is registered to the conference
|
||||||
#
|
#
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
class ConferenceSerializer < ActiveModel::Serializer
|
class ConferenceSerializer < ActiveModel::Serializer
|
||||||
|
include ApplicationHelper
|
||||||
attributes :short_title, :title, :description, :start_date, :end_date, :picture_url,
|
attributes :short_title, :title, :description, :start_date, :end_date, :picture_url,
|
||||||
:difficulty_levels, :event_types, :rooms, :tracks,
|
:difficulty_levels, :event_types, :rooms, :tracks,
|
||||||
:date_range, :revision
|
:date_range, :revision
|
||||||
|
|
@ -58,8 +59,8 @@ class ConferenceSerializer < ActiveModel::Serializer
|
||||||
end
|
end
|
||||||
|
|
||||||
def date_range
|
def date_range
|
||||||
if defined? object.date_range_string
|
if defined? date_string(object.start_date, object.end_date)
|
||||||
object.date_range_string.try(:split, ',').try(:first)
|
date_string(object.start_date, object.end_date).try(:split, ',').try(:first)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -60,6 +60,24 @@ describe ApplicationHelper, type: :helper do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#date_string' do
|
||||||
|
it 'when conference lasts 1 day' do
|
||||||
|
expect(date_string('Sun, 19 Feb 2017'.to_time, 'Sun, 19 Feb 2017'.to_time)).to eq 'February 19 2017'
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'when conference starts and ends in the same month and year' do
|
||||||
|
expect(date_string('Sun, 19 Feb 2017'.to_time, 'Tue, 28 Feb 2017'.to_time)).to eq 'February 19 - 28, 2017'
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'when conference ends in another month, of the same year' do
|
||||||
|
expect(date_string('Sun, 19 Feb 2017'.to_time, 'Tue, 28 March 2017'.to_time)).to eq 'February 19 - March 28, 2017'
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'when conference ends in another month, of a different year' do
|
||||||
|
expect(date_string('Sun, 19 Feb 2017'.to_time, 'Sun, 12 March 2018'.to_time)).to eq 'February 19, 2017 - March 12, 2018'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe '#registered_text' do
|
describe '#registered_text' do
|
||||||
describe 'returns correct string' do
|
describe 'returns correct string' do
|
||||||
it 'when there are no registrations' do
|
it 'when there are no registrations' do
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue