Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Larry Gebhardt
    @lgebhardt
    Thanks. I'm also working from home, but that's normal for me :)
    Julius-Rapp
    @Julius-Rapp
    @lgebhardt: I am happy to see #1317 . I would like to use your fix. What is the procedure for merging? Can I help with testing or anything else? If you expect a long delay before this is available as a gem then I need to change my setup. If you expect this to take no more than a few days then I keep my current setup and wait for the fix to be merged.
    Please do not feel urged. I highly appreciate your effort, and I understand that I am not your customer. I am just considering if I should change my setup.
    Larry Gebhardt
    @lgebhardt
    @Julius-Rapp If you want to test it out by loading the gem grom the github branch that would be great. I have a few other changes I want to get into the project before making a new release so it may be a little bit before I get the release out.
    @joegaudet I didn't get a good solution worked out this morning, but I have not forgotten.
    Federico Martinez
    @fedxgibson

    Hey guys, hope you are doing great. I'm Federico, currently working for M-RETS (midwest renewable tracking system) and we are about to deploy a public API. We are using jsonapi-resource for this. We have already implemented the GET methods. Now we are approaching the Create/Updates.

    I was wondering if you guys can help me figure things out.

    So my first question is, give this code:

    def create
          @account = Account.new(create_account_params)
          @account.organization = current_user.current_organization
          @account.status = Account.statuses[:open]
    
          authorize @account, :create?
          if @account.save
            render json: @account, status: :created, location: @account
          else
            render_error @account, :unprocessable_entity
          end
    end
        def create_account_params
          params.fetch(:data, {})
                .fetch(:attributes, {})
                .permit(:account_type, :name)
        end

    Where should I whitelist parameters, where should I add add new data before creating the resource?

    Larry Gebhardt
    @lgebhardt
    Please take a look at the Basic Usage https://jsonapi-resources.com/v0.10/guide/basic_usage.html
    you will not need to create any of the controller methods if you derive your controller from JSONAPI::ResourceController.
    Federico Martinez
    @fedxgibson
    Thanks for answering! Yes! That's the code from our private api. My job now is achieving the same but using jsonapi-resources

    So I'm not sure where should I implement this part of the code:

          @account = Account.new(create_account_params)
          @account.organization = current_user.current_organization
          @account.status = Account.statuses[:open]

    It's just basically adding additional data, apart from the request's parameters

    Is there a place where I can modify the parameters sent to the resource ?
    Larry Gebhardt
    @lgebhardt
    You can in a processor, or in a controller override. But in most cases you won't actually want to do that.
    What are you trying to accomplish?
    Federico Martinez
    @fedxgibson

    Basically, I need to add additional data to the request's parameters, and then create the record.
    In this case, as you can see, after creating the request with the user's param, I set the current organization and set the account to active.

    In general, where's the place to intercept the request parameters and do something with them? whitelisting, etc?

    hope it's clear, english is not my first language
    Larry Gebhardt
    @lgebhardt
    Once your user is authenticated store the user record on the context see https://jsonapi-resources.com/v0.10/guide/resources.html#Context
    Federico Martinez
    @fedxgibson
    got it, I can use the before_save callback.
    Thanks you so much!
    Larry Gebhardt
    @lgebhardt
    From there you could use a resource after_create callback, https://jsonapi-resources.com/v0.10/guide/resources.html#JSONAPI-Resource-Callbacks, to set the organization from the context which will be on the resource (see the after_save example in https://jsonapi-resources.com/v0.10/guide/resources.html#Context for a similar example.
    Sorry, missed your earlier reply. I'd use after_create so it doesn't get changed on every save.
    Federico Martinez
    @fedxgibson
    got it! thanks!
    Joe Gaudet
    @joegaudet
    Awesome thanks @lgebhardt
    Julius-Rapp
    @Julius-Rapp
    I would like to add custom top-level meta. Should I use a custom processor for this? https://github.com/cerebris/jsonapi-resources/issues/1067#issuecomment-329485167
    Hannu Kröger
    @hkroger
    Hi there, I'm trying to PUT an object that I got via GET. Just PUTting exactly the same what I got with GET.
    However that doesn't work because I get an error that "links is not allowed."
    Why is it not allowed? and OTOH, is there some way for me to hack around this?
    JSONAPI::Processor is too late in the processing chain to help with this.
    Joe Gaudet
    @joegaudet
    @lgebhardt any movement on this?
    Hannu Kröger
    @hkroger
    ah, and worth mentioning that I use 0.9
    upgrading to 0.10.x probably not a biggie
    Larry Gebhardt
    @lgebhardt
    @joegaudet, sorry I didn't get very far on it yet.
    @hkroger Check the spec https://jsonapi.org/format/#crud-updating, but I don't think the links is allowed.
    Hannu Kröger
    @hkroger
    @lgebhardt hmm, it's doesn't really mention links there
    which I guess could be interpreted as "not allowed"
    Joe Gaudet
    @joegaudet
    @lgebhardt no worries, I assume you’re quite busy, just doing some planning.
    Larry Gebhardt
    @lgebhardt
    @hkroger I'll look closer in a bit, but I read that as it's not allowed
    @joegaudet Yep, got a deadline fast approaching
    But I should still be able to make a bit of time in the evening
    Tom Simnett
    @simmerz
    Does anyone have guidance on how to has_one relationship for an STI model?
    Federico Martinez
    @fedxgibson
    @arcreative what I need something more complex. Like allowing nested attributes?
    regarding strong parameters
    Gilles Crofils
    @gcrofils
    @ghost~5df1b9ecd73408ce4fd3d27d Have you managed to make it work with Ancestry ?
    Julius-Rapp
    @Julius-Rapp
    Is it legal to include polymorphic relationship data? Say /tags?include=taggable
    Julius-Rapp
    @Julius-Rapp
    I just found a way that works.
    If I omit the second and third belongs_to in the following snippet then the test fails. Is that how it is intended?
    class Tag < ActiveRecord::Base
      belongs_to :taggable, polymorphic: true
      belongs_to :post, -> { where(tags: { taggable_type: 'Post' }).eager_load(:posts) }, foreign_key: 'taggable_id'
      belongs_to :user, -> { where(tags: { taggable_type: 'User' }).eager_load(:users) }, foreign_key: 'taggable_id'
    end
    Julius-Rapp
    @Julius-Rapp
    I also wonder why there is no .to_sym here, while it is present here. That forces me to use symbols:
    class Post < ActiveRecord::Base
      # ATTENTION: do not use a string here because that confuses
      # `JSONAPI::Relationship.polymorphic_types`
      # has_many :tags, as: 'taggable'
      has_many :tags, as: :taggable
    end
    Julius-Rapp
    @Julius-Rapp

    Third and finally, I was surprised that my script must not assign to variables during database setup:

    # ATTENTION: do not assign to `post` because that confuses
    # `JSONAPI::Relationship.polymorphic_types`
    # post = Post.create! title: 'my first post'
    Post.create! title: 'my first post'

    Those instances ended up in ObjectSpace here and caused troubles.

    Sorry, that was a lot and not too well organized. Just let me know if any of my two latter points is relevant. I will open a github issue in that case.