Introduction to Ruby on Rails - Hasso-Plattner-Institut · Introduction to Ruby on Rails —...

26
Introduction to Ruby on Rails Thomas Kowark Prof. Plattner, Dr. Uflacker Enterprise Platform and Integration Concepts group Software Engineering II WS 2015/16

Transcript of Introduction to Ruby on Rails - Hasso-Plattner-Institut · Introduction to Ruby on Rails —...

Introduction to Ruby on Rails

Thomas Kowark

Prof. Plattner, Dr. Uflacker Enterprise Platform and Integration Concepts group

Software Engineering II WS 2015/16

Introduction to Ruby on Rails

1.   Ruby&RubyonRails■ WhatisRubyonRails?■ AfewwordsaboutRuby■  Rails'corecomponents■  RESTfularchitecture

2.  YourfirstRailsapplicaAon3.  YourintroductoryRailsexercise4.  AddiAonalLiterature

Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 2 October 16, 2015

WebapplicaAondevelopmentframeworkwriHeninRuby■  hHp://rubyonrails.org/Philosophy■  "Don'trepeatyourself"–DRY■  ConvenAonoverConfiguraAon–thereis"theRailsway"■  RESTfularchitecture■  Everythinginitsplace

■  UsedbyGithub,Groupon,TwiHer(parAally)

What is Ruby on Rails?

Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 3

Rails5est.2015

Rails42013

Rails32009

Rails22006

Rails12003

October 16, 2015

hHp://www.ruby-lang.org/

■  Dynamic,reflecAve,general-purpose,object-oriented■  InfluencedbyPerl,Smalltalk,Eiffel,andLisp■  Open-source,maturesoaware■ Matz’sRubyInterpreter(MRI)versions:

□  AddiAonallydifferentVMsavailable(JRuby,Rubinius,IronRuby,Maglev)

A few words about Ruby

Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 4

Ruby2.2.22015

Ruby2.0.02013

Ruby1.9.32011

Ruby1.8.72010

Ruby1.01996

Yukihiro"Matz"MatsumotowithR.Stallman

October 16, 2015 ImagebyRubénRodríguez(CCBY3.0)-hHp://commons.wikimedia.org/wiki/File:Matz.jpg

Rails Core Components

Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 5

Ac1onPack

Ac1onDispatch(parsesHTTP,sessions,cookies,etc.)

Ac1onController(makedataavailable,applica1onflow)

View

Controller

Model

Raili1es(corecode,e.g.rake)

Ac1veSupport(u1lityclasses,

e.g.i18n)

Ac1onMailer(emailservices)

Ac1veModel

(e.g.valida1ons)

Gems(packagedlibraries)hKps://rubygems.org/

Database(SQL,Graph..)

Ac1onView(renderstemplate)

Ac1veRecord(ORM)Datastorage

October 16, 2015 hHp://adrianmejia.com/blog/2011/08/11/ruby-on-rails-architectural-design/

my_first_rails_app/app/ assets/ controller/ applicaAon_controller.rb helpers/ applicaAon_helper.rb models/ views/ applicaAon/ index.html.erb layouts/ applicaAon.html.erb

Rails Application Layout

October 16, 2015 6 Introduction to Ruby on Rails — Software Engineering II — WS 2015/16

■  RepresentaAonalStateTransfer(REST)isasoawarearchitecturestylefordistributedsystems

■  Principles□  UniformInterface□  StatelessInteracAons□  Cacheable□  Clientsandservers□  LayeredSystem□  CodeonDemand(opAonal)

■  LargestRESTfulimplementaAon:WorldWideWeb

RESTful Architecture

Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 7 October 16, 2015

■  RESTsupportsall4HTTP1.1verbs:GET,PUT,POST,DELETE■  DifferenAaAonofcollecAonsandindividualelements

RESTful Architecture – HTTP verbs

Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 8

Resource GET PUT POST DELETE

SingleelementhHp://localhost:3000/authors/1

Retrieve Updateorcreate Create Delete

CollecAonhHp://localhost:3000/authors

List Replace Create Delete

October 16, 2015

■  GET/ #invoke“home”controller■  GET/authors #retrievealistofallauthors■  GET/authors/new #gettheformtoenteranewauthor■  POST/authors #createanewauthor■  GET/authors/1 #showdetailsofthefirstauthor■  GET/authors/1/edit #gettheformtoeditthefirstauthor■  PUT/authors/1 #updatethefirstauthor■  DELETE/authors/1 #deletethefirstauthor

Examples of Routes

Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 9 October 16, 2015

Introduction to Ruby on Rails

1.  Ruby&RubyonRails2.   YourfirstRailsapplica1on3.  YourintroductoryRailsexercise4.  AddiAonalLiterature

Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 10 October 16, 2015

■ Op1on1:YouuseMacorLinux□  InstallanduseRubyonRailsdirectlyonyourOS□  Rubyversionmanager(e.g.RVM,rbenv)ifolderversionsofRubyshouldbekept□ hHp://guides.rubyonrails.org/gesng_started.html#installing-rails□  OruseopAon2

■ Op1on2:YouhaveWindowsorwanttouseaVM(recommended)□ WepreparedoneforyouviaVagrant(hHps://www.vagrantup.com/)

□  UsesVirtualBoxinthebackend(freeonallplatorms)(hHps://www.virtualbox.org/)□  Useyourowntools&editors,runtheprojectinaheadlessVM□  SeeprojectREADMEforsetupinstrucAons

■ Op1on3:YouhaveWindowsandinstallRubyonRailsdirectlyonyourOS□  TendstoconsumesomeAme,mightcauseproblemswithcertaindependencies□  hHp://railsinstaller.org/en

How to Start?

Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 11 October 16, 2015

Recommendedtoworkthrough/readthishands-ontutorial.Seriously.hHp://guides.rubyonrails.org/gesng_started.htmlThefollowingslidesgiveageneraloverview

Comprehensive RoR tutorial

Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 12

Info:Beforeyoustartcoding,makesure,thecorrectversionsareinstalled.

$ruby--version

$rails--version

October 16, 2015

rails – Main executable

Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 13

$railsnewStartnewrailsapplicaAon

$railsserverStartthedevelopmentserver

$railsgenerateGenerateboilerplateformodels,controllers&views

$railsdbconsoleStartadirectdatabaseshell

■ Example:generatemodel,controllerandviewwithoutcontrollerspecs$railsgscaffoldauthorlast_name:stringhomepage:string--controller-specsfalse

$railsconsoleStartinteracAveshelltotestoutideas

hHp://guides.rubyonrails.org/command_line.html October 16, 2015

Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 14

Bundler – Ruby package manager

$geminstallManuallyinstallagem(Rubypackage)

$bundleinstallInstallallgemslistedasdepenciesinGemfile

■ Rubylibrariesarepackagedas"gems"■ OnlinerepositoryathHps://rubygems.org/■ Bundlerresolvesdependenciesofgems■  Gemfileholdsalistofrequiredgems□  Specifyversions,e.g.gem'rails'>='4.1.6’□  Alt.sources,e.g.:github=>”tkowark/sawyer”

■  Gemfile.lockispopulatedwithresolveddependencies□  Shouldbeunderversioncontrol

October 16, 2015

Info:Gemfile.lockcontainsalltheactuallyinstalledversionsofgems.

rake – Ruby make

Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 15

$rake-TListallavailablerakecommands

$rakeroutesListallconfiguredroutes

$rakedb:setup&&rakedb:migrateSetupthedatabaseandrunallmigraAons

$rakedb:schema:load

Replacedatabasewithdblayoutfromdb/schema.rbDonotrunmigraAons.

$rakespecRunRspec(tesAngframeworkforRoR)tests

Info:Runningschema:loadisadvisablewhensesngupacompletelynewproject.ItisnotintendedtoworkaroundbadmigraAons.

October 16, 2015

■  InstallGit:□  sudoapt-getinstallgit□  hHp://git-scm.com/(Installersforallsystems)

■  SeZngupusernameandemail:□ Mandatorytocommitchanges□  UseyourgithubcredenAals!

■  AlternaAve:sesngparametersonlyforoneproject:

Git – distributed version control system

Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 16

$gitconfig--globaluser.email“[email protected]”$gitconfig--globaluser.name“MaxMustermann”

$cd/path/to/your/project$gitconfiguser.email“[email protected]”$gitconfiguser.name“MaxMustermann”

October 16, 2015

Git workflow – commiting a change

Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 17

$gitclonehttps://github.com/hpi-swt2/wimi-portalCheckoutremoterepositorytolocalcopy

$gitaddapp/views/layouts

Changemainlayouttemplateapp/views/layouts/applicaAon.html.erbStagechanges(addfilesfromworkingcopytorepositoryindex)$gitstatus

ListchangestobecommiHed

$gitcommit–m"Fixedissue#25"Commitwithcommitmessages.ReferenceGithubissue#25

$gitpullFetchandmergechangesfromremoterepository

$gitpushPublishlocalcommits

October 16, 2015

Introduction to Ruby on Rails

1.  Ruby&RubyonRails2.  YourfirstRailsapplicaAon3.   YourintroductoryRailsexercise4.  AddiAonalLiterature

Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 18 October 16, 2015

■  Goals□  GetfamiliarwithRubyonRails□  Createnecessaryaccountsfortheproject

■  Tasks□  CreateaGithubaccount□  LinkitwithCodeSchool.com(unlessyoualreadyhaveanaccount)□  CompleteRailscodingschool:hHp://railsforzombies.com□  CompleteGitTutorial:hHps://www.codeschool.com/courses/try-git□ Whendone,createascreenshotofyourreportcard

■  Deadline:Nov6,11:59pmCET(firm)

Exercise 1 – Code School

October 16, 2015 Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 19

■  Goals□  DeepenyourunderstandingofRailsApps□  GetusedtoTDD/BDD

■  Tasks□  SignuponGithubClassroom

–  hHps://classroom.github.com/assignment-invitaAons/ebc02d7d20638a7ef2660434c04136c2–  RepositorymighttakesomeAmetoreceiveiniAalskeletoncode(bepaAent...)

□  Makethetests“green”–  Startwith‘rakespec’–  Runsinglefeaturetestswith‘rspecspec/features/…/..._spec.rb’

□  CommityourscreenshotfromExercise1■  Deadline□  Nov6,11:59pmCET(firm)□  POsareexemptfromthistask.

Exercise 2 - Develop a Rails App

October 16, 2015 Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 20

■ Web-basedpapermanagementsystem□  Author–  Firstname–  Lastname–  Homepage

□  Paper–  Title–  PublicaAon–  Year

□  Apaperhasmanyauthors(max.5)□  Anauthorhasmanypapers

■ WepreparedaRailsapplicaAonforyou

Exercise 2 – Requirements

October 16, 2015 Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 21

Exercise 2 – Use Cases & Site Map

October 16, 2015 Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 22

Home

ShowAuthors

AddAuthor

ShowAuthorDetails

EditAuthor

ShowPapers

AddPaper

ShowPaperDetails

EditPaper

hHps://gomockingbird.com/mockingbird/index.html?project=v890g6l

Exercise 2 – Mock Up

October 16, 2015 Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 23

Additional Literature

Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 24 October 16, 2015

Generalliterature■  Ruby,S.;Thomas,D.;HanssonD.H.:AgileWebDevelopmentwithRails4,

2013(ebook)■  Swicegood,T.:PragmaAcGuidetoGit(P3.0),2012(ebook)■  Rappin,N.:RailsTestPrescripAons,2012■  Rasmusson,J.:TheAgileSamurai(P4.0),2012(ebook)■  Pichler,R.:AgileProductManagementwithScrum,2010■  Cohn,M.:SucceedingwithAgile,2012■  Larman,C.;Vodde,B.:ScalingLean&AgileDevelopment,2009■  Ludewig,J.;Lichter,H.:SoawareEngineering,2006■  Sommerville,I.:SoawareEngineering,2004Ebookswillbemadeavailableforchosenbooks

Next Weeks’ Schedule

Week1(Oct12–Oct16)■  IntroducAonlectures

Week2(Oct19–Oct23)■  Findteams,enroll!■  CodeSchoolexercise■  11-12:30LectureonScrum■  13:30–15ScrumLegoExercise

Week3(Oct26–Oct30)■  POs:CustomermeeAng■  RubyonRailsexercise■  LectureonGitandtesAng

Week4(Nov2–Nov6)■  Kick-offpresentaAon■  Startofproject

October 16, 2015 Software Engineering II — WS 2014/15 25

Introduction to Ruby on Rails

1.  Ruby&RubyonRails■ WhatisRubyonRails?■  Rails'corecomponents■  RESTfularchitecture

2.  YourfirstRailsapplicaAon■  Folderstructure■ rails,rake,git

3.  YourintroductoryRailsexercise■  CodeSchool■ GithubClassroom

4.  AddiAonalLiterature

Introduction to Ruby on Rails — Software Engineering II — WS 2015/16 26 October 16, 2015