scragz / augmentations

Use Rails caches_action to serve the same cached page to everyone and augment special cases with JS after it's loaded.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Augmentations

A tiny framework for helping solve the common problem when using caches_action in rails apps: Still needing to show custom info for the current user, order, etc.

First it loads the JSON of the current session info, by default from /session/info.json. All the augmentations are then called with this information.

app/assets/application.js:

//= require augmentations/application
//= require augmentations

app/assets/javascripts/augmentations.js.coffee:

class Augmentations.AdminMenu extends Augmentations.Base
  url: =>
    '/admin/augmentations/menu.html'
  # only attaches if selector exists
  # @root() gives a jquery object of this selector
  selector: =>
    '#hd_menu'
  # make a test on the session info
  test: =>
    @session.is_admin
  # load the menu and add it to the top menu
  augment: =>
    $.get(@url()).success (data)=>
      $(data).appendTo(@root())
$(->
  # listen for sessionLoad
  (new Augmentations.AdminMenu).attach()
)

About

Use Rails caches_action to serve the same cached page to everyone and augment special cases with JS after it's loaded.

License:MIT License


Languages

Language:CoffeeScript 61.6%Language:Ruby 36.1%Language:JavaScript 2.4%