Skip to content

Commit

Permalink
make script levels redirect to 2017 versions again
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsbailey committed Jun 6, 2018
1 parent a696e81 commit 13e9abf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
4 changes: 3 additions & 1 deletion dashboard/app/controllers/script_levels_controller.rb
@@ -1,10 +1,12 @@
require 'cdo/script_config'
require 'dynamic_config/dcdo'
require 'dynamic_config/gatekeeper'
require 'cdo/script_constants'

class ScriptLevelsController < ApplicationController
check_authorization
include LevelsHelper
include ScriptConstants

# Default s-maxage to use for script level pages which are configured as
# publicly cacheable. Used if the DCDO.public_proxy_max_age is not defined.
Expand Down Expand Up @@ -67,7 +69,7 @@ def next
def show
@current_user = current_user && User.includes(:teachers).where(id: current_user.id).first
authorize! :read, ScriptLevel
@script = Script.get_from_cache(params[:script_id])
@script = Script.get_from_cache(params[:script_id], version_year: DEFAULT_VERSION_YEAR)

# Redirect to the same script level within @script.redirect_to.
# There are too many variations of the script level path to use
Expand Down
43 changes: 29 additions & 14 deletions dashboard/app/models/script.rb
Expand Up @@ -354,7 +354,7 @@ def cached
self.class.get_from_cache(id)
end

def self.get_without_cache(id_or_name)
def self.get_without_cache(id_or_name, version_year: nil)
# Also serve any script by its new_name, if it has one.
script = id_or_name && Script.find_by(new_name: id_or_name)
return script if script
Expand All @@ -369,36 +369,51 @@ def self.get_without_cache(id_or_name)
unless is_id
# We didn't find a script matching id_or_name. Next, look for a script
# in the id_or_name script family to redirect to, e.g. csp1 --> csp1-2017.
script_with_redirect = Script.get_script_family_redirect(id_or_name)
script_with_redirect = Script.get_script_family_redirect(id_or_name, version_year: version_year)
return script_with_redirect if script_with_redirect
end

raise ActiveRecord::RecordNotFound.new("Couldn't find Script with id|name=#{id_or_name}")
raise ActiveRecord::RecordNotFound.new("Couldn't find Script with id|name|family_name=#{id_or_name}")
end

def self.get_from_cache(id_or_name)
return get_without_cache(id_or_name) unless should_cache?

script_cache.fetch(id_or_name.to_s) do
# Returns the script with the specified id, or a script with the specified
# name, or a redirect to the latest version of the script within the specified
# family. Also populates the script cache so that future responses will be cached.
# For example:
# get_from_cache('11') --> script_cache['11'] = <Script id=11, name=...>
# get_from_cache('frozen') --> script_cache['frozen'] = <Script name="frozen", id=...>
# get_from_cache('csp1') --> script_cache['csp1'] = <Script redirect_to="csp1-2018">
# get_from_cache('csp1', version_year: '2017') --> script_cache['csp1/2017'] = <Script redirect_to="csp1-2017">
#
# @param id_or_name [String|Integer] script id, script name, or script family name.
# @param version_year [String] If specified, when looking for a script to redirect
# to within a script family, redirect to this version rather than the latest.
def self.get_from_cache(id_or_name, version_year: nil)
return get_without_cache(id_or_name, version_year: version_year) unless should_cache?
cache_key_suffix = version_year ? "/#{version_year}" : ''
cache_key = "#{id_or_name}#{cache_key_suffix}"
script_cache.fetch(cache_key) do
# Populate cache on miss.
script_cache[id_or_name.to_s] = get_without_cache(id_or_name)
script_cache[cache_key] = get_without_cache(id_or_name, version_year: version_year)
end
end

# Given a script family name, return a dummy Script with redirect_to field
# pointing toward the latest stable script in that family.
# pointing toward the latest stable script in that family, or to a specific
# version_year if one is specified.
# @param family_name [String] The name of the script family to search in.
# @param version_year [String] Version year to return. Optional.
# @return [Script|nil] A dummy script object, not persisted to the database,
# with only the redirect_to field set.
def self.get_script_family_redirect(family_name)
script_name =
def self.get_script_family_redirect(family_name, version_year: nil)
scripts =
Script.
where(family_name: family_name).
all.
select(&:is_stable).
sort_by(&:version_year).
last.
try(:name)
sort_by(&:version_year)
scripts.select! {|s| s.version_year == version_year} if version_year
script_name = scripts.last.try(:name)
script_name ? Script.new(redirect_to: script_name) : nil
end

Expand Down

0 comments on commit 13e9abf

Please sign in to comment.