Rails Concerns and Turbolinks Presented by Zahidul Hasan
Rails Concerns and Turbolinks
Presented by
Zahidul Hasan
What is Concerns?
• Concerns provides a way that allow us to keep our code organizedand DRY
• Basically, the idea is to extract common and/or context specific chunks of code in order to clean up the models/controllers and avoid them from getting too fat and messy
More Information
• Concerns are supported by Rails 3 but have become more official in Rails 4
• Those who have worked with a Rails 4 app, might have probably noticed the app/models/concerns and app/controllers/concernsdirectories
Why should we use concerns?
• DRYing up model codes
• Skin-nizing Fat Models
DRYing up model codes
Create a commentable.rb file in app/model/concerns
Skin-nizing Fat Models
• Create a attendable.rb file in app/model/concern/event folder
• Create a commentable.rb file in app/model/concern/event folder
Controller Concerns
• Controllers can also have concerns
• Purpose will be the same: DRYing
Turbolinks
• Feature of rails 4
• Prevents the browser from reloading the JavaScript and CSS between each page change
• Its almost similar to PJAX
JavaScript pushState
• Javascript API for HTML5: History Interface
• pushState can be used to store the navigation history
PJAX
• PJAX is a combination of the terms pushState and AJAX
• Instead of reloading the whole page when clicking a link, just load the part of the page that needs updating
• On the client-side we have to define the container that should be replaced when a link is clicked
Turbolinks
• Instead of replacing only parts a page it loads a complete website from the server and replaces the <title> and <body> in the currently loaded DOM
• Unlike PJAX we don't have to mark links and containers, Turbolinkswill handle that for us
Comparison between Turbolinks and PJAX
• Turbolinks definitely improve client-side page loading, but the server still has to render the complete website
• PJAX renders only those parts of a website that really need to be updated, need additional work when developing the application
• Turbolinks will improve page load significantly if the pages share JavaScript and CSS styling. PJAX comes in handy, when server-side performance is an issue
Thank You