ruby-grape / ruby-grape/grape-entity

Truncate Formatter

Open
#372 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
729
Forks
154
PR merge metrics
No merged PRs in 30d

Description

Do we have a wiki to stick this content into? Or maybe we can add it to the README.md?

I thought identifying how to truncate long fields may be useful for others as well.

For large character length fields that you wish to truncate and append ellipse (...), you can utilize the format_with function.

Code example:

  class MyEntity < Grape::Entity
    format_with(:truncate) { |str| str.truncate(50) }

    with_options(format_with: :truncate) do
      expose :summary
      expose :raw_content
    end
  end

This will render your content as following when the field is over 50 characters long:

{
      "summary": "adfasdf sdafjlksdaf kslafj lksdaf jlkdsfafdkasl...",
      "raw_content": "adfasdf sdafjlksdaf kslafj lksdaf jlkdsfafdkasl..."
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review README.md and the existing documentation around Grape::Entity and format_with. Confirm whether the example belongs in README.md or a wiki, then document the truncate formatter example and its rendered output. Done means the guidance is easy to find and accurately explains truncating long fields with an ellipsis.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
documentation
Issue type
Documentation
Difficulty
1/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.