Added support for :class_name option to mimic the behavior of ActiveRecord and help to unify code. :class / :class_name can now be strings too!

This commit is contained in:
Mario Uher 2012-01-28 00:01:01 +01:00
parent 1af010c105
commit abb96e564f
2 changed files with 40 additions and 1 deletions

View File

@ -48,7 +48,8 @@ module Draper
#
# @param [Symbol] class_name snakecase name of the decorated class, like `:product`
def self.decorates(input, options = {})
self.model_class = options[:class] || input.to_s.camelize.constantize
self.model_class = options[:class] || options[:class_name] || input.to_s.camelize
self.model_class = model_class.constantize if model_class.respond_to?(:constantize)
model_class.send :include, Draper::ModelSupport
define_method(input){ @model }
end

View File

@ -53,6 +53,44 @@ describe Draper::Base do
BusinessDecorator.model_class.should == Business
end.should_not raise_error
end
context("accepts ActiveRecord like :class_name option too") do
it "accepts constants for :class" do
expect do
class CustomDecorator < Draper::Base
decorates :product, :class => Product
end
CustomDecorator.model_class.should == Product
end.should_not raise_error
end
it "accepts constants for :class_name" do
expect do
class CustomDecorator < Draper::Base
decorates :product, :class_name => Product
end
CustomDecorator.model_class.should == Product
end.should_not raise_error
end
it "accepts strings for :class" do
expect do
class CustomDecorator < Draper::Base
decorates :product, :class => 'Product'
end
CustomDecorator.model_class.should == Product
end.should_not raise_error
end
it "accepts strings for :class_name" do
expect do
class CustomDecorator < Draper::Base
decorates :product, :class_name => 'Product'
end
CustomDecorator.model_class.should == Product
end.should_not raise_error
end
end
it "creates a named accessor for the wrapped model" do
pd = ProductDecorator.new(source)