summaryrefslogtreecommitdiff
path: root/lib/api/entities/entity_helpers.rb
blob: 3a68044ad35e9061c8bfcbbf70a89bdec16e5499 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# frozen_string_literal: true

module API
  module Entities
    module EntityHelpers
      def can_read(attr, &block)
        ->(obj, opts) { Ability.allowed?(opts[:user], "read_#{attr}".to_sym, yield(obj)) }
      end

      def can_destroy(attr, &block)
        ->(obj, opts) { Ability.allowed?(opts[:user], "destroy_#{attr}".to_sym, yield(obj)) }
      end

      def expose_restricted(attr, &block)
        expose attr, if: can_read(attr, &block)
      end
    end
  end
end