% Apéro on Rails % Jean-François Labbé % 04 07 2019
Dans 45 minutes
1 - What is Ruby on Rails?
2 - Démo
- Framework d'application web type MVC
- Langage Ruby
- V1.0 en 2005
- Créateur David Heinemeier Hansson (DHH)
- Extrait d'une application (Basecamp)
- Première version en 1995 (comme Java)
- Inspiré de: Lisp, Samlltalk, Perl, Python
- Langage interprété
- Orienté Objet
- Tout est Objet
# Java
public class HelloWorld {
public static void main(String[] args)
{
for (int i = 0; i < 3; i++)
{
System.out.println("Hello, world!");
}
}
}
# Ruby
3.times do
puts "Hello, world!"
end
welcome = "Hello World"
status = :in_progress # Symbole
array = [1, "Bonjour", nil] # Tableau non typé
hash = { title: "Apéro", content: "Ruby on Rails", attendees: 27 }
nil # représente null
1.class # => Integer
"hello".class # => String
1.nil? # => false
nil.nil? # => NilClass
Github, Gitlab, Airbnb, Shopify, 500px, Twitch, Zendesk
Rails doesn't scale?
Shopify: 600_000 site marchands, 80_000 req/s (max) [Source 2018](https://engineering.shopify.com/blogs/engineering/e-commerce-at-scale-inside-shopifys-tech-stack)
Dev.to, Discourse, E-petitions, Gitlab, Mastodon, Rubygems...
- Sélection d'outils
- Convention over Configuration
- DRY (don't repeat yourself)
- CRUD (Create, Read, Update, Delete)
- REST (Representational state transfer)
- Basé sur Rack Ruby Webserver Interface
- Parallel testing
- Wepacker
- Multiple databases
- ActionMailbox
- ActionText
- Développement rapide
- Beaucoup de librairies
- Maintenabilité
- Intégrations (front, apis, services)
- Tests
- Performances
- Peut être lourd pour des besoins simples
- Développeurs?
- Une appli web
- E-commerce
- Prototypes
├── app
│ ├── assets
│ ├── channels
│ ├── controllers
│ ├── helpers
│ ├── javascript
│ ├── jobs
│ ├── mailers
│ ├── models
│ └── views
├── Gemfile
├── db
│ └── migrate
├── log
├── public
├── test
│ ├── channels
│ ├── controllers
│ ├── fixtures
│ ├── helpers
│ ├── integration
│ ├── mailers
│ ├── models
│ ├── system
- Migrations de la base de données
- Gestionnaire de dépendances
- Générateurs
- Console
- Administration: Administrate, ActiveAdmin
- Authentification: Devise
- Autorisation: ActionPolicy, CanCanCan, Pundit
- Async jobs: Sidekick
- Login Google, Facebook, Twitter: Omniauth
- Elasticsearch: Searchkick
- Feature flags: Flipper
- Filtering: Ransack
- Kafka: Karafka
- Mock http: Vcr
<h1>Apéro on Rails</h1>
<p>Time: <%= Time.now %></p>
controllers/posts_controller.rb
def index
@posts = Post.all
end
posts/index.html.erb
<h1>Les posts</h1>
<% @posts.each do |post| %>
<h2><%= @post.title %></h2>
<p><%= @post.content %></p>
<% end %>
controllers/posts_controller.rb
def index
@posts = Post.all
end
posts/index.html.erb
<h1>Les posts</h1>
<% @posts.each do |post| %>
<%= render post %>
<% end %>
posts/_post.html.erb
<h2><%= @post.title %></h2>
<p><%= @post.content %></p>
Rails.application.routes.draw do
resources :posts
end
url | Posts controlleur |
---|---|
GET /posts | #index |
GET /posts/:id | #show |
GET /posts/new | #new |
POST /posts | #create |
GET /posts/:id/edit | #edit |
PUT /posts/id | #update |
DELETE /posts/id | #destroy |
Un blog est composé de posts
Un post a des commentaires
rails generate scaffold post title content:text
class CreateArticles < ActiveRecord::Migration[6.0]
def change
create_table :posts do |t|
t.string :title
t.text :content
t.timestamps
end
end
end
class Post < ApplicationRecord
end
vide?
array = [-1, -2, -3]
array.map { |number| number.abs } # => [1, 2, 3]
.map { |number| number * 2 } # => [2, 4, 6]
def welcome(name: "bob")
puts "Welcome #{name}"
end
welcome # => Welcome bob
welcome("john") # => Welcome john
def saved?
true
end
def overwrite!
...
end
class World
def initialize(name)
@name = name # Instance variable
end
def name
@name
end
def name=(name)
@name = name
end
end
world = World.new("level 1")
world.name # => "level 1"
world.name = "new world" # => "new world"
class World
attr_accessor :name
def initialize(name)
@name = name # Instance variable
end
end
world = World.new("level 1")
world.name # => "level 1"
world.name = "new world" # => "new world"