Sleepy cowboy coding on the train

This commit is contained in:
Jeff Casimir 2011-07-13 01:00:10 -04:00
parent 40943761fc
commit 6c78b6cca6
6 changed files with 85 additions and 19 deletions

View File

@ -1,2 +1,7 @@
require "draper/version"
require 'draper/base'
require 'draper/system'
require 'draper/all_helpers'
require 'draper/base'
require 'draper/lazy_helpers'
Draper::System.setup

43
lib/draper/all_helpers.rb Normal file
View File

@ -0,0 +1,43 @@
module Draper
module AllHelpers
# Provide access to helper methods from outside controllers and views,
# such as in Presenter objects. Rails provides ActionController::Base.helpers,
# but this does not include any of our application helpers.
def all_helpers
@all_helpers_proxy ||= begin
# Start with just the rails helpers. This is the same method used
# by ActionController::Base.helpers
# proxy = ActionView::Base.new.extend(_helpers)
proxy = ActionController::Base.helpers
# url_for depends on _routes method being defined
proxy.instance_eval do
def _routes
Rails.application.routes
end
end
# Import all named path methods
proxy.extend(Rails.application.routes.named_routes.module)
# Load all our application helpers to extend
modules_for_helpers([:all]).each do |mod|
proxy.extend(mod)
end
proxy.instance_eval do
# A hack since this proxy doesn't pick up default_url_options from anywhere
def url_for(*args)
if args.last.is_a?(Hash) && !args.last[:only_path]
args = args.dup
args << args.pop.merge(host: ActionMailer::Base.default_url_options[:host])
end
super(*args)
end
end
proxy
end
end
end
end

View File

@ -1,14 +1,10 @@
module Draper
class Base
include ActionView::Helpers::TagHelper
include ActionView::Helpers::UrlHelper
include ActionView::Helpers::TextHelper
class Base
require 'active_support/core_ext/class/attribute'
class_attribute :denied, :allowed, :source_class
attr_accessor :source
DEFAULT_DENIED = Object.new.methods << :method_missing
DEFAULT_DENIED = Object.new.methods
self.denied = DEFAULT_DENIED
def initialize(subject)
@ -35,13 +31,14 @@ module Draper
end
def helpers
ActionController::Base.helpers
@helpers ||= ApplicationController::all_helpers
end
alias :h :helpers
def self.model_name
ActiveModel::Name.new(source_class)
end
private
def select_methods
self.allowed || (source.public_methods - denied)

View File

@ -0,0 +1,11 @@
module Draper
module LazyHelpers
def method_missing(method_name, *args, &block)
begin
helpers.send method_name, *args, &block
rescue NoMethodError
super
end
end
end
end

7
lib/draper/system.rb Normal file
View File

@ -0,0 +1,7 @@
module Draper
class System
def self.setup
ActionController::Base.send(:extend, Draper::AllHelpers)
end
end
end

View File

@ -1,24 +1,27 @@
class <%= singular_name.camelize %>Decorator < Draper::Base
# Rails Helpers
# Rails helpers like content_tag, link_to, and pluralize are already
# available to you. If you need access to other helpers, include them
# like this:
# include ActionView::Helpers::TextHelper
# Or pull in the whole kitchen sink:
# include ActionView::Helpers
# Helpers from Rails an Your Application
# You can access any helper via a proxy to ApplicationController
#
# Normal Usage: helpers.number_to_currency(2)
# Abbreviated : h.number_to_currency(2)
#
# You can optionally enable "lazy helpers" by including this module:
# include Draper::LazyHelpers
# Then use the helpers with no prefix:
# number_to_currency(2)
# Wrapper Methods
# Control access to the wrapped subject's methods using one of the following:
#
# To allow _only_ the listed methods:
# To allow only the listed methods (whitelist):
# allows :method1, :method2
#
# To allow everything _except_ the listed methods:
# To allow everything except the listed methods (blacklist):
# denies :method1, :method2
# Presentation Methods
# Define presentation-related instance methods. Ex:
# Define your own instance methods. Ex:
# def formatted_created_at
# content_tag :span, created_at.strftime("%A")
# end