jeudi 21 janvier 2021

ruby on rails fix bad quality codes

My codes is working well but i don't believe it's the right way to write it

I have products and each product have different steps so i created if statement for each product to show the correct steps

Here is my codes

<% if @case.steptations.present? %>
        <% current_step = @case.steptations.last.step.id if @case.steptations.present? %>
          <% if @case.products.where(id: [1]).present?%>
          <ul class="breadcrumb">
            <% Step.where(id: [1,3,4,11,12,13]).each do |step| %>
              <%  if step.id == current_step || step.id < current_step %>
                <li> <%= link_to step.name, steptations_path(case_id: @case.id , step_id: step.id, product_id: 1, doctor_id: @case.user.id), class:  "#{'shadow' if (step.id == current_step )}"%>
                  <% @case.steptations.where(step_id: step.id).each do |im| %>
                      <% im.step_images.where(product_id: 1).limit(1).each do |ima|%>
                        <% if ima.image1.present? %>
                          <%= link_to steptations_path(case_id: @case.id , step_id: step.id, product_id: 1, doctor_id: @case.user.id) do %><%= image_tag ima.image1.url,title: ima.text %>
                        <% end %>
                      <% end %>
                    <% end %>
                  <% end %>
                  <% else %>
                    <li><span> <%= step.name%></span>
                      <% @case.steptations.where(step_id: step.id).each do |im| %>
                          <% im.step_images.where(product_id: 1).limit(1).each do |ima|%>
                            <% if ima.image1.present? %>
                              <%= image_tag ima.image1.url,title: ima.text  %>
                          <% end %>
                        <% end %>
                      <% end %>
                  <% end %>
            <% end %></ul>
        <%end %>
        <% current_step = @case.steptations.last.step.id if @case.steptations.present? %>
        <% if @case.products.where(id: 12).present?%>
        <ul class="breadcrumb">
          <% Step.where(id: [14]).each do |step| %>
            <%  if step.id == current_step || step.id < current_step %>
                <li> <%= link_to step.name, steptations_path(case_id: @case.id , step_id: step.id, product_id: 12, doctor_id: @case.user.id ), class:  "#{'shadow' if (step.id == current_step)}"%>
                  <% @case.steptations.where(step_id: step.id).each do |im| %>
                      <% im.step_images.where(product_id: 12 ).limit(1).each do |ima|%>
                        <% if ima.image1.present? %>
                          <%= link_to steptations_path(case_id: @case.id , step_id: step.id, product_id: 12, doctor_id: @case.user.id) do %><%= image_tag ima.image1.url,title: ima.text  %>
                        <% end %>
                      <% end %>
                    <% end %>
                  <% end %>
                  <% else %>
                    <li><span><%= step.name%></span>
                      <% @case.steptations.where(step_id: step.id).each do |im| %>
                          <% im.step_images.where(product_id: 12).limit(1).each do |ima|%>
                            <% if ima.image1.present? %>
                              <%= image_tag ima.image1.url,title: ima.text  %>
                          <% end %>
                        <% end %>
                      <% end %>
                  <% end %>
            <% end %></ul>
        <%end %>
        <% current_step = @case.steptations.last.step.id if @case.steptations.present? %>
        <% if @case.products.where(id: 2).present?%>
        <ul class="breadcrumb">
          <% Step.where(id: [1,3,4,11,13]).each do |step| %>
            <%  if step.id == current_step || step.id < current_step %>
                <li> <%= link_to step.name, steptations_path(case_id: @case.id , step_id: step.id, product_id: 2, doctor_id: @case.user.id ), class:  "#{'shadow' if (step.id == current_step)}"%>
                  <% @case.steptations.where(step_id: step.id).each do |im| %>
                      <% im.step_images.where(product_id: 2 ).limit(1).each do |ima|%>
                        <% if ima.image1.present? %>
                          <%= link_to steptations_path(case_id: @case.id , step_id: step.id, product_id: 2, doctor_id: @case.user.id) do %><%= image_tag ima.image1.url,title: ima.text  %>
                        <% end %>
                      <% end %>
                    <% end %>
                  <% end %>
                  <% else %>
                    <li><span><%= step.name%></span>
                      <% @case.steptations.where(step_id: step.id).each do |im| %>
                          <% im.step_images.where(product_id: 2).limit(1).each do |ima|%>
                            <% if ima.image1.present? %>
                              <%= image_tag ima.image1.url,title: ima.text  %>
                          <% end %>
                        <% end %>
                      <% end %>
                  <% end %>
            <% end %></ul>
        <%end %>
        <% current_step = @case.steptations.last.step.id if @case.steptations.present? %>
        <% if @case.products.where(id: 3).present?%>
        <ul class="breadcrumb">
          <% Step.where(id: [1,3,4,11,13]).each do |step| %>
            <%  if step.id == current_step || step.id < current_step %>
                <li> <%= link_to step.name, steptations_path(case_id: @case.id , step_id: step.id, product_id: 3, doctor_id: @case.user.id ), class:  "#{'shadow' if (step.id == current_step )}"%>
                  <% @case.steptations.where(step_id: step.id).each do |im| %>
                      <% im.step_images.where(product_id: 3 ).limit(1).each do |ima|%>
                        <% if ima.image1.present? %>
                          <%= link_to steptations_path(case_id: @case.id , step_id: step.id, product_id: 3, doctor_id: @case.user.id) do %><%= image_tag ima.image1.url,title: ima.text  %>
                        <% end %>
                      <% end %>
                    <% end %>
                  <% end %>
                  <% else %>
                    <li><span><%= step.name%></span>
                      <% @case.steptations.where(step_id: step.id).each do |im| %>
                          <% im.step_images.where(product_id: 3).limit(1).each do |ima|%>
                            <% if ima.image1.present? %>
                              <%= image_tag ima.image1.url,title: ima.text  %>
                          <% end %>
                        <% end %>
                      <% end %>
                  <% end %>
            <% end %></ul>
        <%end %>
        <% current_step = @case.steptations.last.step.id if @case.steptations.present? %>
        <% if @case.products.where(id: 7).present?%>
        <ul class="breadcrumb">
          <% Step.where(id: [1,3,4,11,13]).each do |step| %>
            <%  if step.id == current_step || step.id < current_step %>
                <li> <%= link_to step.name, steptations_path(case_id: @case.id , step_id: step.id, product_id: 7, doctor_id: @case.user.id ), class:  "#{'shadow' if (step.id == current_step )}"%>
                  <% @case.steptations.where(step_id: step.id).each do |im| %>
                      <% im.step_images.where(product_id: 7 ).limit(1).each do |ima|%>
                        <% if ima.image1.present? %>
                          <%= link_to steptations_path(case_id: @case.id , step_id: step.id, product_id: 7, doctor_id: @case.user.id) do %><%= image_tag ima.image1.url,title: ima.text  %>
                        <% end %>
                      <% end %>
                    <% end %>
                  <% end %>
                  <% else %>
                    <li><span><%= step.name%></span>
                      <% @case.steptations.where(step_id: step.id).each do |im| %>
                          <% im.step_images.where(product_id: 7).limit(1).each do |ima|%>
                            <% if ima.image1.present? %>
                              <%= image_tag ima.image1.url ,title: ima.text %>
                          <% end %>
                        <% end %>
                      <% end %>
                  <% end %>
            <% end %></ul>
        <%end %>
        <% current_step = @case.steptations.last.step.id if @case.steptations.present? %>
          <% if @case.products.where(id: 4).present?%>
          <ul class="breadcrumb">
            <% Step.where(id: [1,2,3,4,10,11,13]).each do |step| %>
              <%  if step.id == current_step || step.id < current_step %>
                <li> <%= link_to step.name, steptations_path(case_id: @case.id , step_id: step.id, product_id: 4, doctor_id: @case.user.id ), class:  "#{'shadow' if (step.id == current_step )}"%>
                  <% @case.steptations.where(step_id: step.id).each do |im| %>
                      <% im.step_images.where(product_id: 4 ).limit(1).each do |ima|%>
                        <% if ima.image1.present? %>
                          <%= link_to steptations_path(case_id: @case.id , step_id: step.id, product_id: 4, doctor_id: @case.user.id) do %><%= image_tag ima.image1.url,title: ima.text  %>
                        <% end %>
                      <% end %>
                    <% end %>
                  <% end %>
                  <% else %>
                    <li><span><%= step.name%></span>
                      <% @case.steptations.where(step_id: step.id).each do |im| %>
                          <% im.step_images.where(product_id: 4).limit(1).each do |ima|%>
                            <% if ima.image1.present? %>
                              <%= image_tag ima.image1.url ,title: ima.text %>
                          <% end %>
                        <% end %>
                      <% end %>
                  <% end %>
            <% end %></ul>
        <%end %>
        <% current_step = @case.steptations.last.step.id if @case.steptations.present? %>
          <% if @case.products.where(id: 5).present?%>
          <ul class="breadcrumb">
            <% Step.where(id: [1,2,3,4,10,11,13]).each do |step| %>
              <%  if step.id == current_step || step.id < current_step %>
                <li> <%= link_to step.name, steptations_path(case_id: @case.id , step_id: step.id, product_id: 5, doctor_id: @case.user.id ), class:  "#{'shadow' if (step.id == current_step )}"%>
                  <% @case.steptations.where(step_id: step.id).each do |im| %>
                      <% im.step_images.where(product_id: 5 ).limit(1).each do |ima|%>
                        <% if ima.image1.present? %>
                          <%= link_to steptations_path(case_id: @case.id , step_id: step.id, product_id: 5, doctor_id: @case.user.id) do %><%= image_tag ima.image1.url ,title: ima.text %>
                        <% end %>
                      <% end %>
                    <% end %>
                  <% end %>
                  <% else %>
                    <li><span><%= step.name%></span>
                      <% @case.steptations.where(step_id: step.id).each do |im| %>
                          <% im.step_images.where(product_id: 5).limit(1).each do |ima|%>
                            <% if ima.image1.present? %>
                              <%= image_tag ima.image1.url ,title: ima.text %>
                          <% end %>
                        <% end %>
                      <% end %>
                  <% end %>
            <% end %></ul>
        <%end %>
        <% current_step = @case.steptations.last.step.id if @case.steptations.present? %>
        <% if @case.products.where(id: [6]).present?%>
        <ul class="breadcrumb">
          <% Step.where(id: [1,3,4,9,11,13]).each do |step| %>
            <%  if step.id == current_step || step.id < current_step %>
                <li> <%= link_to step.name, steptations_path(case_id: @case.id , step_id: step.id, product_id: 6, doctor_id: @case.user.id), class:  "#{'shadow' if (step.id == current_step )}"%>
                  <% @case.steptations.where(step_id: step.id).each do |im| %>
                      <% im.step_images.where(product_id: 6).limit(1).each do |ima|%>
                        <% if ima.image1.present? %>
                          <%= link_to steptations_path(case_id: @case.id , step_id: step.id, product_id: 6, doctor_id: @case.user.id) do %><%= image_tag ima.image1.url,title: ima.text  %>
                        <% end %>
                      <% end %>
                    <% end %>
                  <% end %>
                  <% else %>
                    <li><span><%= step.name%></span>
                      <% @case.steptations.where(step_id: step.id).each do |im| %>
                          <% im.step_images.where(product_id: 6).limit(1).each do |ima|%>
                            <% if ima.image1.present? %>
                              <%= image_tag ima.image1.url,title: ima.text  %>
                          <% end %>
                        <% end %>
                      <% end %>
                  <% end %>
            <% end %></ul>
        <%end %>
        <% current_step = @case.steptations.last.step.id if @case.steptations.present? %>
        <% if @case.products.where(id: [8]).present?%>
        <ul class="breadcrumb">
          <% Step.where(id: [1,5,6,13]).each do |step| %>
          <%  if step.id == current_step || step.id < current_step %>
                <li> <%= link_to step.name, steptations_path(case_id: @case.id , step_id: step.id, product_id: 8, doctor_id: @case.user.id), class:  "#{'shadow' if (step.id == current_step )}"%>
                  <% @case.steptations.where(step_id: step.id).each do |im| %>
                      <% im.step_images.where(product_id: 8).limit(1).each do |ima|%>
                        <% if ima.image1.present? %>
                          <%= link_to steptations_path(case_id: @case.id , step_id: step.id, product_id: 8, doctor_id: @case.user.id) do %><%= image_tag ima.image1.url,title: ima.text  %>
                        <% end %>
                      <% end %>
                    <% end %>
                  <% end %>
                  <% else %>
                    <li><span><%= step.name%></span>
                      <% @case.steptations.where(step_id: step.id).each do |im| %>
                          <% im.step_images.where(product_id: 8).limit(1).each do |ima|%>
                            <% if ima.image1.present? %>
                              <%= image_tag ima.image1.url,title: ima.text  %>
                          <% end %>
                        <% end %>
                      <% end %>
                  <% end %>
            <% end %></ul>
        <%end %>
        <% current_step = @case.steptations.last.step.id if @case.steptations.present? %>
        <% if @case.products.where(id: 9).present?%>
        <ul class="breadcrumb">
          <% Step.where(id: [1,7,11,13]).each do |step| %>
            <%  if step.id == current_step || step.id < current_step %>
                <li> <%= link_to step.name, steptations_path(case_id: @case.id , step_id: step.id, product_id: 9, doctor_id: @case.user.id), class:  "#{'shadow' if (step.id == current_step )}"%>
                  <% @case.steptations.where(step_id: step.id).each do |im| %>
                      <% im.step_images.where(product_id: 9).limit(1).each do |ima|%>
                        <% if ima.image1.present? %>
                          <%= link_to steptations_path(case_id: @case.id , step_id: step.id, product_id: 9, doctor_id: @case.user.id) do %><%= image_tag ima.image1.url,title: ima.text  %>
                        <% end %>
                      <% end %>
                    <% end %>
                  <% end %>
                  <% else %>
                    <li><span><%= step.name%></span>
                      <% @case.steptations.where(step_id: step.id).each do |im| %>
                          <% im.step_images.where(product_id: 9).limit(1).each do |ima|%>
                            <% if ima.image1.present? %>
                              <%= image_tag ima.image1.url ,title: ima.text %>
                          <% end %>
                        <% end %>
                      <% end %>
                  <% end %>
            <% end %></ul>
        <%end %>
                <% current_step = @case.steptations.last.step.id if @case.steptations.present? %>
        <% if @case.products.where(id: 10).present?%>
        <ul class="breadcrumb">
          <% Step.where(id: [1,7,11,13]).each do |step| %>
            <%  if step.id == current_step || step.id < current_step %>
                <li> <%= link_to step.name, steptations_path(case_id: @case.id , step_id: step.id, product_id: 10, doctor_id: @case.user.id), class:  "#{'shadow' if (step.id == current_step )}"%>
                  <% @case.steptations.where(step_id: step.id).each do |im| %>
                      <% im.step_images.where(product_id: 10).limit(1).each do |ima|%>
                        <% if ima.image1.present? %>
                          <%= link_to steptations_path(case_id: @case.id , step_id: step.id, product_id: 10, doctor_id: @case.user.id) do %><%= image_tag ima.image1.url,title: ima.text  %>
                        <% end %>
                      <% end %>
                    <% end %>
                  <% end %>
                  <% else %>
                    <li><span><%= step.name%></span>
                      <% @case.steptations.where(step_id: step.id).each do |im| %>
                          <% im.step_images.where(product_id: 10).limit(1).each do |ima|%>
                            <% if ima.image1.present? %>
                              <%= image_tag ima.image1.url,title: ima.text  %>
                          <% end %>
                        <% end %>
                      <% end %>
                  <% end %>
            <% end %></ul>
        <%end %>
        <% current_step = @case.steptations.last.step.id if @case.steptations.present? %>
        <% if @case.products.where(id: 23).present?%>
        <ul class="breadcrumb">
          <% Step.where(id: [27]).each do |step| %>
            <%  if step.id == current_step || step.id < current_step %>
                <li> <%= link_to step.name, steptations_path(case_id: @case.id , step_id: step.id, product_id: 23, doctor_id: @case.user.id), class:  "#{'shadow' if (step.id == current_step )}"%>
                  <% @case.steptations.where(step_id: step.id).each do |im| %>
                      <% im.step_images.where(product_id: 23).limit(1).each do |ima|%>
                        <% if ima.image1.present? %>
                          <%= link_to steptations_path(case_id: @case.id , step_id: step.id, product_id: 23, doctor_id: @case.user.id) do %><%= image_tag ima.image1.url,title: ima.text  %>
                        <% end %>
                      <% end %>
                    <% end %>
                  <% end %>
                  <% else %>
                    <li><span><%= step.name%></span>
                      <% @case.steptations.where(step_id: step.id).each do |im| %>
                          <% im.step_images.where(product_id: 23).limit(1).each do |ima|%>
                            <% if ima.image1.present? %>
                              <%= image_tag ima.image1.url,title: ima.text  %>
                          <% end %>
                        <% end %>
                      <% end %>
                  <% end %>
            <% end %></ul>
        <%end %>
        <% current_step = @case.steptations.last.step.id if @case.steptations.present? %>
        <% if @case.products.where(id: [11]).present?%>
        <ul class="breadcrumb">
          <% Step.where(id: [1,2,3,4,8,9,11,13]).each do |step| %>
            <%  if step.id == current_step || step.id < current_step %>
                <li> <%= link_to step.name, steptations_path(case_id: @case.id , step_id: step.id, product_id: 11, doctor_id: @case.user.id), class:  "#{'shadow' if (step.id == current_step )}"%>
                  <% @case.steptations.where(step_id: step.id).each do |im| %>
                      <% im.step_images.where(product_id: 11).limit(1).each do |ima|%>
                        <% if ima.image1.present? %>
                          <%= link_to steptations_path(case_id: @case.id , step_id: step.id, product_id: 11, doctor_id: @case.user.id),title: ima.text  do %><%= image_tag ima.image1.url,title: ima.text  %>
                        <% end %>
                      <% end %>
                    <% end %>
                  <% end %>
                  <% else %>
                    <li><span><%= step.name%></span>
                      <% @case.steptations.where(step_id: step.id).each do |im| %>
                          <% im.step_images.where(product_id: 11).limit(1).each do |ima|%>
                            <% if ima.image1.present? %>
                              <%= image_tag ima.image1.url ,title: ima.text,alt: ima.text%>
                          <% end %>
                        <% end %>
                      <% end %>
                 
            <% end %>
        <%end %>
        <% end %></ul>

Aucun commentaire:

Enregistrer un commentaire