2013-01-07 09:04:09 +01:00
class ApplicationController < ActionController :: Base
include ApplicationHelper
2015-12-08 16:05:29 +01:00
protect_from_forgery with : :exception
2013-06-27 16:58:20 +02:00
before_filter :get_conferences
2013-08-25 13:40:44 +03:00
before_filter :store_location
2014-07-10 21:29:07 +05:30
helper_method :date_string
2014-08-12 11:51:59 +03:00
# Ensure every controller authorizes resource or skips authorization (skip_authorization_check)
check_authorization unless : :devise_controller?
2016-03-01 22:14:11 +05:30
add_flash_types :error , :alert
2013-08-25 13:40:44 +03:00
def store_location
2014-11-04 17:42:47 +01:00
# store last url - this is needed for post-login redirect to whatever the user last visited.
return unless request . get?
if ( request . path != '/accounts/sign_in' &&
request . path != '/accounts/sign_up' &&
request . path != '/accounts/password/new' &&
request . path != '/accounts/password/edit' &&
request . path != '/accounts/confirmation' &&
request . path != '/accounts/sign_out' &&
request . path != '/users/ichain_registration/ichain_sign_up' &&
! request . path . starts_with? ( Devise . ichain_base_url ) &&
! request . xhr? ) # don't store ajax calls
session [ :return_to ] = request . fullpath
end
2013-08-25 13:40:44 +03:00
end
2014-11-04 17:42:47 +01:00
def after_sign_in_path_for ( _resource )
2014-08-12 11:51:59 +03:00
if ( can? :view , Conference ) &&
2014-05-23 17:05:19 +02:00
( ! session [ :return_to ] ||
session [ :return_to ] &&
session [ :return_to ] == root_path )
admin_conference_index_path
2014-03-24 12:45:04 +01:00
else
2014-11-04 17:42:47 +01:00
session [ :return_to ] || root_path
2014-03-24 12:45:04 +01:00
end
2013-08-25 13:40:44 +03:00
end
2013-06-27 16:58:20 +02:00
def get_conferences
@conferences = Conference . all
end
2013-01-07 09:04:09 +01:00
2014-08-12 11:51:59 +03:00
def current_ability
@current_ability || = Ability . new ( current_user )
2013-01-07 09:04:09 +01:00
end
rescue_from CanCan :: AccessDenied do | exception |
2015-03-12 23:53:57 +01:00
Rails . logger . debug " Access denied on #{ exception . action } #{ exception . subject . inspect } "
2015-04-23 11:50:22 +02:00
message = exception . message
message << ' Maybe you need to sign in?' if ! current_user
redirect_to root_path , alert : message
2013-01-07 09:04:09 +01:00
end
2014-06-17 00:47:49 +05:30
2014-11-04 17:42:47 +01:00
rescue_from IChainRecordNotFound do
Rails . logger . debug ( 'IChain Record was not Unique!' )
sign_out ( current_user )
2016-03-01 22:14:11 +05:30
redirect_to root_path , error : 'Your E-Mail adress is already registered at OSEM. Please contact the admin if you want to attach your openSUSE Account to OSEM!'
2014-11-04 17:42:47 +01:00
end
2014-11-06 16:17:11 +01:00
rescue_from UserDisabled do
Rails . logger . debug ( 'User is disabled!' )
sign_out ( current_user )
mail = User . admin . first ? User . admin . first . email : 'the admin!'
2016-03-01 22:14:11 +05:30
redirect_to User . ichain_logout_url , error : " This User is disabled. Please contact #{ mail } ! "
2014-11-06 16:17:11 +01:00
end
2014-06-17 00:47:49 +05:30
def not_found
raise ActionController :: RoutingError . new ( 'Not Found' )
end
2014-07-10 21:29:07 +05:30
##
# Returns a string build from the start and end date of the given conference.
#
2016-02-16 23:00:11 +01:00
# If the conference is only one day long
# * %B %d %Y (January 17 2014)
2014-07-10 21:29:07 +05:30
# 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
2016-02-16 23:00:11 +01:00
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
2014-07-10 21:29:07 +05:30
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
2013-01-07 09:04:09 +01:00
end