-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathprofiles_controller.rb
More file actions
183 lines (158 loc) · 5.23 KB
/
profiles_controller.rb
File metadata and controls
183 lines (158 loc) · 5.23 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
class ProfilesController < ApplicationController
include Secured
before_action :set_conference
before_action :set_current_profile, only: [:edit, :update, :destroy, :checkin, :entry_sheet, :view_qr]
skip_before_action :logged_in_using_omniauth?, only: [:new]
before_action :find_profile, only: [:destroy_id, :set_role]
before_action :is_admin?, :find_profile, only: [:destroy_id, :set_role]
before_action :set_speaker, only: [:entry_sheet]
def new
@profile = Profile.new
@conference = Conference.find_by(abbr: params[:event])
# FormItemの数だけform_valuesを初期化
FormItem.where(conference_id: @conference.id).each do |form_item|
@profile.form_values.build(form_item_id: form_item.id)
end
if current_user && current_user_model && Profile.find_by(conference_id: @conference.id, user_id: current_user_model.id)
redirect_to(dashboard_path)
end
@event = params[:event]
end
def edit
end
def create
postal_code = profile_params[:company_postal_code].gsub(/-/, '')
tel = profile_params[:company_tel].gsub(/-/, '')
@profile = Profile.new(profile_params.merge(conference_id: @conference.id, company_postal_code: postal_code, company_tel: tel))
@profile.sub = current_user_model&.sub
@profile.email = current_user[:info][:email]
if @profile.save
# フォーム項目の値を保存
if params[:form_item].present?
params[:form_item].each do |attr, value|
form_item = FormItem.find_by(attr:, conference_id: @conference.id)
if form_item
FormValue.create!(profile_id: @profile.id, form_item_id: form_item.id, value:)
end
end
end
ProfileMailer.registered(@profile, @conference).deliver_later
if @profile.public_profile.present?
redirect_to("/#{event_name}/public_profiles/#{@profile.public_profile.id}/edit")
else
redirect_to("/#{event_name}/public_profiles/new")
end
else
respond_to do |format|
format.html { render(:new) }
format.json { render(json: @profile.errors, status: :unprocessable_entity) }
end
end
end
def update
postal_code = profile_params[:company_postal_code].gsub(/-/, '')
tel = profile_params[:company_tel].gsub(/-/, '')
respond_to do |format|
if @profile.update(profile_params.merge(conference_id: @conference.id, company_postal_code: postal_code, company_tel: tel))
# フォーム項目の値を更新
if params[:form_item].present?
params[:form_item].each do |attr, value|
form_item = FormItem.find_by(attr:, conference_id: @conference.id)
if form_item
# 既存のフォーム値を探すか、新しく作成
form_value = @profile.form_values.find_or_initialize_by(form_item_id: form_item.id)
form_value.value = value
form_value.save!
end
end
end
format.html { redirect_to(dashboard_path, notice: '登録情報の変更が完了しました') }
format.json { render(:show, status: :ok, location: @profile) }
else
format.html { render(:edit, notice: '登録情報の変更時にエラーが発生しました') }
format.json { render(json: @profile.errors, status: :unprocessable_entity) }
end
end
end
def destroy
@profile.destroy
redirect_to(logout_url)
end
def destroy_id
@found_profile.destroy
respond_to do |format|
format.json { head(:no_content) }
end
end
def set_role
puts(params[:roles])
end
def calendar
respond_to do |format|
format.ics do
code = params[:code]
filename = Profile.find_by(calendar_unique_code: code).export_ics
stat = File.stat(filename)
send_file(filename, filename: "#{code}.ics", length: stat.size)
end
end
end
def profile_url
case action_name
when 'new'
"/#{params[:event]}/profiles"
when 'edit'
"/#{params[:event]}/profiles/#{params[:id]}"
end
end
def view_qr
end
def entry_sheet
end
helper_method :profile_url
private
def find_profile
@found_profile = Profile.find(parmas[:id])
unless @found_profile
format.json { render(json: 'Not found', status: :not_found) }
end
end
def set_current_profile
@profile = Profile.find_by(user_id: current_user_model.id, conference_id: set_conference.id)
end
def profile_params
params.require(:profile).permit(
:sub,
:email,
:last_name,
:first_name,
:last_name_kana,
:first_name_kana,
:industry_id,
:company_name_prefix_id,
:company_name,
:company_name_suffix_id,
:company_email,
:company_postal_code,
:company_address_level1,
:company_address_level2,
:company_address_line1,
:company_address_line2,
:company_address_prefecture_id,
:company_tel,
:department,
:position,
:participation,
:roles,
:conference_id,
:number_of_employee_id,
:annual_sales_id,
:company_fax,
:occupation_id,
form_items_attributes: form_items_params
)
end
def form_items_params
FormItem.where(conference_id: @conference.id).map { |item| item.attr.to_sym }
end
end