Strategies for Rapid Web Prototyping Ruby on Rails
Strategies for Rapid Web Prototyping DRY: Don't repeat yourself Convention over Configuration Separation of Concern Templating MVC: Model View Controler CRUD and Rest Scaffolding
DRY: Don't Repeat Yourself OAOO: Once And Only Once Information in the specification of a system should not be duplicated Modification at one place should not be allowed to produce inconsistency at another place Do not use copy and paste while programming Usually implemented with reflection techniques allowing a programming language to analyze it's own objects metadata Examples Ruby: Describe an object, DB glue code is generated by reflection Counterexamples PHP Web: Metadata in SQL create statement, SQL query, PHP object Validation: Duplicate code on server and client
Convention over Configuration Prefer convention (ie. useful standards and defaults) over the necessity of having to configure every minuscule detail Issue: Documentation Programmer must know all conventions otherwise there is surprise over unexpected convention depent d behaviour Examples: Default behaviour in Prototype Rails: Every table has a primary key of name ID and type integer Rails: Object of name Car described in /app/models/car.rb table name is customers Rails: If object Car has 1:N relation to object Driver then table Drivers expects a key named car_id
Separation of Concern Separate a computer program into distinct features Implement every feature in a single component Minimize overlap between different components Examples: Object orientation: i Split system into single objects Procedural programming: Split system into procedures with I/O behavior MVC: Split application into content, presentation and interaction HTML / CSS / JS: Content, presentation, dynamics
Templating Template: Content with "holes" Object: Attributes for filling in object Template Description Advantages: Indepent change of look and feel Separation of areas of competency Template Engine HTML Text
MVC Pattern Controller User Event generated by View Interaction Requesting a view refresh Activate Setters of Business Logic View Activate Getters of Business Logic Model
MVC Pattern as used in Rails Dispatch Controller Method Rails Dispatcher Controller Mouse Click User Event generated by View Interaction Requesting a view refresh Activate Setters of Business Logic View Interaction View Activate Getters of Business Logic Model
CRUD & REST CRUD Named after the basic 4 operations on persistent storage Create, Read, Update, Delete Can be applied to all forms of data base architectures Architectural concept for data intensive web applications REST Representational State Transfer State of application is abstracted into resources Resource identified by a URL Client server protocol which is stateless and cacheable Set of well defined operations Commonly implemented on top of HTTP (GET, POST, PUT and others)
Scaffolding Method for rapid prototyping of web applications Provide metadata information on the objects Generate code for model: persistence: corresponding ruby object via reflection API CRUD: basic persistence operations presentation: tables for output forms for input links for manipulation glue code for paging etc.
Ruby on Rails
Who is who? Rails Application Framework for web development Main target: Agile programming Uses most of the previously outlined principles Written in Ruby, uses Javascript snippets Ail Agile programming: Continuous stream of results not deadline oriented results Regular adaption to changing requirements not frozen specifications Only measure of progress is working software Result driven not plan driven Close customer cooperation
Who is who? Ruby Interpreted programming language Functional can store lambda functions in a variable Object oriented with class based and prototype based inheritance Imperative classical features as in C, Java, etc. Duck typing do not check class membership but presence of method at time of interpretation i Issue: No compile time type check Automated garbage collection Implementations Interpreter based: Virtual machine based: Read code and execute Translate to bytecode, execute in Ruby VM
MVC Pattern Classical Design Pattern Model Represents the business logic Stores the state of the application View Controller Represents the model for the user Allows the user to change the model Several views and controllers per model are possible Data encapsulation Change of data only via methods of the model Supports semantic correctness inside of the model Supports consistency between representation and model
Model: Controller: Action: message echo say_hello Hello World Example get http://localhost:3000/echo/say_hello Controller in app/controllers/echo_controller.rb class EchoController < ApplicationController def say_hello @msgobj = Message.new @when = Time.now View in app/views/echo/say_hello.erb Model in app/models/message.rb <html><head><title>test</title></head> <body> <h1><%= @msgobj.gettext %></h1> at <%= @when %> </body></html> class Message def gettext return "Hello World" getter
Model: Controller: Action: message echo say_hello Hello World Example (1) dispatch to correct controller file get http://localhost:3000/echo/say_hello (3) invoke correct action method Ok?? (6) request update using correct view template file View in app/views/echo/say_hello.erb Controller in app/controllers/echo_controller.rb (2) select correct class in file class EchoController < ApplicationController def say_hello @msgobj = Message.new @when = Time.now (7) controller provides references to model (@msgobj) and private date (@when) (4) class name leads to file Model in app/models/message.rb (5) controller invokes setter / constr. in model <html><head><title>test</title></head> <body> <h1><%= @msgobj.gettext %></h1> at <%= @when %> </body></html> (9) view invokes getter of model (8) class name leads to file class Message def gettext return "Hello World" getter
Model: Controller: Action: message echo say_hello Hello World Example (1) dispatch to correct controller file get http://localhost:3000/echo/say_hello (3) invoke correct action method (6) request update using correct view template file View in app/views/echo/say_hello.erb Controller in app/controllers/echo_controller.rb (2) select correct class in file class EchoController < ApplicationController def say_hello @msgobj = Message.new @when = Time.now (7) controller provides references to model (@msgobj) and private date (@when) (4) class name leads to file Model in app/models/message.rb (5) controller invokes setter / constr. in model <html><head><title>test</title></head> <body> <h1><%= @msgobj.gettext %></h1> at <%= @when %> </body></html> (9) view invokes getter of model (8) class name leads to file class Message def gettext return "Hello World" getter
Structure Dispatch: From URL to Action URL contains name of controller and action Controller abc implemented in file abc_controller.rb Action is function / method of this controller Update: From Action to View Every controller has directory of same name with view templates Action uvw has view template uvw.erb View template is HTML with embedded Ruby tags Naming: From class name to class definition Class Xyz is defined in file xyz.rb Wllk Well known from Java Note: Windows is case insensitive, Linux is case sensitive
Generation Many code portions are generated using diverse scripts Eg: Basic structure for application me: rails me Generates directory me and subdirectories i MVC basis: Area for Helper: Area for Data Bases Area for Documentation Area for Testing app/models, app/views, app/controllers app/helpers Eg: Controller contro: ruby script/generate controller contro
Database Conventions Application me Connected databases me_development me_test me_production During development During testing During production Changes to databases (ie. schema versioning) fully supported Allows on the fly up and downgrade of DB schema Persistent model mod Model mod Database table mods SQL Create File db/mods.sql
Modifications Eg: Change the DB schema Adapt the meta information and regenerate code DB adaption and code generation to reflect the changes Eg: remove Usecases Eg: No edit for a certain model Manually: Remove action from controller and link from view Eg: Reduction / Adaption of form Edit a generic, autogenerated form file Provide additional info for automated code generation Eg: Input data validation
Modelling Relationships 2 objects in a 1:n relationship Eg: Author Message Generate both tables Scaffolding for both objects Add associations in both directions class Message < ActiveRecord::Base belongs_to :author, :class_name => "User", :foreign_key => "author_id" Adapt views of the controller list: Also show author of a message _form: Also edit author of the message show: Also show author as such