gimmyxd/mentors4me-api

View on GitHub
app/abilities/ability.rb

Summary

Maintainability
A
0 mins
Test Coverage
# frozen_string_literal: true

class Ability
  include CanCan::Ability

  def self.build_ability_for(user)
    user ||= User.new
    ability = GuestFactory.new(user)
    begin
      klasses = user.roles.pluck(:slug)
                    .map { |role| "#{role}_factory" }
                    .map(&:classify)
                    .map(&:constantize)
      klasses.each do |klass|
        ability = ability.merge(klass.new(user))
      end
    rescue NameError => e
      Rails.logger e.message
    end
    ability
  end
end