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:
Sam-Serpoosh 2013-05-19 13:01:36 -05:00 committed by Andrew Haines
parent 66e33e553b
commit f5f27243c3
2 changed files with 26 additions and 0 deletions

View File

@ -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

View File

@ -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)