Code Monkey home page Code Monkey logo

Comments (9)

guilleiguaran avatar guilleiguaran commented on July 17, 2024

Can you provide an example to illustrate this better?

from fast_jsonapi.

jankmet avatar jankmet commented on July 17, 2024

asset:

{ 
  id: 2232,
  filename: '2232.pdf',
  attributes: {
    key1: val1,
    key2: val2,
    key3: val3
  }
}

key1,key2,key3 are dynamic keys from attributes table (asset has many attributes)

from fast_jsonapi.

shishirmk avatar shishirmk commented on July 17, 2024

Do you use Active Model Serializers for this today? If yes can you share an example serializer class where you would use this?

from fast_jsonapi.

jankmet avatar jankmet commented on July 17, 2024

No, we are using jbuilder. Serializer with 'attributes' property as hash would probably work,
but I'm not sure how is this hash serialized? Is this 'attributes' hash serialized with fast_jsonapi as well?

class Asset
  attr_accessor :id, :name, :attributes
end
class AssetSerializer
  include FastJsonapi::ObjectSerializer
  set_type :asset
  attributes :name, :attributes
end

from fast_jsonapi.

laertispappas avatar laertispappas commented on July 17, 2024

@jankmet So your models are as follows:

an assets has many attributes?

Asset 1-------* Attribute

if so your schema should be something like this (attributes is an array instead of a hash):

{ 
  id: 2232,
  filename: '2232.pdf',
  attributes: [
    { key1: val1 },
    { key2: val2 },
    { key3: val3 }
  ]
}

Which can be modeled with a has_many relationship

With the current implementation you can generate the hash inside your asset model and declare it in your serializer as an attribute So you will have something like this:

# file: asset_serializer.rb
attributes :id, :filename, :attributes_as_json

# file asset.rb
def attributes_as_json
  attributes.map{|a| [a.key, a.value]}.to_h
end

The downside is that you have the json serialisation inside you model though...

@shishirmk I think it would be better if we added support for method definition inside the serializers like active model serializers do. What do you think?

from fast_jsonapi.

shishirmk avatar shishirmk commented on July 17, 2024

@laertispappas it would be better but it seems to be really tricky to do give our current design. Look at the discussion in #49

from fast_jsonapi.

shishirmk avatar shishirmk commented on July 17, 2024

@jankmet I am closing this right now as dynamic keys is not really part of json:api standard. We need to know all attribute and relationship keys ahead of time in order to support features like sparse field sets and includes.

from fast_jsonapi.

TrevorHinesley avatar TrevorHinesley commented on July 17, 2024

Wouldn't this be possible with a block since #54 is merged in?

For instance:

attributes :id, :filename
attribute :attributes_as_json do |record, params|
  record.attributes.map{|a| [a.key, a.value]}.to_h
end

You could even render certain attributes in that hash conditionally based on params that are passed in from the calling controller/context.

from fast_jsonapi.

ababich avatar ababich commented on July 17, 2024

@TrevorHinesley please follow fields in JSONAPI and multiply PRs already made to fast_jsonapi to introduce fields here

unless fields are release nullifying unwanted attrs works well for me

from fast_jsonapi.

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.