3
\$\begingroup\$

I was asked to make a view which is a mix of HTML & ERB logic available in multiple views. At first this seemed pretty simple. However, some of these views required the output to be plaintext instead of HTML & ERB. The rational behind this was that the strings can be added to, removed and updated in one place rather than having duplicates in the different views. I proceeded to move the view logic to my model. What I ended with worked however, it just doesn't feel right - I can't articulate exactly why. Am I overlooking a simpler solution to my problem?

def agreement_type_explanation(view, state, agreement)

  heading_1 = "Important"
  heading_2 = "Warning"
  heading_2 = "Recommendation"
  heading_3 = "Hold up!"
  paragraph_1 = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
  paragraph_2 = "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
  paragraph_3 = "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt."
  paragraph_4 = "Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. "
  paragraph_5 = "Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?"
  paragraph_6 = "At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga"
  paragraph_7 = "Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus."

  if view == true
    if state == 'state_1'
      if agreement_type = 'agreement_1'
        simple_format(heading_1, {}, wrapper_tag: "h6")
        simple_format(paragraph_1)
      elsif agreement_type == 'agreement_2'
        simple_format(heading_1, {}, wrapper_tag: "h6")
        simple_format(paragraph_2)
      end
      simple_format(heading_2, {}, wrapper_tag: "h6")
      simple_format(paragraph_3)
    elsif state == 'state_2'
      if agreement_type == 'agreement_1'
        simple_format(heading_1, {}, wrapper_tag: "h6")
        simple_format(paragraph_1)
        simple_format(heading_2, {}, wrapper_tag: "h6")
        simple_format(paragraph_3)
        simple_format(paragraph_4)
      elsif agreement_type == 'agreement_2'
        simple_format(heading_1, {}, wrapper_tag: "h6")
        simple_format(paragraph_2)
        simple_format(paragraph_4)
      else
        simple_format(heading_2, {}, wrapper_tag: "h6")
        simple_format(paragraph_3)
        simple_format(paragraph_4)
      end
    elsif state == 'state_3'
      simple_format(heading_2, {}, wrapper_tag: "h6")
      simple_format(paragraph_5)
      simple_format(heading_2, {}, wrapper_tag: "h6")
      simple_format(paragraph_6)
    else
      simple_format(heading_3, {}, wrapper_tag: "h6")
      simple_format(paragraph_7)
    end
  elsif view == false
    if state == 'state_1'
      if agreement_type == 'agreement_1'
        heading_1 +
        paragraph_1
      elsif agreement_type == 'agreement_2'
        heading_1 +
        paragraph_2
      else
        heading_2 +
        paragraph_3
      end
    elsif state == 'state_2'
      if agreement_type == 'agreement_1'
        heading_1 +
        paragraph_1 +
        heading_2 +
        paragraph_4
      elsif agreement_type == 'agreement_2'
        heading_1 +
        paragraph_2 +
        paragraph_4
      else
        heading_2 +
        paragraph_3 +
        paragraph_4
      end
    elsif state == 'state_3'
      heading_2 +
      paragraph_5 +
      heading_2 +
      paragraph_6
    else
      heading_3 +
      paragraph_7
    end
  end
end
\$\endgroup\$
  • \$\begingroup\$ Could you include screenshots of examples of each output (one html, one for plaintext)? It's not strictly necessary to answer your question, but would be helpful. Also, could you include the code for simple_format? \$\endgroup\$ – Jonah Jan 20 '16 at 19:35
  • \$\begingroup\$ simple_format is a rails text helper method: apidock.com/rails/ActionView/Helpers/TextHelper/simple_format. So simple_format(heading_1, {}, wrapper_tag: "h6") would output <h6>Important</h6> and simple_format(paragraph_1) would output <p>my paragraph_1 variable</p> \$\endgroup\$ – Thomas Taylor Jan 21 '16 at 3:45
1
\$\begingroup\$

As far as I understand, the only difference between if / else branches is that the output has HTML tags in one case and is plain text in another. We can use this to simplify the statements.

Let's create 2 helper functions:

def heading(text, format: false)
  format ? simple_format(text, {}, wrapper_tag: "h6") : text
end

def paragraph(text, format: false)
  format ? simple_format(text) : text  
end

Here we return either html or text based on the value of format argument (which is the view basically).

Second step would be removing one of the branches and turning another one into a case. As a side effect we avoid typos like the one you have in agreement_type = 'agreement_1' (which almost always guarantees the developer a very fun debugging session).

After that the code can be divided into 4 blocks, one for each of the states. We extract these blocks into proper methods.

So the final code might look like that:

def heading(text, format: false)
  format ? simple_format(text, {}, wrapper_tag: "h6") : text
end

def paragraph(text, format: false)
  format ? simple_format(text) : text  
end

def agreement_type_explanation(view, state, agreement)
  # Here be headers

  case state
  when 'state_1' then explanation_for_state1(view, agreement_type)
  when 'state_2' then explanation_for_state2(view, agreement_type)
  when 'state_3' then explanation_for_state3(view, agreement_type)
  else agreement_for_unknown_state(view, agreement_type)
  end
end

def explanation_for_state1(view, agreement_type)
  case agreement_type
  when 'agreement_1'
    heading(heading_1, format: view) +
    paragraph(paragraph_1, format: view)
  when 'agreement_2'
    heading(heading_1, format: view) +
    paragraph(paragraph_2, format: view)
  else
    heading(heading_2, format: view) +
    paragraph(paragraph_3, format: view)
  end
end

def explanation_for_state2(view, agreement_type)
  case agreement_type 
  when 'agreement_1'
    heading(heading_1, format: view) +
    paragraph(paragraph_1, format: view) +
    heading(heading_2, format: view) +
    paragraph(paragraph_4, format: view)
  when 'agreement_2'
    heading(heading_1, format: view) +
    paragraph(paragraph_2, format: view) +
    paragraph_4
  else
    heading(heading_2, format: view) +
    paragraph(paragraph_3, format: view) +
    paragraph_4
  end
end

def explanation_for_state3(view, agreement_type)
  heading(heading_2, format: view) +
  paragraph(paragraph_5, format: view) +
  heading(heading_2, format: view) +
  paragraph(paragraph_6, format: view)
end

def explanation_for_unknown_state(view, agreement_type)
  heading(heading_3, format: view) + paragraph(paragraph_7, format: view)
end

I would also move all the static strings out of the models and put them in a I18n file. After that hey can be accessed via I18n.t('agreement_explanations.header1') etc.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.