Sha256: 5e03f1f86b6c0c9982daa2a17ec447a87b6608320bd6e2881a6612ab82b71eb1

Contents?: true

Size: 1.52 KB

Versions: 39

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

module PgEngine
  class BasePolicy
    attr_reader :user, :record

    def initialize(user, record)
      @user = user
      @record = record
    end

    def index?
      base_access_to_collection?
    end

    def show?
      base_access_to_record?
    end

    def create?
      puede_crear?
    end

    def new?
      create?
    end

    def update?
      puede_editar? && !objeto_borrado?
    end

    def edit?
      update?
    end

    def destroy?
      puede_borrar? && !objeto_borrado?
    end

    def scope
      Pundit.policy_scope!(user, record.class)
    end

    class Scope
      attr_reader :user, :scope

      def initialize(user, scope)
        @user = user
        @scope = scope
      end

      def resolve
        if policy.base_access_to_collection?
          scope.all
        else
          scope.none
        end
      end

      def policy
        raise "el scope debe ser una clase modelo y no #{scope.class}" unless scope.class

        Pundit.policy!(user, scope)
      end
    end

    def puede_editar?
      base_access_to_record?
    end

    def puede_crear?
      base_access_to_collection?
    end

    def puede_borrar?
      base_access_to_record?
    end

    def export?
      base_access_to_collection?
    end

    def base_access_to_record?
      user&.developer?
    end

    def base_access_to_collection?
      user&.developer?
    end

    def objeto_borrado?
      if record.respond_to?(:kept?)
        !record.kept?
      else
        false
      end
    end
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
pg_rails-7.6.5 pg_engine/app/policies/pg_engine/base_policy.rb
pg_rails-7.6.4 pg_engine/app/policies/pg_engine/base_policy.rb
pg_rails-7.6.3 pg_engine/app/policies/pg_engine/base_policy.rb
pg_rails-7.6.2 pg_engine/app/policies/pg_engine/base_policy.rb
pg_rails-7.6.1 pg_engine/app/policies/pg_engine/base_policy.rb
pg_rails-7.6.0 pg_engine/app/policies/pg_engine/base_policy.rb
pg_rails-7.5.7 pg_engine/app/policies/pg_engine/base_policy.rb
pg_rails-7.5.6 pg_engine/app/policies/pg_engine/base_policy.rb
pg_rails-7.5.5 pg_engine/app/policies/pg_engine/base_policy.rb
pg_rails-7.5.4 pg_engine/app/policies/pg_engine/base_policy.rb
pg_rails-7.5.3 pg_engine/app/policies/pg_engine/base_policy.rb
pg_rails-7.5.2 pg_engine/app/policies/pg_engine/base_policy.rb
pg_rails-7.5.1 pg_engine/app/policies/pg_engine/base_policy.rb
pg_rails-7.5.0 pg_engine/app/policies/pg_engine/base_policy.rb
pg_rails-7.4.3 pg_engine/app/policies/pg_engine/base_policy.rb
pg_rails-7.4.2 pg_engine/app/policies/pg_engine/base_policy.rb
pg_rails-7.4.1 pg_engine/app/policies/pg_engine/base_policy.rb
pg_rails-7.4.0 pg_engine/app/policies/pg_engine/base_policy.rb
pg_rails-7.3.5 pg_engine/app/policies/pg_engine/base_policy.rb
pg_rails-7.3.4 pg_engine/app/policies/pg_engine/base_policy.rb