anyone-oslo/pages

View on GitHub
app/models/concerns/pages_core/has_roles.rb

Summary

Maintainability
A
0 mins
Test Coverage
A
92%
# frozen_string_literal: true

module PagesCore
  module HasRoles
    extend ActiveSupport::Concern

    def role?(role_name)
      role_names.include?(role_name.to_s)
    end
    alias has_role? role?

    def role_names
      roles.map(&:name)
    end

    def role_names=(names)
      self.roles = names.map do |name|
        if role?(name)
          roles.find_by(name:)
        else
          roles.new(name:)
        end
      end
    end
  end
end