Slide 1

Slide 1 text

TORINO !ODING SO!IETY ~ 14/10/2013 Runnin g On Rails @a"#h4m A SPEEDY INTRODUCTION TO RUBY ON RAILS.

Slide 2

Slide 2 text

[{ fullname: “Ju Liu”, twitter: “@a!"h4m” }]

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Why Rails

Slide 5

Slide 5 text

« » DAVID HEINEMEIER HANSSON Ruby is a dynamic and object-oriented programming language created in 1995 by Yukihiro Matsumoto. Ruby is, more than anything else, a language for writing beautiful code that makes programmers happy.

Slide 6

Slide 6 text

« » Rails, then, is an attempt to mold the beauty and productiveness of Ruby into a solution for Web applications. We’ve sought to adhere to the same core principle that guided the development of Ruby: make the programmer happy! DAVID HEINEMEIER HANSSON

Slide 7

Slide 7 text

≈ WHY HAPPINESS? The Single Most Important Thing™ for a programmer is motivation Happy programmers are motivated programmers Motivated programmers are way more productive Productivity is GOOD!

Slide 8

Slide 8 text

Convention Over Configuration

Slide 9

Slide 9 text

≈ EVERY PROJECT IS UNIQUE® Every developer has its own taste in tools and practices Every team has its own peculiarities Projects are not inter-changeable: Customer lock-in Developer lock-in

Slide 10

Slide 10 text

≈ They make projects repeatable and instantly familiar. Everyone likes familiar! It’s much easier to: Start a new project Take on an existing project WE NEED SOME KIND OF FRAMEWORK

Slide 11

Slide 11 text

≈ THE STRUCTURE OF ANY RAILS PROJECT ᴷᴷ app ᴹ ᵓᴷᴷ asse$s ᴹ ᴹ ᵓᴷᴷ %ma&es/ ᴹ ᴹ ᵓᴷᴷ 'a(as)"%p$s/ ᴹ ᴹ ᵋᴷᴷ s$*+eshee$s/ ᴹ ᵓᴷᴷ )on$"o++e"s/ ᴹ ᵓᴷᴷ he+pe"s/ ᴹ ᵓᴷᴷ ma%+e"s/ ᴹ ᵓᴷᴷ mode+s/ ᴹ ᵋᴷᴷ (%ews/ ᵋᴷᴷ )on,%& ᵓᴷᴷ app+%)a$%on."b ᵓᴷᴷ boo$."b ᵓᴷᴷ da$abase.*m+ ᵓᴷᴷ en(%"onmen$."b ᵓᴷᴷ en(%"onmen$s/ ᵓᴷᴷ %n%$%a+%ze"s/ ᵓᴷᴷ +o)a+es/ ᵋᴷᴷ "ou$es."b

Slide 12

Slide 12 text

≈ CONVENTIONS: MVC STACK Browser Router Controller Model View Postgres/MySQL

Slide 13

Slide 13 text

A look inside Rails

Slide 14

Slide 14 text

≈ CONVENTIONS: MVC STACK Browser Router Controller Model View Postgres/MySQL

Slide 15

Slide 15 text

≈ CONVENTIONS: DATABASE ABSTRACTION !"ass Pos! < A"!#$eRe"o%d::Base end

Slide 16

Slide 16 text

≈ CONVENTIONS: DATABASE ABSTRACTION !"ass Pos! < A"!#$eRe"o%d::Base end pos! & Pos!.new pos!.!#!'e & “To%#no (ode%s” pos!.sa$e #$ue

Slide 17

Slide 17 text

≈ CONVENTIONS: DATABASE ABSTRACTION !"ass Pos! < A"!#$eRe"o%d::Base end pos! & Pos!.new pos!.!#!'e & “To%#no (ode%s” pos!.sa$e #$ue pos#s pos#s pos#s %d #%#"e !$ea#ed_a# 1 To%#no (ode%s 2013-10-14

Slide 18

Slide 18 text

≈ CONVENTIONS: DATABASE ABSTRACTION !"ass Pos! < A"!#$eRe"o%d::Base $a'#da!es :#%#"e, p%esen"e: #$ue end

Slide 19

Slide 19 text

≈ CONVENTIONS: DATABASE ABSTRACTION !"ass Pos! < A"!#$eRe"o%d::Base $a'#da!es :#%#"e, p%esen"e: #$ue end pos! & Pos!.new(!#!'e: "") pos!.sa$e &a"se

Slide 20

Slide 20 text

≈ CONVENTIONS: DATABASE ABSTRACTION !"ass Pos! < A"!#$eRe"o%d::Base $a'#da!es :#%#"e, p%esen"e: #$ue end pos! & Pos!.new(!#!'e: "") pos!.sa$e &a"se pos!.e%%o%s.messa)es {:#%#"e'>["non può esse$e "as!%a#o %n b%an!o"]}

Slide 21

Slide 21 text

≈ CONVENTIONS: DATABASE ABSTRACTION !"ass Pos! < A"!#$eRe"o%d::Base $a'#da!es :#%#"e, p%esen"e: #$ue has_man* :!ommen#s end

Slide 22

Slide 22 text

≈ CONVENTIONS: DATABASE ABSTRACTION !"ass Pos! < A"!#$eRe"o%d::Base $a'#da!es :#%#"e, p%esen"e: #$ue has_man* :!ommen#s end pos! & Pos!.new(!#!'e: "Foo") pos!."ommen!s << (ommen!.new(!ex!: "(oo'!") pos!.sa$e

Slide 23

Slide 23 text

≈ CONVENTIONS: DATABASE ABSTRACTION !"ass Pos! < A"!#$eRe"o%d::Base $a'#da!es :#%#"e, p%esen"e: #$ue has_man* :!ommen#s end pos! & Pos!.new(!#!'e: "Foo") pos!."ommen!s << (ommen!.new(!ex!: "(oo'!") pos!.sa$e ( INSERT INTO "pos#s" ("#%#"e") )ALUES (..) ( INSERT INTO "!ommen#s" ("#ex#") )ALUES (..)

Slide 24

Slide 24 text

≈ CONVENTIONS: DATABASE ABSTRACTION !"ass Pos! < A"!#$eRe"o%d::Base $a'#da!es :#%#"e, p%esen"e: #$ue has_man* :!ommen#s end Pos!.whe%e(!#!'e: "Foo").'#m#!(2)

Slide 25

Slide 25 text

≈ CONVENTIONS: DATABASE ABSTRACTION !"ass Pos! < A"!#$eRe"o%d::Base $a'#da!es :#%#"e, p%esen"e: #$ue has_man* :!ommen#s end Pos!.whe%e(!#!'e: "Foo").'#m#!(2) SELE*T "pos#s".* FROM "pos#s" WHERE #%#"e ' "Foo" LIMIT 2

Slide 26

Slide 26 text

≈ CONVENTIONS: DATABASE ABSTRACTION !"ass Pos! < A"!#$eRe"o%d::Base $a'#da!es :#%#"e, p%esen"e: #$ue has_man* :!ommen#s s"ope :pub"%shed, whe%e(pub'#shed: #$ue) end

Slide 27

Slide 27 text

≈ CONVENTIONS: DATABASE ABSTRACTION !"ass Pos! < A"!#$eRe"o%d::Base $a'#da!es :#%#"e, p%esen"e: #$ue has_man* :!ommen#s s"ope :pub"%shed, whe%e(pub'#shed: #$ue) end Pos!.pub'#shed.whe%e(!#!'e: "Foo").'#m#!(2)

Slide 28

Slide 28 text

≈ CONVENTIONS: DATABASE ABSTRACTION !"ass Pos! < A"!#$eRe"o%d::Base $a'#da!es :#%#"e, p%esen"e: #$ue has_man* :!ommen#s s"ope :pub"%shed, whe%e(pub'#shed: #$ue) end Pos!.pub'#shed.whe%e(!#!'e: "Foo").'#m#!(2) SELE*T "pos#s".* FROM "pos#s" WHERE pub"%shed ' TRUE AND #%#"e ' "Foo" LIMIT 2

Slide 29

Slide 29 text

≈ CONVENTIONS: MVC STACK Browser Router Controller Model View Postgres/MySQL

Slide 30

Slide 30 text

≈ CONVENTIONS: REST ROUTING # config/routes.rb B"o+::App"%!a#%on.%ou!es.d%aw do %esou%"es :pos#s end GET /posts -> posts#index GET /posts/:id -> posts#show GET /posts/new -> posts#new POST /posts -> posts#create GET /posts/:id/edit -> posts#edit PUT /posts/:id -> posts#update DELETE /posts/:id -> posts#destroy

Slide 31

Slide 31 text

≈ I18n Has-many through relationships 100+ view helpers (text/number/date formatting, HTML tags, sanitization) Form builders Nested urls, nested models, nested forms ... CONVENTIONS ARE EVERYWHERE

Slide 32

Slide 32 text

≈ HTML code mixed with application logic Duplicated/insecure SQL logic Boilerplate code (just code the differences) Fear of taking on to other people’s project TO RECAP: SAY GOODBYE TO

Slide 33

Slide 33 text

Testing Rails Applications

Slide 34

Slide 34 text

≈ TESTING RAILS APPLICATIONS Lack of a well-designed architecture is what makes code unmanageable in the long run The only way to improve design over time is overcome the fear of refactoring

Slide 35

Slide 35 text

≈ TESTS MAKES YOU FEARLESS [#ou!-p!o$e%&] $ "spe) spe) ............................................ ............................................ ............................................ ............................................ ............................................ .................................. F%n%shed %n 75.23 se)onds 751 examp'es, 0 (a)'u!es

Slide 36

Slide 36 text

≈ TESTING WITH RUBY Ruby ecosystem has one of the most mature and vital testing cultures. Ruby syntax makes it a joy to perform testing and work in TDD/BDD So easy to read it almost feels like documentation des)"%be Pos$ do %$ { shou+d_no$ a++ow_(a+ue('').,o"(:$%$+e) } end

Slide 37

Slide 37 text

≈ TESTING WITH RAILS Rails has built-in support for: Unit tests Functional tests Integration (full-stack) tests And produces skeleton test code for you.

Slide 38

Slide 38 text

Managing Multiple Evironments

Slide 39

Slide 39 text

≈ MANAGING MULTIPLE ENVIRONMENTS Too often the production server is seen as somewhat holy, aka “if it works, don’t fix it” Rails embraces the presence of multiple environments, making them easily reproducible and “less magic”

Slide 40

Slide 40 text

≈ MANAGING MULTIPLE ENVIRONMENTS # config/environments/development.rb # Log the query plan for queries taking more than this )on,%&.a)$%(e_"e)o"d.au$o_exp+a%n_$h"esho+d_%n_se)onds - 0.5 # config/environments/production.rb )on,%&.a)$%on_ma%+e".de+%(e"*_me$hod - :sm$p )on,%&.a)$%on_ma%+e".sm$p_se$$%n&s - { ... }

Slide 41

Slide 41 text

≈ GEMFILE sou")e 'h$$p://"ub*&ems.o"&' "ub* '2.0.0' &em '"a%+s', '>- 3.2.11' &"oup :de*e'opmen&, :&es& do &em 'be$$e"_e""o"s' &em '"spe)' end Ruby manages dependencies better than most of other languages

Slide 42

Slide 42 text

≈ GEMFILE.LOCK! GEM "emo$e: h$$ps://"ub*&ems.o"&/ spe)s: a)$%onma%+e" (3.2.13) a)$%onpa)# (- 3.2.13) ma%+ (~> 2.5.3) a)$%onpa)# (3.2.13) a)$%(emode+ (- 3.2.13) a)$%(esuppo"$ (- 3.2.13) bu%+de" (~> 3.0.0) e"ub%s (~> 2.7.0) 'ou"ne* (~> 1.0.4) "a)# (~> 1.4.5) "a)#-)a)he (~> 1.2) "a)#-$es$ (~> 0.6.1) sp"o)#e$s (~> 2.2.1) a)$%(eadm%n (0.5.1) a"b"e (>- 1.0.1) bou"bon (>- 1.0.0) de(%se (>- 1.1.2)

Slide 43

Slide 43 text

Let me deploy and go home

Slide 44

Slide 44 text

≈ DEPLOY NIGHTMARES 1. But.. network doesn’t work 2. Whoops! I just broke production.. 3. Recover latest known working backup, manually. Do it now. 4. Deploy done(?) Live coding on production maine

Slide 45

Slide 45 text

≈ DEPLOY NIGHTMARES, PART II 1. Write code on my laptop 2. Copy code to production 3. Database lacks new tables, have to create new tables and columns 4. Update plugins/libs on production 5. Deploy done(?) Manually updating production env

Slide 46

Slide 46 text

e Rails way 1.rspec spec 2.cap deploy

Slide 47

Slide 47 text

≈ INTRODUCING CAPISTRANO De facto standard for Rails deploys Locks up the deployment process Hooks up to a Git/Hg repository Deploys seamlessly to multiple machines / environments Keeps latest N versions of code, for easy revert Automatic gems updates, database migrations, assets compilation & maintenance scripts

Slide 48

Slide 48 text

≈ RAILS MIGRATIONS db/m%&"a$e ᵓᴷᴷ 20130128103849_)"ea$e_+ea&ue_da$a."b ᵓᴷᴷ 20130128110342_)"ea$e_boo#ma#e"s."b ᵓᴷᴷ 20130128112549_)"ea$e_ma$)h_quo$es."b ᵓᴷᴷ 20130128112550_)"ea$e_de+a*ed_'obs."b ᵓᴷᴷ 20130128112551_)"ea$e_wo"#e"_'ob_w"appe"s."b ᵓᴷᴷ 20130206104748_add_,e$)h_'ob_%d_$o_+ea&ues."b ᵓᴷᴷ 20130213084107_)"ea$e_seasons."b ᵓᴷᴷ 20130213095131_"emo(e_ma$)hes_,"om_+ea&ues."b ᵋᴷᴷ 20130213095433_ed%$_quo$es."b

Slide 49

Slide 49 text

≈ RAILS MIGRATIONS %'ass !"ea$ePos$ < A)$%(eRe)o"d::M)+!a&)on de( )han&e )"ea$e_$ab+e :pos&s do |$| $.s$"%n& :&)&'e $.$ex$ :%on&en& $.da$e$%me :pos&ed_a& $.%n$e&e" :au&ho!_)d $.$%mes$amps end end end

Slide 50

Slide 50 text

So Why Rails

Slide 51

Slide 51 text

≈ SO WHY RAILS? Rails is mature Rails isn’t “cool” anymore Rails makes you much more productive There is a huge ecosystem of Rails businesses, SaaS and startups Ruby On Rails makes you happier :)

Slide 52

Slide 52 text

» @arkh4m ≈ dev.welaika.com ≈ welaika.github.io/torino-coding-society THAN,S! -