Skip to content

Instantly share code, notes, and snippets.

@niklas
Created February 22, 2012 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niklas/1888149 to your computer and use it in GitHub Desktop.
Save niklas/1888149 to your computer and use it in GitHub Desktop.
# make the RJS helper proxy `page` available in Draper's Decorator
# in your .js.rjs templates, you can call #decorate on the magic page object
# and then use RJS-powered methods from within your Decorator
#
# page.decorate User.first do |user|
# user.blink
# end
#
# class UserDecorator < ApplicationDecorator
# def blink
# page[ "user_#{model.id}" ].animate 'blink'
# end
# end
module Draper::RJS
def self.included(base)
base.class_eval do
alias_method_chain :initialize, :rjs
attr_accessor :page
end
end
def initialize_with_rjs(input, options={})
initialize_without_rjs(input, options)
self.page = options.delete(:page)
end
def page?
!!page
end
end
Draper::Base.class_eval do
include Draper::RJS
end
module VersatileRJS::Draper
def decorate(resource, options={}, &block)
resource.decorate options.merge(:page => self), &block
end
end
VersatileRJS::Page.class_eval do
include VersatileRJS::Draper
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment