Replaced Draper.validate_options with assert_valid_keys. Much better!
This commit is contained in:
parent
b2f63882ec
commit
fd359976d4
|
@ -38,13 +38,6 @@ module Draper
|
||||||
end
|
end
|
||||||
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
|
class UninferrableDecoratorError < NameError
|
||||||
def initialize(klass)
|
def initialize(klass)
|
||||||
super("Could not infer a decorator for #{klass}.")
|
super("Could not infer a decorator for #{klass}.")
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
require 'draper'
|
|
||||||
|
|
||||||
module Draper
|
module Draper
|
||||||
class CollectionDecorator
|
class CollectionDecorator
|
||||||
include Enumerable
|
include Enumerable
|
||||||
|
@ -19,7 +17,7 @@ module Draper
|
||||||
def initialize(source, options = {})
|
def initialize(source, options = {})
|
||||||
@source = source
|
@source = source
|
||||||
@decorator_class = options.delete(:with) || self.class.inferred_decorator_class
|
@decorator_class = options.delete(:with) || self.class.inferred_decorator_class
|
||||||
Draper.validate_options(options, :with, :context)
|
options.assert_valid_keys(:with, :context)
|
||||||
@options = options
|
@options = options
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
require 'draper'
|
|
||||||
|
|
||||||
module Draper
|
module Draper
|
||||||
class DecoratedAssociation
|
class DecoratedAssociation
|
||||||
|
|
||||||
|
@ -8,7 +6,7 @@ module Draper
|
||||||
def initialize(base, association, options)
|
def initialize(base, association, options)
|
||||||
@base = base
|
@base = base
|
||||||
@association = association
|
@association = association
|
||||||
Draper.validate_options(options, :with, :scope, :context)
|
options.assert_valid_keys(:with, :scope, :context)
|
||||||
@options = options
|
@options = options
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
require 'active_support/core_ext/array/extract_options'
|
require 'active_support/core_ext/array/extract_options'
|
||||||
require 'draper'
|
|
||||||
|
|
||||||
module Draper
|
module Draper
|
||||||
class Decorator
|
class Decorator
|
||||||
|
@ -30,7 +29,7 @@ module Draper
|
||||||
def initialize(source, options = {})
|
def initialize(source, options = {})
|
||||||
source.to_a if source.respond_to?(:to_a) # forces evaluation of a lazy query from AR
|
source.to_a if source.respond_to?(:to_a) # forces evaluation of a lazy query from AR
|
||||||
@source = source
|
@source = source
|
||||||
Draper.validate_options(options, :context)
|
options.assert_valid_keys(:context)
|
||||||
@options = options
|
@options = options
|
||||||
handle_multiple_decoration if source.is_a?(Draper::Decorator)
|
handle_multiple_decoration if source.is_a?(Draper::Decorator)
|
||||||
end
|
end
|
||||||
|
@ -83,7 +82,7 @@ module Draper
|
||||||
# passed the base decorator's `context` Hash and should return the desired
|
# passed the base decorator's `context` Hash and should return the desired
|
||||||
# context Hash for the decorated items.
|
# context Hash for the decorated items.
|
||||||
def self.decorates_association(association, options = {})
|
def self.decorates_association(association, options = {})
|
||||||
Draper.validate_options(options, :with, :scope, :context)
|
options.assert_valid_keys(:with, :scope, :context)
|
||||||
define_method(association) do
|
define_method(association) do
|
||||||
decorated_associations[association] ||= Draper::DecoratedAssociation.new(self, association, options)
|
decorated_associations[association] ||= Draper::DecoratedAssociation.new(self, association, options)
|
||||||
decorated_associations[association].call
|
decorated_associations[association].call
|
||||||
|
@ -142,7 +141,7 @@ module Draper
|
||||||
# items, or `:infer` to call each item's `decorate` method instead
|
# items, or `:infer` to call each item's `decorate` method instead
|
||||||
# @option options [Hash] :context context available to decorated items
|
# @option options [Hash] :context context available to decorated items
|
||||||
def self.decorate_collection(source, options = {})
|
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))
|
Draper::CollectionDecorator.new(source, options.reverse_merge(with: self))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -103,7 +103,7 @@ describe Draper::CollectionDecorator do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "raises error on invalid options" do
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@ describe Draper::DecoratedAssociation do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "raises error on invalid options" do
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -15,7 +15,7 @@ describe Draper::Decorator do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "raises error on invalid options" do
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -113,7 +113,7 @@ describe Draper::Decorator do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "raises error on invalid options" do
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -290,7 +290,7 @@ describe Draper::Decorator do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "raises error on invalid options" do
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue