Sha256: 5561c5b4ce156887a8a3c89727690b64a7997b3f13de0b038df6caff05d23b84

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

module Underworld
  module Discovery
    # This class is responsible to discoering all Permissions
    # classes and provide a list of available permissions
    class Permissions

      # Run the given block for each object with permissions
      def self.permission_objects(&block)
        objects = []

        # Walkthrough all models in application by using
        # model_discovery gem and check for `permission_strings`
        # method in models (If `Underworld::Permissions` included in
        # model it has the permission_strings). And concat the
        # permissions to `@permissions` instance method.
        ::ApplicationModels.to_adapter.find_all.each do |m|
          # `m` is a string contains name of a model and
          # we use contstantize to get the model class with
          # such name
          model = m.model.constantize
          if model.respond_to? :permission_strings
            block.call(model)
          end
        end
      end

      def self.all_permissions
        permissions = []
        permission_objects do |object|
          permissions.concat(object.permission_strings(object))
        end

        permissions.uniq
      end

      # Creating all possible permissions in database using
      # `Underworld::Permission` model
      def self.create_all_permissions

        permission_objects do |object|
          object.possible_permissions.each do |perm|
            Underworld::Permission.where(model: object.to_s,
                                     permission_type: perm).first_or_create
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
underworld-1.0.0 lib/underworld/discovery/permissions.rb