Class: Decidim::Surveys::Seeds
- Inherits:
-
Decidim::Seeds
- Object
- Decidim::Seeds
- Decidim::Surveys::Seeds
- Defined in:
- decidim-surveys/lib/decidim/surveys/seeds.rb
Instance Attribute Summary collapse
-
#participatory_space ⇒ Object
readonly
Returns the value of attribute participatory_space.
Instance Method Summary collapse
- #call ⇒ Object
- #create_component! ⇒ Object
- #create_questionnaire!(component:) ⇒ Object
- #create_questions!(questionnaire:) ⇒ Object
- #create_response_for_matrix_question_type(options) ⇒ Object
- #create_response_for_multiple_choice_question_type(options) ⇒ Object
- #create_response_for_sorting_question_type(options) ⇒ Object
- #create_response_for_text_question_type!(options) ⇒ Object
- #create_responses!(questionnaire:, user: nil) ⇒ Object
-
#initialize(participatory_space:) ⇒ Seeds
constructor
A new instance of Seeds.
Constructor Details
#initialize(participatory_space:) ⇒ Seeds
Returns a new instance of Seeds.
10 11 12 |
# File 'decidim-surveys/lib/decidim/surveys/seeds.rb', line 10 def initialize(participatory_space:) @participatory_space = participatory_space end |
Instance Attribute Details
#participatory_space ⇒ Object (readonly)
Returns the value of attribute participatory_space.
8 9 10 |
# File 'decidim-surveys/lib/decidim/surveys/seeds.rb', line 8 def participatory_space @participatory_space end |
Instance Method Details
#call ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'decidim-surveys/lib/decidim/surveys/seeds.rb', line 14 def call component = create_component! number_of_records.times do questionnaire = create_questionnaire!(component:) create_questions!(questionnaire:) next if questionnaire.questionnaire_for.allow_responses rand(200).times { create_responses!(questionnaire:) } end end |
#create_component! ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'decidim-surveys/lib/decidim/surveys/seeds.rb', line 27 def create_component! params = { name: Decidim::Components::Namer.new(participatory_space.organization.available_locales, :surveys).i18n_name, manifest_name: :surveys, published_at: Time.current, participatory_space: } Decidim.traceability.perform_action!( "publish", Decidim::Component, admin_user, visibility: "all" ) do Decidim::Component.create!(params) end end |
#create_questionnaire!(component:) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'decidim-surveys/lib/decidim/surveys/seeds.rb', line 45 def create_questionnaire!(component:) questionnaire = Decidim::Forms::Questionnaire.new( title: Decidim::Faker::Localized.paragraph, description: Decidim::Faker::Localized.wrapped("<p>", "</p>") do Decidim::Faker::Localized.paragraph(sentence_count: 3) end, tos: Decidim::Faker::Localized.wrapped("<p>", "</p>") do Decidim::Faker::Localized.paragraph(sentence_count: 2) end ) params = { component:, questionnaire:, allow_responses: [true, false].sample, published_at: Time.current } Decidim.traceability.create!( Decidim::Surveys::Survey, admin_user, params, visibility: "all" ) questionnaire end |
#create_questions!(questionnaire:) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'decidim-surveys/lib/decidim/surveys/seeds.rb', line 73 def create_questions!(questionnaire:) %w(short_response long_response files).each_with_index do |text_question_type, index| Decidim::Forms::Question.create!( mandatory: text_question_type == "short_response", questionnaire:, body: Decidim::Faker::Localized.paragraph, question_type: text_question_type, position: index ) next if text_question_type == "files" end %w(single_option multiple_option sorting).each_with_index do |multiple_choice_question_type, index| question = Decidim::Forms::Question.create!( questionnaire:, body: Decidim::Faker::Localized.paragraph, question_type: multiple_choice_question_type, position: index + 2 ) 3.times do question..create!(body: Decidim::Faker::Localized.sentence) end # Files type questions do not support being conditionals for another questions files_question = questionnaire.questions.where(question_type: "files") possible_condition_questions = questionnaire.questions.excluding(files_question) question.display_conditions.create!( condition_question: possible_condition_questions.sample, question:, condition_type: :responded, mandatory: true ) end %w(matrix_single matrix_multiple).each_with_index do |matrix_question_type, index| question = Decidim::Forms::Question.create!( questionnaire:, body: Decidim::Faker::Localized.paragraph, question_type: matrix_question_type, position: index ) 3.times do |position| question..create!(body: Decidim::Faker::Localized.sentence) question.matrix_rows.create!(body: Decidim::Faker::Localized.sentence, position:) end end Decidim::Forms::Question.create!( questionnaire:, body: nil, question_type: "separator", position: 3 ) end |
#create_response_for_matrix_question_type(options) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'decidim-surveys/lib/decidim/surveys/seeds.rb', line 193 def create_response_for_matrix_question_type() response = Decidim::Forms::Response.create!(**) response_option = [:question]..sample matrix_row = [:question].matrix_rows.sample body = response_option[I18n.locale] Decidim::Forms::ResponseChoice.create!( response:, response_option:, matrix_row:, body: ) end |
#create_response_for_multiple_choice_question_type(options) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 |
# File 'decidim-surveys/lib/decidim/surveys/seeds.rb', line 181 def create_response_for_multiple_choice_question_type() response = Decidim::Forms::Response.create!(**) response_option = [:question]..sample body = response_option[I18n.locale] Decidim::Forms::ResponseChoice.create!( response:, response_option:, body: ) end |
#create_response_for_sorting_question_type(options) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'decidim-surveys/lib/decidim/surveys/seeds.rb', line 163 def create_response_for_sorting_question_type() response = Decidim::Forms::Response.create!(**) = [:question]. available_positions = (0..(.size - 1)).to_a .each do |response_option| position = available_positions.sample body = response_option[I18n.locale] Decidim::Forms::ResponseChoice.create!( response:, response_option:, body:, position: ) end end |
#create_response_for_text_question_type!(options) ⇒ Object
157 158 159 160 161 |
# File 'decidim-surveys/lib/decidim/surveys/seeds.rb', line 157 def create_response_for_text_question_type!() Decidim::Forms::Response.create!( **, body: ::Faker::Lorem.paragraph(sentence_count: 1) ) end |
#create_responses!(questionnaire:, user: nil) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'decidim-surveys/lib/decidim/surveys/seeds.rb', line 131 def create_responses!(questionnaire:, user: nil) user = find_or_initialize_user_by(email: "survey-#{questionnaire.id}-#{rand(1_000_000)}@example.org") if user.nil? = { user:, questionnaire:, session_token: Digest::SHA256.hexdigest("#{user.id}-#{Rails.application.secret_key_base}"), ip_hash: Faker::Internet.device_token.slice(0, 24) } questionnaire.questions.each do |question| case question.question_type when "short_response", "long_response" create_response_for_text_question_type!(.merge({ question: })) when "single_option", "multiple_option" create_response_for_multiple_choice_question_type(.merge({ question: })) when "sorting" create_response_for_sorting_question_type(.merge({ question: })) when "matrix_single", "matrix_multiple" create_response_for_matrix_question_type(.merge({ question: })) end end rescue ActiveRecord::RecordInvalid # Silently ignore the error as we do not care if the user already exists end |