MVC Demystified: Essence of Ruby on Rails

100 %
0 %
Information about MVC Demystified: Essence of Ruby on Rails

Published on May 4, 2007

Author: codeinmotion

Source: slideshare.net

Description

Examines the MVC design pattern and how Rails adheres to this powerful design pattern. Good introduction to Ruby on Rails framework.

MVC Demystified Webvisions 2007 Michael P. Jones [email_address] Understanding the Essence of Ruby on Rails

About Session Web applications MVC Pix Patisserie MVC Another Look Rails Essence More Rails Rails Testing Other MVC

Web applications

MVC

Pix Patisserie

MVC Another Look

Rails Essence

More Rails

Rails Testing

Other MVC

About Speaker Past Educational and Reservation software. (Java) Founded Code in Motion. http://codeinmotion.com/ Present Creating custom Ruby on Rails applications. Recovery and prevention. Screening and Interview PDF generation of applications Widgets Tailoring CMS Solutions Radiant, Rails based CMS with extensions for business needs of the client.

Past

Educational and Reservation software. (Java)

Founded Code in Motion. http://codeinmotion.com/

Present

Creating custom Ruby on Rails applications.

Recovery and prevention.

Screening and Interview

PDF generation of applications

Widgets

Tailoring CMS Solutions

Radiant, Rails based CMS with extensions for business needs of the client.

Traditional Applications Browser directly accesses page. Does not centralize control No content/style separation Easy and fast to produce Difficult to maintain

Browser directly accesses page.

Does not centralize control

No content/style separation

Easy and fast to produce

Difficult to maintain

MVC Applications Browser accesses a “controller” Centralizes control Clean separation of content/style More involved to produce Easier to maintain and expand

Browser accesses a “controller”

Centralizes control

Clean separation of content/style

More involved to produce

Easier to maintain and expand

MVC MVC is an Architectural Design Pattern Separates a web application into three different modules.

MVC is an Architectural Design Pattern

Separates a web application into three different modules.

Design Pattern A pattern that has been developed to help programmers cope with common problems. Blueprints on how to construct something.

A pattern that has been developed to help programmers cope with common problems.

Blueprints on how to construct something.

MVC Means Most Vexing Conundrum (Amy Hoy) Model | View | Controller

Most Vexing Conundrum (Amy Hoy)

Model | View | Controller

Trip to Pix A Patisserie in Portland http://qwendy.typepad.com/

A Patisserie in Portland

Typical Bakery Interaction Request a tasty treat from the baker http://www.pixpatisserie.com/

Request a tasty treat from the baker

Baker Gathers Ingredients Baker gathers raw ingredients to fulfill the request. Some requests utilize same ingredients. http://www.flickr.com/photos/moria/92792777/

Baker gathers raw ingredients to fulfill the request.

Some requests utilize same ingredients.

Baker Select Pan The pan dictates what the response looks like. http://www.flickr.com/photos/tracyhunter/113563802/

The pan dictates what the response looks like.

Baker responds with your treat http://www.pixpatisserie.com/

Pix Flow

MVC Diagram

Controller (Baker) Dispatches Requests and controls flow. Centralizes access. Interacts with Model and View.

Dispatches Requests and controls flow.

Centralizes access.

Interacts with Model and View.

Model (Ingredients) Data representation and business logic. Can be database/xml/etc Business Logic Examples: User Bike Question

Data representation and business logic.

Can be database/xml/etc

Business Logic

Examples:

User

Bike

Question

View (Pan) Data presentation and user input. Renders the Model in to a View. Can be HTML/PDF/WML/Javascript No computations, very little logic, display logic i.e. loops

Data presentation and user input.

Renders the Model in to a View.

Can be HTML/PDF/WML/Javascript

No computations, very little logic, display logic i.e. loops

MVC Diagram

MVC Advantages Separation of interests. Model centralizes business logic. View centralizes display logic. Controller centralizes application flow. Clean separation of content/style. Improved decoupling. Easier testing. Allow multiple people to work on different parts.

Separation of interests.

Model centralizes business logic.

View centralizes display logic.

Controller centralizes application flow.

Clean separation of content/style.

Improved decoupling.

Easier testing.

Allow multiple people to work on different parts.

Rails and MVC Rails in an MVC framework ”… Rails is such a framework that tries to remove the complexity and drudgery of MVC, while still allowing you to realize all the benefits.” - DHH (David Heinemeier Hansson)

Rails in an MVC framework

”… Rails is such a framework that tries to remove the complexity and drudgery of MVC, while still allowing you to realize all the benefits.” - DHH (David Heinemeier Hansson)

Model: ActiveRecord ActiveRecord is a design pattern. (Martin Fowler) Object wraps a row in the database. Encapsulates data access. Contains business logic. Handles relationships. Handles validation.

ActiveRecord is a design pattern. (Martin Fowler)

Object wraps a row in the database.

Encapsulates data access.

Contains business logic.

Handles relationships.

Handles validation.

ActiveRecord Code

View: ActionView Renders the view. Both .rhtml and .rxml files. Provides master layouts. Uses ERb (embedded ruby) for templating and control.

Renders the view.

Both .rhtml and .rxml files.

Provides master layouts.

Uses ERb (embedded ruby) for templating and control.

ActionView Code

Controller: ActionController Controls application flow. Controls which view to use. Makes data available as instance variables @var Contains several actions/defs Controls rendering and redirection.

Controls application flow.

Controls which view to use.

Makes data available as instance variables @var

Contains several actions/defs

Controls rendering and redirection.

ActionController Code

All Together Now http://wiki.rubyonrails.org/

How it works your_domain.com/controller/action/id Controller has actions. View directory has directory for each controller. Same action name as .rhtml file your_domain.com/bikes/edit/2 Controller: bikes Action: edit Id: 2

your_domain.com/controller/action/id

Controller has actions.

View directory has directory for each controller.

Same action name as .rhtml file

your_domain.com/bikes/edit/2

Controller: bikes

Action: edit

Id: 2

Rails Concepts DRY (Don’t Repeat Yourself) Convention of configuration Generators script/generate generator_name :symbols or ‘symbols’ represents names and strings YAML machine parsable human readable.

DRY (Don’t Repeat Yourself)

Convention of configuration

Generators script/generate generator_name

:symbols or ‘symbols’

represents names and strings

YAML machine parsable human readable.

More ActiveRecord Automatically maps to a table No XML configs Each row is an Object Several databases supported. Provides CRUD: Create, Read, Update, Destroy Getters/Setters Validation: validates_presence_of, validates_format_of Callbacks: hooks into lifecycle, trigger events before/after Finding: Person.find(:all, :conditions => [ "category IN (?)", categories], :limit => 50)

Automatically maps to a table

No XML configs

Each row is an Object

Several databases supported.

Provides

CRUD: Create, Read, Update, Destroy

Getters/Setters

Validation: validates_presence_of, validates_format_of

Callbacks: hooks into lifecycle, trigger events before/after

Finding: Person.find(:all, :conditions => [ "category IN (?)", categories], :limit => 50)

More ActiveRecord Code Callbacks Relations Validation

Callbacks

Relations

Validation

More ActionView Layouts app/view/layouts/application.rhtml Partials _partial_view.rhtml Reuse of common view elements Helpers Helper module per Controller Contains Ruby code

Layouts

app/view/layouts/application.rhtml

Partials

_partial_view.rhtml

Reuse of common view elements

Helpers

Helper module per Controller

Contains Ruby code

More ActionView Code

More ActionController Filters before , after and around processing hooks Routes Replaces apache mod_rewrite config/routes.rb Caching Has caching to improve performance page action fragment

Filters

before , after and around processing hooks

Routes

Replaces apache mod_rewrite

config/routes.rb

Caching

Has caching to improve performance

page

action

fragment

More ActionController Image

Pitfalls: Excess Model: God Object (anti-pattern) Too much logic in a Model, one Model too powerful. View: Too much logic in View Controller: Anemic Domain Model (anti-pattern) Too much business logic in Controller. Cargo cult programming (over-)applying a design principle blindly without understanding the reasons. - (wikipedia)

Model: God Object (anti-pattern)

Too much logic in a Model, one Model too powerful.

View: Too much logic in View

Controller: Anemic Domain Model (anti-pattern)

Too much business logic in Controller.

Cargo cult programming

(over-)applying a design principle blindly without understanding the reasons. - (wikipedia)

Testing Rails Rails has great testing framework ‘baked in’ the framework Test stubs created when code is ‘generated’. Unit to test models. test/unit Functional to test views/controllers test/functional Fixtures Supply test data (yaml)

Rails has great testing framework ‘baked in’ the framework

Test stubs created when code is ‘generated’.

Unit to test models.

test/unit

Functional to test views/controllers

test/functional

Fixtures

Supply test data (yaml)

Unit Testing Separate ‘test’ database is used. Fixtures supply data Variety of ‘assert’ statements.

Separate ‘test’ database is used.

Fixtures supply data

Variety of ‘assert’ statements.

Functional Testing Fixture Setup Tests

Fixture

Setup

Tests

Rake Rake is your friend. Database tasks Deployment Documentation Testing Cleanup

Rake is your friend.

Database tasks

Deployment

Documentation

Testing

Cleanup

MVC Around The Web Java Struts, Spring .NET MonoRail (inspired by Rails) Perl Catalyst, MayPole Php CakePHP, Code Igniter Python Django, TurboGears Ruby Nitro, Wee

Java

Struts, Spring

.NET

MonoRail (inspired by Rails)

Perl

Catalyst, MayPole

Php

CakePHP, Code Igniter

Python

Django, TurboGears

Ruby

Nitro, Wee

Questions? MVC Rails Pix

MVC

Rails

Pix

Add a comment

Related pages

Ruby on Rails

Ruby on Rails is open source software, so not only is it free to use, you can also help make it better. More than 4,200 people already have contributed ...
Read more

Ruby And Rails | LinkedIn

Ruby And Rails Articles, experts ... High quality Ruby on Rails web development ... the advantage of MVC architecture based Ruby on Rails framework talk to ...
Read more

Ruby on Rails MVC Framework - Tutorials for Foundation ...

Ruby on Rails MVC Framework - Learn Ruby on Rails in simple and easy steps. ... Given below is a pictorial representation of Ruby on Rails Framework ...
Read more

Webvisions 2007 - Day 2 | Ruby on Rails | Location Based ...

MVC Demystified: Understanding the Essence of Ruby on Rails. ... We then looked specifically at how Ruby on Rails utilizes the MVC pattern, ...
Read more

Ruby / Rails | LinkedIn

Ruby Rails Ruby on Rails developer at Contractor. Lancaster, Pennsylvania Area. Information Technology and Services. See more. Current Ruby on Rails ...
Read more

Getting Started with Rails — Ruby on Rails Guides

Getting Started with Rails. This guide covers getting up and running with Ruby on Rails. ... The basic principles of MVC (Model, ...
Read more

Ruby on Rails Guides

What versions of Ruby on Rails are currently supported, and when to expect new versions. Release Notes Upgrading Ruby on Rails.
Read more

WebVisions 2007 in review | spiral::notepad by Ian ...

WebVisions 2007 in review. Friday, May 4, ... Understanding the Essence of Ruby on Rails Michael Jones, ... (MVC) architectural ...
Read more