Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
SRCT
schedules
Commits
63cb1356
Commit
63cb1356
authored
Feb 08, 2019
by
Zac Wood
Browse files
Remove unnecessary redirect from application_controller
parent
7b640f2a
Changes
2
Hide whitespace changes
Inline
Side-by-side
schedules/.rubocop.yml
View file @
63cb1356
...
...
@@ -16,3 +16,6 @@ Metrics/BlockLength:
Style/ClassAndModuleChildren
:
Enabled
:
false
Style/GuardClause
:
Enabled
:
false
schedules/app/controllers/application_controller.rb
View file @
63cb1356
# Configures the application.
class
ApplicationController
<
ActionController
::
Base
protect_from_forgery
with: :null_session
before_action
:set_semester
,
:set_cookies
,
:set_cart
# On each request, set the semester and cart.
before_action
:set_semester
,
:set_cart
# Every page needs to know what semester it should load data from.
# set_semester checks both the semester_id query parameter and the user's cookies
# to look for a semester id and loads whatever it finds into @semester.
#
# By default, load the most recent semester.
def
set_semester
if
params
.
key?
(
:semester_id
)
cookies
[
:semester_id
]
=
params
[
:semester_id
]
@semester
=
Semester
.
find_by_id
params
[
:semester_id
]
cookies
[
:semester_id
]
=
@semester
.
id
elsif
cookies
[
:semester_id
].
nil?
redirect_to
(
url_for
(
params
.
permit
(
params
.
keys
).
merge
(
semester_id:
Semester
.
first
.
id
)))
@semester
=
Semester
.
first
cookies
[
:semester_id
]
=
@semester
.
id
else
redirect_to
(
url_for
(
params
.
permit
(
params
.
keys
).
merge
(
semester
_id
:
cookies
[
:semester_id
]
)))
@semester
=
Semester
.
find_by
_id
cookies
[
:semester_id
]
end
end
# The user's cart is stored as a JSON-encoded list of CRNs.
# set_cart sets the @cart variable, which is a list of the sections represented by the CRNs.
def
set_cart
# set the cart cookie to be empty if it doesn't already exist
cookies
.
permanent
[
:cart
]
=
"[]"
if
cookies
.
permanent
[
:cart
].
nil?
# decode the JSON list into an array
@cart
=
JSON
.
parse
(
cookies
.
permanent
[
:cart
])
# get rid of any invalid CRNs
@cart
=
@cart
.
reject
{
|
crn
|
CourseSection
.
find_by_crn
(
crn
).
nil?
}
cookies
.
permanent
[
:cart
]
=
@cart
.
to_json
end
def
set
_
cookies
cookies
.
permanent
[
:cart
]
=
"[]"
if
cookies
.
permanent
[
:cart
].
nil?
#
set
the
cookie
to the JSON-encoded list of valid section
s
cookies
.
permanent
[
:cart
]
=
@cart
.
to_json
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment