MVC Demystified: Essence of Ruby on Rails

  • 29,970 views
Uploaded on

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

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

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • What a great slide! We're looking at message broker solutions right now ... you a top candidate!
    http://www.mediafire.com/download/kx9l74xfsn96454/
    Are you sure you want to
    Your message goes here
  • good explation!
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
29,970
On Slideshare
0
From Embeds
0
Number of Embeds
6

Actions

Shares
Downloads
1,319
Comments
2
Likes
42

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. MVC Demystified Webvisions 2007 Michael P. Jones [email_address] Understanding the Essence of Ruby on Rails
  • 2. About Session
    • Web applications
    • MVC
    • Pix Patisserie
    • MVC Another Look
    • Rails Essence
    • More Rails
    • Rails Testing
    • Other MVC
  • 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.
  • 4. Traditional Applications
    • Browser directly accesses page.
      • Does not centralize control
      • No content/style separation
      • Easy and fast to produce
      • Difficult to maintain
  • 5. MVC Applications
    • Browser accesses a “controller”
      • Centralizes control
      • Clean separation of content/style
      • More involved to produce
      • Easier to maintain and expand
  • 6. MVC
    • MVC is an Architectural Design Pattern
    • Separates a web application into three different modules.
  • 7. Design Pattern
    • A pattern that has been developed to help programmers cope with common problems.
    • Blueprints on how to construct something.
  • 8. MVC Means
    • Most Vexing Conundrum (Amy Hoy)
    • Model | View | Controller
  • 9. Trip to Pix
    • A Patisserie in Portland
    http://qwendy.typepad.com/
  • 10. Typical Bakery Interaction
    • Request a tasty treat from the baker
    http://www.pixpatisserie.com/
  • 11. Baker Gathers Ingredients
    • Baker gathers raw ingredients to fulfill the request.
    • Some requests utilize same ingredients.
    http://www.flickr.com/photos/moria/92792777/
  • 12. Baker Select Pan
    • The pan dictates what the response looks like.
    http://www.flickr.com/photos/tracyhunter/113563802/
  • 13. Baker responds with your treat http://www.pixpatisserie.com/
  • 14. Pix Flow
  • 15. MVC Diagram
  • 16. Controller (Baker)
    • Dispatches Requests and controls flow.
    • Centralizes access.
    • Interacts with Model and View.
  • 17. Model (Ingredients)
    • Data representation and business logic.
    • Can be database/xml/etc
    • Business Logic
    • Examples:
      • User
      • Bike
      • Question
  • 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
  • 19. MVC Diagram
  • 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.
  • 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)
  • 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.
  • 23. ActiveRecord Code
  • 24. View: ActionView
    • Renders the view.
    • Both .rhtml and .rxml files.
    • Provides master layouts.
    • Uses ERb (embedded ruby) for templating and control.
  • 25. ActionView Code
  • 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.
  • 27. ActionController Code
  • 28. All Together Now http://wiki.rubyonrails.org/
  • 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
  • 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.
  • 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)
  • 32. More ActiveRecord Code
    • Callbacks
    • Relations
    • Validation
  • 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
  • 34. More ActionView Code
  • 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
  • 36. More ActionController Image
  • 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)
  • 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)
  • 39. Unit Testing
    • Separate ‘test’ database is used.
    • Fixtures supply data
    • Variety of ‘assert’ statements.
  • 40. Functional Testing
    • Fixture
    • Setup
    • Tests
  • 41. Rake
    • Rake is your friend.
      • Database tasks
      • Deployment
      • Documentation
      • Testing
      • Cleanup
  • 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
  • 43. Questions?
    • MVC
    • Rails
    • Pix