nihilence / RailsLite

Metaprogramming demonstration that creates the core functionality of Rails

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RailsLite

##Core Functionality

Basic render and redirect_to

  • render_content populates response content_type and body
  • redirect_to populates response status and location
  • render creates filepath to a template, i.e.'/views/cats_controller/show.html.erb'
  • render creates ERB instance with filepath, evaluates by binding local variables

Session method and hash

  • session method in controller base allows insertion into session hash
  • session parses request cookie into session hash if it exists, else the session hash is an empty hash
  • session has store_session method which populates response cookie with session hash
  • render and redirect_to methods overridden to store the session

Params method and hash

  • params instance created in controller on initialization
  • params hash populated with route parameters, as well as decoded request body and query string
  • params returns values as strings

Router and routes

  • route objects have attributes such as the controller they are associated with, the http method they use, the regex url pattern to match and the controller action it takes
  • request path is matched against the pattern and the http method is confirmed identical to ensure the correct route is chosen
  • route run pulls route_params from request, creates new controller and invokes action
  • controller base invoke action calls the method matching the argument name on itself then renders the appropriate template unless the response was already built
  • router's routes array is populated with standard http routes with add_route
  • router draw method evaluates proc allowing multiple routes to be created at once
  • router searches routes for match to request, returns 404 error if none exists and runs the route otherwise

##Bonus

Flash

  • flash method in controller base constructs a flash object if one doesn't exist
  • flash parses cookie for appropriate key and stores value in flash hash
  • flash only persists messages for one additional request
  • flash now doesn't persist the message for an additional request

URL helpers

  • link_to and button_to creates html
  • routes modified to create a url helper in the controller which returns the correct path

About

Metaprogramming demonstration that creates the core functionality of Rails


Languages

Language:Ruby 99.0%Language:HTML 1.0%