-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(request): update a request #93
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -29,6 +29,15 @@ def complete? | |||||||||||||||||||||||||||||
true | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
def update? | ||||||||||||||||||||||||||||||
if record.request_distributions.exists? | ||||||||||||||||||||||||||||||
record.errors.add(:base, 'cannot update a request with associated request distributions') | ||||||||||||||||||||||||||||||
false | ||||||||||||||||||||||||||||||
else | ||||||||||||||||||||||||||||||
true | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||
Comment on lines
+32
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok pour la logique 👌 si on veut l'écrire de manière un peu plus "rails-ish" et concise, ça pourrait donner quelque chose comme ça :
Suggested change
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
def destroy? | ||||||||||||||||||||||||||||||
if record.pending? | ||||||||||||||||||||||||||||||
true | ||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -192,9 +192,7 @@ | |||||
request.update(status: :pending) | ||||||
end | ||||||
|
||||||
example 'Cancel a request' \ | ||||||
'If the request is already accepted and the current user is a requester, ' \ | ||||||
'the request will be set as `in_cancelation`' do | ||||||
example 'Cancel a request' do | ||||||
authentication :basic, "Bearer #{user_token.token}" | ||||||
|
||||||
do_request | ||||||
|
@@ -238,4 +236,58 @@ | |||||
expect { request.reload }.to raise_exception(ActiveRecord::RecordNotFound) | ||||||
end | ||||||
end | ||||||
|
||||||
put '/api/v1/requests/:id' do | ||||||
let!(:request3) { requests(:request3) } | ||||||
let(:id) { request3.id } | ||||||
|
||||||
parameter :plant_stage_id, 'ID of the requested plant_stage', with_example: true | ||||||
parameter :name, 'Name of the request', with_example: true | ||||||
parameter :plant_name, | ||||||
'Name of the requested plant (ignored if plant_stage_id given)', | ||||||
with_example: true | ||||||
parameter :plant_stage_name, | ||||||
'Name of the requested plant stage (ignored if plant_stage_id given)', | ||||||
with_example: true | ||||||
parameter :due_date, 'Due date of the request', with_example: true, type: :date | ||||||
parameter :quantity, 'Quantity of plants desired', with_example: true, type: :integer | ||||||
parameter :temperature, | ||||||
'(Optional) Temperature of cultivation desired', | ||||||
with_example: true, | ||||||
type: :integer | ||||||
parameter :photoperiod, | ||||||
'(Optional) Photoperiod of cultivation desired (in hour/day)', | ||||||
with_example: true, | ||||||
type: :integer | ||||||
|
||||||
let(:plant_stage_id) { Plant.last.plant_stages.last.id } | ||||||
let(:name) { 'My request' } | ||||||
let(:plant_name) { Plant.last.name } | ||||||
let(:plant_stage_name) { Plant.last.plant_stages.last.name } | ||||||
let(:due_date) { Date.current + 6.months } | ||||||
let(:quantity) { 150 } | ||||||
let(:temperature) { 'Froid' } | ||||||
let(:photoperiod) { 12 } | ||||||
|
||||||
let(:raw_post) { params.to_json } | ||||||
|
||||||
example 'Update a request' do | ||||||
authentication :basic, "Bearer #{user_token.token}" | ||||||
do_request | ||||||
|
||||||
expect(status).to eq(200) | ||||||
|
||||||
request.reload | ||||||
Comment on lines
+279
to
+280
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. vu qu'ici on n'utilise pas l'objet
Suggested change
|
||||||
|
||||||
response = JSON.parse(response_body) | ||||||
expect(response['plant_stage_id']).to eq(plant_stage_id) | ||||||
expect(response['plant_stage_name']).to eq(plant_stage_name) | ||||||
expect(response['plant_name']).to eq(plant_name) | ||||||
expect(response['name']).to eq(name) | ||||||
expect(response['due_date']).to eq(due_date.strftime('%F')) | ||||||
expect(response['quantity']).to eq(quantity) | ||||||
expect(response['temperature']).to eq(temperature) | ||||||
expect(response['photoperiod']).to eq(photoperiod) | ||||||
end | ||||||
end | ||||||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,33 @@ request2: | |
zone: *2 | ||
time: *3 | ||
|
||
request3: | ||
id: 3 | ||
requester_first_name: Alice | ||
requester_last_name: Johnson | ||
requester_email: [email protected] | ||
laboratory: Research Lab | ||
handler_id: 3 | ||
plant_stage_id: 5 | ||
name: Research request | ||
plant_name: Lettuce | ||
plant_stage_name: germination | ||
status: pending | ||
comment: This is a request for research purposes. | ||
due_date: 2021-06-15 | ||
quantity: 50 | ||
temperature: "Cool" | ||
photoperiod: 12 | ||
created_at: !ruby/object:ActiveSupport::TimeWithZone | ||
utc: &1 2021-01-20 14:35:08.434029000 Z | ||
zone: &2 !ruby/object:ActiveSupport::TimeZone | ||
name: Etc/UTC | ||
time: *1 | ||
updated_at: !ruby/object:ActiveSupport::TimeWithZone | ||
utc: &3 2021-01-20 14:35:08.434029000 Z | ||
zone: *2 | ||
time: *3 | ||
|
||
# == Schema Information | ||
# | ||
# Table name: requests | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
En mettant les instructions dans cet ordre, on peut s'exposer à un petit souci de cohérence.
La première méthode appelée,
plant_attributes_from_params
, regarde si un ID est passé en paramètre, et assigne les champsplant_stage_name
etplant_name
si c'est le cas.Puis, la méthode
update
assigne tous les paramètres passés par l'utilisateur ; dont les fameuxplant_stage_name
etplant_name
.Ce qui veut dire que dans l'idée, la méthode
plant_attributes_from_params
peut ici être overridée par les paramètres passés par le front, et donc créer des valeurs "illogiques" (par exemple, si passe l'id d'une rose mais je mets en dur que le nom est une tulipe ; et ça sera enregistré tel quel dans la bdd).En inversant l'ordre d'appel des méthodes, on évite cette potentielle "faille" :)
(si tu regardes bien, c'est d'ailleurs bien dans cet ordre que sont appelées les méthodes dans le
create
, ou qu'elles l'étaient dans la méthodeupdate
d'origine, qui a récemment été supprimée dans une autre PR)