Class: Fe::QuestionGridWithTotal

Inherits:
QuestionGrid show all
Defined in:
app/models/fe/question_grid_with_total.rb

Instance Attribute Summary

Attributes inherited from Element

#old_id

Instance Method Summary collapse

Methods inherited from QuestionGrid

#export_hash, #has_response?, #num_cols

Methods inherited from Element

#all_elements, #conditional_answers, #conditional_match, #content, create_from_import, #css_classes, #duplicate, #export_hash, #export_to_yaml, #has_response?, #hidden?, #hidden_by_choice_field?, #hidden_by_conditional?, #label, #limit, #matches_filter, max_label_length, #page_id, #pages_on, #position, #previous_element, #ptemplate, #question?, #required?, #reuseable?, #set_conditional_element, #set_position, #tooltip, #update_any_previous_conditional_elements, #update_page_all_element_ids, #visibility_affecting_element_ids, #visibility_affecting_questions, #visible?

Instance Method Details

#totals(app) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/models/fe/question_grid_with_total.rb', line 10

def totals(app)
  totals = []
  col = 0
  row = []
  elements.each do |el|
    value = el.display_response(app) if el.respond_to?(:display_response) && el.display_response(app).present?

    if value && value.present? # keep totals nil until there actually is a value, so that we can only display a total only if at least one row had a value
      value = value.tr("^0-9.", '').to_f
      totals[col] = (totals[col].present? ? totals[col] + value : value)
    end
    col = (col + 1) % num_cols
  end
  totals
end