From d2a730b8b5f16f860fcaf61c2c5188610321b37d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Guitaut?= Date: Wed, 20 Mar 2024 17:00:43 +0100 Subject: [PATCH] DEV: Expose extra data from themes This patch exposes a normalized repository URL and how many users are using a given theme. --- app/models/theme.rb | 14 +++++++++ spec/models/theme_spec.rb | 62 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/app/models/theme.rb b/app/models/theme.rb index f4b66c16852..de06a767bfc 100644 --- a/app/models/theme.rb +++ b/app/models/theme.rb @@ -90,6 +90,8 @@ class Theme < ActiveRecord::Base ) end + delegate :remote_url, to: :remote_theme, private: true, allow_nil: true + def notify_color_change(color, scheme: nil) scheme ||= color.color_scheme changed_colors << color if color @@ -949,6 +951,18 @@ class Theme < ActiveRecord::Base [content, Digest::SHA1.hexdigest(content)] end + def repository_url + return unless remote_url + remote_url.gsub( + %r{([^@]+@)?(http(s)?://)?(?[^:/]+)[:/](?((?!\.git).)*)(\.git)?(?.*)}, + '\k/\k\k', + ) + end + + def user_selectable_count + UserOption.where(theme_ids: [id]).count + end + private attr_accessor :theme_setting_requests_refresh diff --git a/spec/models/theme_spec.rb b/spec/models/theme_spec.rb index f892730ede1..d3a561537c0 100644 --- a/spec/models/theme_spec.rb +++ b/spec/models/theme_spec.rb @@ -1433,4 +1433,66 @@ HTML expect(Theme.lookup_field(theme_1.id, :translations, :fr)).to eq(en_field.value_baked) end end + + describe "#repository_url" do + subject(:repository_url) { theme.repository_url } + + context "when theme is not a remote one" do + it "returns nothing" do + expect(repository_url).to be_blank + end + end + + context "when theme is a remote one" do + let!(:remote_theme) { theme.create_remote_theme(remote_url: remote_url) } + + context "when URL is a SSH one" do + let(:remote_url) { "git@github.com:discourse/graceful.git" } + + it "normalizes it" do + expect(repository_url).to eq "github.com/discourse/graceful" + end + end + + context "when URL is a HTTPS one" do + let(:remote_url) { "https://github.com/discourse/graceful.git" } + + it "normalizes it" do + expect(repository_url).to eq "github.com/discourse/graceful" + end + end + + context "when URL is a HTTP one" do + let(:remote_url) { "http://github.com/discourse/graceful" } + + it "normalizes it" do + expect(repository_url).to eq "github.com/discourse/graceful" + end + end + + context "when URL contains query params" do + let(:remote_url) { "http://github.com/discourse/graceful.git?param_id=1" } + + it "keeps the query params" do + expect(repository_url).to eq "github.com/discourse/graceful?param_id=1" + end + end + end + end + + describe "#user_selectable_count" do + subject(:count) { theme.user_selectable_count } + + let!(:users) { Fabricate.times(5, :user) } + let!(:another_theme) { Fabricate(:theme) } + + before do + users.take(3).each { _1.user_option.update!(theme_ids: [theme.id]) } + users.slice(3..4).each { _1.user_option.update!(theme_ids: [another_theme.id]) } + end + + it "returns how many users are currently using the theme" do + expect(count).to eq 3 + end + end end