Sha256: 385777334a0db8bcd584991961cf64c970fd88a2f5498b4864bdb1ec1103070d

Contents?: true

Size: 1.35 KB

Versions: 19

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

module Apicasso
  # Ability to parse a scope object from Apicasso::Key
  class Ability
    include CanCan::Ability

    def initialize(key)
      key ||= Apicasso::Key.new
      cannot :manage, :all
      cannot :read, :all
      key.scope.each do |permission, klasses_clearances|
        klasses_clearances.each do |klasses|
          klasses.each do |klass, clearance|
            if clearance == true
              # Usage:
              # To have a key reading all channels and all accouts
              # you would have a scope:
              # => `{read: [{channel: true}, {accout: true}]}`
              can permission.to_sym, klass.underscore.to_sym
              can permission.to_sym, klass.classify.constantize
            elsif clearance.class == Hash
              # Usage:
              # To have a key reading all banners from a channel with id 999
              # you would have a scope:
              # => `{read: [{banner: {owner_id: [999]}}]}`
              can permission.to_sym,
                  klass.underscore.to_sym
              clearance.each do |by_field, values|
                can permission.to_sym,
                    klass.classify.constantize,
                    by_field => values
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
apicasso-0.2.14 app/models/apicasso/ability.rb
apicasso-0.2.13 app/models/apicasso/ability.rb
apicasso-0.2.12 app/models/apicasso/ability.rb
apicasso-0.2.11 app/models/apicasso/ability.rb
apicasso-0.2.10 app/models/apicasso/ability.rb
apicasso-0.2.9 app/models/apicasso/ability.rb
apicasso-0.2.8 app/models/apicasso/ability.rb
apicasso-0.2.7 app/models/apicasso/ability.rb
apicasso-0.2.6 app/models/apicasso/ability.rb
apicasso-0.2.5 app/models/apicasso/ability.rb
apicasso-0.2.4 app/models/apicasso/ability.rb
apicasso-0.2.3 app/models/apicasso/ability.rb
apicasso-0.2.2 app/models/apicasso/ability.rb
apicasso-0.2.1 app/models/apicasso/ability.rb
apicasso-0.2.0 app/models/apicasso/ability.rb
apicasso-0.1.6 app/models/apicasso/ability.rb
apicasso-0.1.2 app/models/apicasso/ability.rb
apicasso-0.1.1 app/models/apicasso/ability.rb
apicasso-0.1.0 app/models/apicasso/ability.rb