forked from LINCnil/pia-back
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpia.rb
75 lines (66 loc) · 2.41 KB
/
pia.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class Pia < ApplicationRecord
include ActionView::Helpers::SanitizeHelper
has_many :answers, inverse_of: :pia, dependent: :destroy
has_many :comments, inverse_of: :pia, dependent: :destroy
has_many :evaluations, inverse_of: :pia, dependent: :destroy
has_many :measures, inverse_of: :pia, dependent: :destroy
has_many :attachments, inverse_of: :pia, dependent: :destroy
has_many :revisions, inverse_of: :pia, dependent: :destroy
has_many :user_pias, dependent: :destroy
belongs_to :structure, optional: true
validates :name, presence: true
after_initialize :overwrite_to_safety_values
def self.import(json_string)
json = JSON.parse(json_string)
json.each do |pia_in|
current_pia = Pia.new
current_pia.status = pia_in['status']
current_pia.name = pia_in['name']
current_pia.author_name = pia_in['author_name']
current_pia.evaluator_name = pia_in['evaluator_name']
current_pia.validator_name = pia_in['validator_name']
current_pia.dpo_status = pia_in['dpo_status']
current_pia.dpo_opinion = pia_in['dpo_opinion']
current_pia.concerned_people_opinion = pia_in['concerned_people_opinion']
current_pia.concerned_people_status = pia_in['concerned_people_status']
current_pia.rejection_reason = pia_in['rejection_reason']
current_pia.applied_adjustments = pia_in['applied_adjustments']
current_pia.created_at = pia_in['created_at']
current_pia.updated_at = pia_in['updated_at']
%w[answers evaluations comments measures].each do |assoc|
values = pia_in[assoc]
values.each do |value|
current_pia.send(assoc).build(value.except('id'))
end
end
current_pia.save
end
end
def duplicate
duplicate_self
duplicate_answers
duplicate_comments
duplicate_evaluations
duplicate_measures
@clone
end
private
def duplicate_self
@clone = dup
@clone.save
end
%w[answers evaluations comments measures].each do |association|
define_method("duplicate_#{association}") do
send(association).each do |value|
@clone.send(association) << value.dup
end
end
end
def overwrite_to_safety_values
self.name = sanitize self[:name]
self.author_name = sanitize self[:author_name]
self.evaluator_name = sanitize self[:evaluator_name]
self.validator_name = sanitize self[:validator_name]
self.category = sanitize self[:category]
end
end