move #helpers (delegates to HelperProxy) to module

Since the #helpers interface lives in a module, we
can now re-use the HelperProxy logic in custom
"Presenter" classes which do not inherit from
Draper::Decorator. These custom classes may either
wrap 1..N different collections of AR models
and/or POROs.
This commit is contained in:
Jacques Fuentes 2012-10-16 10:38:51 -04:00
parent 7b645eae23
commit 7db9e224a0
4 changed files with 79 additions and 28 deletions

View File

@ -3,6 +3,7 @@ require 'action_view'
require 'draper/version'
require 'draper/system'
require 'draper/active_model_support'
require 'draper/view_helpers'
require 'draper/decorator'
require 'draper/helper_proxy'
require 'draper/lazy_helpers'

View File

@ -4,6 +4,7 @@ require 'active_support/core_ext/array/extract_options'
module Draper
class Decorator
include ActiveModelSupport
include Draper::ViewHelpers
class_attribute :model_class
attr_accessor :model, :options
@ -162,34 +163,6 @@ module Draper
decorate(model_class.last, options)
end
# Access the helpers proxy to call built-in and user-defined
# Rails helpers. Aliased to `h` for convenience.
#
# @return [HelperProxy] the helpers proxy
def helpers
self.class.helpers
end
alias :h :helpers
# Localize is something that's used quite often. Even though
# it's available through helpers, that's annoying. Aliased
# to `l` for convenience.
def localize(*args)
helpers.localize(*args)
end
alias :l :localize
# Access the helpers proxy to call built-in and user-defined
# Rails helpers from a class context.
#
# @return [HelperProxy] the helpers proxy
class << self
def helpers
@helpers ||= HelperProxy.new
end
alias :h :helpers
end
# Fetch the original wrapped model.
#
# @return [Object] original_model

View File

@ -0,0 +1,36 @@
module Draper
module ViewHelpers
extend ActiveSupport::Concern
module ClassMethods
# Access the helpers proxy to call built-in and user-defined
# Rails helpers from a class context.
#
# @return [HelperProxy] the helpers proxy
def helpers
@helpers ||= Draper::HelperProxy.new
end
alias :h :helpers
end
# Access the helpers proxy to call built-in and user-defined
# Rails helpers. Aliased to `h` for convenience.
#
# @return [HelperProxy] the helpers proxy
def helpers
self.class.helpers
end
alias :h :helpers
# Localize is something that's used quite often. Even though
# it's available through helpers, that's annoying. Aliased
# to `l` for convenience.
def localize(*args)
helpers.localize(*args)
end
alias :l :localize
end
end

View File

@ -0,0 +1,41 @@
require 'spec_helper'
describe Draper::ViewHelpers do
let(:helper_proxy) { Draper::HelperProxy.new }
let(:view_context) { Object.new }
before { view_helpers.helpers.stub(:view_context).and_return(view_context) }
subject { Class.new { include Draper::ViewHelpers } }
let(:view_helpers) { subject.new }
describe "#helpers" do
it "returns a HelperProxy" do
view_helpers.helpers.should be_a Draper::HelperProxy
end
it "is aliased to #h" do
view_helpers.h.should be subject.helpers
end
end
it "delegates #localize to #helpers" do
view_context.should_receive(:localize).with(Date.new)
view_helpers.localize(Date.new)
end
it "aliases #l to #localize" do
view_context.should_receive(:localize).with(Date.new)
view_helpers.l(Date.new)
end
describe ".helpers" do
it "returns a HelperProxy" do
subject.helpers.should be_a Draper::HelperProxy
end
it "is aliased to #h" do
subject.h.should be subject.helpers
end
end
end