From 0434de6ceee599d8cce111db58ee286a6e6145f1 Mon Sep 17 00:00:00 2001 From: Rafael dos Santos Silva Date: Tue, 13 Dec 2022 16:03:53 -0300 Subject: [PATCH] DEV: Compatibility with Ruby 3.2 (#19303) --- app/controllers/static_controller.rb | 2 +- app/controllers/user_avatars_controller.rb | 4 ++-- app/models/theme.rb | 2 +- spec/integrity/i18n_spec.rb | 14 ++++++++++--- spec/models/reviewable_score_spec.rb | 20 +++++++++---------- spec/requests/user_avatars_controller_spec.rb | 2 +- 6 files changed, 26 insertions(+), 18 deletions(-) diff --git a/app/controllers/static_controller.rb b/app/controllers/static_controller.rb index b93bde227d3..b0eaaa2f566 100644 --- a/app/controllers/static_controller.rb +++ b/app/controllers/static_controller.rb @@ -169,7 +169,7 @@ class StaticController < ApplicationController immutable_for 1.year response.headers["Expires"] = 1.year.from_now.httpdate response.headers["Content-Length"] = data.bytesize.to_s - response.headers["Last-Modified"] = Time.new('2000-01-01').httpdate + response.headers["Last-Modified"] = Time.new(2000, 01, 01).httpdate render body: data, content_type: "image/png" end end diff --git a/app/controllers/user_avatars_controller.rb b/app/controllers/user_avatars_controller.rb index 7a525359fd4..7b1ea6afbf7 100644 --- a/app/controllers/user_avatars_controller.rb +++ b/app/controllers/user_avatars_controller.rb @@ -48,7 +48,7 @@ class UserAvatarsController < ApplicationController hijack do begin - proxy_avatar("https://avatars.discourse-cdn.com/#{params[:version]}/letter/#{params[:letter]}/#{params[:color]}/#{params[:size]}.png", Time.new('1990-01-01')) + proxy_avatar("https://avatars.discourse-cdn.com/#{params[:version]}/letter/#{params[:letter]}/#{params[:color]}/#{params[:size]}.png", Time.new(1990, 01, 01)) rescue OpenURI::HTTPError render_blank end @@ -190,7 +190,7 @@ class UserAvatarsController < ApplicationController def render_blank path = Rails.root + "public/images/avatar.png" expires_in 10.minutes, public: true - response.headers["Last-Modified"] = Time.new('1990-01-01').httpdate + response.headers["Last-Modified"] = Time.new(1990, 01, 01).httpdate response.headers["Content-Length"] = File.size(path).to_s send_file path, disposition: nil end diff --git a/app/models/theme.rb b/app/models/theme.rb index 3c991a6445a..e14fa527edc 100644 --- a/app/models/theme.rb +++ b/app/models/theme.rb @@ -759,7 +759,7 @@ class Theme < ActiveRecord::Base attr_accessor :theme_setting_requests_refresh def to_scss_variable(name, value) - escaped = SassC::Script::Value::String.quote(value, sass: true) + escaped = SassC::Script::Value::String.quote(value.to_s, sass: true) "$#{name}: unquote(#{escaped});" end diff --git a/spec/integrity/i18n_spec.rb b/spec/integrity/i18n_spec.rb index b175e8a459f..a78811a395b 100644 --- a/spec/integrity/i18n_spec.rb +++ b/spec/integrity/i18n_spec.rb @@ -20,6 +20,14 @@ def is_yaml_compatible?(english, translated) true end +def load_yaml(path) + if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.1.0") + YAML.load_file(path, aliases: true) + else + YAML.load_file(path) + end +end + RSpec.describe "i18n integrity checks" do it "has an i18n key for each Site Setting" do SiteSetting.all_settings.each do |s| @@ -47,7 +55,7 @@ RSpec.describe "i18n integrity checks" do Dir["#{Rails.root}/config/locales/client.*.yml"].each do |path| it "has valid client YAML for '#{path}'" do - yaml = YAML.load_file(path) + yaml = load_yaml(path) locale = extract_locale(path) expect(yaml.keys).to eq([locale]) @@ -62,7 +70,7 @@ RSpec.describe "i18n integrity checks" do end Dir["#{Rails.root}/**/locale*/*.en.yml"].each do |english_path| - english_yaml = YAML.load_file(english_path)["en"] + english_yaml = load_yaml(english_path)["en"] context(english_path) do it "has no duplicate keys" do @@ -76,7 +84,7 @@ RSpec.describe "i18n integrity checks" do context(path) do locale = extract_locale(path) - yaml = YAML.load_file(path) + yaml = load_yaml(path) it "has no duplicate keys" do duplicates = DuplicateKeyFinder.new.find_duplicates(path) diff --git a/spec/models/reviewable_score_spec.rb b/spec/models/reviewable_score_spec.rb index 03765b9ead3..4b2d38d4e88 100644 --- a/spec/models/reviewable_score_spec.rb +++ b/spec/models/reviewable_score_spec.rb @@ -112,45 +112,45 @@ RSpec.describe ReviewableScore, type: :model do it "returns the users weighted accuracy bonus" do user_stat.flags_agreed = 10 user_stat.flags_disagreed = 42 - expect(ReviewableScore.user_accuracy_bonus(user).floor(2)).to eq(-10.34) + expect(ReviewableScore.user_accuracy_bonus(user)).to be_within(0.1).of(-10.34) user_stat.flags_agreed = 2 user_stat.flags_disagreed = 12 - expect(ReviewableScore.user_accuracy_bonus(user).floor(2)).to eq(-7.58) + expect(ReviewableScore.user_accuracy_bonus(user)).to be_within(0.1).of(-7.58) user_stat.flags_agreed = 1 user_stat.flags_disagreed = 6 - expect(ReviewableScore.user_accuracy_bonus(user).floor(2)).to eq(-5.59) + expect(ReviewableScore.user_accuracy_bonus(user)).to be_within(0.1).of(-5.59) user_stat.flags_agreed = 2 user_stat.flags_disagreed = 4 - expect(ReviewableScore.user_accuracy_bonus(user).floor(2)).to eq(-3.39) + expect(ReviewableScore.user_accuracy_bonus(user)).to be_within(0.1).of(-3.39) user_stat.flags_agreed = 7 user_stat.flags_disagreed = 3 - expect(ReviewableScore.user_accuracy_bonus(user).floor(2)).to eq(0) + expect(ReviewableScore.user_accuracy_bonus(user)).to be_within(0.1).of(0) user_stat.flags_agreed = 14 user_stat.flags_disagreed = 6 - expect(ReviewableScore.user_accuracy_bonus(user).floor(2)).to eq(0) + expect(ReviewableScore.user_accuracy_bonus(user)).to be_within(0.1).of(0) # Ignored flags don't count user_stat.flags_agreed = 121 user_stat.flags_disagreed = 44 user_stat.flags_ignored = 4 - expect(ReviewableScore.user_accuracy_bonus(user).floor(2)).to eq(2.04) + expect(ReviewableScore.user_accuracy_bonus(user)).to be_within(0.1).of(2.05) user_stat.flags_agreed = 9 user_stat.flags_disagreed = 2 - expect(ReviewableScore.user_accuracy_bonus(user).floor(2)).to eq(3.41) + expect(ReviewableScore.user_accuracy_bonus(user)).to be_within(0.1).of(3.41) user_stat.flags_agreed = 25 user_stat.flags_disagreed = 4 - expect(ReviewableScore.user_accuracy_bonus(user).floor(2)).to eq(6.56) + expect(ReviewableScore.user_accuracy_bonus(user)).to be_within(0.1).of(6.56) user_stat.flags_agreed = 120 user_stat.flags_disagreed = 12 - expect(ReviewableScore.user_accuracy_bonus(user).floor(2)).to eq(12.27) + expect(ReviewableScore.user_accuracy_bonus(user)).to be_within(0.1).of(12.27) end end diff --git a/spec/requests/user_avatars_controller_spec.rb b/spec/requests/user_avatars_controller_spec.rb index dea799fda00..04c4654dcb4 100644 --- a/spec/requests/user_avatars_controller_spec.rb +++ b/spec/requests/user_avatars_controller_spec.rb @@ -158,7 +158,7 @@ RSpec.describe UserAvatarsController do expect(response.status).to eq(200) # this image should be really old so when it is fixed various algorithms pick it up - expect(response.headers["Last-Modified"]).to eq(Time.new('1990-01-01').httpdate) + expect(response.headers["Last-Modified"]).to eq(Time.new(1990, 01, 01).httpdate) end it 'serves image even if size missing and its in local mode' do