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