Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Joe Gaudet
    @joegaudet
    No worries at all, let me know if there’s anything I can do to help with the serializer stuff, I’ve got some downtime would love to get on v10
    The perf benefits are considerable. :)
    Keith Gould
    @keithmgould
    Hi guys, I'm looking for examples (or docs) on how to add functionality to a controller. basic stuff, I'm sure I'm missing it in the main docs. For example, lets say that during the creation of a model (User), we want to perform some functionality in the controller. I don't think that happens in the Resource. If it happens in the controller, I don't understand how to pass the user object back to the resource before the response is created.
    Actually I'm not even sure if thats the control flow (between controllers and resources). I sort of think its not, but I don't see info on how the flow works. For example, if the controller action calls the resource for rendering the response, I don't see how to manually do that in the controller, passing my newly created user object...
    the ref I've been using is: https://jsonapi-resources.com/v0.10/guide/
    Keith Gould
    @keithmgould
    (I also looked at the example app peeps and it had totally empty controllers so that did not offer insight
    Scott González
    @scottgonzalez
    @keithmgould You won’t really use the controller for anything. When you say “we want to perform some functionality in the controller” what do you actually want to do?
    Keith Gould
    @keithmgould
    @scottgonzalez for every example I can come up with I know there are alternatives, so its not any specific thing. But to name a few: send off an email (yes this can be done in a call back in the model, but lets say I want to do it in the controller). Or lets say I want to update some second model when I create a primary model. Again, I know I can do these in the model, but its less about specific examples and more about a general understanding of how to perform actions in the controller, when using jsonapi-resources.
    does that help?
    Scott González
    @scottgonzalez
    @keithmgould You wouldn’t perform actions in the controller. You would perform them in the resource.
    Keith Gould
    @keithmgould
    gotcha. thank you @scottgonzalez
    Keith Gould
    @keithmgould
    Hi guys back with another question. I have a model that has a standard integer primary key, and also a UUID. I'd like to use the standard primary key internally, but have the API use UUID as the id. Wondering how best to do that wth JR. thank you!
    ( I looked at this section already: https://jsonapi-resources.com/v0.10/guide/resources.html#Primary-Key) but that is more for telling JR when the PK is a UUID. In my case the PK inside the app still is an integer, and only externally I'd like to use a UUID.
    Scott González
    @scottgonzalez
    @keithmgould Take a look at the finders: https://jsonapi-resources.com/v0.10/guide/resources.html#Finders
    That may not be fully up to date for 0.10 though.
    I think this covers all the methods you would care about: https://gist.github.com/scottgonzalez/b84343cb4cf14c4639c11294c732ec67
    There may be some abstractions in there that you won’t care about.
    Scott González
    @scottgonzalez
    Oh, actually, this is probably not at all what you want now that I’m thinking about it.
    Because you’re only going to care about the find operation for IDs coming in on that resource. But then you’re also going to have to deal with any relationship data for related resources.
    The responses should be easy to handle by just overriding the id method on the resource.
    But I’m not sure that would make relationship data work correctly.
    Keith Gould
    @keithmgould
    Hey @scottgonzalez I meant to mention this off the bat but the first thing I tried was overriding the id method in the resource, but that caused a spooky silent error where the data object came back empty. Have you ever tried? I'm guessing its easy to reproduce.
    I'll be caring both about finding via UUID, and also sending down those UUIDs, so I'll def look into the finder stuff you sent, but I'm still at a loss as to how to send down UUIDs (as the ID), since I can't override the id method (as mentioned).
    Keith Gould
    @keithmgould
    Thank you for the help, btw, both yesterday and today. Are you a core dev of JR?
    Scott González
    @scottgonzalez
    @keithmgould Well, not quite. I’m a committer, but very shortly after getting commit access, there were big changes at work and I haven’t had much time to work on it.
    But I know a decent amount about how it works, so I try to at least help out here when I can since it’s a much smaller commitment.
    I haven’t tried overriding the id method before. I’d need to do quite a big of digging to see everything it would affect.
    Out of curiosity, why don’t you just change that model to use the UUID as the actual primary key?
    Another approach that might work out pretty well would be to create another model that just wraps the original model, but uses the UUID as the primary key.
    You’d probably only need to override a few methods, like self.find, id, id=, etc.
    Keith Gould
    @keithmgould
    Yeah, I could do either of those. To answer your question though, I don't like making the UUID a primary key because it does not feel beneficial outside the world of the API. Internally (inside the app), integers are just fine, and fast, and small, and easy for a human dev to see and type when debugging. These are a lot of perks to an int PK. And again no benefits inside the app for UUIDs. So where I've landed architecturally over the years is to have internal ints, and an external UUID, because to surface an integer PK to the world is not good for a few reasons.
    I'd rather not write a wrapper, it feels like thats a lot make JR happy. But I'll go with that for now unless you find a way to make it work with overriding id, which I'd prefer.
    Keith Gould
    @keithmgould
    This message was deleted
    Keith Gould
    @keithmgould
    Also, are there examples I can find where JR sits in front of a service layer? Like lets say I have a service in the app that accepts user info from a client, does a few things, and returns something to the client. I don't want this behavior to be defined in a single model, since the behavior spans multiple models. So traditionally I create a service in the app that handles this. Can JR reference this service? What methods would need to be defined in the service? Are there examples?
    Keith Gould
    @keithmgould
    (Also the Serialize portion of Documentation is out of date, I think. there is no longer a serialize_to_hash method.
    Keith Gould
    @keithmgould
    I imagine its something like this now but what I have is not working:
        survey = Survey.first
        resource = SurveyResource.new(survey, nil)
        serializer = JSONAPI::ResourceSerializer.new(SurveyResource)
        serializer_set = JSONAPI::ResourceSet.new
        serializer_set.populate!(serializer, nil, {})
        foo = serializer.serialize_resource_set_to_hash_single(serializer_set)
        render json: foo
    The above is what I'm thinking about doing to address the service layer question...
    Lele Forzani
    @wmlele
    Using 0.10.x. I have a resource with several filters that may be applied to the collection, that return a subset of the entire collection, which is also paginated. Is there a way to add metadata that are calculated on the full set matching the filters?
    Paul Vilchez
    @pvilchez
    @keithmgould Have you had any luck with the Serializer? I am just running across the same issue.
    Iuval
    @iuval

    Hello, I'm looking into scoping associated resources and can't make it work.
    I'm trying records_for it's not being called.

    class OrganizationResource < JSONAPI::Resource
      has_many :accounts
    
      # I've tried this
      def records_for_accounts(relation_name, options = {})
        byebug
        # Not being called
        Pundit.policy_scope!(options[:context][:current_user], _model.public_send(relation_name))
      end
    
      # And this
      def records_for(relation_name, options = {})
        byebug
        # Not being called
        Pundit.policy_scope!(options[:context][:current_user], _model.public_send(relation_name))
      end
    
      # This is working but it returns empty hashes on the included list 
      # (one hash per resource that was not allowed to be shown)
      def records_for_populate(options = {})
        Pundit.policy_scope!(options[:context][:current_user], _model_class.all)
      end
    end

    Any idea what's going on?
    Is this deprecated or something?

    The endpoint is /organizations?include=accounts

    Adam Robertson
    @arcreative
    Is there no after_update callback for processors?
    at least on 0.9.x?
    it makes more sense that there isn't one for after_destroy since it returns :no_content, but I find myself in a position to add some meta to a response after an update
    Keith Gould
    @keithmgould
    @pvilchez hi. Not really, and I decided that while this gem is great for a restricted use-case, I found myself fighting against it a bit more than was comfortable. So I switched to the jsonapi-rails gem, which I am liking a lot. http://jsonapi-rb.org/. They also have a gitter: https://gitter.im/jsonapi-rb/Lobby. Again I think jsonapi-resources is very cool if your use-case falls into its parameters.
    1 reply
    Matic Jurglič
    @matixmatix

    Hi! I'm looking for some help. Is it possible to add a has_one relationship to a resource but implement it with a custom query?

    My use case is the following: I have a UserResource and would like to include one Note - the most recent one. Note is a table with foreign keys user_id, coach_id. So I would like the has_one :note to go to the notes table and find the most recent one and attach it to the response. Is this possible with jsonapi-resources?

    Matic Jurglič
    @matixmatix
    I tried this, but I get an exception: Internal Server Error: undefined method 'note_id'
    has_one :note, eager_load_on_include: false
      def note
        note = @model.notes.first
        Api::V1::NoteResource.new(note, context)
      end
    Scott González
    @scottgonzalez
    @matixmatix The gem cares about the IDs, so that’s the method you have to implement.
    Matic Jurglič
    @matixmatix
    @scottgonzalez thanks for your reply. Could you provide more info, please? What kind of method and where?
    Scott González
    @scottgonzalez
    Just add note_id to the resource.
    23 replies