From 5881795d5f0ffaa4b6d04c7b9ad78707ae8b0e47 Mon Sep 17 00:00:00 2001 From: Sebastian Serth Date: Fri, 23 Sep 2022 11:39:08 +0200 Subject: [PATCH] Memorize config options instead of reading them from file over and over again --- app/controllers/application_controller.rb | 1 + app/helpers/exercise_helper.rb | 9 +++------ app/policies/codeharbor_link_policy.rb | 4 +++- spec/controllers/codeharbor_links_controller_spec.rb | 7 ++----- spec/policies/codeharbor_link_policy_spec.rb | 12 ++---------- 5 files changed, 11 insertions(+), 22 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 941d7606..9836e10b 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -5,6 +5,7 @@ class ApplicationController < ActionController::Base include Pundit::Authorization MEMBER_ACTIONS = %i[destroy edit show update].freeze + RENDER_HOST = CodeOcean::Config.new(:code_ocean).read[:render_host] after_action :verify_authorized, except: %i[welcome] around_action :mnemosyne_trace diff --git a/app/helpers/exercise_helper.rb b/app/helpers/exercise_helper.rb index 4302a3fb..da8153c2 100644 --- a/app/helpers/exercise_helper.rb +++ b/app/helpers/exercise_helper.rb @@ -3,6 +3,8 @@ module ExerciseHelper include LtiHelper + CODEPILOT_CONFIG = CodeOcean::Config.new(:code_ocean).read[:code_pilot] + def embedding_parameters(exercise) "locale=#{I18n.locale}&token=#{exercise.token}" end @@ -12,11 +14,6 @@ module ExerciseHelper end def qa_url - config = CodeOcean::Config.new(:code_ocean) - enabled = config.read[:code_pilot][:enabled] - - if enabled - config.read[:code_pilot][:url] - end + CODEPILOT_CONFIG[:url] if CODEPILOT_CONFIG[:enabled] end end diff --git a/app/policies/codeharbor_link_policy.rb b/app/policies/codeharbor_link_policy.rb index 1785f77d..d8358f88 100644 --- a/app/policies/codeharbor_link_policy.rb +++ b/app/policies/codeharbor_link_policy.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true class CodeharborLinkPolicy < ApplicationPolicy + CODEHARBOR_CONFIG = CodeOcean::Config.new(:code_ocean).read[:codeharbor] + def index? false end @@ -30,7 +32,7 @@ class CodeharborLinkPolicy < ApplicationPolicy end def enabled? - CodeOcean::Config.new(:code_ocean).read[:codeharbor][:enabled] + CODEHARBOR_CONFIG[:enabled] end private diff --git a/spec/controllers/codeharbor_links_controller_spec.rb b/spec/controllers/codeharbor_links_controller_spec.rb index 65a4cd7e..752d9811 100644 --- a/spec/controllers/codeharbor_links_controller_spec.rb +++ b/spec/controllers/codeharbor_links_controller_spec.rb @@ -5,12 +5,9 @@ require 'rails_helper' describe CodeharborLinksController do let(:user) { create(:teacher) } - let(:codeocean_config) { instance_double(CodeOcean::Config) } - let(:codeharbor_config) { {codeharbor: {enabled: true, url: 'https://test.url'}} } - before do - allow(CodeOcean::Config).to receive(:new).with(:code_ocean).and_return(codeocean_config) - allow(codeocean_config).to receive(:read).and_return(codeharbor_config) + allow(CodeharborLinkPolicy::CODEHARBOR_CONFIG).to receive(:[]).with(:enabled).and_return(true) + allow(CodeharborLinkPolicy::CODEHARBOR_CONFIG).to receive(:[]).with(:url).and_return('https://test.url') allow(controller).to receive(:current_user).and_return(user) end diff --git a/spec/policies/codeharbor_link_policy_spec.rb b/spec/policies/codeharbor_link_policy_spec.rb index f3541e50..e5ab0729 100644 --- a/spec/policies/codeharbor_link_policy_spec.rb +++ b/spec/policies/codeharbor_link_policy_spec.rb @@ -8,12 +8,8 @@ describe CodeharborLinkPolicy do let(:codeharbor_link) { create(:codeharbor_link) } context 'when CodeHarbor link is enabled' do - let(:codeocean_config) { instance_double(CodeOcean::Config) } - let(:codeharbor_config) { {codeharbor: {enabled: true}} } - before do - allow(CodeOcean::Config).to receive(:new).with(:code_ocean).and_return(codeocean_config) - allow(codeocean_config).to receive(:read).and_return(codeharbor_config) + allow(CodeharborLinkPolicy::CODEHARBOR_CONFIG).to receive(:[]).with(:enabled).and_return(true) end %i[index? show?].each do |action| @@ -64,12 +60,8 @@ describe CodeharborLinkPolicy do end context 'when CodeHabor link is disabled' do - let(:codeocean_config) { instance_double(CodeOcean::Config) } - let(:codeharbor_config) { {codeharbor: {enabled: false}} } - before do - allow(CodeOcean::Config).to receive(:new).with(:code_ocean).and_return(codeocean_config) - allow(codeocean_config).to receive(:read).and_return(codeharbor_config) + allow(CodeharborLinkPolicy::CODEHARBOR_CONFIG).to receive(:[]).with(:enabled).and_return(false) end permissions :enabled? do