tranzito_utils is a ruby gem containing these Rails helpers, concerns and services:
| Name | Type | Description |
|---|---|---|
SetPeriod |
Controller Concern | Time period selection and browsing |
SortableTable |
Controller Concern | Sort column and direction |
TimeParser |
Service | Parse time strings into activerecord time objects |
Normalize |
Service | Normalize truthy and falsey strings |
GraphingHelper |
Helper | Graphing helper for chartkick charts |
SortableHelper |
Helper | Sort table headers |
Install the gem by adding this line to your application's Gemfile:
gem "tranzito_utils"Then run bundle install
To include the config file inside your host app run this command.
rails g tranzito_utils:installThis will add the tranzito_utils.rb file in the initializer where you can alter the default behaviour of several variables being used throughout the gem.
To use the SortableTable module, include it in your required controllers:
include TranzitoUtils::SortableTableYou can use the following methods of TranzitoUtils::SortableTable in your controllers. These are also the helper methods.
sort_column, sort_directionInclude the SetPeriod module in your ApplicationController by adding this line:
include TranzitoUtils::SetPeriodAdd this line in the controller where you want to use the SetPeriod module:
before_action :set_period, only: [:index]SetPeriod depends on a partial file to show the period_select view for filtering. Include in your views with this:
<%= render partial: "/tranzito_utils/period_select" %>For gems helpers, you need to add this into your application_helper.rb file.
include TranzitoUtils::HelpersYou can include the flash messages into your application by add it's partial in your layouts file.
<%= render partial: "/tranzito_utils/flash_messages" %>To include the styles from the gem you need to add this into your application.scss, this will include the compiled CSS into your application.
@import url('/tranzito_utils-compiled.css')You also need to add this NPM package in order to use the gem without any issue. You can install it using yarn or npm.
For yarn:
yarn add tranzito_utils_js
For npm
npm install tranzito_utils_jsThen import it into your application by adding this line in application.js or any .js file where you need it:
import { PeriodSelector, TimeParser } from "tranzito_utils_js"Initialize them like this:
document.addEventListener('DOMContentLoaded', function () {
if (!window.timeParser) { window.timeParser = new TimeParser() }
window.timeParser.localize()
if (document.getElementById('timeSelectionBtnGroup')) {
const periodSelector = new PeriodSelector()
periodSelector.init()
}
}(if using turbo, switch 'DOMContentLoaded' to 'turbo:load')
The gem is available as open source under the terms of the MIT License.
Compile the updated styles:
cd tranzito_utils_js
yarn build:cssTo setup testing, you have to create the database:
cd spec/dummy
RAILS_ENV=test bundle exec rails db:create db:schema:load db:migrate