mirror of
https://github.com/drapergem/draper
synced 2023-03-27 23:21:17 -04:00
Improve aliasing to object class name
This commit is contained in:
parent
9bd86b982a
commit
a239d93288
2 changed files with 43 additions and 19 deletions
|
@ -16,15 +16,6 @@ module Draper
|
||||||
# @return [Hash] extra data to be used in user-defined methods.
|
# @return [Hash] extra data to be used in user-defined methods.
|
||||||
attr_accessor :context
|
attr_accessor :context
|
||||||
|
|
||||||
def self.inherited(klass)
|
|
||||||
begin
|
|
||||||
alias_name = klass.name.downcase.gsub(/decorator/, "").to_sym
|
|
||||||
klass.send(:define_method, alias_name) do
|
|
||||||
object
|
|
||||||
end
|
|
||||||
rescue; end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Wraps an object in a new instance of the decorator.
|
# Wraps an object in a new instance of the decorator.
|
||||||
#
|
#
|
||||||
# Decorators may be applied to other decorators. However, applying a
|
# Decorators may be applied to other decorators. However, applying a
|
||||||
|
@ -66,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
|
||||||
|
@ -228,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,20 +361,43 @@ module Draper
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "aliasing object to wrapped model name" do
|
describe "aliasing object to object class name" do
|
||||||
class ::ProductDecorator < Decorator; end
|
context "when object_class is inferrable from the decorator name" do
|
||||||
class ::Product
|
it "aliases object to the object class name" do
|
||||||
attr_reader :name
|
object = stub
|
||||||
def initialize
|
decorator = ProductDecorator.new(object)
|
||||||
@name = "bob"
|
|
||||||
|
expect(decorator.product).to be object
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "aliases object to wrapped model name" do
|
context "when object_class is set by decorates" do
|
||||||
decorator = ProductDecorator.new(Product.new)
|
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).not_to be nil
|
expect(decorator.product).to be object
|
||||||
expect(decorator.product.name).to eq "bob"
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue