Class: Fe::Page

Inherits:
ApplicationRecord show all
Defined in:
app/models/fe/page.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#old_idObject

Returns the value of attribute old_id.



6
7
8
# File 'app/models/fe/page.rb', line 6

def old_id
  @old_id
end

Class Method Details

.create_from_import(page_data, question_sheet) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'app/models/fe/page.rb', line 192

def self.create_from_import(page_data, question_sheet)
  elements = page_data.delete(:elements)
  page_data.delete(:all_element_ids) # this can get build again
  page_data[:old_id] = page_data.delete('id')
  page_data[:question_sheet_id] = question_sheet.id
  puts("Import page from data #{page_data}")
  page = Fe::Page.create!(page_data)
  elements.each do |el|
    page.elements << Fe::Element.create_from_import(el, page, question_sheet)
  end
  page.rebuild_all_element_ids
  page
end

Instance Method Details

#all_element_idsObject



95
96
97
98
# File 'app/models/fe/page.rb', line 95

def all_element_ids
  rebuild_all_element_ids if self[:all_element_ids].nil?
  self[:all_element_ids]
end

#all_element_ids_arrObject



100
101
102
# File 'app/models/fe/page.rb', line 100

def all_element_ids_arr
  @all_element_ids_arr ||= all_element_ids.split(',').collect(&:to_i)
end

#all_elementsObject

Include nested elements



89
90
91
92
93
# File 'app/models/fe/page.rb', line 89

def all_elements
  ids = all_element_ids_arr
  order = ids.collect{ |id| "id=#{id} DESC" }.join(', ')
  ids.present? ? Element.where(id: ids).order(Arel.sql(order)) : Element.where("1 = 0")
end

#all_hidden_elements(answer_sheet) ⇒ Object



158
159
160
161
# File 'app/models/fe/page.rb', line 158

def all_hidden_elements(answer_sheet)
  @all_hidden_elements ||= {}
  @all_hidden_elements[answer_sheet.cache_key] ||= build_all_hidden_elements(answer_sheet)
end

#all_questionsObject



80
81
82
# File 'app/models/fe/page.rb', line 80

def all_questions
  all_elements.questions
end

#build_all_hidden_elements(answer_sheet) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
# File 'app/models/fe/page.rb', line 163

def build_all_hidden_elements(answer_sheet)
  @all_hidden_elements ||= {}
  @all_hidden_elements[answer_sheet.cache_key] = []
  all_elements.each do |e|
    next if @all_hidden_elements[answer_sheet.cache_key].include?(e)
    if e.hidden_by_choice_field?(answer_sheet) || e.hidden_by_conditional?(answer_sheet, self)
      @all_hidden_elements[answer_sheet.cache_key] += ([e] + e.all_elements)
      @all_hidden_elements[answer_sheet.cache_key].uniq!
    end
  end
  @all_hidden_elements[answer_sheet.cache_key]
end

#clear_all_hidden_elementsObject



176
177
178
# File 'app/models/fe/page.rb', line 176

def clear_all_hidden_elements
  @all_hidden_elements = nil
end

#clear_hidden_cacheObject



142
143
144
# File 'app/models/fe/page.rb', line 142

def clear_hidden_cache
  @hidden_cache = nil
end

#complete?(answer_sheet) ⇒ Boolean

Returns:

  • (Boolean)


146
147
148
149
150
151
152
# File 'app/models/fe/page.rb', line 146

def complete?(answer_sheet)
  return true if hidden?(answer_sheet)

  all_elements.all? {|e|
    e.hidden?(answer_sheet, self) || !e.required?(answer_sheet, self) || e.has_response?(answer_sheet)
  }
end

#conditionally_visible?Boolean

a page is disabled if there is a condition, and that condition evaluates to false could set multiple conditions to influence this question, in which case all must be met def active?

# find first condition that doesn't pass (nil if all pass)
self.conditions.detect { |c| !c.evaluate? }.nil?  # true if all pass

end

def question?

false

end

Returns:

  • (Boolean)


61
62
63
# File 'app/models/fe/page.rb', line 61

def conditionally_visible?
  question_sheet&.all_elements&.where(conditional_type: 'Fe::Page', conditional_id: self)&.any?
end

#copy_to(question_sheet) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/models/fe/page.rb', line 108

def copy_to(question_sheet)
  new_page = Fe::Page.new(self.attributes.merge(id: nil))
  new_page.question_sheet_id = question_sheet.id
  new_page.save(validate: false)
  self.elements.each do |element|
    if !question_sheet.archived? && element.reuseable?
      Fe::PageElement.create(element: element, page: new_page)
    else
      element.duplicate(new_page)
    end
  end
  new_page.rebuild_all_element_ids
  new_page
end

#export_hashObject



180
181
182
183
184
185
186
# File 'app/models/fe/page.rb', line 180

def export_hash
  base_attributes = self.attributes.to_hash
  base_attributes[:elements] = elements.collect(&:export_hash)
  base_attributes.delete(:id)
  base_attributes[:question_sheet_id] = :question_sheet_id
  base_attributes
end

#export_to_yamlObject



188
189
190
# File 'app/models/fe/page.rb', line 188

def export_to_yaml
  export_hash.to_yaml
end

#has_questions?Boolean

returns true if there is a question element on the page, including one inside a grid

Returns:

  • (Boolean)


76
77
78
# File 'app/models/fe/page.rb', line 76

def has_questions?
  all_questions.any?
end

#hidden?(answer_sheet) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/models/fe/page.rb', line 123

def hidden?(answer_sheet)
  return true if hidden

  @hidden_cache ||= {}
  return @hidden_cache[answer_sheet] if !@hidden_cache[answer_sheet].nil?

  unless conditionally_visible?
    @hidden_cache[answer_sheet] = false
    return false
  end

  # if any of the conditional questions matches, it's visible
  r = !question_sheet.all_elements.where(conditional_type: 'Fe::Page', conditional_id: self).any?{ |e|
    e.visible?(answer_sheet) && e.conditional_match(answer_sheet)
  }
  @hidden_cache[answer_sheet] = r
  return r
end

#label(locale = nil) ⇒ Object



71
72
73
# File 'app/models/fe/page.rb', line 71

def label(locale = nil)
  label_translations[locale] || self[:label]
end

#no_cacheObject

any page that’s conditionally visible should not use cache, there are race conditions otherwise that happen when the conditional value is set and the now visible page loaded in ajax



67
68
69
# File 'app/models/fe/page.rb', line 67

def no_cache
  conditionally_visible? || self[:no_cache]
end

#questions_before_position(position) ⇒ Object



84
85
86
# File 'app/models/fe/page.rb', line 84

def questions_before_position(position)
  self.elements.where(["#{Fe::PageElement.table_name}.position < ?", position])
end

#rebuild_all_element_idsObject



104
105
106
# File 'app/models/fe/page.rb', line 104

def rebuild_all_element_ids
  self.update_column :all_element_ids, elements.collect{ |e| [e] + e.all_elements }.flatten.collect(&:id).join(',')
end

#started?(answer_sheet) ⇒ Boolean

Returns:

  • (Boolean)


154
155
156
# File 'app/models/fe/page.rb', line 154

def started?(answer_sheet)
  all_questions.any? {|e| e.has_response?(answer_sheet)}
end