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

Routes helpers available inside ArraySerializer #336

Closed
wants to merge 1 commit into from

Conversation

lucasfais
Copy link

I'm trying to do something like this:

class TasksSerializer < ActiveModel::ArraySerializer

  self.root = false

  def as_json(*args)
    {
      _results: super,
      _links: links
    }
  end

  def links
    {
      tasks: tasks_url
    }
  end
end

So I need to use the route helper tasks_url.

I'm not sure what is the best way to implement that, but here is my suggestion.

@coveralls
Copy link

Coverage Status

Coverage remained the same when pulling b5517a3 on lucasfais:master into 4f185ef on rails-api:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0%) when pulling b5517a3 on lucasfais:master into 4f185ef on rails-api:master.

@lukaszx0
Copy link
Member

I think it's useful addition and it's quite common to use url helpers in API apps. Your implementation looks good after quick review, however I think it might be relevant after @spastorino's rewrite.

Still, I think we should add this at some point.

@spastorino
Copy link
Contributor

Something like this will be added for 0.9 for sure. JSONAPI url based style needs something like that.

@lucasfais
Copy link
Author

Nice guys! Thanks.

@plexus
Copy link
Contributor

plexus commented Dec 5, 2013

Important side-note here, ActiveModel::Serializers does not depend on Rails. Rails-specific code should go in the Railtie.

For url-style JSONAPI we should have some isolation so non-rails frameworks can implement support for generating the URL of an object, or the URL template of a model.

@EverybodyKurts
Copy link

What's the status on this? Would love url helpers in this gem.

@steveklabnik
Copy link
Contributor

Status is I have a big announcement about the future of AMS, but have been a few days late in delivering it. Soon. Sorry. :/

@EverybodyKurts
Copy link

ActiveModel::Serializers is being rewritten in Rust? ;)

From a random developer, thanks for all the hard and great work you've done Steve. You've made my professional life much easier :). Whatever it is you decide to do, I wish you luck and success in that endeavor.

@steveklabnik
Copy link
Contributor

Nothing that extreme :)

And you're welcome. ❤️

@jherdman
Copy link
Contributor

@steveklabnik sorry to pester you, I'm just eager to hear what your big announcement is :)

@steveklabnik
Copy link
Contributor

Writing it up right now. Check the rails-api-core mailing list shortly.

@steveklabnik
Copy link
Contributor

Said announcement: https://groups.google.com/forum/#!topic/rails-api-core/8zu1xjIOTAM

@lucasfais , I'm interested in merging this pull request, but it is out of date. Could you rebase it, please?

@steveklabnik
Copy link
Contributor

It's been a month, and no rebase. I'm still interested in pulling this in, please re-submit to the 0-9-stable branch. Thank you!

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

Successfully merging this pull request may close these issues.

None yet

8 participants