Giter VIP home page Giter VIP logo

Comments (9)

tiagopog avatar tiagopog commented on May 30, 2024

Hey there, @griley!

As you mentioned, JSON::Utils only applies equality filters – like the one in the example – automatically. Unfortunately JU doesn't provide any fancy macro to customize filters since it doesn't assume any responsibility over your controller action's operation.

For example, in our production JSON API, when we need to apply a very specific filter, like filtering records by a given scope, we apply that filter at the interactor/service object level. The reason for such decision is that we understand that it's a good pattern do make non-trivial filters explicit in the context where the operation of the action happens.

Hope it helps you and if you have any other ideas, just let me know.

from jsonapi-utils.

griley avatar griley commented on May 30, 2024

Thanks @tiagopog . I agree with the decision to make the filters explicit.

However, the problem I'm running into (and the origin of the issue above) is that the default equality filter is still scoped to any custom filtering I may do in a controller.

For example:

class UserResource < JSONAPI::Resource
  attributes :first_name, :last_name, :full_name
  has_many :posts

  # I'd like to expose a filter on a derived attribute
  filter :full_name

  def full_name
    "#{@model.first_name} #{@model.last_name}"
  end
end
class UsersController < BaseController
  # GET /users
  def index
    jsonapi_render json: filtered_users
  end

  private

  def filtered_users
    first_name, last_name = params[:filter][:full_name].split
    User.where(first_name: first_name, last_name: last_name)
  end
end

When filtered_users is run above, the scope implied by the filter in UserResource is also applied to it. Resulting in:

User.where(first_name: first_name, last_name: last_name).where(full_name: params[:filter][:full_name])

Obviously, this is a problem because the attribute full_name doesn't actually exist.

Any suggestion on a way around this or a different approach?

from jsonapi-utils.

griley avatar griley commented on May 30, 2024

I guess I could unscope(where: :full_name) in filtered_users but that smells a bit to me.

from jsonapi-utils.

tiagopog avatar tiagopog commented on May 30, 2024

Ok, now I gotcha! In this case you might simply skip the default equality filter: jsonapi_render json: filtered_users, options: { filter: false }

from jsonapi-utils.

tiagopog avatar tiagopog commented on May 30, 2024

You might apply the same to pagination (options: { paginate: false }). I'm sorry for the lack of documentation for this feature. I may write something covering this case in the README.md later.

from jsonapi-utils.

griley avatar griley commented on May 30, 2024

Thanks for the quick response @tiagopog and pointers!

The jsonapi_render json: filtered_users, options: { filter: false } addressed my issue.

from jsonapi-utils.

griley avatar griley commented on May 30, 2024

I finally realized why Tiago suggested setting a pagination option after I enabled the pagination feature in the initializer:

config.default_paginator = :paged

The record count information applies the same default filter to it. But even after adding the paginate: false option, it continued to occur. I sniffed around the code a little bit and instead tried to add my own custom record count:

jsonapi_render json: filtered_users, options: { filter: false , count: filtered_users.length }

However, this line of code is thwarting that feature because it's overwriting the options I passed in.

from jsonapi-utils.

tiagopog avatar tiagopog commented on May 30, 2024

@griley, thanks for reporting it. I might take a look on this possible bug later as well as I'll try to remember if there was any particular motivation behind this line of code.

from jsonapi-utils.

griley avatar griley commented on May 30, 2024

On a side note...

If the options weren't reinitialized, the apply_filters(records, options) found here would cause the record count to be calculated correctly because of the filter: false

This would obviate the need for a count option in this issue.

Thanks, and good luck on Sunday in the football match vs Germany ;)

from jsonapi-utils.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.