35
Ruby on Rails Technology

Ride on the Fast Track of Web with Ruby on Rails- Part 2

Embed Size (px)

DESCRIPTION

In July 2004 David Heinemeier Hansson aka dhh, a Danish programmer came up with a web application framework while building a project management tool named Basecamp. In 2012 it is now one of the fastest growing web frameworks in world. Tens of Thousands Rails applications like Basecamp, Twitter, Github, Groupon, and our own (in Bangladesh) akhoni.com and bdipo.com are live. Ruby on Rails (RoR) claims to be a breakthrough in lowering the barriers of entry to programming. Powerful web applications that formerly took months to develop can be produced now in a days. RoR is now official platform for many startup incubators. In this session we will introduce and explore the zeal of RoR. We will see how this can optimize programmer's happiness and sustainable productivity. This was presented in Basis Softexpo 2012 and arranged by Nascenia IT, the leading Ruby on Rails development company in Bangladesh.

Citation preview

Page 1: Ride on the Fast Track of Web with Ruby on Rails- Part 2

Ruby on RailsTechnology

Page 2: Ride on the Fast Track of Web with Ruby on Rails- Part 2

MVC

Page 3: Ride on the Fast Track of Web with Ruby on Rails- Part 2

ModelController

View

Page 4: Ride on the Fast Track of Web with Ruby on Rails- Part 2

MVC

ControllerModel

●View

Page 5: Ride on the Fast Track of Web with Ruby on Rails- Part 2

MVC

Page 6: Ride on the Fast Track of Web with Ruby on Rails- Part 2

RESTful routing

RESTRepresentational state transfer

Page 7: Ride on the Fast Track of Web with Ruby on Rails- Part 2

RESTful routingHTTP Verb Path Action

GET /posts index

GET /posts/new new

POST /posts create

GET /posts/:id show

GET /posts/:id/edit edit

PUT /posts/:id update

DELETE /posts/:id destroy

Page 8: Ride on the Fast Track of Web with Ruby on Rails- Part 2

RESTful routing

resources :posts

Configuration

Page 9: Ride on the Fast Track of Web with Ruby on Rails- Part 2

Model

class Post < ActiveRecord::Base validates :title, :presence => trueend

Page 10: Ride on the Fast Track of Web with Ruby on Rails- Part 2

Controller

class PostController < ApplicationController

def index @posts = Post.all logger.info 'Post loaded' end

end

Page 11: Ride on the Fast Track of Web with Ruby on Rails- Part 2

View<h1>Listing posts</h1> <table> <tr> <th>Name</th> <th>Title</th> <th>Content</th> </tr> <% @posts.each do |post| %> <tr> <td><%= post.name %></td> <td><%= post.title %></td> <td><%= post.content %></td> </tr><% end %></table>

Page 12: Ride on the Fast Track of Web with Ruby on Rails- Part 2

Technology● Unobtrusive Javascript● Built in jquery and prototype● Coffeescript● HTML 5● Haml / Sass● Internationalization● Data migration● Dependency management

Page 13: Ride on the Fast Track of Web with Ruby on Rails- Part 2

PerformanceImprove performance● Caching● Asset pipeline

Test performance

Page 14: Ride on the Fast Track of Web with Ruby on Rails- Part 2

Improve performanceCaching

- Page caching

- Action caching

- Fragment Caching

● Reference● http://guides.rubyonrails.org/caching_with_rails.html

Page 15: Ride on the Fast Track of Web with Ruby on Rails- Part 2

Configuration

config.action_controller.perform_caching = true

Page 16: Ride on the Fast Track of Web with Ruby on Rails- Part 2

Page caching

class ProductsController < ActionController caches_page :index def index @products = Products.all end def create expire_page :action => :index endend

Page 17: Ride on the Fast Track of Web with Ruby on Rails- Part 2

Page caching

Appropriate for● Public pages● Pages that do not

change often

Not for● Pages that require

authentication● Pages that changes

often

Page 18: Ride on the Fast Track of Web with Ruby on Rails- Part 2

Action caching

class ProductsController < ActionController before_filter :authenticate caches_action :index def index @products = Product.all end def create expire_action :action => :index endend

Page 19: Ride on the Fast Track of Web with Ruby on Rails- Part 2

Fragment caching

<% cache('all_available_products') do %> All available products: <% Product.all.each do |p| %> <%= link_to p.name, product_url(p) %> <% end %><% end %>

expire_fragment('all_available_products')

Expire cach

In template

Page 20: Ride on the Fast Track of Web with Ruby on Rails- Part 2

Caching

Use Sweepers to expire cache

Page 21: Ride on the Fast Track of Web with Ruby on Rails- Part 2

Sweepers to expire caching

class ProductSweeper < ActionController::Caching::Sweeper observe Product # This sweeper is going to keep an eye on the Product model

def after_create(product) expire_cache_for(product) end def after_update(product) expire_cache_for(product) end def after_destroy(product) expire_cache_for(product) end private def expire_cache_for(product) expire_page(:controller => 'products', :action => 'index') expire_fragment('all_available_products') endend

Page 22: Ride on the Fast Track of Web with Ruby on Rails- Part 2

Sweepers to expire caching

class ProductsController < ActionController before_filter :authenticate caches_action :index cache_sweeper :product_sweeper def index @products = Product.all end end

Page 23: Ride on the Fast Track of Web with Ruby on Rails- Part 2

Configure caching

ActionController::Base.cache_store = :memory_storeor

ActionController::Base.cache_store = :file_store, "/path/to/cache/directory"or

ActionController::Base.cache_store = :mem_cache_store, "cache-1.example.com"

Page 24: Ride on the Fast Track of Web with Ruby on Rails- Part 2

Test performance● Benchmarking web site performance●

http://guides.rubyonrails.org/performance_testing.html

class HomepageTest < ActionDispatch::PerformanceTest # Replace this with your real tests. def test_homepage get '/' endend

Page 25: Ride on the Fast Track of Web with Ruby on Rails- Part 2

Improve performance

Asset pipeline ???

● Reference● http://guides.rubyonrails.org/asset_pipeline.html

Page 26: Ride on the Fast Track of Web with Ruby on Rails- Part 2

Asset pipelining

Page 27: Ride on the Fast Track of Web with Ruby on Rails- Part 2

Asset pipelining

Assets

Page 28: Ride on the Fast Track of Web with Ruby on Rails- Part 2

Asset pipelining

Com

p ile

Assets

Compiled asset

Page 29: Ride on the Fast Track of Web with Ruby on Rails- Part 2

Asset pipelining

Assets

Compiled asset

Usage

app/assets/javascripts/application.jsapp/assets/javascripts/home.jsapp/assets/javascripts/menu.jsapp/assets/javascripts/slider.js

Page 30: Ride on the Fast Track of Web with Ruby on Rails- Part 2

Asset pipelining

Assets

Compiled asset

UsageIn application.js add the following,

//= require home//= require moovinator//= require slider

Page 31: Ride on the Fast Track of Web with Ruby on Rails- Part 2

Asset pipelining

Assets

Compiled asset

UsageAdd application.js in you template as follows,

<%= javascript_include_tag "application" %>

Page 32: Ride on the Fast Track of Web with Ruby on Rails- Part 2

Deployment

Assets

Compiled asset● Reference● https://github.com/capistrano/capistrano/wiki

● Using capistrano magic●

● cap deploy

Page 33: Ride on the Fast Track of Web with Ruby on Rails- Part 2

Learn Rails

Assets

Compiled asset

● Guides● http://guides.rubyonrails.org●

● Video casts● http://railscasts.com● http://rubyonrails.org/screencasts

Page 34: Ride on the Fast Track of Web with Ruby on Rails- Part 2

A.K.M. Ashrafuzzaman Sr. Technology Analyst,

Orbund

www.ashrafuzzaman.comjitu-blog.blogspot.comwww.orbund.com

Page 35: Ride on the Fast Track of Web with Ruby on Rails- Part 2

http://www.softexpo.com.bd/technical_details.php?id=9To see 1st part of this presentation visit:http://www.slideshare.net/fuadcse/ride-on-the-fast-track-of-web-with-ruby-on-rails-part-1

Presented At