Project

cutword

0.0
The project is in a healthy, maintained state
This gem provides string truncation and word cutting functionality for both Rails and Hanami Framework.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

Cutword

Gem Version Downloads

Table of Contents

  • Description

  • Release Notes

  • Features

  • Installation

  • Paradigm

  • Example

  • Rails Framework

  • Hanami Framework

Description

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.

Release-notes

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.

Features

Attributes Functionality
Cutword gem module that can be directly used as a function

Installation

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

Paradigm

Cutword(number, 'Your text here')

Cutword(limit, 'words / names / titles / label / articles / paragraph / sentence')

Cutword(5, 'This is a short text.')

Example

Comparison Example words
Before Mary had a little lamb, its fleece was white as snow.
After Mary had a little lamb, its...

Rails-framework


Method 1 - Rails


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) %>

Method 2

# 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-framework

Hanami version 2.1.0 or greater


go to:

app/views/helpers.rb

add:

require "cutword"

Method 1 ( Direct )


# app/views/helpers.rb

module Bookshelf
  module Views
    module Helpers
      # Add your view helpers here
      require "cutword"

    end
  end
end

or

Method 2 ( InDirect )

# app/views/helpers.rb

module Bookshelf
  module Views
    module Helpers
      include CutwordHelper
    end
  end
end

then create file:

# app/views/helpers/cutword_helper.rb
module Bookshelf
    module Views
      module Helpers
        module CutwordHelper
          require "cutword"
        end
      end
    end
end

Let say your application is about books


Method 1 - Hanami

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

Method 2 - Hanami

don't forget to add:

require "cutword"

in app/views/helpers.rb see above: require "cutword"


If you want to use the template directly:

<!-- 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>

License

MIT


Author

Demjhon Silver