-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathform_field.rb
More file actions
127 lines (103 loc) · 2.59 KB
/
form_field.rb
File metadata and controls
127 lines (103 loc) · 2.59 KB
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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
class FormField < ApplicationRecord
# Associations
belongs_to :form, inverse_of: :form_fields
has_many :form_field_answer_options, dependent: :destroy
has_many :report_form_field_answers, dependent: :destroy
has_many :answer_options, through: :form_field_answer_options
has_many :childs, foreign_key: "parent_id", class_name: "FormField"
# Validations
validates_presence_of :question
# Enum
enum status: [:inactive, :active]
# TODO: Rails 6.1 requires enums to be symbols
# need additional refactoring in methods that call answer_type & answer_datatype to account for change to enum
enum answer_type: [
:free_form_input_one_line,
:free_form_input_paragraph,
:multiple_choice_radio,
:no_user_input,
:multiple_choice_checkbox,
:group_header
]
enum answer_datatype: [
:text_alphanumeric,
:number_integer,
:number_decimal,
:date,
]
rails_admin do
# exclude_fields :answer_options
end
accepts_nested_attributes_for :form_field_answer_options
default_scope { order(ordering: :desc) }
# Methods
def name
question
end
def multiple_choice?
answer_type ? answer_type.include?('multiple choice') : false
end
def html_id
self.question.tr(" /#,')(.","_").downcase
end
def html_input_type
case answer_type
when !self.parent_id.nil?
:child
when 'free-form input - one line'
self.parent_id.nil? ? :text : :child
when 'free-form input - paragraph'
:textarea
when 'multiple choice - checkbox'
:checkbox
when 'multiple choice - radio'
:radio
when 'no user input'
!self.childs.empty? ? :group_header : :label
else
:hidden
end
end
# This one bellow should be removed and use
# html_input_type
def input_type
case answer_type
when 'free-form input - one line'
:text_field
when 'free-form input - paragraph'
:text_area
when 'multiple choice - checkbox'
:check_box
when 'multiple choice - radio'
:radio_button
when 'no user input'
:label
else
:hidden_field
end
end
def find_answer(report)
return if report.nil?
report.report_form_field_answers.select{|fa| fa.form_field == self}.first
end
def answer report
answer = find_answer(report)
answer.response unless answer.nil?
end
def checked report, value
answer = find_answer(report)
if answer.nil?
false
else
answer.response == value
end
end
def selected report, value
answer = find_answer(report)
if answers.include? value
true
else
false
end
end
end