Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

MVC Demystified: Essence of Ruby on Rails


Published on

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

Published in: Technology
  • Ruby (Oprah's Book Club 2.0) ---
    Are you sure you want to  Yes  No
    Your message goes here
  • Sterling Silver Created Ruby and Created White Sapphire Wrapped Heart Pendant Necklace, 18" ---
    Are you sure you want to  Yes  No
    Your message goes here
  • The Ruby Programming Language ---
    Are you sure you want to  Yes  No
    Your message goes here
  • What a great slide! We're looking at message broker solutions right now ... you a top candidate!
    Are you sure you want to  Yes  No
    Your message goes here
  • good explation!
    Are you sure you want to  Yes  No
    Your message goes here

MVC Demystified: Essence of Ruby on Rails

  1. MVC Demystified Webvisions 2007 Michael P. Jones [email_address] Understanding the Essence of Ruby on Rails
  2. About Session <ul><li>Web applications </li></ul><ul><li>MVC </li></ul><ul><li>Pix Patisserie </li></ul><ul><li>MVC Another Look </li></ul><ul><li>Rails Essence </li></ul><ul><li>More Rails </li></ul><ul><li>Rails Testing </li></ul><ul><li>Other MVC </li></ul>
  3. About Speaker <ul><li>Past </li></ul><ul><ul><li>Educational and Reservation software. (Java) </li></ul></ul><ul><ul><li>Founded Code in Motion. </li></ul></ul><ul><li>Present </li></ul><ul><ul><li>Creating custom Ruby on Rails applications. </li></ul></ul><ul><ul><ul><li>Recovery and prevention. </li></ul></ul></ul><ul><ul><ul><li>Screening and Interview </li></ul></ul></ul><ul><ul><ul><li>PDF generation of applications </li></ul></ul></ul><ul><ul><ul><li>Widgets </li></ul></ul></ul><ul><ul><li>Tailoring CMS Solutions </li></ul></ul><ul><ul><ul><li>Radiant, Rails based CMS with extensions for business needs of the client. </li></ul></ul></ul>
  4. Traditional Applications <ul><li>Browser directly accesses page. </li></ul><ul><ul><li>Does not centralize control </li></ul></ul><ul><ul><li>No content/style separation </li></ul></ul><ul><ul><li>Easy and fast to produce </li></ul></ul><ul><ul><li>Difficult to maintain </li></ul></ul>
  5. MVC Applications <ul><li>Browser accesses a “controller” </li></ul><ul><ul><li>Centralizes control </li></ul></ul><ul><ul><li>Clean separation of content/style </li></ul></ul><ul><ul><li>More involved to produce </li></ul></ul><ul><ul><li>Easier to maintain and expand </li></ul></ul>
  6. MVC <ul><li>MVC is an Architectural Design Pattern </li></ul><ul><li>Separates a web application into three different modules. </li></ul>
  7. Design Pattern <ul><li>A pattern that has been developed to help programmers cope with common problems. </li></ul><ul><li>Blueprints on how to construct something. </li></ul>
  8. MVC Means <ul><li>Most Vexing Conundrum (Amy Hoy) </li></ul><ul><li>Model | View | Controller </li></ul>
  9. Trip to Pix <ul><li>A Patisserie in Portland </li></ul>
  10. Typical Bakery Interaction <ul><li>Request a tasty treat from the baker </li></ul>
  11. Baker Gathers Ingredients <ul><li>Baker gathers raw ingredients to fulfill the request. </li></ul><ul><li>Some requests utilize same ingredients. </li></ul>
  12. Baker Select Pan <ul><li>The pan dictates what the response looks like. </li></ul>
  13. Baker responds with your treat
  14. Pix Flow
  15. MVC Diagram
  16. Controller (Baker) <ul><li>Dispatches Requests and controls flow. </li></ul><ul><li>Centralizes access. </li></ul><ul><li>Interacts with Model and View. </li></ul>
  17. Model (Ingredients) <ul><li>Data representation and business logic. </li></ul><ul><li>Can be database/xml/etc </li></ul><ul><li>Business Logic </li></ul><ul><li>Examples: </li></ul><ul><ul><li>User </li></ul></ul><ul><ul><li>Bike </li></ul></ul><ul><ul><li>Question </li></ul></ul>
  18. View (Pan) <ul><li>Data presentation and user input. </li></ul><ul><li>Renders the Model in to a View. </li></ul><ul><li>Can be HTML/PDF/WML/Javascript </li></ul><ul><li>No computations, very little logic, display logic i.e. loops </li></ul>
  19. MVC Diagram
  20. MVC Advantages <ul><li>Separation of interests. </li></ul><ul><ul><li>Model centralizes business logic. </li></ul></ul><ul><ul><li>View centralizes display logic. </li></ul></ul><ul><ul><li>Controller centralizes application flow. </li></ul></ul><ul><li>Clean separation of content/style. </li></ul><ul><li>Improved decoupling. </li></ul><ul><li>Easier testing. </li></ul><ul><li>Allow multiple people to work on different parts. </li></ul>
  21. Rails and MVC <ul><li>Rails in an MVC framework </li></ul><ul><li>”… 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) </li></ul>
  22. Model: ActiveRecord <ul><li>ActiveRecord is a design pattern. (Martin Fowler) </li></ul><ul><li>Object wraps a row in the database. </li></ul><ul><li>Encapsulates data access. </li></ul><ul><li>Contains business logic. </li></ul><ul><li>Handles relationships. </li></ul><ul><li>Handles validation. </li></ul>
  23. ActiveRecord Code
  24. View: ActionView <ul><li>Renders the view. </li></ul><ul><li>Both .rhtml and .rxml files. </li></ul><ul><li>Provides master layouts. </li></ul><ul><li>Uses ERb (embedded ruby) for templating and control. </li></ul>
  25. ActionView Code
  26. Controller: ActionController <ul><li>Controls application flow. </li></ul><ul><li>Controls which view to use. </li></ul><ul><li>Makes data available as instance variables @var </li></ul><ul><li>Contains several actions/defs </li></ul><ul><li>Controls rendering and redirection. </li></ul>
  27. ActionController Code
  28. All Together Now
  29. How it works <ul><li> </li></ul><ul><li>Controller has actions. </li></ul><ul><li>View directory has directory for each controller. </li></ul><ul><li>Same action name as .rhtml file </li></ul><ul><li> </li></ul><ul><ul><li>Controller: bikes </li></ul></ul><ul><ul><li>Action: edit </li></ul></ul><ul><ul><li>Id: 2 </li></ul></ul>
  30. Rails Concepts <ul><li>DRY (Don’t Repeat Yourself) </li></ul><ul><li>Convention of configuration </li></ul><ul><li>Generators script/generate generator_name </li></ul><ul><li>:symbols or ‘symbols’ </li></ul><ul><ul><li>represents names and strings </li></ul></ul><ul><li>YAML machine parsable human readable. </li></ul>
  31. More ActiveRecord <ul><li>Automatically maps to a table </li></ul><ul><ul><li>No XML configs </li></ul></ul><ul><ul><li>Each row is an Object </li></ul></ul><ul><li>Several databases supported. </li></ul><ul><li>Provides </li></ul><ul><ul><li>CRUD: Create, Read, Update, Destroy </li></ul></ul><ul><ul><li>Getters/Setters </li></ul></ul><ul><ul><li>Validation: validates_presence_of, validates_format_of </li></ul></ul><ul><ul><li>Callbacks: hooks into lifecycle, trigger events before/after </li></ul></ul><ul><ul><li>Finding: Person.find(:all, :conditions => [ &quot;category IN (?)&quot;, categories], :limit => 50) </li></ul></ul>
  32. More ActiveRecord Code <ul><li>Callbacks </li></ul><ul><li>Relations </li></ul><ul><li>Validation </li></ul>
  33. More ActionView <ul><li>Layouts </li></ul><ul><ul><li>app/view/layouts/application.rhtml </li></ul></ul><ul><li>Partials </li></ul><ul><ul><li>_partial_view.rhtml </li></ul></ul><ul><ul><li>Reuse of common view elements </li></ul></ul><ul><li>Helpers </li></ul><ul><ul><li>Helper module per Controller </li></ul></ul><ul><ul><li>Contains Ruby code </li></ul></ul>
  34. More ActionView Code
  35. More ActionController <ul><li>Filters </li></ul><ul><ul><li>before , after and around processing hooks </li></ul></ul><ul><li>Routes </li></ul><ul><ul><li>Replaces apache mod_rewrite </li></ul></ul><ul><ul><li>config/routes.rb </li></ul></ul><ul><li>Caching </li></ul><ul><ul><li>Has caching to improve performance </li></ul></ul><ul><ul><ul><li>page </li></ul></ul></ul><ul><ul><ul><li>action </li></ul></ul></ul><ul><ul><ul><li>fragment </li></ul></ul></ul>
  36. More ActionController Image
  37. Pitfalls: Excess <ul><li>Model: God Object (anti-pattern) </li></ul><ul><ul><li>Too much logic in a Model, one Model too powerful. </li></ul></ul><ul><li>View: Too much logic in View </li></ul><ul><li>Controller: Anemic Domain Model (anti-pattern) </li></ul><ul><ul><li>Too much business logic in Controller. </li></ul></ul><ul><li>Cargo cult programming </li></ul><ul><ul><li>(over-)applying a design principle blindly without understanding the reasons. - (wikipedia) </li></ul></ul>
  38. Testing Rails <ul><li>Rails has great testing framework ‘baked in’ the framework </li></ul><ul><ul><li>Test stubs created when code is ‘generated’. </li></ul></ul><ul><li>Unit to test models. </li></ul><ul><ul><li>test/unit </li></ul></ul><ul><li>Functional to test views/controllers </li></ul><ul><ul><li>test/functional </li></ul></ul><ul><li>Fixtures </li></ul><ul><ul><li>Supply test data (yaml) </li></ul></ul>
  39. Unit Testing <ul><li>Separate ‘test’ database is used. </li></ul><ul><li>Fixtures supply data </li></ul><ul><li>Variety of ‘assert’ statements. </li></ul>
  40. Functional Testing <ul><li>Fixture </li></ul><ul><li>Setup </li></ul><ul><li>Tests </li></ul>
  41. Rake <ul><li>Rake is your friend. </li></ul><ul><ul><li>Database tasks </li></ul></ul><ul><ul><li>Deployment </li></ul></ul><ul><ul><li>Documentation </li></ul></ul><ul><ul><li>Testing </li></ul></ul><ul><ul><li>Cleanup </li></ul></ul>
  42. MVC Around The Web <ul><li>Java </li></ul><ul><ul><li>Struts, Spring </li></ul></ul><ul><li>.NET </li></ul><ul><ul><li>MonoRail (inspired by Rails) </li></ul></ul><ul><li>Perl </li></ul><ul><ul><li>Catalyst, MayPole </li></ul></ul><ul><li>Php </li></ul><ul><ul><li>CakePHP, Code Igniter </li></ul></ul><ul><li>Python </li></ul><ul><ul><li>Django, TurboGears </li></ul></ul><ul><li>Ruby </li></ul><ul><ul><li>Nitro, Wee </li></ul></ul>
  43. Questions? <ul><li>MVC </li></ul><ul><li>Rails </li></ul><ul><li>Pix </li></ul>