Sha256: 19d2c5cc5a16f92ea427924aca3a191bc9e358120bc39ac850bd0ff409c7c09f

Contents?: true

Size: 1.45 KB

Versions: 2

Compression:

Stored size: 1.45 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.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) } }
          exec_arg = permissions.reduce({ }) { |a, b| a.merge(b) { |_, x, y| [x, y] } }
          names = permissions.map { |pms| pms.values.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

2 entries across 2 versions & 1 rubygems

Version Path
i_am_i_can-4.3.0 lib/i_am_i_can/permission/assignment.rb
i_am_i_can-4.2.0 lib/i_am_i_can/permission/assignment.rb