Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactor the initialization of allowed methods

  • Loading branch information...
commit 3ac642140f76310df3e7647b6a8f8d9ba019b8d9 1 parent a4927a5
@yabawock yabawock authored
Showing with 4 additions and 3 deletions.
  1. +4 −3 lib/draper/base.rb
View
7 lib/draper/base.rb
@@ -5,6 +5,7 @@ class Base
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 @@ class Base
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 @@ def self.decorates_associations(*association_symbols)
# @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,6 @@ def self.denies(*input_denied)
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 ||= []
self.allowed += input_allows
end
@@ -236,7 +237,7 @@ def source
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
Please sign in to comment.
Something went wrong with that request. Please try again.