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

Delegate + aliases not working as expected #102

Open
danielnc opened this issue Jun 24, 2021 · 1 comment
Open

Delegate + aliases not working as expected #102

danielnc opened this issue Jun 24, 2021 · 1 comment

Comments

@danielnc
Copy link

danielnc commented Jun 24, 2021

I have a AR model that has a db column called state, and I want to transform it before serializing the data. That method that transforms data lives on the AR model

When delegating the method to the AR object + aliasing it, panko is not working the way I would expect. it's returning the actual persisted database value for object#state

class StateSerializer  < Panko::Serializer
  attributes :state

  aliases friendly_state: :state
  delegate :friendly_state, to: :object
end
class ActivityLog < ApplicationRecord
   def friendly_state
    case state
    when "error"
      "User Error"
    else
      state
    end
  end
end

I can accomplish what I want by doing but I think it would be a better approach to be able to rely on aliases + method delegation

class StateSerializer  < Panko::Serializer
  attributes :state

  def state
	object.friendly_state
  end
end
@yosiat
Copy link
Owner

yosiat commented Sep 21, 2021

@danielnc Thanks for reporting this issue!

I see what you say and totally agree, this happens because aliases works on attributes and not on methods.
Since serializer is not strict to specific model (as of today), I can't know if "friendly_state" is a method on a model and then alias to it's method.

Making serializers strongly typed, is something I want to achieve in the future but not now.

I'll try to think about an options how to solve this without complicating the library.

If you want to submit PR and need help with it, I'll be gladly guide you (you can join the Slack group)

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

No branches or pull requests

2 participants