diff --git a/lib/draper.rb b/lib/draper.rb index a821518..f5b1fbc 100644 --- a/lib/draper.rb +++ b/lib/draper.rb @@ -6,6 +6,7 @@ require 'active_model/serializers/xml' require 'active_support/inflector' require 'active_support/core_ext/hash/keys' require 'active_support/core_ext/hash/reverse_merge' +require 'active_support/core_ext/name_error' require 'draper/version' require 'draper/view_helpers' diff --git a/lib/draper/decorator.rb b/lib/draper/decorator.rb index b2593c7..bb12b96 100755 --- a/lib/draper/decorator.rb +++ b/lib/draper/decorator.rb @@ -57,6 +57,7 @@ module Draper # @return [void] def self.decorates(object_class) @object_class = object_class.to_s.camelize.constantize + alias_object_to_object_class_name end # Returns the source class corresponding to the decorator class, as set by @@ -219,6 +220,15 @@ module Draper private + def self.inherited(subclass) + subclass.alias_object_to_object_class_name + super + end + + def self.alias_object_to_object_class_name + alias_method object_class.name.underscore, :object if object_class? + end + def self.object_class_name raise NameError if name.nil? || name.demodulize !~ /.+Decorator$/ name.chomp("Decorator") diff --git a/spec/draper/decorator_spec.rb b/spec/draper/decorator_spec.rb index 329c2f4..e317b36 100755 --- a/spec/draper/decorator_spec.rb +++ b/spec/draper/decorator_spec.rb @@ -361,6 +361,46 @@ module Draper end end + describe "aliasing object to object class name" do + context "when object_class is inferrable from the decorator name" do + it "aliases object to the object class name" do + object = stub + decorator = ProductDecorator.new(object) + + expect(decorator.product).to be object + end + end + + context "when object_class is set by decorates" do + it "aliases object to the object class name" do + decorator_class = Class.new(Decorator) { decorates Product } + object = stub + decorator = decorator_class.new(object) + + expect(decorator.product).to be object + end + end + + context "when object_class's name is several words long" do + it "underscores the method name" do + stub_const "LongWindedModel", Class.new + decorator_class = Class.new(Decorator) { decorates LongWindedModel } + object = stub + decorator = decorator_class.new(object) + + expect(decorator.long_winded_model).to be object + end + end + + context "when object_class is not set" do + it "does not alias object" do + decorator_class = Class.new(Decorator) + + expect(decorator_class.instance_methods).to eq Decorator.instance_methods + end + end + end + describe "#to_model" do it "returns the decorator" do decorator = Decorator.new(Model.new) diff --git a/spec/dummy/app/decorators/mongoid_post_decorator.rb b/spec/dummy/app/decorators/mongoid_post_decorator.rb index 61596e4..42d87e0 100644 --- a/spec/dummy/app/decorators/mongoid_post_decorator.rb +++ b/spec/dummy/app/decorators/mongoid_post_decorator.rb @@ -1,2 +1,4 @@ -class MongoidPostDecorator < Draper::Decorator +if defined?(Mongoid) + class MongoidPostDecorator < Draper::Decorator + end end