0
我有兩個部分有相似的輸出,但主要變化。Rails - 幹出類似的意見
部分1
<h2 class="text-center">Current Incident</h2> <% active_incidents.each do |i| %> <div class="incident-block"> <p class="incident-date"><span class="inner-date"><%= i.updated_at.strftime('%e %b, %Y') %></span></p> <span class="incident-name text-<%= i.convert_to_level %>"><%= i.name %> <% if signed_in? %><a href="/incidents/deactivate/<%= i.id %>"> <button class="btn btn-success deactivate-button btn-xs">Deactivate incident</button> </a> <% end %></span> <span class="incident-component text-<%= i.convert_to_level %>"><%= i.component %></span> <% all_events(i).each do |e| %> <hr class="event-hr"/> <p class="incident-description"><b><%= e.status %></b>- <%= e.message %></p> <p class="incident-updated-at"><%= e.updated_at.strftime('%b %e, %H:%M %Z') %></p> <% end %> <% if signed_in? %> <a href="/incidents/<%= i.id %>"> <button class="btn btn-warning btn-sm">Update Incident</button> </a> <a href="/incidents/delete/<%= i.id %>" data-confirm="Are you sure? Deleting an incident is irreversible."> <button class="btn btn-danger btn-sm">Delete Incident</button> </a> <% end %> </div> <% end %>
部分2
<% if inactive_incidents.any? %><h2 class="text-center">Past Incidents</h2><% end %> <% inactive_incidents.each do |i| %> <div class="incident-block"> <p class="incident-date"><span class="inner-date"><%= i.updated_at.strftime('%e %b, %Y') %></span></p> <span class="incident-name text-<%= i.convert_to_level %>"><%= i.name %></span><span class="incident-component text-<%= i.convert_to_level %>"><%= i.component %></span> <% all_events(i).each do |e| %> <hr class="event-hr"/> <p class="incident-description"><b><%= e.status %></b>- <%= e.message %></p> <p class="incident-updated-at"><%= e.updated_at.strftime('%b %e, %H:%M %Z') %></p> <% end %> <% if signed_in? %> <a href="/incidents/<%= i.id %>"> <button class="btn btn-warning btn-sm">Update Incident</button> </a> <a href="/incidents/delete/<%= i.id %>" data-confirm="Are you sure? Deleting an incident is irreversible."> <button class="btn btn-danger btn-sm">Delete Incident</button> </a> <% end %> </div> <% end %>
現在,這些諧音家類似的代碼,但與渲染不同的輸出顯著的變化。由於代碼可維護性現在成爲我的問題(我必須做兩次所有更改)。我如何幹掉代碼但仍保持不同的邏輯?
萬一有人需要更多的情況下,[這裏的源(https://github.com/ur0/statusify)。 –