Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby mining with Rails

Ruby mining with Rails

Stefano Verna

April 16, 2013
Tweet

More Decks by Stefano Verna

Other Decks in Programming

Transcript

  1. « » 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.
  2. « » 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
  3. ≈ 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
  4. ≈ 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
  5. ≈ 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
  6. ≈ 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
  7. ≈ CONVENTIONS: DATABASE ABSTRACTION %&ass Pos& < A%&$)eRe%o!d::Base end pos&

    , Pos&.new pos&.&$&#e , “Rub* Tuesda*” pos&.sa)e #!ue pos#s pos#s pos#s 'd #'#&e %!ea#ed_a# 1 Rub* Tuesda* 2013-04-16
  8. ≈ 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"]}
  9. ≈ 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 (..)
  10. ≈ 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
  11. ≈ 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
  12. ≈ 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
  13. ≈ 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
  14. ≈ 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
  15. ≈ 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
  16. ≈ 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
  17. ≈ 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
  18. ≈ TESTING WITH RAILS Rails has built-in support for: Unit

    tests Functional tests Integration (full-stack) tests And produces skeleton test code for you.
  19. ≈ 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”
  20. ≈ 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 , { ... }
  21. ≈ 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
  22. ≈ 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)
  23. ≈ 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
  24. ≈ 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
  25. ≈ 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
  26. ≈ 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
  27. ≈ 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
  28. ≈ 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 & Rails make you happy :)