diff --git a/app/assets/stylesheets/layout.css b/app/assets/stylesheets/layout.css
index 20fb868cc2b..4ee7ec14c51 100644
--- a/app/assets/stylesheets/layout.css
+++ b/app/assets/stylesheets/layout.css
@@ -20,6 +20,9 @@
.l-mb-4 {
margin-bottom: 1rem; }
+.l-mb-6 {
+ margin-bottom: 1.5rem; }
+
.l-mb-8 {
margin-bottom: 2rem; }
diff --git a/app/assets/stylesheets/modules/gem.css b/app/assets/stylesheets/modules/gem.css
index 188cf83a791..8f00a724774 100644
--- a/app/assets/stylesheets/modules/gem.css
+++ b/app/assets/stylesheets/modules/gem.css
@@ -76,7 +76,6 @@
margin-bottom: 6px; }
.gem__downloads {
- margin-bottom: 20px;
display: block;
font-weight: 800;
font-size: 24px; }
diff --git a/app/assets/stylesheets/type.css b/app/assets/stylesheets/type.css
index fbd38e0eeba..4b01bad1135 100644
--- a/app/assets/stylesheets/type.css
+++ b/app/assets/stylesheets/type.css
@@ -33,8 +33,7 @@
.t-text--s {
font-weight: 500;
- font-size: 12px;
- text-transform: uppercase; }
+ font-size: 12px; }
.t-hidden {
position: absolute !important;
diff --git a/app/models/rubygem.rb b/app/models/rubygem.rb
index c17a5df221d..80427763f34 100644
--- a/app/models/rubygem.rb
+++ b/app/models/rubygem.rb
@@ -183,6 +183,15 @@ def downloads
gem_download&.count || 0
end
+ def gem_rank
+ gem_ranking = update_gems_ranking
+
+ return gem_ranking[id] if gem_ranking.key?(id)
+
+ Rails.cache.delete("gems_ranking")
+ update_gems_ranking[id]
+ end
+
def most_recent_version
versions.most_recent
end
@@ -446,4 +455,17 @@ def bulk_reorder_versions
sanitized_query = ActiveRecord::Base.send(:sanitize_sql_array, update_query)
ActiveRecord::Base.connection.execute(sanitized_query)
end
+
+ def update_gems_ranking
+ Rails.cache.fetch("gems_ranking", expires_in: 24.hours) do
+ update_query = <<-SQL.squish
+ SELECT rubygem_id, row_number() OVER(ORDER BY count DESC) AS position
+ FROM gem_downloads
+ WHERE version_id = 0 AND rubygem_id <> 0
+ SQL
+ sanitized_query = ActiveRecord::Base.send(:sanitize_sql_array, [update_query])
+ pg_result = ActiveRecord::Base.connection.execute(sanitized_query)
+ pg_result.to_h { |row| [row["rubygem_id"], row["position"]] }
+ end
+ end
end
diff --git a/app/views/profiles/show.html.erb b/app/views/profiles/show.html.erb
index 6e6ac8e47c4..ae24f73af8c 100644
--- a/app/views/profiles/show.html.erb
+++ b/app/views/profiles/show.html.erb
@@ -98,19 +98,19 @@
-
+
<%= t('stats.index.total_gems') %>
-
+
<%= @user.total_rubygems_count.to_s %>
-
+
<%= t('stats.index.total_downloads') %>
-
+
<%= number_with_delimiter(@user.total_downloads_count) %>
diff --git a/app/views/rubygems/_aside.html.erb b/app/views/rubygems/_aside.html.erb
index 7f56c004ab9..50516324708 100644
--- a/app/views/rubygems/_aside.html.erb
+++ b/app/views/rubygems/_aside.html.erb
@@ -10,10 +10,11 @@
- <%= t('stats.index.total_downloads') %>
+ <%= t('stats.index.total_downloads') %>
<%= number_with_delimiter(@rubygem.downloads) %>
+ <%= t('stats.index.ranking', count: number_with_delimiter(@rubygem.gem_rank), total: number_with_delimiter(Rubygem.total_count)) %>
-
+
<%= t('.downloads_for_this_version') %>
<%= number_with_delimiter(@latest_version.downloads_count) %>
diff --git a/config/locales/de.yml b/config/locales/de.yml
index 80e3a53ebc4..c0ba9f323f2 100644
--- a/config/locales/de.yml
+++ b/config/locales/de.yml
@@ -606,6 +606,7 @@ de:
total_downloads: Downloads insgesamt
total_gems: Gems insgesamt
total_users: Benutzer insgesamt
+ ranking: "%{count} von %{total}"
users:
create:
email_sent:
diff --git a/config/locales/en.yml b/config/locales/en.yml
index dcf567cde21..c47eb062f7f 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -605,6 +605,7 @@ en:
total_downloads: Total downloads
total_gems: Total gems
total_users: Total users
+ ranking: "%{count} of %{total}"
users:
create:
email_sent: A confirmation mail has been sent to your email address.
diff --git a/config/locales/es.yml b/config/locales/es.yml
index 8837c7bae0b..80de76c174c 100644
--- a/config/locales/es.yml
+++ b/config/locales/es.yml
@@ -650,6 +650,7 @@ es:
total_downloads: Total de descargas
total_gems: Gemas totales
total_users: Usuarios totales
+ ranking: "%{count} de %{total}"
users:
create:
email_sent: Se ha enviado un correo de confirmación a tu casilla de correo electrónico.
diff --git a/config/locales/fr.yml b/config/locales/fr.yml
index 6f615490b7e..6217fafabb0 100644
--- a/config/locales/fr.yml
+++ b/config/locales/fr.yml
@@ -657,6 +657,7 @@ fr:
total_downloads: Total de téléchargements
total_gems: Total de gems
total_users: Total d'utilisateurs
+ ranking: "%{count} de %{total}"
users:
create:
email_sent: Un email de confirmation a été envoyé sur votre adresse email.
diff --git a/config/locales/ja.yml b/config/locales/ja.yml
index fdc72586499..36033d1e09c 100644
--- a/config/locales/ja.yml
+++ b/config/locales/ja.yml
@@ -595,6 +595,7 @@ ja:
total_downloads: 累計ダウンロード数
total_gems: Gem総数
total_users: ユーザー総数
+ ranking:
users:
create:
email_sent: 確認メールがあなたのメールアドレスに送信されました。
diff --git a/config/locales/nl.yml b/config/locales/nl.yml
index 6eafde8b0ee..e4e6b69eb9c 100644
--- a/config/locales/nl.yml
+++ b/config/locales/nl.yml
@@ -610,6 +610,7 @@ nl:
total_downloads:
total_gems:
total_users:
+ ranking:
users:
create:
email_sent: Een bevestigingsemail is naar je e-mailadres gestuurd
diff --git a/config/locales/pt-BR.yml b/config/locales/pt-BR.yml
index 1dad8826a31..694fd4dbf7e 100644
--- a/config/locales/pt-BR.yml
+++ b/config/locales/pt-BR.yml
@@ -633,6 +633,7 @@ pt-BR:
total_downloads: Total de downloads
total_gems: Total de gems
total_users: Total de usuários
+ ranking: "%{count} de %{total}"
users:
create:
email_sent:
diff --git a/config/locales/zh-CN.yml b/config/locales/zh-CN.yml
index 71785eccbae..12334d3be2b 100644
--- a/config/locales/zh-CN.yml
+++ b/config/locales/zh-CN.yml
@@ -592,6 +592,7 @@ zh-CN:
total_downloads: 下载总次数
total_gems: Gems 总数
total_users: 用户总数
+ ranking:
users:
create:
email_sent: 确认邮件已发送到您的电子邮件地址。
diff --git a/config/locales/zh-TW.yml b/config/locales/zh-TW.yml
index 9f052c73bd0..3fa44dbe9a7 100644
--- a/config/locales/zh-TW.yml
+++ b/config/locales/zh-TW.yml
@@ -593,6 +593,7 @@ zh-TW:
total_downloads: 總下載次數
total_gems: Gems 總數
total_users: 總使用者數量
+ ranking:
users:
create:
email_sent: 已發送確認信到你的信箱地址。
diff --git a/test/models/rubygem_test.rb b/test/models/rubygem_test.rb
index 5b752a09e99..487d515280c 100644
--- a/test/models/rubygem_test.rb
+++ b/test/models/rubygem_test.rb
@@ -1102,4 +1102,41 @@ class RubygemTest < ActiveSupport::TestCase
refute_predicate @version_three, :yanked?
end
end
+
+ context "#gem_rank" do
+ setup do
+ @rank_one = create(:rubygem, downloads: 5)
+ @rank_two = create(:rubygem, downloads: 1)
+
+ @dummy_rank = { @rank_one.id => 1, @rank_two.id => 2 }
+ end
+
+ should "write to cache" do
+ @rank_one.gem_rank
+
+ assert_equal Rails.cache.read("gems_ranking"), @dummy_rank
+ end
+
+ should "read from cache" do
+ @rank_two.gem_rank
+
+ GemDownload.increment(10, rubygem_id: @rank_two.id)
+
+ @rank_two.gem_rank
+
+ assert_equal Rails.cache.read("gems_ranking"), @dummy_rank
+ end
+
+ should "return the gem rank" do
+ assert_equal 1, @rank_one.gem_rank
+ end
+
+ should "update when rubygem not ranked" do
+ @rank_two.gem_rank
+
+ @rank_three = create(:rubygem, downloads: 3)
+
+ assert_equal(2, @rank_three.gem_rank)
+ end
+ end
end