Sha256: f7a188cc135ebdbc289587805bacc7d19c74a32a34a30a885e333081f318b2e0

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

module Recurso
  class ResourcePolicy < BasePolicy
    def method_missing(method)
      action = method.to_s.sub('?', '').to_sym
      super unless Recurso::Config.instance.levels_for_action.keys.include?(action)

      Recurso::Queries::Single.new(identity, resource, action).permission?
    end

    def resources_with_permission(relation_name, all_columns: true, include_actions: [:modify, :administer])
      include_actions.reduce(resource_query_for(relation_name, :view, all_columns: all_columns)) do |resources, action|
        resources
          .joins("LEFT OUTER JOIN(#{resource_query_for(relation_name, action).to_sql}) AS #{action} ON #{action}.id = #{relation_name}.id")
          .select("#{action}.id IS NOT NULL AS can_#{action}")
      end
    end

    private

    def resource_query_for(relation_name, action, all_columns: false)
      Recurso::Queries::Relation.new(
        identity,
        resource,
        relation_name,
        all_columns: all_columns,
        action: action
      ).resources
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
recurso-0.5.3 lib/recurso/policies/resource_policy.rb