1

Say I have a User model that has a username and a full_name. I want to render a user object using Active Model Serializer and only render specific parameters based on if the current_user is nil or logged in. Since I'm using pundit as well, I was wondering if there was a way to do this using pundit.

e.g.

user is nil

{id:1, username: 'foo'}

user is logged in

{id:1, username: 'foo', full_name: 'bar'}
Derek
  • 11,980
  • 26
  • 103
  • 162

1 Answers1

2

Why not just check if current_user is nil in your User serializer. Not sure what version you are on but you can do this with the 0.9 version

class UserSerializer < ActiveModel::Serializer
  attributes :id, :username, :full_name

  def filter(keys)
    if scope.blank? #current_user is not logged in
      keys - [:full_name]
    else
      keys
    end
  end
end
dasnixon
  • 988
  • 7
  • 18