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

Is it possible to ignore all adapters? #935

Closed
eliduke opened this issue Jun 2, 2015 · 11 comments
Closed

Is it possible to ignore all adapters? #935

eliduke opened this issue Jun 2, 2015 · 11 comments

Comments

@eliduke
Copy link

eliduke commented Jun 2, 2015

I am working on a large rails-api project with AMS v0.8.3 that currently has 2 native apps and a front-end web app. I am interested in upgrading to AMS v0.10.0 for some of the new features (primarily caching), but then it forces us into the JSON API adapter and essentially renders our end points useless without a substantial rewrite of all the front ends. Is it possible to override the default adapter to the "standard" that existed with v0.8.3?

The concept of adapters is new to me with AMS v0.10.0 and I see that the JSON API just hit v1.0 a couple days ago. It's exciting, but not practical to try to implement it right now.

@groyoh
Copy link
Member

groyoh commented Jun 2, 2015

JSON API isn't the default on v0.10. At least not yet (or not anymore #930 ). By default it will format as json by rendering attributes and attributes of associations.

The serializers allow you to define WHAT (attributes and associations) would be serialized whereas the adapter basically allow you to define HOW they are serialized. Using adapters also allow you to add your own output format if need be. For example, you could write your own XML format that would output your serialized objects as XML.

@eliduke
Copy link
Author

eliduke commented Jun 3, 2015

Thank for the breakdown, but I think I am still a little confused. You say that there isn't a default adapter for v0.10, but without doing anything else aside from switching from v0.8.3 to v0.10, this app_label end point went from outputting this:

{
  "app_labels": [
    {
      "id": 1,
      "key": "username",
      "label": "Corporate Email Address",
      "label_plural": ""
    },
    {
      "id": 2,
      "key": "password",
      "label": "Password",
      "label_plural": ""
    }
  ]
}

To this:

{
  "data": [
    {
      "id": "1",
      "type": "app_labels",
      "attributes": {
        "key": "username",
        "label": "Corporate Email Address",
        "label_plural": ""
      }
    },
    {
      "id": "2",
      "type": "app_labels",
      "attributes": {
        "key": "password",
        "label": "Password",
        "label_plural": ""
      }
    }
  ]
}

I understand the technical benefits of the latter, but it's just not plausible at this time to rewrite the front ends to receive that new json data format.

Is it possible for v0.10 to output like the former?

@groyoh
Copy link
Member

groyoh commented Jun 3, 2015

You can try setting the config:

ActiveModel::Serializer.config.adapter = :json

@eliduke
Copy link
Author

eliduke commented Jun 3, 2015

And where is the best place to put that?

@groyoh
Copy link
Member

groyoh commented Jun 3, 2015

If you use Rails, then config/initializers/active_model_serializer.rb.

@eliduke
Copy link
Author

eliduke commented Jun 3, 2015

Awesome. That did the trick! In my mind that seems like there IS a default adapter then, right?

@groyoh
Copy link
Member

groyoh commented Jun 3, 2015

BTW are you using 0.10.0.rc1 version or master?

@eliduke
Copy link
Author

eliduke commented Jun 3, 2015

I'm using 0.10.0.rc1 as such:

gem 'active_model_serializers', '0.10.0.rc1', git: 'https://github.com/rails-api/active_model_serializers.git'

@groyoh
Copy link
Member

groyoh commented Jun 3, 2015

Then try bundle update active_model_serializers and try to run it without the config initialized.

@eliduke
Copy link
Author

eliduke commented Jun 3, 2015

That did it! Wow. So, that was a recent change? Thanks for all your help!

@groyoh
Copy link
Member

groyoh commented Jun 3, 2015

Yep, it was changed to JSON API 3 weeks days ago and then reverted a few days ago 😉
Please close this issue if you're all done 😄

@eliduke eliduke closed this as completed Jun 3, 2015
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

2 participants