Share
Embed
Transcript
52 slides
-
TORINO !ODING SO!IETY ~ 14/10/2013 Runnin g On Rails @a"#h4m
A SPEEDY INTRODUCTION TO RUBY ON RAILS. -
« » 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. -
« » 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 -
≈ 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! -
≈ 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 -
≈ 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 -
≈ 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 -
≈ CONVENTIONS: DATABASE ABSTRACTION !"ass Pos! < A"!#$eRe"o%d::Base end pos!
& Pos!.new pos!.!#!'e & “To%#no (ode%s” pos!.sa$e #$ue -
≈ 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 -
≈ 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 -
≈ 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"]} -
≈ CONVENTIONS: DATABASE ABSTRACTION !"ass Pos! < A"!#$eRe"o%d::Base $a'#da!es :#%#"e,
p%esen"e: #$ue has_man* :!ommen#s end -
≈ 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 -
≈ 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 (..) -
≈ 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) -
≈ 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 -
≈ 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 -
≈ 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) -
≈ 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 -
≈ 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 -
≈ 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 -
≈ 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 -
≈ 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 -
≈ 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 -
≈ 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 -
≈ TESTING WITH RAILS Rails has built-in support for: Unit
tests Functional tests Integration (full-stack) tests And produces skeleton test code for you. -
≈ 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” -
≈ 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 - { ... } -
≈ 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 -
≈ 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) -
≈ 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 maine -
≈ 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 -
≈ 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 -
≈ 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 -
≈ 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 -
≈ 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 :)