Sha256: 4b165d3076f96caf6cdc574678e2e6eaed36bf2705bba0de2a114118f59ab934

Contents?: true

Size: 1.86 KB

Versions: 15

Compression:

Stored size: 1.86 KB

Contents

# :trigger_id     - question that controls the state of the dependent pages/questions
# :expresion      - a single expression for eval() by both Javascript and Ruby
#                   i.e. "answer == 'yes'"
# a question can have more than one answer (choose many) in which case ANY answer will do (find)
module Fe
  class Condition < ApplicationRecord
    self.table_name = self.table_name.sub('fe_', Fe.table_name_prefix)

    belongs_to :question_sheet

    belongs_to :trigger,
               class_name: "Question",
               foreign_key: "trigger_id"

    validates_presence_of :expression
    validates_length_of :expression, maximum: 255, allow_nil: true

    # evaluate triggering element against expression and return match|nil
    def evaluate?
      true
      # answers = self.trigger.response # answers loaded?
      # expression = self.expression.downcase
      # answers.find {|answer| answer = answer.downcase; eval(expression)}
    end

    # javascript to toggle pages/elements based on the "response"
    def trigger_js
      # will find the first answer (if multiple/checkboxes) where the expression evaluates to true (downcase both to be case insensitive)
      # if no match, disabled will be true, otherwise false
      js = <<-JS
    disabled = (response.find(function(answer) {
      answer = toLowerCase(answer);
      return eval("#{escape_javascript(self.expression.downcase)}");
    }) == undefined);
      JS

      if toggle_id.nil?
        # toggling a whole page (link), which will affect final page validation
      else
        # toggling an element (form element)... if page is loaded/cached (if not, the server-side will take care of it on load)
        js = js + <<-JS
      if(page_handler.isPageLoaded('page_#{self.toggle_page_id}'))
      {
        $('element_#{self.toggle_id}').disabled = disabled;
      }
        JS
      end

      js
    end

  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
fe-2.1.6.1 app/models/fe/condition.rb
fe-2.1.6 app/models/fe/condition.rb
fe-2.1.5 app/models/fe/condition.rb
fe-2.1.4 app/models/fe/condition.rb
fe-2.1.3 app/models/fe/condition.rb
fe-2.1.2 app/models/fe/condition.rb
fe-2.1.1 app/models/fe/condition.rb
fe-2.0.8 app/models/fe/condition.rb
fe-2.0.6 app/models/fe/condition.rb
fe-2.0.5 app/models/fe/condition.rb
fe-2.0.4 app/models/fe/condition.rb
fe-2.0.3 app/models/fe/condition.rb
fe-2.0.2 app/models/fe/condition.rb
fe-2.0.1 app/models/fe/condition.rb
fe-2.0.0 app/models/fe/condition.rb