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

Issue with conditionals when extending base Serializable::Resource class directly in 0.2.1 #80

Open
richmolj opened this issue Aug 6, 2017 · 7 comments

Comments

@richmolj
Copy link
Contributor

richmolj commented Aug 6, 2017

I believe this commit, which changed from prepend to extend, broke the conditional support. I noticed this when trying to upgrade jsonapi_compliable - my if procs no longer get called, which broke extra_fields support.

The expectation is that I will get this overridden #requested_attributes, but debugging the code I see this is never called. The original #requested_attributes method is called.

I'm not sure how specs are passing, because this script mimics the same pattern and illustrates the problem:

class Foo
  def bar
    'original bar'
  end
end

module Mix
  def self.extended(klass)
    klass.class_eval do
      include InstanceMethods
    end
  end

  module InstanceMethods
    def bar
      'overridden bar'
    end
  end
end
Foo.extend Mix
puts Foo.new.bar #=> 'original'
@beauby
Copy link
Member

beauby commented Aug 6, 2017

Yeah you shouldn't extend the Serializable::Resource class itself directly but a subclass.

@richmolj
Copy link
Contributor Author

richmolj commented Aug 7, 2017

How is that supposed to work for something like jsonapi_suite? I have existing applications with ~100 serializers inheriting from Serializable::Resource, do I now have to add another serializer class to jsonapi_suite and switch everything to inherit from that?

@beauby
Copy link
Member

beauby commented Aug 7, 2017

Hmm I wasn't aware that you were customizing the base class. Would you mind issuing a PR re-modifying extensions to use prepend instead?

@richmolj
Copy link
Contributor Author

richmolj commented Aug 7, 2017

Would that be more or less reverting e95fd7e?

@richmolj
Copy link
Contributor Author

richmolj commented Aug 9, 2017

@beauby not wild about it, but I solved this by extending after inheriting:

JSONAPI::Serializable::Resource.class_eval do
  def self.inherited(klass)
    super
    klass.class_eval do
      extend JSONAPI::Serializable::Resource::ConditionalFields
    end
  end
end

here. Not wild about it but works. Up to you if you want to keep this pattern or move back to prepend

@beauby
Copy link
Member

beauby commented Aug 9, 2017 via email

daniel-sullivan added a commit to daniel-sullivan/jsonapi-serializable that referenced this issue Aug 9, 2017
@daniel-sullivan
Copy link

daniel-sullivan commented Aug 9, 2017

I've just put through a PR #81 if anyone's interested which keeps the extend in the serializer (base) class but moves back to using prepend for the inclusion of the InstanceMethods in the module_eval fixing the JSONAPI::Serializable::Resource precedence issue.

Tests seem to pass OK (I don't really have time at the moment to go through and check there's full coverage). Using this fix successfully under the example of (this is part of a jsonapi_suite based project):

class SerializableCompany < JSONAPI::Serializable::Resource
  extend JSONAPI::Serializable::Resource::ConditionalFields

  type :companies

  attributes :name, :phone

  attribute :address, unless: -> { @object.address.private? }

  link :self do
      @url_helpers.company_url(@object.id)
  end
end

daniel-sullivan added a commit to daniel-sullivan/jsonapi-serializable that referenced this issue Aug 9, 2017
@beauby beauby changed the title Conditionals No Longer Work in 0.2.1 Issue with conditionals when extending base Serializable::Resource class directly in 0.2.1 Sep 11, 2017
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

3 participants