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

Custom helpers, cleaner way? #102

Closed
raycohen opened this issue Jul 25, 2012 · 2 comments
Closed

Custom helpers, cleaner way? #102

raycohen opened this issue Jul 25, 2012 · 2 comments

Comments

@raycohen
Copy link
Contributor

I want to create a time_ago_in_words version of most time values I am rendering in my json.

I want to specify which keys this happens for, and I think it belongs in the serializer/view layer (not in the model).

I was able to make a helper method that works like this:

  class VideoSerializer < MyBaseSerializer
    time_attributes :recorded_at, :uploaded_at
  end
  # example serializer.as_json => {:recorded_at_ago => "3 days", :uploaded_at_ago => "2 minutes"}

gist is here: https://gist.github.com/3177989

I am just wondering if there is a more concise way to accomplish this?

@josevalim
Copy link
Contributor

That looks fine. I simply wouldn't the _attributes helper though, it is private and you shouldn't be fiddling with it when simply calling attribute would likely suffice.

@raycohen
Copy link
Contributor Author

thanks @josevalim - I updated the gist with your suggestions

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