-
Notifications
You must be signed in to change notification settings - Fork 0
/
scaffolded.notes
300 lines (222 loc) · 5.86 KB
/
scaffolded.notes
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
Scaffolded stepful:
rails new scaffolded -d postgresql
config/database.yml
username: stepful
password: stepful
bin/rails db:create
bin/rails s
bin/rails g scaffold user name type phone
bin/rails g scaffold student --parent=user
bin/rails g scaffold coach --parent=user
bin/rails g scaffold call coach_id student_id start:datetime satisfaction:integer notes:text
bin/rails db:migrate
bin/rails c
Coach.create!(name: "Alice", phone: "001-456-7890")
Student.create!(name: "Bob", phone: "002-456-7890")
### Add relations ###
class Call < ApplicationRecord
belongs_to :coach
belongs_to :student
end
class Coach < User
has_many :calls
end
class Student < User
has_many :calls
end
alice = Coach.first
bob = Student.first
Call.new(coach: alice, student: bob, start: Time.zone.now + 1.day)
# Define root path
config/routes.rb:
root "calls#index"
copy user partial to student and coach
bin/rails g controller sessions new
routes.rb:
get "login" =>"sessions#new"
resources :sessions
sessions/new.html.erb
<%= form_with(url: sessions_path, method: :post) do |f| %>
<%= f.label :id, "User id:" %>
<%= f.number_field :id %>
<%= f.submit "Log In" %>
<% end %>
sessions_controller.rb
def create
@user = User.find params[:id]
if @user
session[:user_id] = @user.id
redirect_to root_path
else
redirect_to login_path
end
end
+++ b/app/controllers/application_controller.rb
@@ -1,2 +1,5 @@
class ApplicationController < ActionController::Base
+ def current_user
+ @current_user ||= User.find session[:user_id] if session[:user_id]
+ end
- @calls = Call.all
+ @calls = current_user.calls.all
seeds.db:
alice = Coach.create!(name: "Alice", phone: "001-456-7890")
bob = Student.create!(name: "Bob", phone: "002-456-7890")
chris = Coach.create!(name: "Chris", phone: "003-456-7890")
diane = Student.create!(name: "Diane", phone: "004-456-7890")
Call.create!(coach: alice, student: bob, start: Time.zone.now + 1.day)
Call.create!(coach: chris, student: diane, start: Time.zone.now + 2.days)
Make current_user a helper:
class ApplicationController < ActionController::Base
def current_user
@current_user ||= User.find session[:user_id] if session[:user_id]
end
helper_method :current_user
redirect to login from calls (root) if not logged in:
class CallsController < ApplicationController
before_action :set_call, only: %i[ show edit update destroy ]
# GET /calls or /calls.json
def index
if current_user
@calls = current_user.calls.all
else
redirect_to login_path
end
end
Show logged in user in application.html.erb:
<body>
<% if current_user %>
<%= "Logged in as #{current_user.name}" %>
<% else %>
<%= link_to "login", login_path %>
<% end %>
<%= yield %>
</body>
destroy session action:
def destroy
session[:user_id] = nil
redirect_to login_path
end
log out link
<% if current_user %>
<%= "Logged in as #{current_user.name} | " %>
<%= link_to "log out", logout_path %>
<% else %>
<%= link_to "log in", login_path %>
<% end %>
<%= yield %>
route:
get "logout" => "sessions#destroy"
coach with no calls yet:
emily = Coach.create!(name: "Emily", phone: "004-456-7890")
Availability partial:
<%= form_with(model: call) do |form| %>
<% if call.errors.any? %>
<div style="color: red">
<h2><%= pluralize(call.errors.count, "error") %> prohibited this call from being saved:</h2>
<ul>
<% call.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div>
<%= form.label :start, style: "display: block" %>
<%= form.datetime_field :start %>
</div>
<div>
<%= form.submit %>
</div>
<% end %>
make belonging optional: belongs_to :student, optional: true
call creation:
@call = Call.new(availability_params)
@call.coach = current_user
def availability_params
params.require(:call).permit(:start)
end
calls index:
<% if current_user.type == "Coach" %>
<%= link_to "Add Availability", new_call_path %>
<% end%>
New call view
<% if current_user.type == "Coach" %>
<h1>Add Availability</h1>
<%= render "availability", call: @call %>
<br>
<div>
<%= link_to "Back to calls", calls_path %>
</div>
<% else %>
<h1>New call</h1>
<%= render "form", call: @call %>
<br>
<div>
<%= link_to "Back to calls", calls_path %>
</div>
<% end %>
coach call view views/calls/coach/new.html.erb:
<h1>Add Availability</h1>
<%= render "availability", call: @call %>
<br>
<div>
<%= link_to "Back to calls", calls_path %>
</div>
calls controller:
before_action :set_role
# GET /calls/new
def new
@call = Call.new
render "calls/#{@role}/new"
end
def set_role
@role ||= current_user && current_user.type == "Coach" ? :coach : :student
end
Make student optional on call: belongs_to :student, optional: true
belongs_to :student, optional: true
Also put index in coach subfolder
# GET /calls or /calls.json
def index
if current_user
@calls = current_user.calls.all
render "calls/#{@role}/index"
else
redirect_to login_path
end
end
copy default templates into student folder too
Calls provider and factory
controller:
def index
if current_user
@calls = @calls_provider.all
def set_role
@calls_provider = CallsProviderFactory.for(current_user)
in models:
class CallsProviderFactory
TYPES = {
"Coach" => CoachCallsProvider,
"Student" => StudentCallsProvider
}
def self.for(user)
TYPES[user.type].new(user)
end
end
class CoachCallsProvider
def initialize(user)
@user = user
end
def all
@user.calls.all
end
end
class StudentCallsProvider
def initialize(user)
@user = user
end
def all
Call.where(student: nil).or(Call.where(student: @user))
end
end
Next: validate calls don't overlap