Class SmerfForm
In: app/models/smerf_form.rb
Parent: ActiveRecord::Base

This model class manages the smerf_forms DB table and stores details about smerf forms. One of the main function it performs it to rebuild a form from a form definition file if required.

Methods

Included Modules

SmerfSystemHelpers SmerfHelpers

Public Class methods

This method retrieves a form record using the name of the form which is the same name as the form definition file. If the file is called testsmerf.yml then the code parameter should be testsmerf.

Public Instance methods

This method checks if the form is active, returns true if it is.

Override read attribute accessor for the cache attribute so that we only unserialize this field once which improves performance, it also allows us to initialize class variables which are not serialized

Alias to smerfform.cache[:smerfform]

This method processed a form definition file and stores the results in the smerf_forms DB record.

When a form definition file is seen for the first time, it will be processed and all resultant objects created during the processing of the file will be serialized to the cache field within the form record. The cache_date is updated with the current date and time.

Subsequently the form will be retrieved directly from the DB record rather than having to rebuilt from the definition file. The timestamp of the file is checked against the value stored in the cache_date field, if they are different the form definition will be reprocessed.

This method validates all user responses.

All validations are specified as part of the question (or subquestion) definition using the ‘validation:’ field. The SMERF validation system is very flexible and allows any number of validation methods to be specified for a question by comma separating each method.

 validation: validate_mandatory_question, validate_years

Currently there are two validation methods provided with the plugin:

validate_mandatory_question:This method will ensure that the user has answered the question
validate_sub_question:Only applies to subquestions and makes sure that the user has selected the answer that relates to the subquestion, it will also ensure the subquestion has been answered if the answer that relates to the subquestion has been selected.

SMERF also allows you to define your own custom validation methods. During the installation process SMERF creates a helper module called smerf_helpers.rb in the /lib directory. You can add new validation methods into this module and access them by simply referencing them in the form definition file. For example we have question ‘How many years have you worked in the industry’ and we want to ensure that the answer provided is between 0-99 we can create a new validation method called ‘validate_years’.

  # Example validation method for "How many years have you worked in the industry"
  # it uses a regex to make sure 0-99 years specified.

  def validate_years(question, responses, form)
    # Validate entry and make sure years are numeric and between 0-99
    answer = smerf_get_question_answer(question, responses)
     if (answer)
       # Expression will return nil if regex fail, also check charcters
       # after the match to determine if > 2 numbers specified
       res = ("#{answer}" =~ /\d{1,2}/)
       return "Years must be between 0 and 99" if (!res or $'.length() > 0)
     end

     return nil
   end

Note: There are some helper methods that you can use within these methods included in the smerf_system_helpers.rb module which you can find in the plugins lib directory.

Your question definition may then look like this:

   how_many_years:
     code: g2q3
     type: textfield
     sort_order: 3
     header:
     question: | How many years have you worked in the industry
     textfield_size: 10
     validation: validate_mandatory_question, validate_years
     help:

When the form is validated your custom validation method will be called. When an error is detected, a summary of the errors are displayed at the top of the form, additionally an error message is displayed for each question that has an error. This makes it very easy for the user to see which question they need to fix.

[Validate]