Merge pull request #129 from yabawock/multiple-allows

Allow multiple allows / inherit allows
This commit is contained in:
Steve Klabnik 2012-02-13 07:14:43 -08:00
commit 36a6e5d1f4
4 changed files with 21 additions and 3 deletions

View File

@ -5,6 +5,7 @@ module Draper
attr_accessor :model, :options
DEFAULT_DENIED = Object.new.methods << :method_missing
DEFAULT_ALLOWED = []
FORCED_PROXY = [:to_param, :id]
FORCED_PROXY.each do |method|
define_method method do |*args, &block|
@ -12,6 +13,7 @@ module Draper
end
end
self.denied = DEFAULT_DENIED
self.allowed = DEFAULT_ALLOWED
# Initialize a new decorator instance by passing in
# an instance of the source class. Pass in an optional
@ -89,7 +91,7 @@ module Draper
# @param [Symbols*] methods to deny like `:find, :find_by_name`
def self.denies(*input_denied)
raise ArgumentError, "Specify at least one method (as a symbol) to exclude when using denies" if input_denied.empty?
raise ArgumentError, "Use either 'allows' or 'denies', but not both." if self.allowed?
raise ArgumentError, "Use either 'allows' or 'denies', but not both." unless (self.allowed == DEFAULT_ALLOWED)
self.denied += input_denied
end
@ -105,7 +107,7 @@ module Draper
def self.allows(*input_allows)
raise ArgumentError, "Specify at least one method (as a symbol) to allow when using allows" if input_allows.empty?
raise ArgumentError, "Use either 'allows' or 'denies', but not both." unless (self.denied == DEFAULT_DENIED)
self.allowed = input_allows
self.allowed += input_allows
end
# Initialize a new decorator instance by passing in
@ -235,7 +237,7 @@ module Draper
private
def allow?(method)
(!allowed? || allowed.include?(method) || FORCED_PROXY.include?(method)) && !denied.include?(method)
(allowed.empty? || allowed.include?(method) || FORCED_PROXY.include?(method)) && !denied.include?(method)
end
end
end

View File

@ -515,6 +515,8 @@ describe Draper::Base do
describe "a sample usage with allows" do
let(:subject_with_allows){ DecoratorWithAllows.new(source) }
let(:subject_with_multiple_allows){ DecoratorWithMultipleAllows.new(source) }
it "should echo the allowed method" do
subject_with_allows.should respond_to(:goodnight_moon)
end
@ -522,6 +524,15 @@ describe Draper::Base do
it "should echo _only_ the allowed method" do
subject_with_allows.should_not respond_to(:hello_world)
end
it "should echo the combined allowed methods" do
subject_with_multiple_allows.should respond_to(:goodnight_moon)
subject_with_multiple_allows.should respond_to(:hello_world)
end
it "should echo _only_ the combined allowed methods" do
subject_with_multiple_allows.should_not respond_to(:title)
end
end
describe "invalid usages of allows and denies" do

View File

@ -7,6 +7,7 @@ require './spec/support/samples/application_controller.rb'
require './spec/support/samples/application_helper.rb'
require './spec/support/samples/decorator.rb'
require './spec/support/samples/decorator_with_allows.rb'
require './spec/support/samples/decorator_with_multiple_allows.rb'
require './spec/support/samples/decorator_with_application_helper.rb'
require './spec/support/samples/decorator_with_denies.rb'
require './spec/support/samples/namespaced_product.rb'

View File

@ -0,0 +1,4 @@
class DecoratorWithMultipleAllows < Draper::Base
allows :goodnight_moon
allows :hello_world
end