https://github.com/demjhonsilver/cutword
A simple gem for truncating words/strings or shortening words and names, compatible with Rails and Hanami Framework.
https://github.com/demjhonsilver/cutword
hanami hanami-application rails ruby string truncate
Last synced: 6 months ago
JSON representation
A simple gem for truncating words/strings or shortening words and names, compatible with Rails and Hanami Framework.
- Host: GitHub
- URL: https://github.com/demjhonsilver/cutword
- Owner: demjhonsilver
- License: other
- Created: 2023-10-03T15:48:34.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-03T12:28:51.000Z (about 1 year ago)
- Last Synced: 2024-10-31T13:54:59.344Z (12 months ago)
- Topics: hanami, hanami-application, rails, ruby, string, truncate
- Language: Ruby
- Homepage: https://rubygems.org/gems/cutword
- Size: 85.9 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
Cutword
[](https://badge.fury.io/rb/cutword)
## Table of Contents
- [Description](#description)
- [Release Notes](#release-notes)
- [Features](#features)
- [Installation](#installation)
- [Paradigm](#paradigm)
- [Example](#example)
- [Rails Framework](#rails-framework)
- [Hanami 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.
```bash
gem "cutword", "~> 2.0"
```
Step 2:
- Run this to your Terminal
```bash
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
```rb
<% @articles.each do |article| %>
<%= Cutword(20, article.title) %>
<%= article.body %>
<% end %>```
ex. show.html
```rb
<%= Cutword(20, @article.title) %>
```
----------------
Method 2
--------------```rb
# app/controllers/articles_controller.rbclass 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:
```rb
<% @articles.each_with_index do |article, index| %>
<%= @articleTitles[index] %>
<%= article.body %>
<% end %>```
--------------------
-----------------
```rb
# app/controllers/articles_controller.rbclass ArticlesController < ApplicationController
def show
@article = Article.find(params[:id])
@articleTitle = Cutword(40, @article.title)
end
end```
In your View Template:
```rb
<%= @articleTitle %>
<%= @article.body %>```
## Hanami-framework
`Hanami version 2.1.0 or greater`
-------
Update config/app.rb with below content
```rbrequire 'hanami'
require 'cutword'```
-----------------------
## Method 1 - Hanami
go to:
app/views/books/index.rb
```rb
# app/views/books/index.rbmodule 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
```ruby
# 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])
endbook_data
end
end
end
end
end
```------------
or
## Method 2 - Hanami
-------
```html
Books
- <%= Cutword(7, book[:title]) %>, by <%= book[:author] %>
<% books.each do |book| %>
<% end %>
```
```html
<%= Cutword(7, book[:title]) %>
By <%= book[:author] %>
```
## License
[MIT](http://www.opensource.org/licenses/MIT)
----------------------------------------------------
## Author
Demjhon Silver