mvc & activerecord by christian mohr & mohamed souiai
TRANSCRIPT
![Page 1: MVC & ActiveRecord by Christian Mohr & Mohamed Souiai](https://reader036.vdocuments.mx/reader036/viewer/2022062423/56649e855503460f94b88019/html5/thumbnails/1.jpg)
MVC & ActiveRecord
by Christian Mohr & Mohamed Souiai
![Page 2: MVC & ActiveRecord by Christian Mohr & Mohamed Souiai](https://reader036.vdocuments.mx/reader036/viewer/2022062423/56649e855503460f94b88019/html5/thumbnails/2.jpg)
Content
• MVC
• ActionController
• ActionView
• ActiveRecord
• Routing
![Page 3: MVC & ActiveRecord by Christian Mohr & Mohamed Souiai](https://reader036.vdocuments.mx/reader036/viewer/2022062423/56649e855503460f94b88019/html5/thumbnails/3.jpg)
Model View Controller
•Architectural pattern for interactive Applications.
•Isolates business logic from user interface consideration.
•Grants flexibility modularity reusability of Objects.
![Page 4: MVC & ActiveRecord by Christian Mohr & Mohamed Souiai](https://reader036.vdocuments.mx/reader036/viewer/2022062423/56649e855503460f94b88019/html5/thumbnails/4.jpg)
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.
![Page 5: MVC & ActiveRecord by Christian Mohr & Mohamed Souiai](https://reader036.vdocuments.mx/reader036/viewer/2022062423/56649e855503460f94b88019/html5/thumbnails/5.jpg)
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.
![Page 6: MVC & ActiveRecord by Christian Mohr & Mohamed Souiai](https://reader036.vdocuments.mx/reader036/viewer/2022062423/56649e855503460f94b88019/html5/thumbnails/6.jpg)
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.
![Page 7: MVC & ActiveRecord by Christian Mohr & Mohamed Souiai](https://reader036.vdocuments.mx/reader036/viewer/2022062423/56649e855503460f94b88019/html5/thumbnails/7.jpg)
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 .
![Page 8: MVC & ActiveRecord by Christian Mohr & Mohamed Souiai](https://reader036.vdocuments.mx/reader036/viewer/2022062423/56649e855503460f94b88019/html5/thumbnails/8.jpg)
Control center ActionController
Our Controller class inherits from the ApplicationController.Class AirportsController < ApplicationController
UML Diagram:
![Page 9: MVC & ActiveRecord by Christian Mohr & Mohamed Souiai](https://reader036.vdocuments.mx/reader036/viewer/2022062423/56649e855503460f94b88019/html5/thumbnails/9.jpg)
Control center ActionController
Controller generator:Syntax:ruby script/generate controller name [action_1 …
action_N]
Generates the controller app/controllers/name_controller.rb with optional actionsand ActionViews: app/views/name/action_n.html.erb.
The Actions can be added manually. In that case, the corresponding views are not generated automatically.
1,2
![Page 10: MVC & ActiveRecord by Christian Mohr & Mohamed Souiai](https://reader036.vdocuments.mx/reader036/viewer/2022062423/56649e855503460f94b88019/html5/thumbnails/10.jpg)
Control center ActionController
Controller Actions:
• The public Methods of the Controller• Access via URL call:
http://<host>:<port>/<controller>/<action>
3,4,5
![Page 11: MVC & ActiveRecord by Christian Mohr & Mohamed Souiai](https://reader036.vdocuments.mx/reader036/viewer/2022062423/56649e855503460f94b88019/html5/thumbnails/11.jpg)
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
![Page 12: MVC & ActiveRecord by Christian Mohr & Mohamed Souiai](https://reader036.vdocuments.mx/reader036/viewer/2022062423/56649e855503460f94b88019/html5/thumbnails/12.jpg)
ActionView
Syntax in ActionView Files
Ruby Code: <% … code … %>
Ruby Output: <%=output%>
By using html_escape(…) or h(…) Tag-specific characters (i.e. <, > and &) are masked.
![Page 13: MVC & ActiveRecord by Christian Mohr & Mohamed Souiai](https://reader036.vdocuments.mx/reader036/viewer/2022062423/56649e855503460f94b88019/html5/thumbnails/13.jpg)
ActionView
Instance variables:
Variables with a leading ‘@’ are accessible to all methods inside the defining controller and the corresponding views .
![Page 14: MVC & ActiveRecord by Christian Mohr & Mohamed Souiai](https://reader036.vdocuments.mx/reader036/viewer/2022062423/56649e855503460f94b88019/html5/thumbnails/14.jpg)
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.
![Page 15: MVC & ActiveRecord by Christian Mohr & Mohamed Souiai](https://reader036.vdocuments.mx/reader036/viewer/2022062423/56649e855503460f94b88019/html5/thumbnails/15.jpg)
ActiveRecordModels:Classes, that
represent a data- table and are responsible for the database operations (CRUD)
Each row in the database represents a model-object
![Page 16: MVC & ActiveRecord by Christian Mohr & Mohamed Souiai](https://reader036.vdocuments.mx/reader036/viewer/2022062423/56649e855503460f94b88019/html5/thumbnails/16.jpg)
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
![Page 17: MVC & ActiveRecord by Christian Mohr & Mohamed Souiai](https://reader036.vdocuments.mx/reader036/viewer/2022062423/56649e855503460f94b88019/html5/thumbnails/17.jpg)
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
![Page 18: MVC & ActiveRecord by Christian Mohr & Mohamed Souiai](https://reader036.vdocuments.mx/reader036/viewer/2022062423/56649e855503460f94b88019/html5/thumbnails/18.jpg)
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, :conditions=>…)
It is possible to add custom methods.7,8
![Page 19: MVC & ActiveRecord by Christian Mohr & Mohamed Souiai](https://reader036.vdocuments.mx/reader036/viewer/2022062423/56649e855503460f94b88019/html5/thumbnails/19.jpg)
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
![Page 20: MVC & ActiveRecord by Christian Mohr & Mohamed Souiai](https://reader036.vdocuments.mx/reader036/viewer/2022062423/56649e855503460f94b88019/html5/thumbnails/20.jpg)
ActiveRecord
Interesting Functions:• Validation• Before- and After-Filter• Associations and Relations• Migrations• Transactions• Automatic attributes (created_at, updated_at)
![Page 21: MVC & ActiveRecord by Christian Mohr & Mohamed Souiai](https://reader036.vdocuments.mx/reader036/viewer/2022062423/56649e855503460f94b88019/html5/thumbnails/21.jpg)
ActiveRecord
• Oracle• PostgreSQL• SQLite• Sybas
Supported relational management database systems
• DB2• Firebird• Frontbase• MySQL• Openbase
![Page 22: MVC & ActiveRecord by Christian Mohr & Mohamed Souiai](https://reader036.vdocuments.mx/reader036/viewer/2022062423/56649e855503460f94b88019/html5/thumbnails/22.jpg)
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:ActionController::Routing::Routes.draw do |map| # ... map.connect ':controller/:action/:id' map.connect ':controller/:action/:id.:format'end
• Restart the server after changing the Routing!
![Page 23: MVC & ActiveRecord by Christian Mohr & Mohamed Souiai](https://reader036.vdocuments.mx/reader036/viewer/2022062423/56649e855503460f94b88019/html5/thumbnails/23.jpg)
Routing
Routing Diagram
![Page 24: MVC & ActiveRecord by Christian Mohr & Mohamed Souiai](https://reader036.vdocuments.mx/reader036/viewer/2022062423/56649e855503460f94b88019/html5/thumbnails/24.jpg)
Routing
LinksTo generate a Link to
http://localhost:3000/bookmarks/show/1
The code in the View would look like this:<%= link_to "show bookmarks", :controller => 'bookmarks',:action => "show", :id => 1 %>
There are ways to simplify this!
![Page 25: MVC & ActiveRecord by Christian Mohr & Mohamed Souiai](https://reader036.vdocuments.mx/reader036/viewer/2022062423/56649e855503460f94b88019/html5/thumbnails/25.jpg)
Routing
Simplified URL’s using map.connect• Given Routing:
http://localhost:3000/authentication/login• Desired URL:
http://localhost:3000/loginAdd routing-entry above standard entries:map.connect 'login', :controller =>
"authentication”, :action => "login"
![Page 26: MVC & ActiveRecord by Christian Mohr & Mohamed Souiai](https://reader036.vdocuments.mx/reader036/viewer/2022062423/56649e855503460f94b88019/html5/thumbnails/26.jpg)
Routing
Named Routes using map.nameAdd routing-entry above standard entries:map.login 'login', :controller =>
"authentication", :action => "login"
Provides login_url and login_path
• name_url contains absolute path incl. host• name_path contains relative path without host.
![Page 27: MVC & ActiveRecord by Christian Mohr & Mohamed Souiai](https://reader036.vdocuments.mx/reader036/viewer/2022062423/56649e855503460f94b88019/html5/thumbnails/27.jpg)
Routing
root route using map.root• An applications default index page URL:
http://localhost:3000/applicationname• If no controller provided (i.e. http://localhost:3000)
the default rails welcome-page is displayed• changing config/routes.rb entry tomap.root :controller => "bookmarks”
makes bookmarks the root controller• Make sure to delete homonymous files in pubic/
![Page 28: MVC & ActiveRecord by Christian Mohr & Mohamed Souiai](https://reader036.vdocuments.mx/reader036/viewer/2022062423/56649e855503460f94b88019/html5/thumbnails/28.jpg)
Routing
Also possible:
• Complex routing with regular expressions• Routing with defined HTTP-Method
(GET, POST, PUT, DELETE)
![Page 29: MVC & ActiveRecord by Christian Mohr & Mohamed Souiai](https://reader036.vdocuments.mx/reader036/viewer/2022062423/56649e855503460f94b88019/html5/thumbnails/29.jpg)
The end
Thank you for listening.