Slajd 1 - Mensfeld

Download Report

Transcript Slajd 1 - Mensfeld

Chapter 4.1 – Deeper into Rails
Deeper into Rails
Presented by:
Maciej Mensfeld
senior ruby [email protected]
[email protected]
mensfeld.pl
github.com/mensfeld
Maciej Mensfeld
Chapter 4.1 – Deeper into Rails
Deeper into Rails
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
Maciej Mensfeld
Chapter 4.1 – Deeper into Rails
I18n – we speak different languages!
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.
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
Every static string in the Rails
framework — e.g. Active Record
validation messages, time and
date formats — has been
internationalized
Chapter 4.1 – Deeper into Rails
I18n – we speak different languages!
Application controller:
Maciej Mensfeld
Chapter 4.1 – Deeper into Rails
I18n – we speak different languages!
Try it
out!
Maciej Mensfeld
Chapter 4.1 – Deeper into Rails
I18n – we speak different languages!
Try it
out!
Maciej Mensfeld
Chapter 4.1 – Deeper into Rails
I18n – we speak different languages!
Workout time!
ruby -Itest test/unit/i18n_test.rb
Maciej Mensfeld
Chapter 4.1 – Deeper into Rails
Be RESTful as much as you can be!
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
Chapter 4.1 – Deeper into Rails
Be RESTful as much as you can be!
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
Chapter 4.1 – Deeper into Rails
Be RESTful as much as you can be!
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
Chapter 4.1 – Deeper into Rails
plataformatec / Responders
https://github.com/plataformatec/responders
Sets the flash based on the controller action and resource status.
Installation process:
Maciej Mensfeld
Chapter 4.1 – Deeper into Rails
plataformatec / Responders
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
Chapter 4.1 – Deeper into Rails
plataformatec / Responders
Workout time!
Install and use responders for our
Post model in our example app (or in
your app for a selected model)
Maciej Mensfeld
Chapter 4.1 – Deeper into Rails
Simplecov – Code coverage
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
Chapter 4.1 – Deeper into Rails
Simplecov – Code coverage
Edit: test_helper.rb
ruby -Itest test/unit/user_test.rb
View Rails.root/coverage.index.html
Maciej Mensfeld
Chapter 4.1 – Deeper into Rails
Haml + Simple form
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
Chapter 4.1 – Deeper into Rails
Haml + Simple form
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
Chapter 4.1 – Deeper into Rails
Haml + Simple form
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
Chapter 4.1 – Deeper into Rails
Haml + Simple form
Rails forms made easy
SimpleForm 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
Chapter 4.1 – Deeper into Rails
Haml + Simple form
Simple form can be used with erb or haml templates (or with both)
Maciej Mensfeld
Chapter 4.1 – Deeper into Rails
Haml + Simple form
Workout time!
Change your forms from Rails std forms to simple form
https://github.com/plataformatec/simple_form
Maciej Mensfeld
Chapter 4.1 – Deeper into Rails
Live long and prosper!
Presented by:
Maciej Mensfeld
[email protected]
mensfeld.pl
github.com/mensfeld
Maciej Mensfeld