Slideshare.net (beta)

 
Post: 
Myspace Hi5 Friendster Xanga LiveJournal Facebook Blogger Tagged Typepad Freewebs BlackPlanet gigya icons



All comments

Add a comment on Slide 1

If you have a SlideShare account, login to comment; else you can comment as a guest


Showing 1-50 of 18 (more)

MVC Demystified: Essence of Ruby on Rails

From codeinmotion, 1 year ago

Examines the MVC design pattern and how Rails adheres to this powe more

5152 views  |  2 comments  |  17 favorites  |  609 downloads  |  8 embeds (Stats)
 

Tags

ruby rails mvc tutorial model view controller introduction webapps on

more

 
 

Groups/Events

 
 

Privacy InfoNew!

This slideshow is Public

 
Embed in your blog
Embed (wordpress.com)
custom

Slideshow transcript

Slide 1: MVC Demystified Understanding the Essence of Ruby on Rails Webvisions 2007 Michael P. Jones michael@codeinmotion.com MVC Demystified 1

Slide 2: About Session • Web applications • MVC • Pix Patisserie • MVC Another Look • Rails Essence • More Rails • Rails Testing • Other MVC MVC Demystified 2

Slide 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. MVC Demystified 3

Slide 4: Traditional Applications • Browser directly accesses page. – Does not centralize control – No content/style separation – Easy and fast to produce – Difficult to maintain MVC Demystified 4

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

Slide 6: MVC • MVC is an Architectural Design Pattern • Separates a web application into three different modules. MVC Demystified 6

Slide 7: Design Pattern • A pattern that has been developed to help programmers cope with common problems. • Blueprints on how to construct something. MVC Demystified 7

Slide 8: MVC Means • Most Vexing Conundrum (Amy Hoy) • Model | View | Controller MVC Demystified 8

Slide 9: Trip to Pix • A Patisserie in Portland http://qwendy.typepad.com/ MVC Demystified 9

Slide 10: Typical Bakery Interaction • Request a tasty treat from the baker http://www.pixpatisserie.com/ MVC Demystified 10

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

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

Slide 13: Baker responds with your treat http://www.pixpatisserie.com/ MVC Demystified 13

Slide 14: Pix Flow MVC Demystified 14

Slide 15: MVC Diagram MVC Demystified 15

Slide 16: Controller (Baker) • Dispatches Requests and controls flow. • Centralizes access. • Interacts with Model and View. MVC Demystified 16

Slide 17: Model (Ingredients) • Data representation and business logic. • Can be database/xml/etc • Business Logic • Examples: – User – Bike – Question MVC Demystified 17

Slide 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 MVC Demystified 18

Slide 19: MVC Diagram MVC Demystified 19

Slide 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. MVC Demystified 20

Slide 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) MVC Demystified 21

Slide 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. MVC Demystified 22

Slide 23: ActiveRecord Code MVC Demystified 23

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

Slide 25: ActionView Code MVC Demystified 25

Slide 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. MVC Demystified 26

Slide 27: ActionController Code MVC Demystified 27

Slide 28: All Together Now http://wiki.rubyonrails.org/ MVC Demystified 28

Slide 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 MVC Demystified 29

Slide 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. MVC Demystified 30

Slide 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) MVC Demystified 31

Slide 32: More ActiveRecord Code • Callbacks • Relations • Validation MVC Demystified 32

Slide 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 MVC Demystified 33

Slide 34: More ActionView Code MVC Demystified 34

Slide 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 MVC Demystified 35

Slide 36: More ActionController Image MVC Demystified 36

Slide 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) MVC Demystified 37

Slide 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) MVC Demystified 38

Slide 39: Unit Testing • Separate ‘test’ database is used. • Fixtures supply data • Variety of ‘assert’ statements. MVC Demystified 39

Slide 40: Functional Testing • Fixture • Setup • Tests MVC Demystified 40

Slide 41: Rake • Rake is your friend. – Database tasks – Deployment – Documentation – Testing – Cleanup MVC Demystified 41

Slide 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 MVC Demystified 42

Slide 43: Questions? • MVC • Rails • Pix MVC Demystified 43