DEV: Expose extra data from themes

This patch exposes a normalized repository URL and how many users are
using a given theme.
This commit is contained in:
Loïc Guitaut 2024-03-20 17:00:43 +01:00 committed by Loïc Guitaut
parent 4e082c7390
commit d2a730b8b5
2 changed files with 76 additions and 0 deletions

View File

@ -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)?://)?(?<host>[^:/]+)[:/](?<path>((?!\.git).)*)(\.git)?(?<rest>.*)},
'\k<host>/\k<path>\k<rest>',
)
end
def user_selectable_count
UserOption.where(theme_ids: [id]).count
end
private
attr_accessor :theme_setting_requests_refresh

View File

@ -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