Story for a Ruby on Rails Single Engineer

TylerJohnson988371 8 views 117 slides Sep 14, 2024
Slide 1
Slide 1 of 157
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83
Slide 84
84
Slide 85
85
Slide 86
86
Slide 87
87
Slide 88
88
Slide 89
89
Slide 90
90
Slide 91
91
Slide 92
92
Slide 93
93
Slide 94
94
Slide 95
95
Slide 96
96
Slide 97
97
Slide 98
98
Slide 99
99
Slide 100
100
Slide 101
101
Slide 102
102
Slide 103
103
Slide 104
104
Slide 105
105
Slide 106
106
Slide 107
107
Slide 108
108
Slide 109
109
Slide 110
110
Slide 111
111
Slide 112
112
Slide 113
113
Slide 114
114
Slide 115
115
Slide 116
116
Slide 117
117
Slide 118
118
Slide 119
119
Slide 120
120
Slide 121
121
Slide 122
122
Slide 123
123
Slide 124
124
Slide 125
125
Slide 126
126
Slide 127
127
Slide 128
128
Slide 129
129
Slide 130
130
Slide 131
131
Slide 132
132
Slide 133
133
Slide 134
134
Slide 135
135
Slide 136
136
Slide 137
137
Slide 138
138
Slide 139
139
Slide 140
140
Slide 141
141
Slide 142
142
Slide 143
143
Slide 144
144
Slide 145
145
Slide 146
146
Slide 147
147
Slide 148
148
Slide 149
149
Slide 150
150
Slide 151
151
Slide 152
152
Slide 153
153
Slide 154
154
Slide 155
155
Slide 156
156
Slide 157
157

About This Presentation

Ruby on Rails Single Engineer story


Slide Content

Ruby on Rails
The Single Engineer Framework
Radoslav Stankov

!

Radoslav Stankov
@rstankov rstankov.com

https://tips.rstankov.com

I began my web development career using
Flash, PHP, and JavaScript at an agency in
Dobrich in 2002.

I was 15 "

... after 22 years #

here some of the technologies, I
have worked with...

Careers in tech are long, and technology trends come and go.
You won't be able to have a meaningful career working with a single
technology your whole career.

- one developer (me) $
- management portal %
- implement a mobile app &
- for iOS ' / Android (

Building mapping )
Mobile app &
Issue tracker *
Employees +
Bulletin board ,
Notifications -
Taxation .
Contacts /
Omni search 0
Reporting 1
Printing 2
Bookkeeping 3
Voting 4
Financials 5
Homebook 6
Debtors 7
Bulk operations ⚙
Messaging 9
Calendar :
Funds ;
ePay / EasyPay <
Bank imports =
Invoicing >
Warranty Issues ?

Technicians @

Individual accounts A
Business accounts )
Audit logs B

Archiving C
Trials D
Demo E

I18n F G H

Marketing site I

& Mobile stack J

% Web stack J

Ruby on Rails and its ecosystem enabled me to develop Angry Building
single-handedly. It felt like having a cheat code.

Ruby on Rails

Ruby on Rails

Ruby on Rails

Ruby on Rails

Ruby
Ruby is an interactive, object-
oriented programming language.
Its motto is:
"Optimized for programmer
happiness"

https://rubygems.org/

Ruby on Rails
Ruby on Rails is a web application framework.

Who uses Ruby? K

... Ruby on Rails old and boring,
designed for monoliths L

https://newsletter.pragmaticengineer.com/p/what-is-old-is-new-again

https://newsletter.pragmaticengineer.com/p/what-is-old-is-new-again

... one interesting story

... one interesting story
... maybe $

https://github.com/maybe-finance/maybe

Ruby

M Syntax
N Symbols
N Blocks
O Expressions
P Everything is object
Q Open classes

Variables
a = 1 # Number
b = "string" # String
c = [1, 2, 3, 4] # Array
d = { key: 'value' } # Hash

Symbols
a = :key
hash[:key]

Everything is an object
person.name
"string".size
array.empty?
array[0] == array.first
array[1] == array.second
array[array.size - 1] == array.last
1.day.ago
1.day.ago - 2.hours

puts("string")
puts "string"
Method calling

Blocks
5.times { puts "Ruby is cool!" }
5.times do
puts "Ruby is cool!"
end

Blocks
[1,2,3,4].each { |value| puts value }
# => [1, 2, 3, 4]
[1,2,3,4].map { |value| value * value }
# => [1, 4, 9, 16]
[1,2,3,4].reduce { |a, b| a + b }
# => 10

Blocks
[1,2,3,4].each { puts _1 }
# => [1, 2, 3, 4]
[1,2,3,4].map { _1 * _1 }
# => [1, 4, 9, 16]
[1,2,3,4].reduce { _1 + _1 }
# => 10

Blocks
File.open('file.txt') do |file|
file.write 'content'
end

Blocks
File.open('file.txt') do |file|
file << 'content'
end

Blocks
File.open('file.txt') do
_1 << 'content'
end

Blocks
File.open('file.txt') { _1 << 'content' }

Blocks
def method_with_block(value)
yield value
end
value = method_with_block( 5) { |v| v + 4 }
puts value # 9

Blocks
def method_with_block(value, &block)
block.call(value)
end
value = method_with_block( 5) { |v| v + 4 }
puts value # 9

Expressions
def explain_number(value)
if value % 2
result = 'even'
else
result = 'odd'
end
return result
end

Expressions
def explain_number(value)
if value % 2
result = 'even'
else
result = 'odd'
end
result
end

Expressions
def explain_number(value)
if value.even?
result = 'even'
else
result = 'odd'
end
result
end

Expressions
def explain_number(value)
result = if value.even?
'even'
else
'odd'
end
result
end

Expressions
def explain_number(value)
if value.even?
'even'
else
'odd'
end
end

Expressions
def error_message(value)
if !value.present?
return nil
end
return 'value is empty'
end

Expressions
def error_message(value)
if !value.present?
return nil
end
'value is empty'
end

Expressions
def error_message(value)
unless value.present?
return nil
end
'value is empty'
end

Expressions
def error_message(value)
return nil unless value.present?
'value is empty'
end

Expressions
def error_message(value)
return unless value.present?
'value is empty'
end

Expressions
def error_message(value)
'value is empty' if value.blank?
end

Expressions
def error_message(value)
return 'value is empty' if value.blank?
return "value #{value} is to long" if value.size > 100
end

Expressions
def error_message(value)
if value.blank?
'value is empty'
elsif value.size > 100
"value #{value} is to long"
end
end

Classes
me = Person.new("Radoslav", "Stankov")

me.first_name # Radoslav
me.last_name # Stankov
me.full_name # Radoslav Stankov

class Person
def initialize(first_name, last_name)
@first_name = first_name
@last_name = last_name
end
def first_name
@first_name
end
def first_name=(value)
@first_name = value
end
Classes

@first_name
end
def first_name=(value)
@first_name = value
end
def last_name
@last_name
end
def last_name=(value)
@last_name = value
end
def full_name
"#{first_name} + #{last_name}"
end
end
Classes

Classes
class Person
attr_reader :first_name, :last_name
attr_writer :first_name, :last_name
def initialize(first_name, last_name)
@first_name = first_name
@last_name = last_name
end
def full_name
"#{first_name} + #{last_name}"
end
end

Classes
class Person
attr_accessor :first_name, :last_name
def initialize(first_name, last_name)
@first_name = first_name
@last_name = last_name
end
def full_name
"#{first_name} + #{last_name}"
end
end

Meta programming
class Person
attr_accessor :first_name, :last_name
end

Meta Programming

R Nothing is special
S Everything is an object
T No definitions, just executions
U Cool syntax V
Meta programming

Open classes
class Array
def first
self[0]
end
def second
self[1]
end
end
array = [1, 2, 3, 4]
array.first # 1
array.second # 2

class MyClass
puts "I'll be executed"
unless RUBY_VERSION >= "3.3.3"

puts "I'll be executed after ruby 3.3.3 "
def some_method
# ... code
end
end
end
No definitions, just executions

Meta programming
class Person
attr_accessor :first_name, :last_name
end

Meta programming
class Person
def first_name
@first_name
end
def first_name=(value)
@first_name = value
end
end

Meta programming
class Person
define_method(:first_name) do
@first_name
end
define_method(:first_name=) do |value|
@first_name = value
end
end

Meta programming
class Person
define_method(:first_name) do
instance_variable_get( :@first_name)
end
define_method(:first_name=) do |value|
instance_variable_set( :@first_name, value)
end
end

Meta programming
class Person
define_method(:first_name) do
instance_variable_get :@first_name
end
define_method(:first_name=) do |value|
instance_variable_set :@first_name, value
end
end

Meta programming
class Person
attr_accessor :first_name, :last_name
end

Meta programming
class Person
def self.attr_accessor(attribute)
define_method(attribute) do
instance_variable_get :"@#{attribute}"
end
define_method(:"#{attribute}=") do |value|
instance_variable_set :"@#{attribute}", value
end
end
end

Meta programming
class Person
def self.attr_accessor(*attributes)
attributes.each do |attribute|
define_method(attribute) do
instance_variable_get :"@#{attribute}"
end
define_method(:"#{attribute}=") do |value|
instance_variable_set :"@#{attribute}", value
end
end
end
end

Meta programming
class Person
attr_accessor :first_name, :last_name
end

W Model View Controller (MVC)
X Batteries included
Y Convention over configuration
Z Extendible platform

W Model View Controller (MVC)

W Model View Controller (MVC)

W Model View Controller (MVC)

W Model View Controller (MVC)

W Model View Controller (MVC)

W Model View Controller (MVC)

W Model View Controller (MVC)

W Model View Controller (MVC)

W Model View Controller (MVC)

W Model View Controller (MVC)

ActiveSupport - utilities
ActiveModel - base model & validations
ActiveRecord - object relation mapper (ORM)
ActiveJob - background job processing
ActiveStorage - file uploads
ActionController - Controller from MVC
ActionView - View from MVC
ActionMailer - Sending emails
ActionMailbox - Receiving & processing emails
ActionCable - Web Socket & real time
ActionText - Rich text editing
Propshaft - assets delivery
I18n - internalization
X Batteries included

Y Convention over configuration

https://github.com/maybe-finance/maybe
https://github.com/discourse/discourse
https://github.com/forem/forem
https://github.com/mastodon/mastodon
https://github.com/thoughtbot/upcase
https://github.com/feedbin/feedbin
https://gitlab.com/gitlab-org/gitlab
Open Source Apps

https://blog.rstankov.com/top-8-ruby-on-rails-engines/
Z Extendible platform

Sidekiq

Blazer

Lookbook

Flipper

GraphiQL

... and a lot more ✨

https://rubygems.org/

rails new blog
rails generate scaffold post title:string content:text

db/migrate/[...]_create_posts.rb
class CreatePosts < ActiveRecord::Migration[7.1]
def change
create_table :posts do |t|
t.string :title, null: false
t.text :content, null: false
t.timestamps
end
end
end

rails db:migrate

class Post < ApplicationRecord
validates :title, presence: true
validates :content, presence: true
end
app/models/post.rb

Rails.application.routes.draw do
resources :posts
# Define your application routes per the DSL in
# https://guides.rubyonrails.org/routing.html
# Reveal health status on /up that returns 200
# if the app boots with no exceptions, otherwise 500.
# Can be used by load balancers and uptime monitors to
# verify that the app is live.
get "up" => "rails/health#show", as: :rails_health_check
# Defines the root path route ("/")
# root "posts#index"
end
config/routes.rb

class PostsController < ApplicationController
# GET /posts
def index
@posts = Post.all
end
# GET /posts/1
def show
@post = Post.find(params[:id])
end
# GET /posts/new
def new
@post = Post.new
end
# GET /posts/1/edit
def edit
@post = Post.find(params[:id])
end
# POST /posts
def create
@post = Post.new(post_params)
app/controllers/posts_controller.rb

# POST /posts
def create
@post = Post.new(post_params)
if @post.save
redirect_to post_url( @post), notice: "Post was successfully created. "
else
render :new, status: :unprocessable_entity
end
end
# PATCH/PUT /posts/1
def update
@post = Post.find(params[:id])
if @post.update(post_params)
redirect_to post_url( @post), notice: "Post was successfully updated. "
else
render :edit, status: :unprocessable_entity
end
end
# DELETE /posts/1
def destroy
app/controllers/posts_controller.rb

if @post.update(post_params)
redirect_to post_url( @post), notice: "Post was successfully updated. "
else
render :edit, status: :unprocessable_entity
end
end
# DELETE /posts/1
def destroy
@post = Post.find(params[:id])
@post.destroy!
redirect_to posts_url, notice: "Post was successfully destroyed. "
end
private
def post_params
params.require(:post).permit(:title, :content)
end
end
app/controllers/posts_controller.rb

<p style="color: green"><%= notice %></p>
<h1>Posts</h1>
<div id="posts">
<% @posts.each do |post| %>
<%= render post %>
<p>
<%= link_to "Show this post", post %>
</p>
<% end %>
</div>
<%= link_to "New post", new_post_path %>
app/views/posts/index.html.erb

<%= form_with(model: post) do |form| %>
<% if post.errors.any? %>
<div style="color: red">
<h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved: </h2>
<ul>
<% post.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div>
<%= form.label :title, style: "display: block" %>
<%= form.text_field :title %>
</div>
<div>
<%= form.label :content, style: "display: block" %>
<%= form.text_area :content %>
</div>
<div>
<%= form.submit %>
</div>
<% end %>
app/views/posts/_form.html.erb

rails server

rails action_text:install

bundle

rails server

class Post < ApplicationRecord
validates :title, presence: true
validates :content, presence: true
has_rich_text :content
end
app/models/post.rb

<%= form_with(model: post) do |form| %>
<% if post.errors.any? %>
<div style="color: red">
<h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved: </h2>
<ul>
<% post.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div>
<%= form.label :title, style: "display: block" %>
<%= form.text_field :title %>
</div>
<div>
<%= form.label :content, style: "display: block" %>
<%= form.rich_text_area :content %>
</div>
<div>
<%= form.submit %>
</div>
<% end %>
app/views/posts/_form.html.erb

https://rubyonrails.org/

Learning resources

https://tips.rstankov.com

https://www.youtube.com/watch?v=X2sgQ38UDVY

https://bit.ly/4cznIfA

https://rubybanitsa.com/

https://balkanruby.com/

https://rstankov.com/appearances
Tags