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

Support the attributes method #21

Open
fonji opened this issue Oct 4, 2013 · 0 comments
Open

Support the attributes method #21

fonji opened this issue Oct 4, 2013 · 0 comments

Comments

@fonji
Copy link

fonji commented Oct 4, 2013

Hello,

As I'm writing a webservice, I want my data to be available through JSON.
During the process of deciding how will I do that, I found a nice-to-have issue for tripod.
Implementing the attributes method so people can use ActiveModel::Serializers::JSON (or any othe ActiveModel Serializer) would be nice.
Here are some details:

The default json serialization of a Tripod object is not the best ever (but understandable):
It uses predicates URIs as vars names, non-multivalued data are in arrays anyways, simple values will be in an object with an "@value" variable, etc.
Steps to produce such an output are below.
Using the default respond_with(tripodObject) gives another output, which is much more detailed so even less readable (but should be way more from_json-able).

In my case, I have to re-write as_json in my model anyway, but to help someone else it might be usefull to implement the "attributes" method.
This allows models to include ActiveModel::Serializers::JSON, which offers nice options and output for the as_json method.
Please read this stack overflow answer for more details.

To produce default json output:
Simply add something like this in your controller:

  def show
    @p = # get your tripod object here
    respond_to do |format|
      format.html # default show.html.rb
      format.json { render json: @p }
    end
  end

And then access /controller/id.json.
The detailed output can be obtained by:

  def show
    @p = # get your tripod object here
    respond_to do |format|
      format.html # show.html.rb
      format.json { respond_with(@p) }
    end
  end

I hope that this is clear enough, do not hesitate to ask a question if needed.

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

1 participant