Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decorate associations of a decorated object #8

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/active_decorator/decorator.rb
Expand Up @@ -28,8 +28,19 @@ def to_a_with_decorator
else
d = decorator_for obj.class
return obj unless d
obj.extend d unless obj.is_a? d
unless obj.is_a? d
obj.extend d
(class << obj ; self ; end).class_eval do
obj.class.reflect_on_all_associations.map(&:name).each do |assoc|
define_method(assoc) { |*args|
associated = super(*args)
Decorator.instance.decorate(associated)
}
end
end if obj.class.respond_to? :reflect_on_all_associations
end
end
obj
end

private
Expand Down
1 change: 1 addition & 0 deletions spec/fake_app/books/show.html.erb
@@ -1,2 +1,3 @@
<%= @book.link %>
<%= @book.cover_image %>
<%= @book.attribution %>
4 changes: 4 additions & 0 deletions spec/fake_app/fake_app.rb
Expand Up @@ -58,6 +58,10 @@ def link
def cover_image
image_tag 'cover.png'
end

def attribution
"#{title}, by #{author.capitalized_name}"
end
end

# controllers
Expand Down
1 change: 1 addition & 0 deletions spec/requests/action_view_helpers_spec.rb
Expand Up @@ -12,5 +12,6 @@
page.should have_content 'RHG'
end
page.should have_css('img')
page.should have_content 'RHG, by Aamine'
end
end