All threads / [HELP] How configure "pundit" to display items belonging to a parent bond

Ask A Question

Notifications

You’re not receiving notifications from this thread.

[HELP] How configure "pundit" to display items belonging to a parent bond

Bruno Wego asked in Gems / Libraries

I lost hours to solve it, but I know, I am a newbie =X.

My use case is, if the user have role :agency, he can see clients. But I have to use the link between the client's agency to verify this. Take a look bellow to see my code:

class Agency < ApplicationRecord
  has_many :agency_clients
  has_many :clients, through: :agency_clients

  resourcify
end
class AgencyClient < ActiveRecord::Base
  belongs_to :agency
  belongs_to :client
end
class Client < ApplicationRecord
  has_many :agency_clients
  has_many :agencies, through: :agency_clients

  resourcify
end
class ClientPolicy < ApplicationPolicy
  def show?
    user.has_role?(:admin) || user.has_role?(:client, record)
  end

  class Scope < Scope
    def resolve
      if user.has_role? :admin
        scope.all
      elsif user.has_role? :client, :any
        scope.with_role(:client, user)
      else
        scope.none
      end
    end
  end
end

Really, please, save my day! Be a hero!

I solve the problem with the help of Chris, thank you Chris Oliver.

Take a look for the answer here.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 37,344+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.