Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Applied Rails: Customizable Text

DZone's Guide to

Applied Rails: Customizable Text

Have you ever been given a task that at first seemed to make no sense? Read on to see how one of our MVBs handled such a situation using Occam's Razor and Ruby on Rails.

· Web Dev Zone
Free Resource

Get deep insight into Node.js applications with real-time metrics, CPU profiling, and heap snapshots with N|Solid from NodeSource. Learn more.

The other day, I was assigned the task of making a Rails application in which the user could add a set of "Terms and Conditions" in the Offer screen. These documents are typically standard, but the business guys wanted the user to be able to customize the individual clauses. It sounded oxymoronic at first, but, after remembering Scott Meyers' words, "pretty this ain’t, but sometimes a programmer’s just gotta do what a programmer’s gotta do," I set out in earnest to figure out what could be done.

From our business analyst, I got the typical standard clauses for these types of documents. To illustrate, here is an example -- "Offer shall be valid for 45 days." My first thought was that I should identify the parts that vary and take them as input data from the user.

In many cases, the thing that varied was a number. Taking the data in the HTML form for these variables and displaying them as inserts in constant text made sense. For the example I quoted above, I would take an input field of a type number with name the "validity" and insert it as follows:

 Offer shall be valid for <%= @offer.validity %> days. 

There were only a few clauses initially. But then, their number started increasing. Also, they were being specified with more variable parts than just one, and sometimes with non-numeric parts too.

Further, in one meeting I was told that the user may enter a totally new clause that is not part of the standard set. There was no question of identifying the part of the sentence that varies. The sentence itself would be new.

I considered different ways of providing the feature, but they all seemed like overkill. Finally, I narrowed it down to a text box. I provided the user with a set of standard clauses above the box, with a note that users should copy-and-paste the clauses into the text box.


This would not only allow them to change the variable parts but also add new sentences. The text in the text box was saved in a MySQL database, in a column type, "text." In order to display the clauses separately, I just had to split them onto a new line and print.


<p>
    <strong>Terms & Conditions:</strong><br>
    <% if @offer.tnc != nil %>
        <% @offer.tnc.split("\n").each do |t| %>
            <span class="bordered-span"> <%= t %> </span><br>
        <% end %>
    <% end %>
</p>


Our business analyst accepted this solution. Problem solved with a simple text box.

Node.js application metrics sent directly to any statsd-compliant system. Get N|Solid

Topics:
ruby on rails ,web dev

Published at DZone with permission of Mahboob Hussain, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}