mirror of
https://github.com/drapergem/draper
synced 2023-03-27 23:21:17 -04:00
Merge pull request #541 from haines/object_class_name_alias
Alias `object` to `#{object_class.name}`
This commit is contained in:
commit
2b2c67664d
4 changed files with 54 additions and 1 deletions
|
@ -6,6 +6,7 @@ require 'active_model/serializers/xml'
|
||||||
require 'active_support/inflector'
|
require 'active_support/inflector'
|
||||||
require 'active_support/core_ext/hash/keys'
|
require 'active_support/core_ext/hash/keys'
|
||||||
require 'active_support/core_ext/hash/reverse_merge'
|
require 'active_support/core_ext/hash/reverse_merge'
|
||||||
|
require 'active_support/core_ext/name_error'
|
||||||
|
|
||||||
require 'draper/version'
|
require 'draper/version'
|
||||||
require 'draper/view_helpers'
|
require 'draper/view_helpers'
|
||||||
|
|
|
@ -57,6 +57,7 @@ module Draper
|
||||||
# @return [void]
|
# @return [void]
|
||||||
def self.decorates(object_class)
|
def self.decorates(object_class)
|
||||||
@object_class = object_class.to_s.camelize.constantize
|
@object_class = object_class.to_s.camelize.constantize
|
||||||
|
alias_object_to_object_class_name
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns the source class corresponding to the decorator class, as set by
|
# Returns the source class corresponding to the decorator class, as set by
|
||||||
|
@ -219,6 +220,15 @@ module Draper
|
||||||
|
|
||||||
private
|
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
|
def self.object_class_name
|
||||||
raise NameError if name.nil? || name.demodulize !~ /.+Decorator$/
|
raise NameError if name.nil? || name.demodulize !~ /.+Decorator$/
|
||||||
name.chomp("Decorator")
|
name.chomp("Decorator")
|
||||||
|
|
|
@ -361,6 +361,46 @@ module Draper
|
||||||
end
|
end
|
||||||
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
|
describe "#to_model" do
|
||||||
it "returns the decorator" do
|
it "returns the decorator" do
|
||||||
decorator = Decorator.new(Model.new)
|
decorator = Decorator.new(Model.new)
|
||||||
|
|
|
@ -1,2 +1,4 @@
|
||||||
|
if defined?(Mongoid)
|
||||||
class MongoidPostDecorator < Draper::Decorator
|
class MongoidPostDecorator < Draper::Decorator
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue