1
0
Fork 0
mirror of https://github.com/drapergem/draper synced 2023-03-27 23:21:17 -04:00

Only return attributes implemented by the decorator

Closes #444
This commit is contained in:
Andrew Haines 2013-01-31 09:28:21 +00:00
parent bb8c1b723b
commit 94f83a2e0b
2 changed files with 16 additions and 1 deletions

View file

@ -187,8 +187,14 @@ module Draper
self
end
# @return [Hash] the source's attributes, sliced to only include those
# implemented by the decorator.
def attributes
source.attributes.select {|attribute, _| respond_to?(attribute) }
end
# ActiveModel compatibility
delegate :attributes, :to_param, :to_partial_path
delegate :to_param, :to_partial_path
# ActiveModel compatibility
singleton_class.delegate :model_name, to: :source_class

View file

@ -345,6 +345,15 @@ module Draper
end
end
describe "#attributes" do
it "returns only the source's attributes that are implemented by the decorator" do
decorator = Decorator.new(double(attributes: {foo: "bar", baz: "qux"}))
decorator.stub(:foo)
expect(decorator.attributes).to eq({foo: "bar"})
end
end
describe ".model_name" do
it "delegates to the source class" do
Decorator.stub source_class: double(model_name: :delegated)