From 7ab9ee86794fe72b6f570f79e83d6c4177a950c2 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Mon, 9 Nov 2015 13:21:52 -0300 Subject: [PATCH] Display badges separated by the owner in dashboard --- lib/gamification_plugin/dashboard_helper.rb | 12 ++++++++++++ test/unit/dashboard_helper_test.rb | 25 +++++++++++++++++++++++++ views/gamification/dashboard.html.erb | 36 +++++++++++++++++++----------------- 3 files changed, 56 insertions(+), 17 deletions(-) create mode 100644 test/unit/dashboard_helper_test.rb diff --git a/lib/gamification_plugin/dashboard_helper.rb b/lib/gamification_plugin/dashboard_helper.rb index 7da1c27..93d7c99 100644 --- a/lib/gamification_plugin/dashboard_helper.rb +++ b/lib/gamification_plugin/dashboard_helper.rb @@ -35,4 +35,16 @@ module GamificationPlugin::DashboardHelper (context_ranking.blank? ? '' : render(:partial => 'gamification/ranking', :locals => {:ranking => context_ranking, :target_ranking => target_ranking, :ranking_class => 'context'})) end + def badges_title(owner) + return _('Badges for %s' % owner.name) if owner.kind_of?(Organization) + _('Badges') + end + + def grouped_badges + environment.gamification_plugin_badges.all.group_by(&:owner).sort do |a, b| + return -1 if a.first.kind_of?(Environment) + a.first.name <=> b.first.name + end + end + end diff --git a/test/unit/dashboard_helper_test.rb b/test/unit/dashboard_helper_test.rb new file mode 100644 index 0000000..64b3034 --- /dev/null +++ b/test/unit/dashboard_helper_test.rb @@ -0,0 +1,25 @@ +require_relative "../test_helper" + +class DashboardHelperTest < ActiveSupport::TestCase + + include GamificationPlugin::DashboardHelper + + should 'return title for global badges' do + owner = Environment.new + assert_equal 'Badges', badges_title(owner) + end + + should 'return title for organization badges' do + owner = Organization.new(:name => 'organization') + assert_equal 'Badges for organization', badges_title(owner) + end + + should 'return badges grouped by owner' do + environment = Environment.default + expects(:environment).at_least_once.returns(environment) + badge1 = GamificationPlugin::Badge.create!(:owner => fast_create(Organization)) + badge2 = GamificationPlugin::Badge.create!(:owner => environment) + assert_equal [[badge2.owner, [badge2]], [badge1.owner, [badge1]]], grouped_badges + end + +end diff --git a/views/gamification/dashboard.html.erb b/views/gamification/dashboard.html.erb index f455ec7..f88d004 100644 --- a/views/gamification/dashboard.html.erb +++ b/views/gamification/dashboard.html.erb @@ -34,24 +34,26 @@ <% unless environment.gamification_plugin_badges.empty? %>
-

<%= _('Badges') %>

-
<% end %> -- libgit2 0.21.2