Sha256: 2d62a7b0270081d2c1f23bb043ae6d419c034d3d5228e8ec580281124afea613

Contents?: true

Size: 723 Bytes

Versions: 1

Compression:

Stored size: 723 Bytes

Contents

module IAmICan
  module Resource
    extend ActiveSupport::Concern

    class_methods do
      # Book.that_allow(User.all).to(:read)
      # Book.that_allow(User.last).to(:write)
      def that_allow(subject)
        ThatAllow.new(self, subject)
      end
    end
  end

  class ThatAllow
    attr_accessor :records, :subject

    def initialize(records, subject)
      self.records = records
      self.subject = subject
    end

    def to(pred)
      roles = Configs.take.subject_model._roles
      permissions = Configs.take.role_model._permissions
      allowed_ids = subject.send(roles).send(permissions).where(pred: pred, obj_type: records.name).pluck(:obj_id)
      records.where(id: allowed_ids)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
i_am_i_can-3.0.0 lib/i_am_i_can/resource.rb