Class: ReportResponseGenerator

Inherits:
Object
  • Object
show all
Defined in:
app/facades/report_response_generator.rb

Constant Summary collapse

RESPONSE_GENERATOR =
{
  pick_one: pick_one = ->(response, response_set, q, context){ context.send(:make_pick_one, response_set, q) },
  pick_any: pick_any = ->(response, response_set, q, context){ context.send(:random_anys, response, response_set, q) },
  dropdown: pick_one,
  slider:   pick_one,
  number:   ->(response, response_set, q, context){ response.integer_value = rand(100); response.save },
  string:   ->(response, response_set, q, context){ response.string_value = context.send(:random_string); response.save },
  box:      ->(response, response_set, q, context){ response.text_value = context.send(:random_string); response.save },
  date:     ->(response, response_set, q, context){ response.datetime_value = context.send(:random_date); response.save },
  datetime: ->(response, response_set, q, context){ response.datetime_value = context.send(:random_date); response.save },
  time:     ->(response, response_set, q, context){ response.datetime_value = context.send(:random_date); response.save },
  file:     ->(response, response_set, q, context){ context.send(:make_blob, response, false) },
  stars:    ->(response, response_set, q, context){ response_set.responses.create(:question_id => q.id, :integer_value => rand(5)+1, :answer_id => q.answers.first.id)},
  grid_one: pick_one,
  grid_any: pick_any,
  grid_dropdown: ->(response, response_set, q, context){ context.send(:grid_dropdown, response, response_set, q) }
}

Instance Method Summary collapse

Constructor Details

#initialize(survey) ⇒ ReportResponseGenerator

Returns a new instance of ReportResponseGenerator.



20
21
22
# File 'app/facades/report_response_generator.rb', line 20

def initialize(survey)
  @survey = survey
end

Instance Method Details

#generate_1_result_set(response_set) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/facades/report_response_generator.rb', line 24

def generate_1_result_set(response_set)
  @survey.survey_sections.each do |ss|
    ss.questions.each do |q|
      response = response_set.responses.build(:question_id => q.id, :answer_id => q.answers.first ? q.answers.first.id : nil)
      if q.repeater?
        rand(5).times.each do 
          response = response_set.responses.build(:question_id => q.id, :answer_id => q.answers.first.id)
          RESPONSE_GENERATOR[q.question_type_id].call(response, response_set, q, self)
        end
      elsif q.question_type_id != :label
        RESPONSE_GENERATOR[q.question_type_id].call(response, response_set, q, self)
      end
    end
  end
end

#grid_dropdown(response, response_set, q) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
# File 'app/facades/report_response_generator.rb', line 135

def grid_dropdown(response, response_set, q)
  q.question_group.questions.is_not_comment.each do |question|
    q.question_group.columns.each do |column|
      response = response_set.responses.build(question_id: q.id, answer_id: random_pick_with_column(question, column), column_id: column.id )
      response.save
    end
  end
  if question = q.question_group.questions.is_comment.first && question && !question.empty?
    response_set.responses.create(question_id: question.id, answer_id: question.answers.first.id, column_id: column.id, string_value: "User added comment here")
  end
end

#grid_one(response, response_set, q) ⇒ Object



122
123
124
125
126
127
# File 'app/facades/report_response_generator.rb', line 122

def grid_one(response, response_set, q)
  q.question_group.questions.each do |question|
    response = response_set.responses.build(question_id: question.id, answer_id: random_pick(question)) 
    response.save
  end
end

#make_blob(response, show_blob) ⇒ Object



95
96
97
98
# File 'app/facades/report_response_generator.rb', line 95

def make_blob(response, show_blob)
  response.save!
  response.blob.store!(File.new(Rails.public_path+'/images/regulations.jpg')) if show_blob
end

#make_pick_comment(response_set, q) ⇒ Object



74
75
76
77
78
79
# File 'app/facades/report_response_generator.rb', line 74

def make_pick_comment(response_set, q)
  comment = q.answers.is_comment
  if comment && !comment.empty?
    response_set.responses.create(question_id: q.id, answer_id: comment.first.id, string_value: "User added a comment here.")
  end
end

#make_pick_one(response_set, q) ⇒ Object



81
82
83
84
85
86
87
88
# File 'app/facades/report_response_generator.rb', line 81

def make_pick_one(response_set, q)
  if q.is_comment?
    response_set.responses.create(question_id: q.id, answer_id: q.answers.first.id, string_value: "User added a comment here.")
  else
    response_set.responses.create(question_id: q.id, answer_id: random_pick(q)) 
    make_pick_comment(response_set, q)
  end
end

#random_any(response, response_set, q) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/facades/report_response_generator.rb', line 100

def random_any(response, response_set, q)
  if !q.answers.empty?
    how_many = random_pick_count(q)
    how_many.times {
      already_checked = response_set.responses.where('question_id=?',q.id).collect(&:answer_id)
      response = response_set.responses.build(:question_id => q.id, :answer_id => random_pick(q,already_checked))
      response.save
    }
  else
    response = nil
  end
end

#random_anys(response, response_set, q) ⇒ Object



113
114
115
116
117
118
119
120
# File 'app/facades/report_response_generator.rb', line 113

def random_anys(response, response_set, q)
  if q.is_comment?
    response_set.responses.create(question_id: q.id, answer_id: q.answers.first.id, string_value: "User added a comment here.")
  else    
    random_any(response, response_set, q)
    make_pick_comment(response_set, q)
  end
end

#random_dateObject



58
59
60
# File 'app/facades/report_response_generator.rb', line 58

def random_date
  Time.now + (rand(100)-50).days
end

#random_pick(question, avoid = []) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'app/facades/report_response_generator.rb', line 62

def random_pick(question, avoid=[])
  answer = nil
  answers = question.answers.is_not_comment
  while !answer && avoid.count < answers.count
    pick = rand(answers.count)
    if !avoid.include?(answers[pick].id)
      answer=answers[pick].id
    end
  end
  return answer
end

#random_pick_count(question) ⇒ Object



90
91
92
93
# File 'app/facades/report_response_generator.rb', line 90

def random_pick_count(question)
  answers = question.answers
  return rand(answers.count)+1
end

#random_pick_with_column(question, column) ⇒ Object



129
130
131
132
133
# File 'app/facades/report_response_generator.rb', line 129

def random_pick_with_column(question, column)
  answers = Answer.where(question_id: question.id, column_id: column.id)
  pick = rand(answers.count)
  answer=answers[pick].id
end

#random_stringObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/facades/report_response_generator.rb', line 40

def random_string
  whichone = rand(5)
  case whichone
  when 0
    'An answer.'
  when 1
    'A different answer.'
  when 2
    'Any answer here.'
  when 3
    'Some response.'
  when 4
    'A random response.'
  when 5
    'A random answer.'
  end
end