Roles into their own controller. Rework interface. Add flash messages to ajax calls. Add description to roles. Possible to edit roles in use.

This commit is contained in:
Stella 2015-01-12 23:13:10 +02:00 committed by Stella Rouzi
parent eda5c2bc07
commit ccd7ecbb90
59 changed files with 618 additions and 356 deletions

View file

@ -19,15 +19,11 @@ class Ability
# This is what sets up the different abilities
if user.new_record?
not_signed_in
# Checks if the user does not have any role and is not an admin
elsif user.roles.any? || user.is_admin
signed_in_with_roles(user)
else
# This maps the actionables name of the role to its name in the DB.
roles = Role::ACTIONABLES.map {|i| i.parameterize.underscore}
# Checks if the user does not have any role and is not an admin
if (user.roles.pluck(:name) & roles).empty? && !user.is_admin
signed_in(user)
else
signed_in_with_roles(user)
end
signed_in(user)
end
end
@ -151,6 +147,19 @@ class Ability
can :manage, Ticket, conference_id: conf_ids_for_organizer
can :index, Comment, commentable_type: 'Event',
commentable_id: Event.where(program_id: Program.where(conference_id: conf_ids_for_organizer).pluck(:id)).pluck(:id)
# Abilities for Role (Conference resource)
can :index, Role
can :manage, Role do |role|
role.resource_type == 'Conference' && (conf_ids_for_organizer.include? role.resource_id)
end
# Can add or remove users from role, when user has that same role for the conference
# Eg. If you are member of the CfP team, you can add more CfP team members (add users to the role 'CfP')
can :toggle_user, Role do |role|
role.resource_type == 'Conference' &&
(Conference.with_role(role.name.parameterize.underscore.to_sym, user).pluck(:id).include? role.resource_id)
end
end
def signed_in_with_cfp_role(user)
@ -172,6 +181,20 @@ class Ability
commercialable_id: Event.where(program_id: Program.where(conference_id: conf_ids_for_cfp).pluck(:id)).pluck(:id)
can :index, Comment, commentable_type: 'Event',
commentable_id: Event.where(program_id: Program.where(conference_id: conf_ids_for_cfp).pluck(:id)).pluck(:id)
# Abilities for Role (Conference resource)
can :index, Role
can :manage, Role do |role|
role.resource_type == 'Conference' && (conf_ids_for_cfp.include? role.resource_id)
end
# Can add or remove users from role, when user has that same role for the conference
# Eg. If you are member of the CfP team, you can add more CfP team members (add users to the role 'CfP')
can :toggle_user, Role do |role|
role.resource_type == 'Conference' &&
(Conference.with_role(role.name.parameterize.underscore.to_sym, user).pluck(:id).include? role.resource_id)
end
end
def signed_in_with_info_desk_role(user)
@ -185,6 +208,19 @@ class Ability
can :manage, Question do |question|
!(question.conferences.pluck(:id) & conf_ids_for_info_desk).empty?
end
# Abilities for Role (Conference resource)
can :index, Role
can :manage, Role do |role|
role.resource_type == 'Conference' && (conf_ids_for_info_desk.include? role.resource_id)
end
# Can add or remove users from role, when user has that same role for the conference
# Eg. If you are member of the CfP team, you can add more CfP team members (add users to the role 'CfP')
can :toggle_user, Role do |role|
role.resource_type == 'Conference' &&
(Conference.with_role(role.name.parameterize.underscore.to_sym, user).pluck(:id).include? role.resource_id)
end
end
def signed_in_with_volunteers_coordinator_role(user)
@ -195,5 +231,18 @@ class Ability
can :manage, Vposition, conference_id: conf_ids_for_volunteers_coordinator
can :manage, Vday, conference_id: conf_ids_for_volunteers_coordinator
# Abilities for Role (Conference resource)
can :index, Role
can :manage, Role do |role|
role.resource_type == 'Conference' && (conf_ids_for_volunteers_coordinator.include? role.resource_id)
end
# Can add or remove users from role, when user has that same role for the conference
# Eg. If you are member of the CfP team, you can add more CfP team members (add users to the role 'CfP')
can :toggle_user, Role do |role|
role.resource_type == 'Conference' &&
(Conference.with_role(role.name.parameterize.underscore.to_sym, user).pluck(:id).include? role.resource_id)
end
end
end

View file

@ -3,7 +3,8 @@
class Conference < ActiveRecord::Base
require 'uri'
serialize :events_per_week, Hash
resourcify # Needed to call 'Conference.with_role' in /models/ability.rb
# Needed to call 'Conference.with_role' in /models/ability.rb
resourcify
default_scope { order('start_date DESC') }
@ -545,6 +546,18 @@ class Conference < ActiveRecord::Base
after_create do
self.create_contact
self.create_program
create_roles
end
##
# Creates the roles of the conference
# after the conference has been successfully created
# Will create 4 new records for roles
def create_roles
Role.where(name: 'organizer', resource: self).first_or_create(description: 'For the organizers of the conference (who shall have full access)')
Role.where(name: 'cfp', resource: self).first_or_create(description: 'For the members of the CfP team')
Role.where(name: 'info_desk', resource: self).first_or_create(description: 'For the members of the Info Desk team')
Role.where(name: 'volunteers_coordinator', resource: self).first_or_create(description: 'For the people in charge of volunteers')
end
##

View file

@ -1,9 +1,10 @@
class Role < ActiveRecord::Base
has_and_belongs_to_many :users
belongs_to :resource, polymorphic: true
has_and_belongs_to_many :users
scopify
LABELS = ['Attendee', 'Volunteer', 'Speaker', 'Sponsor', 'Press', 'Keynote Speaker']
ACTIONABLES = ['Organizer', 'CfP', 'Info Desk', 'Volunteers Coordinator']
validates :name, presence: true
validates :name, uniqueness: { scope: :resource }
end

View file

@ -128,9 +128,9 @@ class User < ActiveRecord::Base
# * +Hash+ * -> e.g. 'organizer' => "(conf1, conf2)"
def get_roles
result = {}
Role::ACTIONABLES.each do |role|
resources = self.roles.where(name: role.parameterize.underscore).map{ |myrole| Conference.find(myrole.resource_id).short_title }.join ', '
result[role.parameterize.underscore] = "(#{ resources })" unless resources.blank?
Role.all.find_each do |role|
resources = self.roles.map{ |myrole| Conference.find(myrole.resource_id).short_title }.join ', '
result[role.name] = "(#{ resources })" unless resources.blank?
end
result
end