Sha256: a164b2ed36fa118520d8d12dbc3d1f5e212255e62e555c0a0216666ca2ea4977

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

module Consent
  # Defines a CanCan(Can)::Ability class based on a permissions hash
  class Ability
    include CanCan::Ability

    def initialize(*args, apply_defaults: true)
      @context = *args
      apply_defaults! if apply_defaults
    end

    def consent!(subject: nil, action: nil, view: nil)
      view = case view
             when Consent::View
               view
             when Symbol
               Consent.find_view(subject, action, view)
             end

      can(
        action, subject,
        view&.conditions(*@context), &view&.object_conditions(*@context)
      )
    end

    def consent(**kwargs)
      consent!(**kwargs)
    rescue Consent::ViewNotFound
      nil
    end

    private

    def apply_defaults!
      Consent.subjects.each do |subject|
        subject.actions.each do |action|
          next unless action.default_view

          consent(
            subject: subject.key,
            action: action.key,
            view: action.default_view
          )
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
consent-1.0.1 lib/consent/ability.rb