Class: Fe::Page
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Fe::Page
- Defined in:
- app/models/fe/page.rb
Instance Attribute Summary collapse
-
#old_id ⇒ Object
Returns the value of attribute old_id.
Class Method Summary collapse
Instance Method Summary collapse
- #all_element_ids ⇒ Object
- #all_element_ids_arr ⇒ Object
-
#all_elements ⇒ Object
Include nested elements.
- #all_hidden_elements(answer_sheet) ⇒ Object
- #all_questions ⇒ Object
- #build_all_hidden_elements(answer_sheet) ⇒ Object
- #clear_all_hidden_elements ⇒ Object
- #clear_hidden_cache ⇒ Object
- #complete?(answer_sheet) ⇒ Boolean
-
#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.
- #copy_to(question_sheet) ⇒ Object
- #export_hash ⇒ Object
- #export_to_yaml ⇒ Object
-
#has_questions? ⇒ Boolean
returns true if there is a question element on the page, including one inside a grid.
- #hidden?(answer_sheet) ⇒ Boolean
- #label(locale = nil) ⇒ Object
-
#no_cache ⇒ Object
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.
- #questions_before_position(position) ⇒ Object
- #rebuild_all_element_ids ⇒ Object
- #started?(answer_sheet) ⇒ Boolean
Instance Attribute Details
#old_id ⇒ Object
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
196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'app/models/fe/page.rb', line 196 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_ids ⇒ Object
99 100 101 102 |
# File 'app/models/fe/page.rb', line 99 def all_element_ids rebuild_all_element_ids if self[:all_element_ids].nil? self[:all_element_ids] end |
#all_element_ids_arr ⇒ Object
104 105 106 |
# File 'app/models/fe/page.rb', line 104 def all_element_ids_arr @all_element_ids_arr ||= all_element_ids.split(',').collect(&:to_i) end |
#all_elements ⇒ Object
Include nested elements
93 94 95 96 97 |
# File 'app/models/fe/page.rb', line 93 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(id: []) end |
#all_hidden_elements(answer_sheet) ⇒ Object
162 163 164 165 |
# File 'app/models/fe/page.rb', line 162 def all_hidden_elements(answer_sheet) @all_hidden_elements ||= {} @all_hidden_elements[answer_sheet.cache_key] ||= build_all_hidden_elements(answer_sheet) end |
#all_questions ⇒ Object
84 85 86 |
# File 'app/models/fe/page.rb', line 84 def all_questions all_elements.questions end |
#build_all_hidden_elements(answer_sheet) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'app/models/fe/page.rb', line 167 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_elements ⇒ Object
180 181 182 |
# File 'app/models/fe/page.rb', line 180 def clear_all_hidden_elements @all_hidden_elements = nil end |
#clear_hidden_cache ⇒ Object
146 147 148 |
# File 'app/models/fe/page.rb', line 146 def clear_hidden_cache @hidden_cache = nil end |
#complete?(answer_sheet) ⇒ Boolean
150 151 152 153 154 155 156 |
# File 'app/models/fe/page.rb', line 150 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
65 66 67 |
# File 'app/models/fe/page.rb', line 65 def conditionally_visible? question_sheet&.all_elements&.where(conditional_type: 'Fe::Page', conditional_id: self)&.any? end |
#copy_to(question_sheet) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'app/models/fe/page.rb', line 112 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_hash ⇒ Object
184 185 186 187 188 189 190 |
# File 'app/models/fe/page.rb', line 184 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_yaml ⇒ Object
192 193 194 |
# File 'app/models/fe/page.rb', line 192 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
80 81 82 |
# File 'app/models/fe/page.rb', line 80 def has_questions? all_questions.any? end |
#hidden?(answer_sheet) ⇒ Boolean
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'app/models/fe/page.rb', line 127 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
75 76 77 |
# File 'app/models/fe/page.rb', line 75 def label(locale = nil) label_translations[locale] || self[:label] end |
#no_cache ⇒ Object
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
71 72 73 |
# File 'app/models/fe/page.rb', line 71 def no_cache conditionally_visible? || self[:no_cache] end |
#questions_before_position(position) ⇒ Object
88 89 90 |
# File 'app/models/fe/page.rb', line 88 def questions_before_position(position) self.elements.where(["#{Fe::PageElement.table_name}.position < ?", position]) end |
#rebuild_all_element_ids ⇒ Object
108 109 110 |
# File 'app/models/fe/page.rb', line 108 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
158 159 160 |
# File 'app/models/fe/page.rb', line 158 def started?(answer_sheet) all_questions.any? {|e| e.has_response?(answer_sheet)} end |