Formatting a long HTML document in a model method











up vote
3
down vote

favorite












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









share|improve this question
















bumped to the homepage by Community 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • 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?
    – Jonah
    Jan 20 '16 at 19:35












  • 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>
    – Thomas Taylor
    Jan 21 '16 at 3:45

















up vote
3
down vote

favorite












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









share|improve this question
















bumped to the homepage by Community 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • 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?
    – Jonah
    Jan 20 '16 at 19:35












  • 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>
    – Thomas Taylor
    Jan 21 '16 at 3:45















up vote
3
down vote

favorite









up vote
3
down vote

favorite











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









share|improve this question















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






ruby html ruby-on-rails






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 20 '16 at 6:07









200_success

127k15148412




127k15148412










asked Jan 20 '16 at 0:01









Thomas Taylor

1839




1839





bumped to the homepage by Community 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.














  • 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?
    – Jonah
    Jan 20 '16 at 19:35












  • 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>
    – Thomas Taylor
    Jan 21 '16 at 3:45




















  • 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?
    – Jonah
    Jan 20 '16 at 19:35












  • 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>
    – Thomas Taylor
    Jan 21 '16 at 3:45


















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?
– Jonah
Jan 20 '16 at 19:35






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?
– Jonah
Jan 20 '16 at 19:35














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>
– Thomas Taylor
Jan 21 '16 at 3:45






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>
– Thomas Taylor
Jan 21 '16 at 3:45












1 Answer
1






active

oldest

votes

















up vote
0
down vote













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.






share|improve this answer





















    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "196"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f117312%2fformatting-a-long-html-document-in-a-model-method%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    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.






    share|improve this answer

























      up vote
      0
      down vote













      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.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 31 at 5:26









        user1610127

        1016




        1016






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Code Review Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f117312%2fformatting-a-long-html-document-in-a-model-method%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Morgemoulin

            Scott Moir

            Souastre