Fix get_roles method of User model

Previous implementation was associating resources with all
the roles regardless of user role in the resource. ie:
if user is cfp member of conf1, the method was returning:
{ organizer => conf1,
  cfp => conf1,
  Info Desk => conf1,
  Volunteers Coordinator => conf1
}
This commit is contained in:
Aditya Prakash 2016-04-17 11:32:06 +05:30
parent 3c356cbaaf
commit 7e04a267cb
4 changed files with 19 additions and 6 deletions

View file

@ -274,7 +274,7 @@ module ApplicationHelper
# Outputs the roles of a user, including the conferences for which the user has the roles # Outputs the roles of a user, including the conferences for which the user has the roles
# Eg. organizer(oSC13, oSC14), cfp(oSC12, oSC13) # Eg. organizer(oSC13, oSC14), cfp(oSC12, oSC13)
def show_roles(roles) def show_roles(roles)
roles.map { |x| x[0].titleize + ' ' + x[1] }.join ', ' roles.map{ |x| x[0].titleize + ' (' + x[1].join(', ') + ')' }.join ', '
end end
def can_manage_volunteers(conference) def can_manage_volunteers(conference)

View file

@ -117,12 +117,16 @@ class User < ActiveRecord::Base
# Gets the roles of the user, groups them by role.name and returns the resource(s) of each role # Gets the roles of the user, groups them by role.name and returns the resource(s) of each role
# ====Returns # ====Returns
# * +Hash+ * -> e.g. 'organizer' => "(conf1, conf2)" # * +Hash+ * -> e.g. 'organizer' => [conf1, conf2]
def get_roles def get_roles
result = {} result = {}
Role.all.find_each do |role| roles.each do |role|
resources = self.roles.map{ |myrole| Conference.find(myrole.resource_id).short_title }.join ', ' resource = Conference.find(role.resource_id).short_title
result[role.name] = "(#{ resources })" unless resources.blank? if result[role.name].nil?
result[role.name] = [resource]
else
result[role.name] << resource
end
end end
result result
end end

View file

@ -1 +0,0 @@
Lorem ipsum dolorem...

View file

@ -0,0 +1,10 @@
require 'spec_helper'
describe ApplicationHelper, type: :helper do
describe 'show_roles' do
it 'formats the hash passed' do
roles = { 'organizer' => ['oSC16', 'oSC15'], 'cfp' => ['oSC16'] }
expect(show_roles(roles)).to eq 'Organizer (oSC16, oSC15), Cfp (oSC16)'
end
end
end