Replaced Draper.validate_options with assert_valid_keys. Much better!

This commit is contained in:
Toby Ovod-Everett 2012-12-12 21:01:59 -09:00 committed by Andrew Haines
parent b2f63882ec
commit fd359976d4
7 changed files with 10 additions and 22 deletions

View File

@ -38,13 +38,6 @@ module Draper
end
end
def self.validate_options(options, *valid_keys)
options_errors = options.keys - valid_keys
unless options_errors.empty?
raise ArgumentError, "Invalid option keys: #{options_errors.map {|k| k.inspect}.join(', ')}", caller
end
end
class UninferrableDecoratorError < NameError
def initialize(klass)
super("Could not infer a decorator for #{klass}.")

View File

@ -1,5 +1,3 @@
require 'draper'
module Draper
class CollectionDecorator
include Enumerable
@ -19,7 +17,7 @@ module Draper
def initialize(source, options = {})
@source = source
@decorator_class = options.delete(:with) || self.class.inferred_decorator_class
Draper.validate_options(options, :with, :context)
options.assert_valid_keys(:with, :context)
@options = options
end

View File

@ -1,5 +1,3 @@
require 'draper'
module Draper
class DecoratedAssociation
@ -8,7 +6,7 @@ module Draper
def initialize(base, association, options)
@base = base
@association = association
Draper.validate_options(options, :with, :scope, :context)
options.assert_valid_keys(:with, :scope, :context)
@options = options
end

View File

@ -1,5 +1,4 @@
require 'active_support/core_ext/array/extract_options'
require 'draper'
module Draper
class Decorator
@ -30,7 +29,7 @@ module Draper
def initialize(source, options = {})
source.to_a if source.respond_to?(:to_a) # forces evaluation of a lazy query from AR
@source = source
Draper.validate_options(options, :context)
options.assert_valid_keys(:context)
@options = options
handle_multiple_decoration if source.is_a?(Draper::Decorator)
end
@ -83,7 +82,7 @@ module Draper
# passed the base decorator's `context` Hash and should return the desired
# context Hash for the decorated items.
def self.decorates_association(association, options = {})
Draper.validate_options(options, :with, :scope, :context)
options.assert_valid_keys(:with, :scope, :context)
define_method(association) do
decorated_associations[association] ||= Draper::DecoratedAssociation.new(self, association, options)
decorated_associations[association].call
@ -142,7 +141,7 @@ module Draper
# items, or `:infer` to call each item's `decorate` method instead
# @option options [Hash] :context context available to decorated items
def self.decorate_collection(source, options = {})
Draper.validate_options(options, :with, :context)
options.assert_valid_keys(:with, :context)
Draper::CollectionDecorator.new(source, options.reverse_merge(with: self))
end

View File

@ -103,7 +103,7 @@ describe Draper::CollectionDecorator do
end
it "raises error on invalid options" do
expect { Draper::CollectionDecorator.new(source, valid_options.merge(foo: 'bar')) }.to raise_error(ArgumentError, 'Invalid option keys: :foo')
expect { Draper::CollectionDecorator.new(source, valid_options.merge(foo: 'bar')) }.to raise_error(ArgumentError, 'Unknown key: foo')
end
end

View File

@ -16,7 +16,7 @@ describe Draper::DecoratedAssociation do
end
it "raises error on invalid options" do
expect { Draper::DecoratedAssociation.new(base, association, valid_options.merge(foo: 'bar')) }.to raise_error(ArgumentError, 'Invalid option keys: :foo')
expect { Draper::DecoratedAssociation.new(base, association, valid_options.merge(foo: 'bar')) }.to raise_error(ArgumentError, 'Unknown key: foo')
end
end
end

View File

@ -15,7 +15,7 @@ describe Draper::Decorator do
end
it "raises error on invalid options" do
expect { decorator_class.new(source, valid_options.merge(foo: 'bar')) }.to raise_error(ArgumentError, 'Invalid option keys: :foo')
expect { decorator_class.new(source, valid_options.merge(foo: 'bar')) }.to raise_error(ArgumentError, 'Unknown key: foo')
end
end
@ -113,7 +113,7 @@ describe Draper::Decorator do
end
it "raises error on invalid options" do
expect { ProductDecorator.decorate_collection(source, valid_options.merge(foo: 'bar')) }.to raise_error(ArgumentError, 'Invalid option keys: :foo')
expect { ProductDecorator.decorate_collection(source, valid_options.merge(foo: 'bar')) }.to raise_error(ArgumentError, 'Unknown key: foo')
end
end
@ -290,7 +290,7 @@ describe Draper::Decorator do
end
it "raises error on invalid options" do
expect { decorator_class.decorates_association :similar_products, valid_options.merge(foo: 'bar') }.to raise_error(ArgumentError, 'Invalid option keys: :foo')
expect { decorator_class.decorates_association :similar_products, valid_options.merge(foo: 'bar') }.to raise_error(ArgumentError, 'Unknown key: foo')
end
end