Created
February 22, 2012 22:54
-
-
Save niklas/1888149 to your computer and use it in GitHub Desktop.
# make the RJS helper proxy `page` available in Draper's Decorator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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