Friday, 3 March 2017

Applied Rails : Customizable Text

I got a requirement in my Rails application in which the user would add a set of "Terms & Conditions" in the Offer screen. These are typically standard, but the user should be able to customize the individual clauses. It sounded oxymoronic, but 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 right earnest to figure out what can be done.

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

In many cases, the thing that varies was a number. Taking the data in the HTML form for these variables and displaying as inserts in constant text made sense. For the example I quoted above, I would take an input field of type number with name "validity" and insert 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 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 overkill. Finally, I narrowed down to a text box. I gave a set of standard clauses above the box with a note that users should copy - paste the clauses in to 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 MySQL database in a column of type "text". In order to display the clauses separately, I just had to split on newline and print.

The business analyst accepted this solution. Problem solved. With a simple text box.


Ref
[1] : Effective C++

No comments:

Post a Comment