Created
October 3, 2012 17:47
-
-
Save sbleon/3828563 to your computer and use it in GitHub Desktop.
Double-decorating objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HouseController < ActionController::Base | |
def show | |
@house = HouseDecorator.decorate(House.find(params[:id])) | |
@windows = WindowBlindsDecorator.decorate(@house.windows) | |
# @windows would be inadvertently double-decorated | |
# (though this is prevented by previous fix from | |
# https://github.com/jcasimir/draper/commit/c945cce6f3147c805b4db73580c198016750ad60) | |
# My pull request would cause an error to be thrown instead. | |
end | |
def edit | |
@house = HouseDecorator.decorate(House.find(params[:id])) | |
@windows_with_blinds = WindowBlindsDecorator.decorate(@house.windows) | |
# As above, double-decoration is prevented | |
@windows_with_blinds_and_drapes = WindowDraperDecorator.decorate(@windows_with_blinds) | |
# This is now incorrect. We now have a window with drapes, and no blinds, | |
# due to the previous fix discarding any previous decorator. | |
# This would work correctly with my pull request. | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HouseDecorator < Draper::Base | |
def windows | |
WindowBlindsDecorator.decorate(model.windows) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment