-
-
Save carlosramireziii/644ff5cabca9a1ba527686a0c2c3c61e to your computer and use it in GitHub Desktop.
Refactoring presenter - https://forum.upcase.com/t/is-there-a-good-way-to-clean-up-this-nested-content-tag-in-this-presenter/6422
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 TicketReportPresenter | |
def ticket_report | |
return if @model.tickets.empty? | |
safe_join([header, table]) | |
end | |
def header | |
content_tag(:h4, "Users have tickets") | |
end | |
def table | |
content_tag(:table, safe_join([table_header, table_body])) | |
end | |
def table_header | |
content_tag(:thead) do | |
safe_join([ | |
content_tag(:th, "User Email"), | |
content_tag(:th, "Reason(s)") | |
]) | |
end | |
end | |
def table_body | |
content_tag(:tbody, table_rows) | |
end | |
def table_rows | |
tickets = @model.unpayable_tickets.map { |ticket| table_row(ticket) } | |
safe_join(tickets) | |
end | |
def table_row(ticket) | |
content_tag(:tr) do | |
safe_join([ | |
content_tag(:td, ticket.email), | |
content_tag(:td, reasons_list(ticket.reasons)) | |
]) | |
end | |
end | |
def reasons_list(reasons) | |
reasons = reasons.map { |reason| reason_list_item(reason) } | |
content_tag(:ul, safe_join(reasons)) | |
end | |
def reason_list_item(reason) | |
content_tag(:li, reason) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
awesome implementation!