38
SO FOLKS SO FOLKS WANT TO WANT TO YOU YOU

Ruby on Rails

Embed Size (px)

Citation preview

Page 1: Ruby on Rails

SO FOLKSSO FOLKS

WANT TOWANT TO

YOUYOU

Page 2: Ruby on Rails
Page 3: Ruby on Rails

SO, RUBY IS...SO, RUBY IS...

A dynamic programming language

Focused on simplicity andproductivity

Has an elegant syntax

Natural to read and easy to write

Page 4: Ruby on Rails

THE IDEALS OF IT’S CREATORTHE IDEALS OF IT’S CREATOR

“ Ruby is simple in appearance, but is verycomplex inside, just like our human body

Yukihiro “Matz” Matsumoto

Ruby is a language of careful balance

Blended of Perl, Smalltalk, Eiffel, Ada, and Lisp

Language that balanced functionalprogramming with imperative programming

Page 5: Ruby on Rails

RUBY’S GROWTHRUBY’S GROWTH

Released in 1995

Achieved mass acceptance in 2006

Ranked among the top 10 on most of theindices that measure the growth andpopularity of programming languagesworldwide

Page 6: Ruby on Rails

DEMAND OF RUBYDEMAND OF RUBY

graph by TIOB

E

Page 7: Ruby on Rails

EVERYTHING IS AN OBJECTEVERYTHING IS AN OBJECT

“ I wanted a scripting language that was more powerfulthan Perl, and more object-oriented than Python

Yukihiro “Matz” Matsumoto

In Ruby, everything is an object

Object-oriented programming calls properties by thename instance variables and actions are known as methods

Ruby follows the influence of the language by giving methods and instancevariables to all of its types

Smalltalk

Page 8: Ruby on Rails

5.times { print "We *love* Ruby -- it's outrageous!\n" }

Page 9: Ruby on Rails

RUBY’S FLEXIBILITYRUBY’S FLEXIBILITY

Ruby is seen as a flexible language

It allows its users to freely alter its parts

Essential parts can be removed/redefined, at will

Existing parts can be added upon

Ruby tries not to restrict the coder

Page 10: Ruby on Rails

class Numeric def plus(x) self.+(x) endend

y = 5.plus 6

Use the readable word plus toRuby’s builtin Numeric class

Page 11: Ruby on Rails

BUILT WITH RUBYBUILT WITH RUBY AND RAILSAND RAILS

Page 12: Ruby on Rails

AND NOWAND NOW

Page 13: Ruby on Rails

WHAT IS RAILS ?WHAT IS RAILS ?

A web application framework written in Ruby

Uses model–view–controller (MVC)

Provides structures for database/web service/pages

JSON or XML for data transfer

Rails emphasizes the use of:

convention over configuration (CoC)

active record pattern

don't repeat yourself (DRY)

Page 14: Ruby on Rails

A WORD ABOUT ARA WORD ABOUT AR (Active Record)

active record pattern is architectural pattern

This is approach to accessing data in a database

Interface of this pattern would include:

Insert Update Delete

+ properties that correspond to columns inunderlying database table

/ /

Page 15: Ruby on Rails

SO...SO...In AR database table or view is wrapped into a class

Object instance is tied to a single row in the table

Object created = new row is added to the tableObject updated = row is also updated

Object implements accessor methods for eachcolumn in the table

Object removed = row removed too

Page 16: Ruby on Rails

C# FELLAS MAY CHECKOUT ACTIVEC# FELLAS MAY CHECKOUT ACTIVERECORD IMPLEMENTATION BY RECORD IMPLEMENTATION BY CASTLECASTLEPROJECTPROJECT::

Page 17: Ruby on Rails

OKAYOKAYLETS GET IT INSTALLEDLETS GET IT INSTALLED

Page 18: Ruby on Rails

FOR LINUXFOR LINUX

Or via Ruby Version Manager (RVM)

$ sudo apt-get install ruby-full

$ curl -sSL https://get.rvm.io | bash -s stable$ rvm install ruby$ rvm use ruby --default$ rvm rubygems current

Simply

EASY AS HELL

Finally$ gem install rails

Page 19: Ruby on Rails

FOR WINDOWSFOR WINDOWS

For God's (and your nerves) sake:

Go to:

Click most inconspicuous button

1.

2.Like this one

AND JUST RUN GODDAMN INSTALLATIONAND JUST RUN GODDAMN INSTALLATION

Page 20: Ruby on Rails

FINALLYFINALLY

Check all things are installed successfully

$ ruby -vruby 2.0.0p (2015-04-13)

IF SO - WE'RE GOOD TO GO

$ gem -v2.4.8

$ rails -vRails 4.2.3

$ sqlite3 --version3.8.11.1 2015-07-15....

Page 21: Ruby on Rails

LETS SCAFFOLD !LETS SCAFFOLD !Create new Rails project in folder `blog`

$ rails new blog

Install all dependency gems$ bundle install

$ cd blog

Enter project folder

Run Rails server$ rails server $ rails sor

Page 22: Ruby on Rails

PROFITPROFIT not yet...

http://localhost:3000

Page 23: Ruby on Rails

WELCOME ABOARDWELCOME ABOARDFolder Description

/app controllers, models, views and assets for application

/config Application's routes, database, etc.

/db Contains database schema and migrations

/public Static files and compiled assets

/test Unit tests and fixtures

/tmp Cache, pid, and session files

/vendor Place for all third-party code

Page 24: Ruby on Rails

LETS CLARIFY

DOES RAILS USEWHAT STACK

Page 25: Ruby on Rails

eRuby

Instead of plain JS

To make CSS debugging even more fun

Templating system similar to ASP, JSP and PHP

Quite unexpected, isn't it ?

&To compile into plain JS

Page 26: Ruby on Rails

COFFEESCRIPTCOFFEESCRIPTIf you ever wondered how it work

square = (x) -> x * xcube = (x) -> square(x) * x

alert square 2

var cube, square;

square = function(x) { return x * x;};

cube = function(x) { return square(x) * x;};

alert(square(2));

CoffeeScript JavaScript

Page 27: Ruby on Rails

RAILS CLIRAILS CLIThere are a few commands of everyday usage.

In the order of how much you'll use them are:

rails consolerails serverrakerails generaterails dbconsolerails new app_name

All commands can run with -h or --help to list more information.

Page 28: Ruby on Rails

GENERATEGENERATE

$ bin/rails generate controller Greetings hello create app/controllers/greetings_controller.rb route get "greetings/hello" invoke erb create app/views/greetings create app/views/greetings/hello.html.erb invoke test_unit create test/controllers/greetings_controller_test.rb invoke helper create app/helpers/greetings_helper.rb invoke assets invoke coffee create app/assets/javascripts/greetings.js.coffee invoke scss create app/assets/stylesheets/greetings.css.scss

Page 29: Ruby on Rails

WE GET THISWE GET THIS

class GreetingsController < ApplicationController def hello @message = "Hello, how are you today?" endend

app/controllers/greetings_controller.rb

<h1>A Greeting for You!</h1><p><%= @message %></p>

app/views/greetings/hello.html.erb

Page 30: Ruby on Rails

RAILS `SCAFFOLD`RAILS `SCAFFOLD`

A scaffold in Rails is a full set of:

AS A MORE SUITABLE TOOL FOR GENERATING STUFF

modeldatabase migration for the modelcontroller

Page 31: Ruby on Rails

FOR EXAMPLEFOR EXAMPLE$ bin/rails generate scaffold HighScore game:string score:integer invoke active_record create db/migrate/20130717151933_create_high_scores.rb create app/models/high_score.rb invoke test_unit create test/models/high_score_test.rb create test/fixtures/high_scores.yml invoke resource_route route resources :high_scores invoke scaffold_controller create app/controllers/high_scores_controller.rb invoke erb create app/views/high_scores create app/views/high_scores/index.html.erb create app/views/high_scores/edit.html.erb create app/views/high_scores/show.html.erb create app/views/high_scores/new.html.erb create app/views/high_scores/_form.html.erb ...

... invoke test_unit create test/controllers/high_scores_controller_test.rb invoke helper create app/helpers/high_scores_helper.rb invoke jbuilder create app/views/high_scores/index.json.jbuilder create app/views/high_scores/show.json.jbuilder invoke assets invoke coffee create app/assets/javascripts/high_scores.js.coffee invoke scss create app/assets/stylesheets/high_scores.css.scss invoke scss identical app/assets/stylesheets/scaffolds.css.scss

Page 32: Ruby on Rails

AND RAKEAND RAKE

Rake is a Make-like program implemented inRuby.

Except tasks and dependencies arespecified in standard Ruby syntax.

WE USE RAILS FOR:WE USE RAILS FOR:

Running db migrationsSeeding database with preset dataGetting list of all routesRunning test

Page 33: Ruby on Rails

LETS FIND OUT WHAT WE CAN RAKELETS FIND OUT WHAT WE CAN RAKE

Simply typing:$ bin/rake --tasks

We get:rake about # List versions of all Rails frameworks and the environmentrake assets:clean # Remove old compiled assetsrake assets:clobber # Remove compiled assetsrake assets:precompile # Compile all the assets named in config.assets.precompilerake db:create # Create the database from config/database.yml for the current Rails.env...rake log:clear # Truncates all *.log files in log/ to zero bytesrake middleware # Prints out your Rack middleware stack...rake tmp:clear # Clear session, cache, and socket files from tmp/rake tmp:create # Creates tmp directories for sessions, cache, sockets, and pids

Page 34: Ruby on Rails

AND THATS ALLAND THATS ALL

Page 35: Ruby on Rails

CHECKOUT `HELLO WORLD` REPOCHECKOUT `HELLO WORLD` REPO

Page 36: Ruby on Rails

FURTHER LEARNINGFURTHER LEARNING

RubyRails

GREAT SCREENCASTS:GREAT SCREENCASTS:

https://mackenziechild.me/12-in-12/

Page 37: Ruby on Rails

THANK YOU !THANK YOU !

Page 38: Ruby on Rails

QUESTIONS ?QUESTIONS ?