Class: Fe::Page
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Fe::Page
- Defined in:
- app/models/fe/page.rb
Instance Method Summary collapse
- #all_element_ids ⇒ Object
- #all_element_ids_arr ⇒ Object
-
#all_elements ⇒ Object
Include nested elements.
- #all_questions ⇒ Object
- #complete?(answer_sheet) ⇒ Boolean
- #copy_to(question_sheet) ⇒ 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
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.
- #questions_before_position(position) ⇒ Object
- #rebuild_all_element_ids ⇒ Object
- #started?(answer_sheet) ⇒ Boolean
Instance Method Details
#all_element_ids ⇒ Object
81 82 83 84 |
# File 'app/models/fe/page.rb', line 81 def all_element_ids rebuild_all_element_ids if self[:all_element_ids].nil? self[:all_element_ids] end |
#all_element_ids_arr ⇒ Object
86 87 88 |
# File 'app/models/fe/page.rb', line 86 def all_element_ids_arr @all_element_ids_arr ||= all_element_ids.split(',').collect(&:to_i) end |
#all_elements ⇒ Object
Include nested elements
75 76 77 78 79 |
# File 'app/models/fe/page.rb', line 75 def all_elements ids = all_element_ids_arr order = ids.collect{ |id| "id=#{id} DESC" }.join(', ') ids.present? ? Element.where(id: ids).order(order) : Element.where("1 = 0") end |
#all_questions ⇒ Object
66 67 68 |
# File 'app/models/fe/page.rb', line 66 def all_questions all_elements.questions end |
#complete?(answer_sheet) ⇒ Boolean
117 118 119 120 121 122 123 124 125 |
# File 'app/models/fe/page.rb', line 117 def complete?(answer_sheet) return true if hidden?(answer_sheet) prev_el = nil all_elements.all? {|e| complete = !e.required?(answer_sheet, self, prev_el) || e.has_response?(answer_sheet) prev_el = e complete } end |
#copy_to(question_sheet) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'app/models/fe/page.rb', line 94 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 end |
#has_questions? ⇒ Boolean
returns true if there is a question element on the page, including one inside a grid
62 63 64 |
# File 'app/models/fe/page.rb', line 62 def has_questions? all_questions.any? end |
#hidden?(answer_sheet) ⇒ Boolean
108 109 110 111 112 113 114 115 |
# File 'app/models/fe/page.rb', line 108 def hidden?(answer_sheet) return false unless question_sheet.all_elements.where(conditional_type: 'Fe::Page', conditional_id: self).any? # if any of the conditional questions matches, it's visible !question_sheet.all_elements.where(conditional_type: 'Fe::Page', conditional_id: self).any?{ |e| e.conditional_match(answer_sheet) } end |
#label(locale = nil) ⇒ Object
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
57 58 59 |
# File 'app/models/fe/page.rb', line 57 def label(locale = nil) label_translations[locale] || self[:label] end |
#questions_before_position(position) ⇒ Object
70 71 72 |
# File 'app/models/fe/page.rb', line 70 def questions_before_position(position) self.elements.where(["#{Fe::PageElement.table_name}.position < ?", position]) end |
#rebuild_all_element_ids ⇒ Object
90 91 92 |
# File 'app/models/fe/page.rb', line 90 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
127 128 129 |
# File 'app/models/fe/page.rb', line 127 def started?(answer_sheet) all_questions.any? {|e| e.has_response?(answer_sheet)} end |