Sha256: 18202bcf664ba9dcd27d0cfb346570c87ce6bc2e331402e2a08a720999be0fec

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

module IAmICan
  module Permission
    module Assignment
      def can *actions, resource: nil, obj: resource,
              _d: i_am_i_can.auto_definition, auto_definition: _d
        self.class.have_permissions *actions, obj: obj if auto_definition
        run_callbacks(:permission_assign) do
          _permissions_assignment(actions, obj)
        end
      end

      alias has_permission can

      def cannot *actions, resource: nil, obj: resource
        run_callbacks(:cancel_permission_assign) do
          _permissions_assignment(:cancel, actions, obj)
        end
      end

      alias is_not_allowed_to cannot

      def can_only *actions, resource: nil, obj: resource
        run_callbacks(:permission_update) do
          _permissions_assignment(:replace, actions, obj)
        end
      end

      def _permissions_assignment(exec = :assign, actions, obj)
        if actions.flatten!.first.is_a?(i_am_i_can.permission_model)
          exec_arg, names = actions, actions.map(&:name)
        else
          objs = obj ? Array(obj) : [nil]
          permissions = actions.product(objs).map { |(p, o)| { action: p, **deconstruct_obj(o) }.values }
          exec_arg = { action: permissions.map(&:first), obj_type: permissions.map { |v| v[1] }, obj_id: permissions.map(&:last) }
          names = permissions.map { |pms| pms.compact.join('_').to_sym }
        end

        assignment = _stored_permissions_exec(exec, exec_arg)
        ResultOf.permission assignment, i_am_i_can, given: [[], names]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
i_am_i_can-4.3.1 lib/i_am_i_can/permission/assignment.rb