-
Notifications
You must be signed in to change notification settings - Fork 8
/
variables_controller.rb
executable file
·55 lines (44 loc) · 1.39 KB
/
variables_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
class VariablesController < BasicController
include Linkable::Controller
prepend_before_action :set_dataset
only_set_object { %i{set_topic add_sources remove_source} }
@model_class = Variable
@params_list = [:name, :label, :var_type, :dataset_id]
def tv
topic_mapping do |format|
format.text { render 'tv.txt.erb', layout: false, content_type: 'text/plain' }
format.json { render 'tv.json.jbuilder' }
end
end
def add_sources
head :bad_request if params[:sources].nil?
params[:sources] = JSON.parse(params[:sources])
begin
ActiveRecord::Base.transaction do
@object.add_sources(params[:sources][:id], params[:sources][:x], params[:sources][:y])
@object.reload
end
render 'variables/show'
rescue => e
render json: {message: e.message}, status: :conflict
end
end
def remove_source
head :bad_request if params[:other].nil?
params[:other] = JSON.parse(params[:other])
@object.maps.where(
source_type: params[:other][:class],
source_id: params[:other][:id],
x: params[:other][:x],
y: params[:other][:y]
).delete_all
render 'variables/show'
end
protected
def collection
@dataset.variables
end
def set_dataset
@dataset = policy_scope(Dataset).includes(variables: [:questions, :src_variables, :der_variables, :topic] ).find(params[:dataset_id])
end
end