Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Serializer like ActiveModel Serializer #249

Closed
stephenbaidu opened this issue May 21, 2015 · 15 comments
Closed

Custom Serializer like ActiveModel Serializer #249

stephenbaidu opened this issue May 21, 2015 · 15 comments

Comments

@stephenbaidu
Copy link

Is there some easy way of forcing my custom serializer on the object I get at $auth.user? Apart from overriding the controller that is rendering and specifying the active model serializer class to use?.

@rentziass
Copy link

Hi @stephenbaidu , did you find a way to do it? I'm looking for the same thing.

@rentziass
Copy link

FYI I only managed to do it by overriding the controllers, didn't find another way for now.

@stephenbaidu
Copy link
Author

@rentziass Still searching since I do not want to override the controller.

@DeanSolecki
Copy link

@stephenbaidu, don't suppose you have any tips on that hack? I'm looking into the overrides right now and not seeing much help anywhere, docs or otherwise. A working example would be invaluable. :)

@rentziass
Copy link

@DeanSolecki I managed to get the response in the format I needed by overriding this:

def token_validation_response
self.as_json(except: [
:tokens, :created_at, :updated_at
])
end

That's just "to_jsoning" the resource, aka User, excluding some of the attributes. Nothing prevents you from replacing those lines with a serializer of yours.

def token_validation_response
  YourUserSerializer.new( self, root: false )
end

@DeanSolecki
Copy link

@rentziass Thanks! This is exactly what I was looking for. :)

@rentziass
Copy link

@DeanSolecki No problem, I'm glad it helped, I spent hours trying to figure this out :)

@stephenbaidu
Copy link
Author

@rentziass Brilliant and thanks.

@AlexVKO
Copy link

AlexVKO commented Sep 22, 2015

I wrote it in User model, but it doest work, am i doing something wrong?

class User < ActiveRecord::Base
  include DeviseTokenAuth::Concerns::User
  # Include default devise modules.
  devise :database_authenticatable, :registerable,
          :recoverable, :rememberable, :trackable, :validatable,
          :omniauthable
  # Exclude: confirmable

  validates_presence_of :name

  def token_validation_response
    UserSerializer.new( self, root: false )
  end
end

@juddey
Copy link
Contributor

juddey commented Oct 4, 2015

@AlexVKO I had success with:
UserSerializer.new( self, root: false ).as_json

@AlexVKO
Copy link

AlexVKO commented Oct 9, 2015

Thank you @juddey

@AlexVKO
Copy link

AlexVKO commented Mar 23, 2016

Does it still works? I can't make it work on my api....
rails 5 api, active model serializer 0.10
========= UPDATE ========
nevermind, I got it (Overriding rendering methods)... sorry... I think this issue can be closed.

@brentonmorris
Copy link

Please close this. This is the preferred solution: https://github.com/lynndylanhurley/devise_token_auth#overriding-rendering-methods

@booleanbetrayal
Copy link
Collaborator

agree

@rmcsharry
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants