Class: Decidim::Surveys::Seeds

Inherits:
Object
  • Object
show all
Defined in:
lib/decidim/surveys/seeds.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(participatory_space:) ⇒ Seeds

Returns a new instance of Seeds.



10
11
12
# File 'lib/decidim/surveys/seeds.rb', line 10

def initialize(participatory_space:)
  @participatory_space = participatory_space
end

Instance Attribute Details

#participatory_spaceObject (readonly)

Returns the value of attribute participatory_space.



8
9
10
# File 'lib/decidim/surveys/seeds.rb', line 8

def participatory_space
  @participatory_space
end

Instance Method Details

#callObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
72
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
# File 'lib/decidim/surveys/seeds.rb', line 14

def call
  admin_user = Decidim::User.find_by(
    organization: participatory_space.organization,
    email: "admin@example.org"
  )

  params = {
    name: Decidim::Components::Namer.new(participatory_space.organization.available_locales, :surveys).i18n_name,
    manifest_name: :surveys,
    published_at: Time.current,
    participatory_space:
  }

  component = Decidim.traceability.perform_action!(
    "publish",
    Decidim::Component,
    admin_user,
    visibility: "all"
  ) do
    Decidim::Component.create!(params)
  end

  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:
  }

  Decidim.traceability.create!(
    Decidim::Surveys::Survey,
    admin_user,
    params,
    visibility: "all"
  )

  %w(short_answer long_answer).each_with_index do |text_question_type, index|
    Decidim::Forms::Question.create!(
      questionnaire:,
      body: Decidim::Faker::Localized.paragraph,
      question_type: text_question_type,
      position: index
    )
  end

  %w(single_option multiple_option).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.answer_options.create!(body: Decidim::Faker::Localized.sentence)
    end

    question.display_conditions.create!(
      condition_question: questionnaire.questions.find_by(position: question.position - 2),
      question:,
      condition_type: :answered,
      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.answer_options.create!(body: Decidim::Faker::Localized.sentence)
      question.matrix_rows.create!(body: Decidim::Faker::Localized.sentence, position:)
    end
  end
end