Chapter 4.1 – Deeper into Rails
Maciej Mensfeld
Presented by:
Maciej Mensfeld
Deeper into Rails
github.com/mensfeld
senior ruby [email protected]
Maciej Mensfeld
Please…
• …ask me to slow down, if I speak to quickly;• …ask me again, if I forget;
• …ask questions, if anything i say is not clear;• …feel free to share your own observations
Deeper into RailsChapter 4.1 – Deeper into Rails
Maciej Mensfeld
I18n – we speak different languages!Chapter 4.1 – Deeper into Rails
I18n - Easy-to-use and extensible framework for translating your application to a single custom language other than English or for providing multi-language support in your application. Every static string in the Rails
framework — e.g. Active Record validation messages, time and
date formats — has been internationalized
You may be tempted to store the chosen locale in a session or a cookie. Do not do so. The locale
should be transparent and a part of the URL. This way you don’t break people’s basic assumptions about the web itself: if you send a URL of some
page to a
http://guides.rubyonrails.org/i18n.html
Maciej Mensfeld
I18n – we speak different languages!Chapter 4.1 – Deeper into Rails
Application controller:
Maciej Mensfeld
I18n – we speak different languages!Chapter 4.1 – Deeper into Rails
Try it out!
Maciej Mensfeld
I18n – we speak different languages!Chapter 4.1 – Deeper into Rails
Try it out!
Maciej Mensfeld
I18n – we speak different languages!Chapter 4.1 – Deeper into Rails
Workout time!
ruby -Itest test/unit/i18n_test.rb
Maciej Mensfeld
Be RESTful as much as you can be!Chapter 4.1 – Deeper into Rails
Representational State Transfer
Representational State Transfer (REST) is a style of software architecture for distributed systems such as the World Wide Web.
• Scalability of component interactions• Generality of interfaces• Independent deployment of components• Intermediary components to reduce latency, enforce security and encapsulate legacy systems
Maciej Mensfeld
Be RESTful as much as you can be!Chapter 4.1 – Deeper into Rails
Don’t work with different models in one controller!
Don’t use non standard actions (unless you really know what you are doing!)
Think with resources, not with actions!
Use restful routes (resources :users, etc)
Maciej Mensfeld
Be RESTful as much as you can be!Chapter 4.1 – Deeper into Rails
Use scopes and namespaces!
Namespaces are good to separate different logic with same controller names (in different modules)
Namespacing stuff helps you manage growing projects
Maciej Mensfeld
plataformatec / RespondersChapter 4.1 – Deeper into Rails
Sets the flash based on the controller action and resource status.
Installation process:
https://github.com/plataformatec/responders
Maciej Mensfeld
plataformatec / RespondersChapter 4.1 – Deeper into Rails
Responders uses the ‘valid?’ method to determine whether or not the object passed to ‘respond_with’ is valid
You can also pass a :location parameter to redirect to a specified route
Maciej Mensfeld
plataformatec / RespondersChapter 4.1 – Deeper into Rails
Workout time!
Install and use responders for our Post model in our example app (or in your
app for a selected model)
Maciej Mensfeld
Simplecov – Code coverageChapter 4.1 – Deeper into Rails
Code coverage is a measure used in software testing. It describes the degree to which the source code of a program has been tested.
Primary gem for code coverage in Ruby project is Simplecov
The main advantage of running a coverage tool on your test suite is to find areas of your code that are poorly tested.
Maciej Mensfeld
Simplecov – Code coverageChapter 4.1 – Deeper into Rails
Edit: test_helper.rb
ruby -Itest test/unit/user_test.rb
View Rails.root/coverage.index.html
Maciej Mensfeld
Haml + Simple formChapter 4.1 – Deeper into Rails
Haml (HTML abstraction markup language) is based on one primary principle: markup should be beautiful. It’s not just beauty for beauty’s sake either; Haml accelerates and
simplifies template creation down to veritable haiku.
gem ‘haml’ # Gemfile => bundle install
Maciej Mensfeld
Haml + Simple formChapter 4.1 – Deeper into Rails
By default Haml uses DIVs
Class declaration starts with dot (.class_name)
Id declaration starts with hash
(#super_id)
To create „not DIV” use % (%h2)
All Haml elements close automatically
You can mix id, class and other attributes!
Rich example: %h3.extra#content{:title => ‘Header title’}
Maciej Mensfeld
Haml + Simple formChapter 4.1 – Deeper into Rails
Workout time!
Change your App layout from erb to haml.
Change your Post views from erb to haml
HTML2HAML converter: http://html2haml.heroku.com/
Maciej Mensfeld
Haml + Simple formChapter 4.1 – Deeper into Rails
Rails forms made easySimpleForm aims to be as flexible as
possible while helping you with powerful components to create your forms. The
basic goal of SimpleForm is to not touch your way of defining the layout, letting
you find the better design for your eyes.
https://github.com/plataformatec/simple_form
Maciej Mensfeld
Haml + Simple formChapter 4.1 – Deeper into Rails
Simple form can be used with erb or haml templates (or with both)
Maciej Mensfeld
Haml + Simple formChapter 4.1 – Deeper into Rails
Workout time!
Change your forms from Rails std forms to simple form
https://github.com/plataformatec/simple_form
Chapter 4.1 – Deeper into Rails
Maciej Mensfeld
Live long and prosper!
Presented by:
Maciej Mensfeld
github.com/mensfeld