-
Notifications
You must be signed in to change notification settings - Fork 8
/
cc_questions_controller.rb
executable file
·58 lines (50 loc) · 1.62 KB
/
cc_questions_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
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
class CcQuestionsController < ConstructController
include Linkable::Controller
only_set_object { %i{variables set_topic add_variables remove_variable} }
@model_class = CcQuestion
@params_list = [:id, :question_id, :question_type, :response_unit_id, :topic]
def variables
@collection ||= @object.variables
render 'variables/index'
end
def tq
topic_mapping do |format|
format.text { render 'tq.txt.erb', layout: false, content_type: 'text/plain' }
format.json { render 'tq.json.jbuilder' }
end
end
def add_variables
variable_names = params[:variable_names].is_a?(Array) ? params[:variable_names] : [params[:variable_names]]
variables = @object.instrument.variables.where(name: variable_names)
variables.to_a.compact!
begin
ActiveRecord::Base.transaction do
variables.each do |variable|
unless @object.variables.find_by_id(variable.id)
if params.has_key?(:x) && params.has_key?(:y)
@object.map.create!(variable: variable, x: params[:x].to_i, y: params[:y].to_i)
else
@object.variables << variable
end
end
end
@object.save
end
respond_to do |format|
format.json { render 'show' }
end
rescue => e
render json: {message: e.message}, status: :conflict
end
end
def remove_variable
@object.maps.where(
variable: Variable.find(params[:variable_id]),
x: params[:x],
y: params[:y]
).delete_all
respond_to do |format|
format.json { render json: true, status: :accepted }
end
end
end