From b9a7d945c3cf230037180087e18e66cfdf1d0219 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 22 Jul 2014 11:11:30 +1000 Subject: [PATCH] Improve badge grouping UI Start work on triggers --- .../admin/routes/admin_badges_route.js | 4 ++++ .../admin/templates/badges.js.handlebars | 9 ++++++++ .../javascripts/discourse/models/badge.js | 3 ++- app/controllers/admin/badges_controller.rb | 21 +++++++++++-------- app/models/badge.rb | 6 ++++++ app/serializers/badge_serializer.rb | 12 ++--------- config/locales/client.en.yml | 1 + config/routes.rb | 1 + .../20140721063820_add_trigger_to_badges.rb | 5 +++++ 9 files changed, 42 insertions(+), 20 deletions(-) create mode 100644 db/migrate/20140721063820_add_trigger_to_badges.rb diff --git a/app/assets/javascripts/admin/routes/admin_badges_route.js b/app/assets/javascripts/admin/routes/admin_badges_route.js index 1c606b557..30da3e52d 100644 --- a/app/assets/javascripts/admin/routes/admin_badges_route.js +++ b/app/assets/javascripts/admin/routes/admin_badges_route.js @@ -5,6 +5,10 @@ Discourse.AdminBadgesRoute = Discourse.Route.extend({ }, setupController: function(controller, model) { + // TODO build into findAll + Discourse.ajax('/admin/badges/groupings').then(function(json) { + controller.set('badgeGroupings', json.badge_groupings); + }); Discourse.ajax('/admin/badges/types').then(function(json) { controller.set('badgeTypes', json.badge_types); }); diff --git a/app/assets/javascripts/admin/templates/badges.js.handlebars b/app/assets/javascripts/admin/templates/badges.js.handlebars index 2d61db40b..450214c2a 100644 --- a/app/assets/javascripts/admin/templates/badges.js.handlebars +++ b/app/assets/javascripts/admin/templates/badges.js.handlebars @@ -47,6 +47,15 @@ disabled=readOnly}} +
+ + {{view Ember.Select name="badge_grouping_id" value=badge_grouping_id + content=controller.badgeGroupings + optionValuePath="content.id" + optionLabelPath="content.name" + disabled=readOnly}} +
+
{{#if controller.canEditDescription}} diff --git a/app/assets/javascripts/discourse/models/badge.js b/app/assets/javascripts/discourse/models/badge.js index 17198074b..fa0a13ff7 100644 --- a/app/assets/javascripts/discourse/models/badge.js +++ b/app/assets/javascripts/discourse/models/badge.js @@ -124,7 +124,8 @@ Discourse.Badge = Discourse.Model.extend({ multiple_grant: !!this.get('multiple_grant'), listable: !!this.get('listable'), enabled: !!this.get('enabled'), - icon: this.get('icon') + icon: this.get('icon'), + badge_grouping_id: this.get('badge_grouping_id') } }).then(function(json) { self.updateFromJson(json); diff --git a/app/controllers/admin/badges_controller.rb b/app/controllers/admin/badges_controller.rb index f85633954..d75bd66d0 100644 --- a/app/controllers/admin/badges_controller.rb +++ b/app/controllers/admin/badges_controller.rb @@ -4,6 +4,11 @@ class Admin::BadgesController < Admin::AdminController render_serialized(badge_types, BadgeTypeSerializer, root: "badge_types") end + def badge_groupings + badge_groupings = BadgeGrouping.all.to_a + render_serialized(badge_groupings, BadgeGroupingSerializer, root: "badge_groupings") + end + def create badge = Badge.new update_badge_from_params(badge) @@ -30,15 +35,13 @@ class Admin::BadgesController < Admin::AdminController end def update_badge_from_params(badge) - 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]) - badge.allow_title = params[:allow_title] - badge.multiple_grant = params[:multiple_grant] - badge.icon = params[:icon] - badge.listable = params[:listable] - badge.enabled = params[:enabled] + allowed = [:icon, :name, :description, :badge_type_id, :allow_title, :multiple_grant, :listable, :enabled, :badge_grouping_id] + params.permit(*allowed) + + allowed.each do |key| + badge.send("#{key}=" , params[key]) if params[key] + end + badge end end diff --git a/app/models/badge.rb b/app/models/badge.rb index e74dcfe15..35ac8c57c 100644 --- a/app/models/badge.rb +++ b/app/models/badge.rb @@ -17,6 +17,12 @@ class Badge < ActiveRecord::Base # other consts AutobiographerMinBioLength = 10 + module Triggers + PostAction = 1 + ReadGuidelines = 2 + PostRevision = 4 + TrustLevelChange = 8 + end module Queries diff --git a/app/serializers/badge_serializer.rb b/app/serializers/badge_serializer.rb index 25a05e4f4..60a026218 100644 --- a/app/serializers/badge_serializer.rb +++ b/app/serializers/badge_serializer.rb @@ -1,13 +1,5 @@ class BadgeSerializer < ApplicationSerializer - attributes :id, :name, :description, :grant_count, :allow_title, :multiple_grant, :icon, :listable, :enabled, :has_badge + attributes :id, :name, :description, :grant_count, :allow_title, + :multiple_grant, :icon, :listable, :enabled, :badge_grouping_id has_one :badge_type - has_one :badge_grouping - - def include_has_badge? - @options[:user_badges] - end - - def has_badge - @options[:user_badges].include?(object.id) - end end diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 3ec2303f8..584eeaaf8 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -1888,6 +1888,7 @@ en: display_name: Display Name description: Description badge_type: Badge Type + badge_grouping: Group granted_by: Granted By granted_at: Granted At save: Save diff --git a/config/routes.rb b/config/routes.rb index ec67de8bd..e96276eed 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -146,6 +146,7 @@ Discourse::Application.routes.draw do resources :badges, constraints: AdminConstraint.new do collection do get "types" => "badges#badge_types" + get "groupings" => "badges#badge_groupings" end end diff --git a/db/migrate/20140721063820_add_trigger_to_badges.rb b/db/migrate/20140721063820_add_trigger_to_badges.rb new file mode 100644 index 000000000..743b2a835 --- /dev/null +++ b/db/migrate/20140721063820_add_trigger_to_badges.rb @@ -0,0 +1,5 @@ +class AddTriggerToBadges < ActiveRecord::Migration + def change + add_column :badges, :trigger, :integer + end +end