Class: Fe::QuestionSheet
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Fe::QuestionSheet
- Defined in:
- app/models/fe/question_sheet.rb
Instance Attribute Summary collapse
-
#element_id_mappings ⇒ Object
Returns the value of attribute element_id_mappings.
-
#old_id ⇒ Object
Returns the value of attribute old_id.
Class Method Summary collapse
- .create_from_yaml(filename) ⇒ Object
-
.new_with_page ⇒ Object
create a new form with a page already attached.
Instance Method Summary collapse
- #all_elements ⇒ Object
-
#duplicate ⇒ Object
Pages get duplicated Question elements get associated non-question elements get cloned.
- #elements ⇒ Object
- #export_to_yaml ⇒ Object
- #questions ⇒ Object
Instance Attribute Details
#element_id_mappings ⇒ Object
Returns the value of attribute element_id_mappings.
7 8 9 |
# File 'app/models/fe/question_sheet.rb', line 7 def element_id_mappings @element_id_mappings end |
#old_id ⇒ Object
Returns the value of attribute old_id.
6 7 8 |
# File 'app/models/fe/question_sheet.rb', line 6 def old_id @old_id end |
Class Method Details
.create_from_yaml(filename) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'app/models/fe/question_sheet.rb', line 67 def self.create_from_yaml(filename) # NOTE: yaml will break if some classes aren't loaded before YAML::load, strange Fe::Element.distinct.where.not(kind: 'Fe::Style').pluck(:kind).each(&:constantize) sheet_data = YAML::load(File.read(filename)) sheet_data[:old_id] = sheet_data.delete("id") pages = sheet_data.delete(:pages) puts("Create import by data #{sheet_data}") question_sheet = Fe::QuestionSheet.create!(sheet_data) question_sheet.element_id_mappings = [] pages.each do |page_atts| page = Page.create_from_import(page_atts, question_sheet) question_sheet.pages << page end # set page conditional_id values to new ids based on old_id question_sheet.all_elements.each do |el| if el.conditional_type != "Fe::Page" && el.conditional_id.present? # noop end # note that conditional elements are already translated to new ids in the element import so no need to do it here if el.conditional_type == "Fe::Page" && el.conditional_id el.update(conditional_id: question_sheet.pages.detect{ |el2| el2.old_id == el.conditional_id }&.id) end end question_sheet end |
.new_with_page ⇒ Object
create a new form with a page already attached
29 30 31 32 33 |
# File 'app/models/fe/question_sheet.rb', line 29 def self.new_with_page question_sheet = self.new(label: next_label) question_sheet.pages.build(label: 'Page 1', number: 1) question_sheet end |
Instance Method Details
#all_elements ⇒ Object
43 44 45 46 |
# File 'app/models/fe/question_sheet.rb', line 43 def all_elements element_ids = pages.pluck(:all_element_ids).compact.join(',').split(',').find_all(&:present?) element_ids.present? ? Element.where(id: element_ids).order(Arel.sql(element_ids.collect{ |id| "id=#{id} DESC" }.join(', '))) : Element.where("1 = 0") end |
#duplicate ⇒ Object
Pages get duplicated Question elements get associated non-question elements get cloned
51 52 53 54 55 56 57 58 59 |
# File 'app/models/fe/question_sheet.rb', line 51 def duplicate new_sheet = QuestionSheet.new(self.attributes.merge(id: nil)) new_sheet.label = self.label + ' - COPY' new_sheet.save(validate: false) self.pages.each do |page| page.copy_to(new_sheet) end new_sheet end |
#elements ⇒ Object
39 40 41 |
# File 'app/models/fe/question_sheet.rb', line 39 def elements pages.collect(&:elements).flatten end |
#export_to_yaml ⇒ Object
61 62 63 64 65 |
# File 'app/models/fe/question_sheet.rb', line 61 def export_to_yaml atts = attributes.to_hash atts[:pages] = pages.collect(&:export_hash) atts.to_yaml end |
#questions ⇒ Object
35 36 37 |
# File 'app/models/fe/question_sheet.rb', line 35 def questions all_elements.questions end |