MVC - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

MVC

Description:

MVC & ActiveRecord by Christian Mohr & Mohamed Souiai Content MVC ActionController ActionView ActiveRecord Routing Model View Controller Architectural pattern for ... – PowerPoint PPT presentation

Number of Views:101
Avg rating:3.0/5.0
Slides: 30
Provided by: mojo9
Category:
Tags: mvc

less

Transcript and Presenter's Notes

Title: MVC


1
MVC ActiveRecord
  • by Christian Mohr Mohamed Souiai

2
Content
  • MVC
  • ActionController
  • ActionView
  • ActiveRecord
  • Routing

3
Model View Controller
  • Architectural pattern for interactive
    Applications.
  • Isolates business logic from user interface
    consideration.
  • Grants flexibility modularity reusability of
    Objects.

4
Model
  • In general the model represents the information
    (the data) of the application and the business
    rules used to manipulate the data.
  • Inside Ruby on Rails the ActiveRecord Module
    corresponds to the model in the MVC paradigm.

5
View
  • In general the view corresponds to elements of
    the user interface such as text, checkbox items.
  • In Rails the ActionView is used to create
    Templates and visualizes the data provided by the
    controller, in different formats.

6
Controller
  • In general the controller manages details
    involving the communication to the model of user
    actions such as keystrokes and mouse movements.
  • In Rails the Controller is realized by the
    ActionController who coordinates the interaction
    between user and Application.

7
A simple bookmark projekt
  • In the following slide we introduce the MVC
    paradigm in the context of Ruby on rails.

8
Control center ActionController
  • Tasks
  • Receiving Http-Request data (i.e. Form data)
  • Database requests via model-classes.
  • Setting and query of cookies and sessions.
  • Setting flash messages.
  • Calling templates.
  • Forwarding Files and data.
  • Authentication .

9
Control center ActionController
UML Diagram
Our Controller class inherits from the
ApplicationController. Class AirportsController lt
ApplicationController
10
Control center ActionController
  • Controller generator
  • Syntax
  • ruby script/generate controller name action_1
    action_N
  • Generates the controller app/controllers/name_con
    troller.rb
  • with optional actions
  • and ActionViews app/views/name/action_n.html.er
    b.
  • The Actions can be added manually.
  • In that case, the corresponding views are not
    generated automatically.

1,2
11
Control center ActionController
  • Controller Actions
  • The public Methods of the Controller
  • Access via URL call
  • http//lthostgtltportgt/ltcontrollergt/ltactiongt

3,4,5
12
ActionView
  • Template-File containing Ruby- and HTML-Code.
  • By Convention, the name is the same as the
    corresponding action.
  • All View-Files have the extension .html.erb

13
ActionView
  • Syntax in ActionView Files
  • Ruby Code lt code gt
  • Ruby Output ltoutputgt
  • By using html_escape() or h() Tag-specific
    characters (i.e. lt, gt and ) are masked.

14
ActionView
  • Instance variables
  • Variables with a leading _at_ are accessible to
    all methods inside the defining controller and
    the corresponding views .

15
ActiveRecord
  • treating Data from a Database like Objects
  • Domain Specific Language (DSL)
  • ActiveReord Design pattern by Martin Fowler
  • Mapping object-oriented Data to relational Data
    and vice versa.

16
ActiveRecord
  • Models
  • Classes, that represent a data- table and are
    responsible for the database operations (CRUD)
  • Each row in the database represents a model-object

17
ActiveRecord
  • Model-Generator Syntax
  • ruby script/generate model modelname
  • Generates the ActiveRecord model file
    app/models/modelname.rb
  • To create a new Row in the data table, we create
    a new Object of the class modelname.
  • By Convention, the data table name is lower case
    and plural and the model class name is in
    singular with upper case initial.

6
18
ActiveRecord
  • CRUD (Create, Read, Update Delete)
  • The four basic database operations
  • Create create a new dataset
  • Read read a dataset
  • Update alter an existing dataset
  • Delete delete a dataset

19
ActiveRecord
  • ActiveRecord-Classes provide methods for the
    following basic database operations
  • new creates a new ActiveRecord-Object
  • Create() creates and saves a new AR-Object
  • Find(ID) finds the correspoding dataset
  • Find(all, conditionsgt)
  • It is possible to add custom methods.

7,8
20
ActiveRecord
  • ActiveRecord-Objects offer the following methods
  • save saving an object to the database
  • update alter all or single object attributes
  • destroy delete an object

21
ActiveRecord
  • Interesting Functions
  • Validation
  • Before- and After-Filter
  • Associations and Relations
  • Migrations
  • Transactions
  • Automatic attributes (created_at, updated_at)

22
ActiveRecord
Supported relational management database systems
  • Oracle
  • PostgreSQL
  • SQLite
  • Sybas
  • DB2
  • Firebird
  • Frontbase
  • MySQL
  • Openbase

23
Routing
  • Defines which internal controller and action
    should be called depending on the URL.
  • Routing rules are stored in config/routes.rb
  • Auto generated entries
  • ActionControllerRoutingRoutes.draw do map
  • ...
  • map.connect 'controller/action/id'
  • map.connect 'controller/action/id.format'
  • end
  • Restart the server after changing the Routing!

24
Routing
Routing Diagram
25
Routing
  • Links
  • To generate a Link to
  • http//localhost3000/bookmarks/show/1
  • The code in the View would look like this
  • lt link_to "show bookmarks", controller gt
    'bookmarks',
  • action gt "show", id gt 1 gt
  • There are ways to simplify this!

26
Routing
  • Simplified URLs using map.connect
  • Given Routing
  • http//localhost3000/authentication/login
  • Desired URL
  • http//localhost3000/login
  • Add routing-entry above standard entries
  • map.connect 'login', controller gt
    "authentication, action gt "login"

27
Routing
  • Named Routes using map.name
  • Add routing-entry above standard entries
  • map.login 'login', controller gt
    "authentication",
  • action gt "login"
  • Provides login_url and login_path
  • name_url contains absolute path incl. host
  • name_path contains relative path without host.

28
Routing
  • root route using map.root
  • An applications default index page URL
  • http//localhost3000/applicationname
  • If no controller provided (i.e.
    http//localhost3000)
  • the default rails welcome-page is displayed
  • changing config/routes.rb entry to
  • map.root controller gt "bookmarks
  • makes bookmarks the root controller
  • Make sure to delete homonymous files in pubic/

29
Routing
  • Also possible
  • Complex routing with regular expressions
  • Routing with defined HTTP-Method
  • (GET, POST, PUT, DELETE)

30
The end
  • Thank you for listening.
Write a Comment
User Comments (0)
About PowerShow.com