From 8a935a4b5fbb836474d3344c33f6e169ee62acdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Wed, 6 Sep 2017 22:35:08 +0200 Subject: [PATCH] FEATURE: new badges when visiting the forum for 10, 100 and 365 consecutive days --- app/models/badge.rb | 4 ++++ config/locales/server.en.yml | 12 ++++++++++++ db/fixtures/006_badges.rb | 19 +++++++++++++++++++ lib/badge_queries.rb | 12 ++++++++++++ 4 files changed, 47 insertions(+) diff --git a/app/models/badge.rb b/app/models/badge.rb index 46502613f58..637cb240103 100644 --- a/app/models/badge.rb +++ b/app/models/badge.rb @@ -56,6 +56,10 @@ class Badge < ActiveRecord::Base GivesBack = 32 Empathetic = 39 + Enthusiast = 45 + Aficionado = 46 + Devotee = 47 + NewUserOfTheMonth = 44 # other consts diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 233414a027f..dfeb7af7b02 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -3392,6 +3392,18 @@ en: description: Outstanding contributions in their first month long_description: | This badge is granted to congratulate two new users each month for their excellent overall contributions, as measured by how often their posts were liked, and by whom. + enthusiast: + name: Enthusiast + description: Has visited the forum for 10 consecutive days + long_description: This badge is granted the first time you visit the forum for 10 consecutive days. + aficionado: + name: Aficionado + description: Has visited the forum for 100 consecutive days + long_description: This badge is granted the first time you visit the forum for 100 consecutive days. + devotee: + name: Devotee + description: Has visited the forum for 365 consecutive days + long_description: This badge is granted the first time you visit the forum for 365 consecutive days. badge_title_metadata: "%{display_name} badge on %{site_title}" admin_login: diff --git a/db/fixtures/006_badges.rb b/db/fixtures/006_badges.rb index 22cf2c77147..18022ab4513 100644 --- a/db/fixtures/006_badges.rb +++ b/db/fixtures/006_badges.rb @@ -415,6 +415,25 @@ Badge.seed do |b| b.system = true end +[ + [Badge::Enthusiast, "Enthusiast", BadgeType::Bronze, 10], + [Badge::Aficionado, "Aficionado", BadgeType::Silver, 100], + [Badge::Devotee, "Devotee", BadgeType::Gold, 365], +].each do |id, name, level, days| + Badge.seed do |b| + b.id = id + b.name = name + b.default_icon = "fa-eye" + b.badge_type_id = level + b.query = BadgeQueries.consecutive_visits(days) + b.badge_grouping_id = BadgeGrouping::Community + b.default_badge_grouping_id = BadgeGrouping::Community + b.trigger = Badge::Trigger::None + b.auto_revoke = false + b.system = true + end +end + Badge.where("NOT system AND id < 100").each do |badge| new_id = [Badge.maximum(:id) + 1, 100].max old_id = badge.id diff --git a/lib/badge_queries.rb b/lib/badge_queries.rb index cdb5ac8d6ab..e249f228bc9 100644 --- a/lib/badge_queries.rb +++ b/lib/badge_queries.rb @@ -232,4 +232,16 @@ SQL SQL end + def self.consecutive_visits(days) + <<~SQL + SELECT user_id, "start" + interval '1' day * COUNT(*) AS "granted_at" + FROM ( + SELECT user_id, visited_at - (DENSE_RANK() OVER (PARTITION BY user_id ORDER BY visited_at))::int "start" + FROM user_visits + ) s + GROUP BY user_id, "start" + HAVING COUNT(*) >= #{days} + SQL + end + end