MVC Demystified: Essence of Ruby on Rails

43
MVC Demystified 1 MVC Demystified Webvisions 2007 Michael P. Jones [email protected] Understanding the Essence of Ruby on Rails
  • date post

    17-Oct-2014
  • Category

    Technology

  • view

    37.372
  • download

    0

description

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

Transcript of MVC Demystified: Essence of Ruby on Rails

Page 1: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 1

MVC Demystified

Webvisions 2007Michael P. Jones

[email protected]

Understanding the Essence of Ruby on Rails

Page 2: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 2

About Session

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

Page 3: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 3

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.

Page 4: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 4

Traditional Applications

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

Page 5: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 5

MVC Applications

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

Page 6: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 6

MVC

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

modules.

Page 7: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 7

Design Pattern

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

• Blueprints on how to construct something.

Page 8: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 8

MVC Means

• Most Vexing Conundrum (Amy Hoy)• Model | View | Controller

Page 9: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 9

Trip to Pix

• A Patisserie in Portland

http://qwendy.typepad.com/

Page 10: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 10

Typical Bakery Interaction

• Request a tasty treat from the baker

http://www.pixpatisserie.com/

Page 11: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 11

Baker Gathers Ingredients

• Baker gathers raw ingredients to fulfill the request.

• Some requests utilize same ingredients.

http://www.flickr.com/photos/moria/92792777/

Page 12: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 12

Baker Select Pan

• The pan dictates what the response looks like.

http://www.flickr.com/photos/tracyhunter/113563802/

Page 13: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 13

Baker responds with your treat

http://www.pixpatisserie.com/

Page 14: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 14

Pix Flow

Page 15: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 15

MVC Diagram

Page 16: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 16

Controller (Baker)

• Dispatches Requests and controls flow.• Centralizes access.• Interacts with Model and View.

Page 17: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 17

Model (Ingredients)

• Data representation and business logic.• Can be database/xml/etc• Business Logic• Examples:

– User– Bike– Question

Page 18: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 18

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

Page 19: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 19

MVC Diagram

Page 20: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 20

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.

Page 21: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 21

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)

Page 22: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 22

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.

Page 23: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 23

ActiveRecord Code

Page 24: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 24

View: ActionView

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

Page 25: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 25

ActionView Code

Page 26: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 26

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.

Page 27: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 27

ActionController Code

Page 28: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 28

All Together Now

http://wiki.rubyonrails.org/

Page 29: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 29

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

Page 30: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 30

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.

Page 31: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 31

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)

Page 32: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 32

More ActiveRecord Code

• Callbacks• Relations• Validation

Page 33: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 33

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

Page 34: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 34

More ActionView Code

Page 35: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 35

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

Page 36: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 36

More ActionController Image

Page 37: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 37

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)

Page 38: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 38

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)

Page 39: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 39

Unit Testing

• Separate ‘test’ database is used.

• Fixtures supply data• Variety of ‘assert’

statements.

Page 40: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 40

Functional Testing

• Fixture• Setup• Tests

Page 41: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 41

Rake

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

Page 42: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 42

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

Page 43: MVC Demystified: Essence of Ruby on Rails

MVC Demystified 43

Questions?

• MVC• Rails• Pix