Alias `object` to `#{model_name}`
If we have an `ArticleDecorator` there is going to be a method added to it named `article` which is convenient for the user.
This commit is contained in:
parent
66e33e553b
commit
f5f27243c3
|
@ -16,6 +16,15 @@ module Draper
|
|||
# @return [Hash] extra data to be used in user-defined methods.
|
||||
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.
|
||||
#
|
||||
# Decorators may be applied to other decorators. However, applying a
|
||||
|
|
|
@ -361,6 +361,23 @@ module Draper
|
|||
end
|
||||
end
|
||||
|
||||
describe "aliasing object to wrapped model name" do
|
||||
class ::ProductDecorator < Decorator; end
|
||||
class ::Product
|
||||
attr_reader :name
|
||||
def initialize
|
||||
@name = "bob"
|
||||
end
|
||||
end
|
||||
|
||||
it "aliases object to wrapped model name" do
|
||||
decorator = ProductDecorator.new(Product.new)
|
||||
|
||||
expect(decorator.product).not_to be nil
|
||||
expect(decorator.product.name).to eq "bob"
|
||||
end
|
||||
end
|
||||
|
||||
describe "#to_model" do
|
||||
it "returns the decorator" do
|
||||
decorator = Decorator.new(Model.new)
|
||||
|
|
Loading…
Reference in New Issue