Rails helpers based on opinionated project practices. Currently useful for structuring CSS and JS.
gem install 'rails_utils'
This helper method returns controller name and action name as a single string value, which can be used to target CSS styles specifically for this controller or action.
For example, when controller and action is anime#show
,
you can use page_class
to include the controller name and action name as CSS classes on the page.
%body{ class: page_class }
becomes
<body class='anime show'>
Then in your CSS, you can either target your styles specific to controller and/or action.
body.anime
background: black
body.anime.show
font-size: 24px
Usually, when the create
or update
actions render, the new
or edit
views will be rendered due to a form error.
Therefore the page_class
helper converts create
to new
and update
to edit
so that you only need to write CSS to target new
and edit
, and not all four actions.
For finer grained control, you can also choose the use the 2 methods that are used to build page_class
individually.
The two methods are page_controller_class
and page_action_class
.
This helper method returns page title based on controller name and action name.
When controller and action is anime#show
you can easily use page_title
like
.page-title= page_title
becomes
<div class='page-title'>Anime Show</div>
Besides, it supports I18n and interpolation:
en:
anime:
show:
title: Showing anime of: %{anime_name}
.page-title= page_title(anime_name: 'Frozen')
becomes
<div class='page-title'>Showing anime of: Frozen</div>
This helper method attempts to initialize JavaScript classes and methods based on a standard structure.
With this standard structure, calling your JavaScript has never been easier.
Add javascript_initialization
to the bottom of your layout.
= javascript_initialization
When application is MyApp, and controller/action is anime#show
, javascript_initialization
compiles to:
<script type="text/javascript">
//<![CDATA[
MyApp.init();
if(MyApp.anime) {
if(MyApp.anime.init) { MyApp.anime.init(); }
if(MyApp.anime.init_show) { MyApp.anime.init_show(); }
}
//]]>
</script>
By looking at the compiled JavaScript output, it should be apparent on how you should structure your JavaScript.
As similar to page_class
, create
is mapped to new
and update
is mapped to edit
.
// Sample CoffeeScript to get you started
window.MyApplication =
init: ->
console.log("Init!")
This helper method prints Rails flash messages with classes that correspond to Bootstrap's convention.
Just invoke flash_messages
anywhere within layout/application
.
= flash_messages
Suppose there's a flash[:success]
, you should see:
<div class="alert alert-success fade in">
<button class="close" data-dismiss-alert="alert" type="button">x</button>
<p>flash is success</p>
</div>
Pull Requests are very welcomed (with specs, of course)!
Minitest-ed. To run all tests, just run rake
or rake test
.
Version 3.2.1
- Update
page_title
to support I18n interpolation - by @JuanitoFatas.
Version 3.2.0
- Add
page_title
that supports I18n - by @huynhquancam.
Version 3.1.2
- Add
alert-danger
class to flash error messages for Bootstrap 3 support.
Version 3.1.1
- Pull Request 1 Support for Rails 4.1.
Version 3.1.0
- Drop string
controller
frompage_class
andjavascript_initialization
.
Version 3.0.0
- Controller namespace added to
page_class
andjavascript_initialization
.
Rails Utils is maintained by Winston Teo.
You should follow Winston on Twitter, or find out more on WinstonYW and LinkedIn.
Copyright © 2014 Winston Teo Yong Wei. Free software, released under the MIT license.