Sha256: 0fd526c7e8c052803af523d59d60d1de5d0af66490ca64c6a44e3482e333b67a

Contents?: true

Size: 1.57 KB

Versions: 12

Compression:

Stored size: 1.57 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 new_from_associable?
      new?
    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

12 entries across 12 versions & 1 rubygems

Version Path
pg_rails-7.6.17 pg_engine/app/policies/pg_engine/base_policy.rb
pg_rails-7.6.16 pg_engine/app/policies/pg_engine/base_policy.rb
pg_rails-7.6.15 pg_engine/app/policies/pg_engine/base_policy.rb
pg_rails-7.6.14 pg_engine/app/policies/pg_engine/base_policy.rb
pg_rails-7.6.13 pg_engine/app/policies/pg_engine/base_policy.rb
pg_rails-7.6.12 pg_engine/app/policies/pg_engine/base_policy.rb
pg_rails-7.6.11 pg_engine/app/policies/pg_engine/base_policy.rb
pg_rails-7.6.10 pg_engine/app/policies/pg_engine/base_policy.rb
pg_rails-7.6.9 pg_engine/app/policies/pg_engine/base_policy.rb
pg_rails-7.6.8 pg_engine/app/policies/pg_engine/base_policy.rb
pg_rails-7.6.7 pg_engine/app/policies/pg_engine/base_policy.rb
pg_rails-7.6.6 pg_engine/app/policies/pg_engine/base_policy.rb