Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON API dasherize property names #173

Closed
thhermansen opened this issue Nov 30, 2015 · 5 comments
Closed

JSON API dasherize property names #173

thhermansen opened this issue Nov 30, 2015 · 5 comments

Comments

@thhermansen
Copy link
Contributor

Hi,

I wasn't able to figure this one out myself. Given the following:

class User < Roar::Decorator
  include Roar::JSON::JSONAPI

  property :full_name
end

Is there a way to make the serialised version of a user to have property named full-name?

I can solve it by using #property's as-option, but I'd like to dasherize all property names automatically.

@apotonick
Copy link
Member

@thhermansen
Copy link
Contributor Author

Thank you for pointing me to defaults. I had forgotten that I had tried it a week or two ago. It didn't seem be inherited in nested properties and has_one. Maybe it isn't supposed to. To give a complete example of what I have, without calling defaults:

require 'roar/decorator'
require 'roar/json/json_api'

module PlaytimeTube
  module Representers
    class Playback < Roar::Decorator
      include Roar::JSON::JSONAPI

      type :playbacks

      property :id
      property :starts_at, as: 'starts-at', render_nil: true
      property :duration_in_seconds, as: 'duration-in-seconds'
      property :type

      property :recording do
        property :local_id, as: 'local-id'
        # more properties
        property :artist_name, as: 'artist-name'
      end

      has_one :playtime_report, as: 'playtime-report' do
        type 'playtime-reports'

        property :id
        # more properties
        property :archive_url, as: 'archive-url'
      end
    end
  end
end

If i provide defaults with a block which dasherize the name right above type :playbacks it works for the Playback's properties, like starts_at, but it doesn't inherit to local_id within recording, nor in to playtime_report's properties like archive_url.

I can call defaults within property/has_one and it will work. Is there a better way, or should I simply use defaults within each given block?

@apotonick
Copy link
Member

Defaults are not "inherited" into nested representers. I need to think about if that's a good idea or if we should introduce a inline: true option or something.

Will let you know tomorrow!

@apotonick apotonick reopened this Dec 3, 2015
@thhermansen
Copy link
Contributor Author

Oki, thank you. I solved it for now by doing:

DASHERIZE_NAME = -> (name, options) { {as: dasherize-the-string(name) } }
defaults &DASHERIZE_NAME

And reuse that block of code in the nested representers.

@myabc
Copy link
Contributor

myabc commented Dec 10, 2016

This issue was moved to trailblazer/roar-jsonapi#13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants