From 638517490e6556e27427e6cc578405413ba0705b Mon Sep 17 00:00:00 2001 From: Campbell Allen Date: Wed, 15 Sep 2021 11:50:24 +0100 Subject: [PATCH] remove server side language content loading remove vestigial code that was used to load translated content via user language preferences, e.g. old *Content models that stored translations before the extraction to translations service --- app/controllers/api/api_controller.rb | 29 ----------------- .../api/v1/workflows_controller.rb | 1 - lib/accept_language_extractor.rb | 31 ------------------- spec/controllers/api/api_controller_spec.rb | 26 ---------------- spec/lib/accept_language_extractor_spec.rb | 15 --------- .../organization_serializer_spec.rb | 2 +- spec/serializers/project_serializer_spec.rb | 2 +- spec/serializers/workflow_serializer_spec.rb | 5 ++- spec/support/api_request_helpers.rb | 6 ---- spec/support/locale_format.rb | 12 ------- 10 files changed, 4 insertions(+), 125 deletions(-) delete mode 100644 lib/accept_language_extractor.rb delete mode 100644 spec/lib/accept_language_extractor_spec.rb delete mode 100644 spec/support/locale_format.rb diff --git a/app/controllers/api/api_controller.rb b/app/controllers/api/api_controller.rb index 766221031..689631817 100644 --- a/app/controllers/api/api_controller.rb +++ b/app/controllers/api/api_controller.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'accept_language_extractor' - module Api include ApiErrors @@ -75,24 +73,6 @@ def api_user @api_user ||= ApiUser.new(current_resource_owner, admin: admin_flag?) end - def current_languages - param_langs = [ params[:language] ] - user_langs = user_accept_languages - header_langs = parse_http_accept_languages - ( param_langs | user_langs | header_langs ).compact - end - - def user_accept_languages - api_user.try(:languages) || [] - end - - def parse_http_accept_languages - language_extractor = AcceptLanguageExtractor - .new(request.env['HTTP_ACCEPT_LANGUAGE']) - - language_extractor.parse_languages - end - def request_ip request.remote_ip end @@ -119,14 +99,5 @@ def ban_user end end end - - def context - case action_name - when "show", "index" - { languages: current_languages } - else - { } - end - end end end diff --git a/app/controllers/api/v1/workflows_controller.rb b/app/controllers/api/v1/workflows_controller.rb index fdb557895..64f762060 100644 --- a/app/controllers/api/v1/workflows_controller.rb +++ b/app/controllers/api/v1/workflows_controller.rb @@ -74,7 +74,6 @@ def context case action_name when "show", "index" { - languages: current_languages, published: params["published"] }.merge(field_context) else diff --git a/lib/accept_language_extractor.rb b/lib/accept_language_extractor.rb deleted file mode 100644 index 92581f58d..000000000 --- a/lib/accept_language_extractor.rb +++ /dev/null @@ -1,31 +0,0 @@ -class AcceptLanguageExtractor - - def initialize(accept_header) - @accept_header = accept_header - @prioritized_languages = [] - end - - def parse_languages - return [] if @accept_header.nil? - extract_http_accept_languages - priority_list_of_languages - end - - private - - def priority_list_of_languages - sorted_languages = @prioritized_languages - .delete_if{ |(lang, _)| !(lang =~ /[A-z\-_]+/)} - .sort_by{ |(_, a), (_, b)| b <=> a } - sorted_languages.map(&:first) - end - - def extract_http_accept_languages - @accept_header.gsub(/\s+/, '').split(',').map do |lang| - lang, priority = lang.split(";q=") - lang = lang.downcase - priority = priority ? priority.to_f : 1.0 - @prioritized_languages << [lang, priority] - end - end -end diff --git a/spec/controllers/api/api_controller_spec.rb b/spec/controllers/api/api_controller_spec.rb index af6834d38..6016d88d7 100644 --- a/spec/controllers/api/api_controller_spec.rb +++ b/spec/controllers/api/api_controller_spec.rb @@ -88,32 +88,6 @@ def index end end - describe "#current_language" do - controller do - def index - render json_api: current_languages - end - end - - before(:each) do - user = create(:user, :languages) - default_request(user_id: user.id) - get :index, language: 'es' - end - - it 'should include langauge param as the first language' do - expect(json_response.first).to eq('es') - end - - it 'should include the user\'s default languages after the lang param' do - expect(json_response[1..-1]).to include('en', 'fr-ca') - end - - it 'should include Accept-Language(s) after the user languages' do - expect(json_response[-3..-1]).to include('zh', 'zh-tw', 'fr-fr') - end - end - describe "when a banned user attempts to take an action" do let(:user) { create(:user, banned: true) } diff --git a/spec/lib/accept_language_extractor_spec.rb b/spec/lib/accept_language_extractor_spec.rb deleted file mode 100644 index 39ed9b71c..000000000 --- a/spec/lib/accept_language_extractor_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require "spec_helper" - -describe AcceptLanguageExtractor do - describe "#parse_languages" do - subject { AcceptLanguageExtractor.new("en-US,en;q=0.5,*") } - - it 'should ignore non-alpha languages' do - expect(subject.parse_languages).to_not include("*") - end - - it 'should include alpha languages' do - expect(subject.parse_languages).to match_array(["en-us", "en"]) - end - end -end diff --git a/spec/serializers/organization_serializer_spec.rb b/spec/serializers/organization_serializer_spec.rb index 23c0c40e2..4c39ad8e7 100644 --- a/spec/serializers/organization_serializer_spec.rb +++ b/spec/serializers/organization_serializer_spec.rb @@ -2,7 +2,7 @@ describe OrganizationSerializer do let(:organization) { create(:organization) } - let(:context) { {languages: ['en'], fields: [:title]} } + let(:context) { { fields: [:title] } } let(:serializer) do s = OrganizationSerializer.new diff --git a/spec/serializers/project_serializer_spec.rb b/spec/serializers/project_serializer_spec.rb index 8ebf21661..5c76ff212 100644 --- a/spec/serializers/project_serializer_spec.rb +++ b/spec/serializers/project_serializer_spec.rb @@ -2,7 +2,7 @@ describe ProjectSerializer do let(:project) { create(:full_project, state: "finished", live: false) } - let(:context) { {languages: ['en'], fields: [:title, :url_labels]} } + let(:context) { { fields: %i[title url_labels] } } let(:serializer) do s = ProjectSerializer.new diff --git a/spec/serializers/workflow_serializer_spec.rb b/spec/serializers/workflow_serializer_spec.rb index 12162651a..2b3b5758d 100644 --- a/spec/serializers/workflow_serializer_spec.rb +++ b/spec/serializers/workflow_serializer_spec.rb @@ -5,8 +5,7 @@ let(:serializer) do serializer = WorkflowSerializer.new serializer.instance_variable_set(:@model, workflow) - serializer.instance_variable_set(:@context, - {languages: ['en']}) + serializer.instance_variable_set(:@context, {}) serializer end @@ -36,7 +35,7 @@ workflow.strings["interest.question"] = "Draw a round thing" workflow.save! - serializer.instance_variable_set(:@context, {languages: ['en'], published: true}) + serializer.instance_variable_set(:@context, { published: true }) expect(serializer.tasks['interest']['question']).to eq('Draw a circle') end end diff --git a/spec/support/api_request_helpers.rb b/spec/support/api_request_helpers.rb index fbe2f46d5..5534d6344 100644 --- a/spec/support/api_request_helpers.rb +++ b/spec/support/api_request_helpers.rb @@ -47,10 +47,6 @@ def set_content_type request.env["CONTENT_TYPE"] = "application/json" end - def set_accept_language - request.env['HTTP_ACCEPT_LANGUAGE'] = 'en, zh;q=0.9, zh-tw;q=0.8, fr-fr;q=0.6' - end - def set_patch_content_type request.headers["Content-Type"] = "application/json-patch" end @@ -89,7 +85,6 @@ def stub_content_filter def default_request(scopes: ["public"], user_id: nil) set_accept set_content_type - set_accept_language set_preconditions stub_content_filter stub_token(scopes: scopes, user_id: user_id) @@ -97,7 +92,6 @@ def default_request(scopes: ["public"], user_id: nil) def unauthenticated_request set_accept - set_accept_language set_preconditions stub_content_filter stub_token diff --git a/spec/support/locale_format.rb b/spec/support/locale_format.rb deleted file mode 100644 index 1e04f0b87..000000000 --- a/spec/support/locale_format.rb +++ /dev/null @@ -1,12 +0,0 @@ -shared_examples "a locale field" do - it "should require languages to be exactly 2 or 5 characters" do - expect(build(factory, locale_field => 'a')).to_not be_valid - expect(build(factory, locale_field => 'abasdf')).to_not be_valid - end - - it "should require languages to conform to a format" do - expect(build(factory, locale_field => 'abasd')).to_not be_valid - expect(build(factory, locale_field => 'ab')).to be_valid - expect(build(factory, locale_field => 'ab-sd')).to be_valid - end -end