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
    @artur79 If you want to deviate from the standard I'd consider overriding the BasicResource._replace_to_many_links method. I think this might work:
        def _replace_to_many_links(relationship_type, relationship_key_values, options)
          relationship = self.class._relationship(relationship_type)
          existing = find_related_ids(relationship, options)
          super(relationship_type, relationship_key_values & existing, options
        end
    You could block it by raising an exception in an override of _replace_to_many_links
    The override could be in your base resource if you want the whole app to behave that way, or in just one specific resource.
    You would lose the ability to clear the relationships with an empty array, but you could also check for that and modify the logic slightly
    Artur
    @artur79
    @lgebhardt why replace_to_many_links not replace_to_many_relationship ? basically I want to return exception 403 according to the standard, if some relationships already exist
    Larry Gebhardt
    @lgebhardt

    @braindeaf I've handled that with an id translation in the users controller like:

    class UsersController < BaseController
      before_action :convert_me_id
    
      def convert_me_id
        if params[:id] && params[:id].casecmp('me') == 0
          params[:id] = context[:current_user].id
          context[:me] = true
        end
      end
    end

    This just uses the UserResource and not a MeResource. You could modify the params or the context in the case of an id using me to alter the response. The methods like fetchable_fields, updatable_fields all have access to the context either as an argument or on the resource instance.

    RobL
    @braindeaf
    ah ok @lgebhardt that could work.... I'm going to give that a whirl.
    thank you :)
    Larry Gebhardt
    @lgebhardt
    @artur79 replace_to_many_relationship is a method on the processor. Other operation types also replace the relationships. The resource's _replace_to_many_links is the easiest place to put an override that should get them all.
    Artur
    @artur79
    thanks for explanation :)
    Artur
    @artur79
    here's my hack, maybe some other users will need it. Thanks again @lgebhardt :)
      def _replace_to_many_links(relationship_type, relationship_key_values, options)
        if relationship_type == :user_groups && @model.user_groups.any?
          fail JSONAPI::Exceptions::ToManySetReplacementForbidden.new(detail: 'User already has user group(s) assigned.')
        end
    
        super(relationship_type, relationship_key_values, options)
      end
    Ayanga
    @aok-solutions
    is there a way to add extra attributes (on create) that are not on the model or a relationship? keep running into the Param not allowed error
    Larry Gebhardt
    @lgebhardt
    @aok-solutions You can add attributes to the resource that don't exist in the model. Add the attribute as normal and then add a getter and setter.
    Ayanga
    @aok-solutions
    @lgebhardt thanks!
    Joe Gaudet
    @joegaudet
    @lgebhardt any idea what kind of timeline on have the linkage included, I definitely appreciate that you’re likely quite busy, just putting together a roadmap for the next two quarters.
    Larry Gebhardt
    @lgebhardt
    @joegaudet I should be able to at least look into what it will take by the end of next week. Hopefully it's a simple thing and I can have a fix then too.
    Joe Gaudet
    @joegaudet
    Awesome man, very much appreciated.
    Anuj Patel
    @patelanuj28
    Upgraded to 0.10.4 and started seeing this 500 server error in the included property.
    [2021-02-22 20:21:14 UTC] P42065R1 ERROR Internal Server Error: undefined method downcase' for nil:NilClass ruby-2.7.2@manager-ruby272/gems/jsonapi-resources-0.10.4/lib/jsonapi/relationship.rb:66:inblock (3 levels) in polymorphic_types'
    /Users/anujp/.rvm/gems/ruby-2.7.2@manager-ruby272/gems/jsonapi-resources-0.10.4/lib/jsonapi/relationship.rb:65:in each' /Users/anujp/.rvm/gems/ruby-2.7.2@manager-ruby272/gems/jsonapi-resources-0.10.4/lib/jsonapi/relationship.rb:65:inblock (2 levels) in polymorphic_types'
    Any one has seen this issue?
    benebrice
    @benebrice
    Did you change key_type default value?
    Anuj Patel
    @patelanuj28
    Nope, I haven't changed anything it is working with 0.9.0, so I am guessing it's regression with 0.10.4
    Benjamin Fleischer
    @bf4
    Yes. I even have a fa8ling
    Failing pr. Been broken for years
    Anuj Patel
    @patelanuj28
    Does 0.10.4 supports include on polymorphism property like include=image.taggable?
    Anuj Patel
    @patelanuj28
    After monkey patching find_related_polymorphic_fragments with following change relation_position = relation_positions[row[2].underscore.pluralize] and updating association in the model. It started working.
    benebrice
    @benebrice
    Hi ! Do you think an integration of Goldiloader could be a good idea? This might solve a lot of time on optimisation with nested relationships...
    Larry Gebhardt
    @lgebhardt
    @benebrice Interesting looking gem. I'll take a look and see if it might be useful.
    benebrice
    @benebrice
    @lgebhardt I've just discovered it myself and test it. Works wonderfully with nested association usual issue. I was not able to integrate it easily on 0.9 but I'm sure you'll know exactly where to add it. 👌🏻
    Vincent Smith
    @Vince-Smith

    Is it no longer possible to inherit from JSONAPI::ResourceController in 0.10.4 on rails 6.0? I'm working on an upgrade from rails 5.2, but I'm also experiencing the same problem if I create a new app following the docs. Whenever I make a request I get the error:

    #<NameError: uninitialized constant JSONAPI::ResourceController
    Did you mean?  JSONAPI::ResourceControllerMetal>

    Tried setting config.autoloader = :classic but that didn't change anything.

    I've been able to work around this by using:

    class MyController < ActionController::Base
      include JSONAPI::ActsAsResourceController
    end

    This is the only relevant commit that I've been able to find so far that might be causing this

    Thanks! (I'm on ruby 2.7.2)

    Harbir Gill
    @harbirg
    Question - When specifying include on a resource, the response will include all relationships in response even if they do not have data or valid links. For example, if I call GET /books?include=author and books has many authors and has many covers. Even though I did /books?include=author, it will show covers in the relationship object with links that are invalid and no data. Is it possible to tell jsonapi-resources to restrict the relationships to respond if include parameter is used? I saw this stackoverflow that will remove links for relationships that have empty data but it doesnt remove the unnecessary relationship objects from the response ( https://stackoverflow.com/questions/44136258/removing-links-using-jasonapi-resources-gem )
    10 replies
    Harbir Gill
    @harbirg
    @lgebhardt - Larry, can you help with the above question if there is an override that i can make to remove unnecessary relationship objects
    Larry Gebhardt
    @lgebhardt
    @harbirg I'll try to look into that soon.
    Artur
    @artur79
    is there any way of adding virtual attributes to the resource ? Or it has to be done via model ?
    Artur
    @artur79
    so the attribute which in can pass with POST, use it to set some real attribute in resource, but I dont need to return it in any response
    Julius-Rapp
    @Julius-Rapp
    @artur79 : You can add getter and setter to the Resource, and the Getter can return nil. Is that what you need?
    Artur
    @artur79
    @Julius-Rapp here's the example code, if I run it it still tries to save time_range_appointment_id in the model, which I don't want to do cuz that field does not exist
    class Api::V1::TagAssociationResource < JSONAPI::Resource
      model_name 'TagAssociation'
    
      attributes :tag_id, :tag_type_id, :taggable_id, :taggable_type, :time_range_appointment_id
    
      # def self.creatable_fields(_context)
      #   super - [:time_range_appointment_id]
      # end
    
      before_save do
        if !@model.taggable && @model.time_range_appointment_id.present?
          time_range = TimeRange.find_by(appointment_id: @model.time_range_appointment_id)
    
          @model.taggable = time_range
        end
      end
    end
    and when I uncomment creatable_fields class method I'm getting error:
    Response body: {"errors":[{"title":"Param not allowed","detail":"time_range_appointment_id is not allowed.","code":"105","status":"400"}]}
    Artur
    @artur79
    I can solve it using attr_accessor in the model but I don't want to spoil it because I need it only in the API
    Artur
    @artur79
    thanks @Julius-Rapp for pointing to the right direction, here's the working solution, not so straightforward but works :)
    class Api::V1::TagAssociationResource < JSONAPI::Resource
      model_name 'TagAssociation'
    
      attributes :tag_id, :tag_type_id, :taggable_id, :taggable_type, :time_range_appointment_id
    
      def time_range_appointment_id=(val)
        @time_range_appointment_id = val
      end
    
      def time_range_appointment_id
        nil
      end
    
      def self.fetchable_fields(_context)
        super - [:time_range_appointment_id]
      end
    
      before_save do
        if !@model.taggable && @time_range_appointment_id.present?
          time_range = TimeRange.find_by(appointment_id: @time_range_appointment_id)
    
          @model.taggable = time_range
        end
      end
    end
    Nicholas
    @wasabigeek_twitter

    Hi all! What would be the best way to define an attribute on each of a resource's records just before returning it? The use case is to mark resources as "recommended" on the fly. I've tried to override the records method as such:

      def self.records(options = {})
        objects = super
        # sets the attr_accessor `recommended = true` on the model
        Recommender.new(context[:current_team]).mark_recommended(objects)
        objects
      end

    This doesn't work though, the response still shows recommended: nil. From what I've dug so far we lose this attribute somewhere during resource_klass.sort_records in apply_request_settings_to_records of ActiveRelationResource

    7 replies
    Artur
    @artur79
    In addition to my solution for accessor, the only issue is that it returns time_range_appointment_id = nil in any response, any way to get rid of it ? I need only to set it to use in before save
    Artur
    @artur79
    I always thought that which attributes are returned is specified in definition and from there swagger.json is created, I don't have this field in response, just in POST params
    I'v also got set def self.fetchable_fields(_context) super - [:time_range_appointment_id] end
    Artur
    @artur79
    found id, my fault :) in distinction of creatable and updatable it should be instance method, not class one, so without "self"
    Harbir Gill
    @harbirg
    @lgebhardt - Larry, wondering if you had time to look at my question above. thanks
    2 replies
    Thomas Carr
    @thomasc-mww
    Does anyone have advice, or can point me in the direction of an example, for implementing asynchronous patch requests (i.e. returning a 202)? It's not clear to me how to override the default behavior of update requests when using jsonapi-resources. Thank you!
    7 replies