Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    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
    You’ll also need to handle the setter if the relationship is writable in your API.
    Chris Trinh
    @chtrinh
    Hi, how do I add a scope to a relationship that I'm including, e.g. api/item/16?include=asset? Currently my workout around is to use a default scope in Asset model. But I'm trying to avoid this.
    Chris Trinh
    @chtrinh
    my first thought was to subclass the ApplicationRecord and apply the default_scope only there to keep it isolated, but that seems iffy. thoughts/advice?
    Chris
    @chrisosborn
    Hello; does anyone know if there is a way to have configuration values apply to some resources/endpoints but not to others? (In particular, we are interested in maximum_page_size.) Thanks in advance for any help!
    Scott González
    @scottgonzalez
    @chrisosborn I believe you can create change the paginator using ResourceClass.paginator(paginator)
    You’d want to inherit from OffsetPaginator and override verify_pagination_params (https://github.com/cerebris/jsonapi-resources/blob/d185ebb76cd20f17d82e05e3298ee291f06aead1/lib/jsonapi/paginator.rb#L108-L119)
    Chris
    @chrisosborn
    @scottgonzalez Thank you! I will check that out!
    Matic Jurglič
    @matixmatix
    is it possible to convince JR to set an instance variable for the resource? for example, if I have a NotesController, I would like to have the @note instance variable set (for show, edit, destroy actions). (I'd like this because I have CanCanCan for resource authorization and want to avoid double loading). Any tips? 🙏
    Scott González
    @scottgonzalez
    @matixmatix Do you mean can the controller set an instance variable on the resource?
    If so, you can use context. It’s shared between the controller and resource.
    Alex Oliveira
    @kurko
    I'm looking for a way of adding to meta, {"pageCount"=>2, "totalCount"=>4}. Any tips. I'm reading the source code and trying to figure out where that is, but can't so far.
    Alex Oliveira
    @kurko
    Ok, I found https://gitter.im/cerebris/jsonapi-resources?at=5cdc5057b149ca509898a326 from may/2019, v0.9. Not sure it'll work on v0.10. v0.10 dropped the Processors doc page so I don't know what a processor is (IMHO everything everywhere is processed by something, so that name is confusing). Going to try that. Thx.
    Julius-Rapp
    @Julius-Rapp
    @kurko: Are you aware of the config options? https://jsonapi-resources.com/v0.10/guide/configuration.html#Defaults
      # Metadata
      # Output record count in top level meta for find operation
      config.top_level_meta_include_record_count = false
      config.top_level_meta_record_count_key = :record_count
    
      config.top_level_meta_include_page_count = false
      config.top_level_meta_page_count_key = :page_count
    Adam Robertson
    @arcreative
    i know this has probably been covered before, but is there any way to make a custom model method behave like a has_many?
    i have a method on my model that returns a relation, but can't be included with JR because it's not a real association
    Alex Oliveira
    @kurko
    Hi everyone. I'm trying to debug a weird issue. I have a relationship, and although I'm using always_include_linkage_data: true, the relationship's data is not present in the relationships key of the parent resource. I'm running byebug throughout the gem's code, and it looks like relationship_data is always empty in https://github.com/cerebris/jsonapi-resources/blob/master/lib/jsonapi/resource_serializer.rb#L264. I'm not sure I'm missing something. My resources are pretty standard. Does anyone have any debugging tips?
    Alex Oliveira
    @kurko
    Can't make resource linkage work ;(
    Scott González
    @scottgonzalez
    @kurko Do you have the relationship defined in the resource?
    Alex Oliveira
    @kurko
    Yes. Simplest relationship you can imagine. Tried all sorts of things, joining the relationship in records_for_populate (e.g includes). Had byebug spread across the gem, relationships related variables/state are always empty. I decided to punt on this and just let it return links. I'll get back to it when I'm about to deliver the API as to not get blocked.
    Alex Oliveira
    @kurko
    @Julius-Rapp apologies I didn't respond. I didn't see your message.
    Benjamin Fleischer
    @bf4
    @kurko we're still on 0.9 but I'd be surprised if the vanilla case didn't work. (when we tried to upgrade to 0.10 soon after stable came out our blocker was that we treat 'calculated' resources as posts/create and the way that was being handled in JR had changed in a way I was unable to resolve at the time. ) do you have a gist or something to share? routes set up with the jsonapi_resources macro, controllers exists, resources exist? it's possible the docs are wrong (seemed at the time at the 0.10 docs were just copied from 0.9 even when wrong)
    @arcreative yes, either just override the instance getter and/or make sure you turn off any eager loading flags on the relationship, like eager_load_on_include: false, and probably exclude_links: :default. I'm assuming there's an unrouted controller for the relation and an abstract resource for the model
    Julius-Rapp
    @Julius-Rapp
    @kurko no worries. If you are still interested in overriding top-level meta then you may have a look at cerebris/jsonapi-resources#1067 .
    sandric
    @sandric
    Hi there. I have a question mb somebody have answer to - how can you call jsonapi resource from code or repl? I mean I have fairly complicated resource with bunch of filters, which have_many and belongs to other resources, that also have queries. And I need to somehow get active record result of calling request to that resource with filters passed as json params. Is it possible somehow?
    Gordon Potter
    @eccegordo
    Has anyone done any range filters like: gte x AND lte y. Curious about the filter syntax for that. And how does the range interact with the pagination feature?
    Scott González
    @scottgonzalez
    @eccegordo You have to create custom filters for that.
    So the syntax is up to you.
    I suggest something like x=>5
    Gordon Potter
    @eccegordo
    I guess the one thing I still kind of hazy on is managing potentially mutually exclusive filters? Can I do this server side? For example if one particular filter is present then ignore other specific filters... I guess I can just not pass the to API. Or conversely if a certain filter is present then also ensure that an additional filter is specified with default value if not provided by client. Wondering how the filter control flow can work. If that makes sense.
    Scott González
    @scottgonzalez
    You really shouldn’t ignore filters. If anything, you should raise an error if there are incompatible filters.
    I’ve always handled that in the controller.
    Cezar Halmagean
    @chalmagean_twitter
    can you create a resource that doesn't belong to a model? I basically need an endpoint for a collection of key/value pairs
    Scott González
    @scottgonzalez
    @chalmagean_twitter Every resource is backed by a model, but it doesn’t have to be an ActiveRecord model.