Cutword is a simple gem for truncating words/strings or shortening words and names, compatible with Rails and Hanami.
- It shortens long text in titles, picture names, labels, and more.
Version 2.0.0
Major notes:
- It supports both Hanami framework and Rails.
- The first parameter specifies the limit (number of characters).
- The second parameter can be text, symbols, strings, or words.
Attributes | Functionality |
---|---|
Cutword |
gem module that can be directly used as a function |
To install the Cutword, you can use the following gem install command:
Step 1:
- Paste to your
Gemfile
then save.
gem "cutword", "~> 2.0"
Step 2:
- Run this to your Terminal
bundle install
Step 3:
- Restart your Server
Cutword(number, 'Your text here')
Cutword(limit, 'words / names / titles / label / articles / paragraph / sentence')
Cutword(5, 'This is a short text.')
Comparison | Example words |
---|---|
Before |
Mary had a little lamb, its fleece was white as snow. |
After |
Mary had a little lamb, its... |
No need to declare cutword module inside the controllers
In your View Template:
just use directly:
ex. index.html
<% @articles.each do |article| %>
<h2><%= Cutword(20, article.title) %></h2>
<%= article.body %>
<% end %>
ex. show.html
<%= Cutword(20, @article.title) %>
# app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
def index
@articles = Article.all.order("created_at DESC")
@articleTitles = @articles.map { |article| Cutword(40, article.title) }
end
end
In your View Template:
<% @articles.each_with_index do |article, index| %>
<h2><%= @articleTitles[index] %></h2>
<div><%= article.body %></div>
<% end %>
# app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
def show
@article = Article.find(params[:id])
@articleTitle = Cutword(40, @article.title)
end
end
In your View Template:
<h1><%= @articleTitle %></h1>
<div><%= @article.body %></div>
Hanami version 2.1.0 or greater
Update config/app.rb with below content
require 'hanami'
require 'cutword'
go to:
app/views/books/index.rb
# app/views/books/index.rb
module Bookshelf
module Views
module Books
class Index < Bookshelf::View
include Deps["persistence.rom"]
expose :books do |page:, per_page:|
rom.relations[:books]
.select(:title, :author)
.order(:title)
.page(page)
.per_page(per_page)
.to_a
.map do |book|
shorten_title = Cutword(10, book[:title])
{
title: shorten_title,
author: book[:author]
}
end
end
end
end
end
end
go to:
app/views/books/show.rb
# app/views/books/show.rb
module Bookshelf
module Views
module Books
class Show < Bookshelf::View
include Deps["persistence.rom"]
expose :book do |id:|
book_data = rom.relations[:books].by_pk(id).one!
# Process the book title with Cutword
if book_data && book_data[:title]
book_data[:title] = Cutword(10, book_data[:title])
end
book_data
end
end
end
end
end
or
<!-- app/templates/books/index.html.erb -->
<h1>Books</h1>
<ul>
<% books.each do |book| %>
<li><%= Cutword(7, book[:title]) %>, by <%= book[:author] %></li>
<% end %>
</ul>
<!-- app/templates/books/show.html.erb -->
<h1><%= Cutword(7, book[:title]) %></h1>
<p>By <%= book[:author] %></p>
Demjhon Silver