Skip to content

Commit

Permalink
Merge pull request drapergem#251 from WizardOfOgz/feature-denies-all
Browse files Browse the repository at this point in the history
Add denies_all feature
  • Loading branch information
steveklabnik committed Aug 12, 2012
2 parents 31600c0 + e02007b commit 148e732
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/draper/base.rb
Expand Up @@ -105,6 +105,16 @@ def self.denies(*input_denied)
self.denied += input_denied
end

# Specifies that all methods may *not* be proxied to
# to the wrapped object.
#
# Do not use `.allows` and `.denies` in combination with '.denies_all'
def self.denies_all
raise ArgumentError, "Use either 'allows' or 'denies', but not both." unless ((self.allowed == DEFAULT_ALLOWED && self.denied == DEFAULT_DENIED) || (self.allowed != DEFAULT_ALLOWED && self.denied != DEFAULT_DENIED))
self.denied += [nil] # Add dummy value to denied to prevent calls to #allows. Hacky???
self.allowed += [nil] # Add dummy value to allowed to prevent calls to #denies
end

# Specifies a white list of methods which *may* be proxied to
# to the wrapped object. When `allows` is used, only the listed
# methods and methods defined in the decorator itself will be
Expand Down
63 changes: 63 additions & 0 deletions spec/draper/base_spec.rb
Expand Up @@ -693,6 +693,69 @@ class DecoratorWithDeniesAndAllows < Draper::Base
end
end

describe "a sample usage with denies_all" do
let(:subject_with_denies_all){ DecoratorWithDeniesAll.new(source) }

[:goodnight_moon, :hello_world, :title].each do |method|
it "does echo #{method} method" do
subject_with_denies_all.should_not respond_to(method)
end
end

let(:using_denies_all_then_denies_all) {
class DecoratorWithDeniesAllAndDeniesAll < Draper::Base
denies_all
denies_all
end
}

it "allows multple calls to .denies_all" do
expect { using_denies_all_then_denies_all }.to_not raise_error(ArgumentError)
end
end

describe "invalid usages of denies_all" do
let(:using_allows_then_denies_all) {
class DecoratorWithAllowsAndDeniesAll < Draper::Base
allows :hello_world
denies_all
end
}
let(:using_denies_then_denies_all) {
class DecoratorWithDeniesAndDeniesAll < Draper::Base
denies :goodnight_moon
denies_all
end
}
let(:using_denies_all_then_allows) {
class DecoratorWithDeniesAllAndAllows < Draper::Base
denies_all
allows :hello_world
end
}
let(:using_denies_all_then_denies) {
class DecoratorWithDeniesAllAndDenies < Draper::Base
denies_all
denies :goodnight_moon
end
}
it "raises an exception when calling allows then denies_all" do
expect {using_allows_then_denies_all}.to raise_error(ArgumentError)
end

it "raises an exception when calling denies then denies_all" do
expect {using_denies_then_denies_all}.to raise_error(ArgumentError)
end

it "raises an exception when calling denies_all then allows" do
expect {using_denies_all_then_allows}.to raise_error(ArgumentError)
end

it "raises an exception when calling denies_all then denies" do
expect {using_denies_all_then_denies}.to raise_error(ArgumentError)
end
end

context "in a Rails application" do
let(:decorator){ DecoratorWithApplicationHelper.decorate(Object.new) }

Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Expand Up @@ -15,6 +15,7 @@
require './spec/support/samples/decorator_with_multiple_allows'
require './spec/support/samples/decorator_with_application_helper'
require './spec/support/samples/decorator_with_denies'
require './spec/support/samples/decorator_with_denies_all'
require './spec/support/samples/decorator_with_special_methods'
require './spec/support/samples/namespaced_product'
require './spec/support/samples/namespaced_product_decorator'
Expand Down
3 changes: 3 additions & 0 deletions spec/support/samples/decorator_with_denies_all.rb
@@ -0,0 +1,3 @@
class DecoratorWithDeniesAll < Draper::Base
denies_all
end

0 comments on commit 148e732

Please sign in to comment.