FEATURE: Allow admins to disable specific badges

This commit is contained in:
Sam 2014-07-14 17:40:01 +10:00
parent 49dbded250
commit 88469721b9
12 changed files with 35 additions and 17 deletions

View file

@ -77,13 +77,20 @@
</span>
</div>
{{#unless readOnly}}
<div class='buttons'>
<button {{action save}} {{bind-attr disabled=controller.disableSave}} class='btn btn-primary'>{{i18n admin.badges.save}}</button>
<span class='saving'>{{savingStatus}}</span>
<div>
<span>
{{input type="checkbox" checked=enabled}}
{{i18n admin.badges.enabled}}
</span>
</div>
<div class='buttons'>
<button {{action save}} {{bind-attr disabled=controller.disableSave}} class='btn btn-primary'>{{i18n admin.badges.save}}</button>
<span class='saving'>{{savingStatus}}</span>
{{#unless readOnly}}
<a {{action destroy}} class='delete-link'>{{i18n admin.badges.delete}}</a>
</div>
{{/unless}}
{{/unless}}
</div>
</form>
</div>

View file

@ -118,6 +118,7 @@ Discourse.Badge = Discourse.Model.extend({
allow_title: !!this.get('allow_title'),
multiple_grant: !!this.get('multiple_grant'),
listable: !!this.get('listable'),
enabled: !!this.get('enabled'),
icon: this.get('icon')
}
}).then(function(json) {

View file

@ -30,7 +30,7 @@ class Admin::BadgesController < Admin::AdminController
end
def update_badge_from_params(badge)
params.permit(:name, :description, :badge_type_id, :allow_title, :multiple_grant, :listable)
params.permit(:name, :description, :badge_type_id, :allow_title, :multiple_grant, :listable, :enabled)
badge.name = params[:name]
badge.description = params[:description]
badge.badge_type = BadgeType.find(params[:badge_type_id])
@ -38,6 +38,7 @@ class Admin::BadgesController < Admin::AdminController
badge.multiple_grant = params[:multiple_grant]
badge.icon = params[:icon]
badge.listable = params[:listable]
badge.enabled = params[:enabled]
badge
end
end

View file

@ -3,7 +3,7 @@ class BadgesController < ApplicationController
def index
badges = Badge.all
badges = badges.where(listable: true) if(params[:only_listable] == "true") || !request.xhr?
badges = badges.where(enabled: true, listable: true) if(params[:only_listable] == "true") || !request.xhr?
badges = badges.to_a
serialized = MultiJson.dump(serialize_data(badges, BadgeSerializer, root: "badges"))
respond_to do |format|
@ -17,7 +17,7 @@ class BadgesController < ApplicationController
def show
params.require(:id)
badge = Badge.find(params[:id])
badge = Badge.enabled.find(params[:id])
if current_user
user_badge = UserBadge.find_by(user_id: current_user.id, badge_id: badge.id)

View file

@ -17,7 +17,8 @@ class UserBadgesController < ApplicationController
user_badges = user_badges.includes(:user, :granted_by, badge: :badge_type, post: :topic)
if params[:grouped]
user_badges = user_badges.group(:badge_id).select(UserBadge.attribute_names.map {|x| "MAX(#{x}) as #{x}" }, 'COUNT(*) as count')
user_badges = user_badges.group(:badge_id)
.select(UserBadge.attribute_names.map {|x| "MAX(#{x}) as #{x}" }, 'COUNT(*) as count')
end
render_serialized(user_badges, UserBadgeSerializer, root: "user_badges")
@ -60,9 +61,9 @@ class UserBadgesController < ApplicationController
params.permit(:badge_name)
if params[:badge_name].nil?
params.require(:badge_id)
badge = Badge.find_by(id: params[:badge_id])
badge = Badge.find_by(id: params[:badge_id], enabled: true)
else
badge = Badge.find_by(name: params[:badge_name])
badge = Badge.find_by(name: params[:badge_name], enabled: true)
end
raise Discourse::NotFound.new if badge.blank?

View file

@ -134,6 +134,8 @@ SQL
validates :allow_title, inclusion: [true, false]
validates :multiple_grant, inclusion: [true, false]
scope :enabled, ->{ where(enabled: true) }
def self.trust_level_badge_ids
(1..4).to_a

View file

@ -23,7 +23,7 @@ class User < ActiveRecord::Base
has_many :user_open_ids, dependent: :destroy
has_many :user_actions, dependent: :destroy
has_many :post_actions, dependent: :destroy
has_many :user_badges, dependent: :destroy
has_many :user_badges, -> {where('user_badges.badge_id IN (SELECT id FROM badges where enabled)')}, dependent: :destroy
has_many :badges, through: :user_badges
has_many :email_logs, dependent: :destroy
has_many :post_timings

View file

@ -1,5 +1,5 @@
class BadgeSerializer < ApplicationSerializer
attributes :id, :name, :description, :grant_count, :allow_title, :multiple_grant, :icon, :listable
attributes :id, :name, :description, :grant_count, :allow_title, :multiple_grant, :icon, :listable, :enabled
has_one :badge_type
end

View file

@ -62,7 +62,7 @@ class BadgeGranter
end
def self.backfill(badge)
return unless badge.query.present?
return unless badge.query.present? && badge.enabled
post_clause = badge.target_posts ? "AND q.post_id = ub.post_id" : ""
post_id_field = badge.target_posts ? "q.post_id" : "NULL"

View file

@ -1892,6 +1892,7 @@ en:
allow_title: Allow badge to be used as a title
multiple_grant: Can be granted multiple times
listable: Show badge on the public badges page
enabled: Enable badge
icon: Icon
lightbox:

View file

@ -0,0 +1,5 @@
class AddEnabledToBadges < ActiveRecord::Migration
def change
add_column :badges, :enabled, :boolean, default: true, null: false
end
end

View file

@ -35,12 +35,12 @@ describe Admin::BadgesController do
context '.update' do
it 'returns success' do
xhr :put, :update, id: badge.id, name: "123456", badge_type_id: badge.badge_type_id, allow_title: false, multiple_grant: false
xhr :put, :update, id: badge.id, name: "123456", badge_type_id: badge.badge_type_id, allow_title: false, multiple_grant: false, enabled: true
response.should be_success
end
it 'updates the badge' do
xhr :put, :update, id: badge.id, name: "123456", badge_type_id: badge.badge_type_id, allow_title: false, multiple_grant: true
xhr :put, :update, id: badge.id, name: "123456", badge_type_id: badge.badge_type_id, allow_title: false, multiple_grant: true, enabled: true
badge.reload.name.should eq('123456')
end
end