Change excludes to denies
This commit is contained in:
parent
c1b4747e51
commit
2c57049832
|
@ -5,21 +5,21 @@ module Draper
|
||||||
include ActionView::Helpers::TextHelper
|
include ActionView::Helpers::TextHelper
|
||||||
|
|
||||||
require 'active_support/core_ext/class/attribute'
|
require 'active_support/core_ext/class/attribute'
|
||||||
class_attribute :exclusions, :allowed
|
class_attribute :denied, :allowed
|
||||||
attr_accessor :source
|
attr_accessor :source
|
||||||
|
|
||||||
DEFAULT_EXCLUSIONS = Object.new.methods
|
DEFAULT_DENIED = Object.new.methods << [:method_missing]
|
||||||
self.exclusions = DEFAULT_EXCLUSIONS
|
self.denied = DEFAULT_DENIED
|
||||||
|
|
||||||
def self.excludes(*input_exclusions)
|
def self.denies(*input_denied)
|
||||||
raise ArgumentError, "Specify at least one method (as a symbol) to exclude when using excludes" if input_exclusions.empty?
|
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 'excludes', but not both." if self.allowed?
|
raise ArgumentError, "Use either 'allows' or 'denies', but not both." if self.allowed?
|
||||||
self.exclusions += input_exclusions
|
self.denied += input_denied
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.allows(*input_allows)
|
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, "Specify at least one method (as a symbol) to allow when using allows" if input_allows.empty?
|
||||||
#raise ArgumentError, "Use either 'allows' or 'excludes', but not both." unless (self.exclusions == DEFAULT_EXCLUSIONS)
|
#raise ArgumentError, "Use either 'allows' or 'denies', but not both." unless (self.denies == DEFAULT_EXCLUSIONS)
|
||||||
self.allowed = input_allows
|
self.allowed = input_allows
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ module Draper
|
||||||
|
|
||||||
private
|
private
|
||||||
def select_methods
|
def select_methods
|
||||||
self.allowed || (source.public_methods - exclusions)
|
self.allowed || (source.public_methods - denied)
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_methods
|
def build_methods
|
||||||
|
|
|
@ -21,10 +21,10 @@ describe Draper::Base do
|
||||||
source.to_s.should_not == subject.to_s
|
source.to_s.should_not == subject.to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "a sample usage with excludes" do
|
describe "a sample usage with denies" do
|
||||||
before(:all) do
|
before(:all) do
|
||||||
class DecoratorWithExcludes < Draper::Base
|
class DecoratorWithDenies < Draper::Base
|
||||||
excludes :upcase
|
denies :upcase
|
||||||
|
|
||||||
def sample_content
|
def sample_content
|
||||||
content_tag :span, "Hello, World!"
|
content_tag :span, "Hello, World!"
|
||||||
|
@ -40,27 +40,31 @@ describe Draper::Base do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:subject_with_excludes){ DecoratorWithExcludes.new(source) }
|
let(:subject_with_denies){ DecoratorWithDenies.new(source) }
|
||||||
|
|
||||||
it "should not echo methods specified with excludes" do
|
it "should not echo methods specified with denies" do
|
||||||
subject_with_excludes.should_not respond_to(:upcase)
|
subject_with_denies.should_not respond_to(:upcase)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should not clobber other decorators methods" do
|
it "should not clobber other decorators' methods" do
|
||||||
subject.should respond_to(:upcase)
|
subject.should respond_to(:upcase)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should be able to use the content_tag helper" do
|
it "should be able to use the content_tag helper" do
|
||||||
subject_with_excludes.sample_content.to_s.should == "<span>Hello, World!</span>"
|
subject_with_denies.sample_content.to_s.should == "<span>Hello, World!</span>"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should be able to use the link_to helper" do
|
it "should be able to use the link_to helper" do
|
||||||
subject_with_excludes.sample_link.should == "<a href=\"/World\">Hello</a>"
|
subject_with_denies.sample_link.should == "<a href=\"/World\">Hello</a>"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should be able to use the pluralize helper" do
|
it "should be able to use the pluralize helper" do
|
||||||
pending("Figure out odd interaction when the wrapped source object already has the text_helper methods (ie: a String)")
|
pending("Figure out odd interaction when the wrapped source object already has the text_helper methods (ie: a String)")
|
||||||
subject_with_excludes.sample_truncate.should == "Once..."
|
subject_with_denies.sample_truncate.should == "Once..."
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should nullify method_missing to prevent AR from being cute" do
|
||||||
|
pending("How to test this without AR? Ugh.")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -82,29 +86,29 @@ describe Draper::Base do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "invalid usages of allows and excludes" do
|
describe "invalid usages of allows and denies" do
|
||||||
let(:blank_allows){
|
let(:blank_allows){
|
||||||
class DecoratorWithInvalidAllows < Draper::Base
|
class DecoratorWithInvalidAllows < Draper::Base
|
||||||
allows
|
allows
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
let(:blank_excludes){
|
let(:blank_denies){
|
||||||
class DecoratorWithInvalidExcludes < Draper::Base
|
class DecoratorWithInvalidDenies < Draper::Base
|
||||||
excludes
|
denies
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
let(:using_allows_then_excludes){
|
let(:using_allows_then_denies){
|
||||||
class DecoratorWithInvalidMixing < Draper::Base
|
class DecoratorWithInvalidMixing < Draper::Base
|
||||||
allows :upcase
|
allows :upcase
|
||||||
excludes :downcase
|
denies :downcase
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
let(:using_excludes_then_allows){
|
let(:using_denies_then_allows){
|
||||||
class DecoratorWithInvalidMixing < Draper::Base
|
class DecoratorWithInvalidMixing < Draper::Base
|
||||||
excludes :downcase
|
denies :downcase
|
||||||
allows :upcase
|
allows :upcase
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
@ -113,16 +117,16 @@ describe Draper::Base do
|
||||||
expect {blank_allows}.should raise_error(ArgumentError)
|
expect {blank_allows}.should raise_error(ArgumentError)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should raise an exception for a blank excludes" do
|
it "should raise an exception for a blank denies" do
|
||||||
expect {blank_excludes}.should raise_error(ArgumentError)
|
expect {blank_denies}.should raise_error(ArgumentError)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should raise an exception for mixing allows then excludes" do
|
it "should raise an exception for calling allows then denies" do
|
||||||
expect {using_allows_then_excludes}.should raise_error(ArgumentError)
|
expect {using_allows_then_denies}.should raise_error(ArgumentError)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should raise an exception for calling excludes then allows" do
|
it "should raise an exception for calling denies then allows" do
|
||||||
expect {using_excludes_then_allows}.should raise_error(ArgumentError)
|
expect {using_denies_then_allows}.should raise_error(ArgumentError)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
Loading…
Reference in New Issue