Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • Jan 08 2020 17:21
    mwpastore commented #19
  • Dec 22 2019 18:07
    senid231 edited #19
  • Dec 22 2019 17:57
    senid231 commented #19
  • Dec 22 2019 17:56
    senid231 opened #19
  • Feb 11 2019 22:55

    mwpastore on namespace-dsl

    wip (compare)

  • Sep 08 2018 13:00
    jnylen closed #17
  • Feb 24 2018 10:59
    jannishuebl commented #18
  • Feb 23 2018 10:54
    jannishuebl opened #18
  • Dec 17 2017 06:34
    jnylen opened #17
  • Oct 27 2017 01:18

    mwpastore on master

    Update Sinatra dependencies (compare)

  • Oct 27 2017 01:16

    mwpastore on v1.3.0

    (compare)

  • Oct 27 2017 01:16

    mwpastore on master

    Bump to 1.3.0 (compare)

  • Oct 27 2017 01:10

    mwpastore on master

    Deprecate `sinja {}' in favor o… Make sure Rubygems is up-to-dat… Dynamic before_<action> arity and 5 more (compare)

  • Oct 06 2017 04:30
    mwpastore commented #16
  • Oct 06 2017 00:39
    jnylen closed #16
  • Oct 06 2017 00:39
    jnylen commented #16
  • Oct 05 2017 22:19
    mwpastore commented #16
  • Oct 05 2017 22:14
    mwpastore commented #16
  • Oct 04 2017 00:02
    jnylen commented #16
  • Oct 03 2017 23:43
    jnylen commented #16
Mike Pastore
@mwpastore
neat
Jonathan Gnagy
@jgnagy
got another question: lets say, for a given resource, when I'm providing a collection I don't want to include an attribute (say indexing Users, I don't want to provide the emails for all the users), but for show I do want that attribute to show up.
so I don't want to modify the serializer, because then I'd never seem email
is there a way to customize the serializer's behavior for just the index block? (and maybe show_many)
looks like serialize_models() is the right answer, but I'm struggling to see how to pass it the options I want (namely how to exclude a field)
Lucas Hosseini
@beauby
@jgnagy You may be looking for the fields JSON API query parameter (http://jsonapi.org/format/#fetching-sparse-fieldsets)
Jonathan Gnagy
@jgnagy
perhaps, but I don't want to make it an option for the client
Lucas Hosseini
@beauby
Sure, what I meant was that, since this query parameter exists, there’s a good chance jsonapi-serializers supports it in some way
Jonathan Gnagy
@jgnagy
I'd like /users to return all the users, allowing pagination, filtering, etc., but I don't want to just allow users to harvest emails
Jonathan Gnagy
@jgnagy
so here's the normal, working /index:
        index(roles: %i[user trusted]) do
          Models::User.all
        end
changing it like so:
        index(roles: %i[user trusted]) do
          serialize_models Models::User.all, fields: { users: indexable_fields }
        end
doesn't error out, and seems to be making use of exactly what you linked to
Lucas Hosseini
@beauby
Yup
Jonathan Gnagy
@jgnagy
however... it goes from returning 3 users to 0
@body=["{\"data\":[],\"jsonapi\":{\"version\":\"1.0\"},\"included\":[]}"]
Lucas Hosseini
@beauby
Hmm that sucks :)
Jonathan Gnagy
@jgnagy
        def indexable_fields
            %i[admin apikeys groups organizations identities].tap do |a|
              if role.include?(:admin)
                a << :verified
                a << :email
              end
            end
          end
Lucas Hosseini
@beauby
I’m not really familiar with sinja so sadly I can’t be of much help
Jonathan Gnagy
@jgnagy
just so you see what that helper is doing
ah, ok
I think the breakdown is that I'm not sure I can use the result of serialize_modelsdirectly in the index sinja route helper
Lucas Hosseini
@beauby
Looking at the docs you could probably hack your way through:
resource :posts do
  helpers do
    def before_index
      params[:fields] = { users: indexable_fields }
    end
  end
Snippet taken and modified from there: https://github.com/mwpastore/sinja#sorting
Jonathan Gnagy
@jgnagy
that's worth trying
Lucas Hosseini
@beauby
You’d have to add a bit more logic to handle actual user-provided sparse fieldsets while removing email, but I think it should work
Jonathan Gnagy
@jgnagy
yeah, I like it
I think that'll work
Lucas Hosseini
@beauby
:+1:
Jonathan Gnagy
@jgnagy
figuring out the right mix of fields will take some work
but testing that brute-force in the before_* helper worked
Jonathan Gnagy
@jgnagy
this seems to do what I wanted
          def before_index
            params[:fields] = {
              users: if params[:fields] && params[:fields].key?(:users)
                       params[:fields].select { |k, _| indexable_fields.include?(k) }
                     else
                       indexable_fields
                     end
            }
          end
almost
Lucas Hosseini
@beauby
You should get rid of && params[:fields].key?(:users)
oops no sorry
Jonathan Gnagy
@jgnagy
          def before_index
            params[:fields] = {
              users: if params[:fields] && params[:fields].key?(:users)
                       params[:fields][:users].select { |k, _| indexable_fields.include?(k) }
                     else
                       indexable_fields
                     end
            }
          end
Lucas Hosseini
@beauby
I meant,
Yeah, that :)
Mike Pastore
@mwpastore
looks good. the problem with your other attempt is that you should just return the collection from your action helper (serialize_models is called behind the scenes), along with a hash of options to pass to jsonapi-serializers. and you can't use return, you have to use next. so you wanted
index(roles: %i[user trusted]) do
  next Models::User.all, fields: { users: indexable_fields }
end
this is covered in the first paragraph of this section here https://github.com/mwpastore/sinja#action-helpers and several examples are scattered throughout the readme
but in general, yes, as Lucas points out I recommend using before_index (and before_show, etc.) to set defaults for query parameters like include, fields, sort, filter, page, etc.
Jonathan Gnagy
@jgnagy
thanks, and I'll keep that in mind going forward
I've enjoyed using Sinja, by the way... it has really made creating my API easier
keep up the great work!
Mike Pastore
@mwpastore
that's great to hear
thank you :)
Jonathan Gnagy
@jgnagy
if you care to see the service I've been hacking on that uses it: https://github.com/knuedge/authify-api