-
Notifications
You must be signed in to change notification settings - Fork 57
Conversation
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.
Looks pretty good overall! There's some refactoring to do; let me know if you have any questions.
@@ -93,10 +93,30 @@ def create # rubocop:disable MethodLength | |||
end | |||
|
|||
def update | |||
p = blackout_params | |||
p[:created_by] = current_user.id |
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.
We shouldn't be updating created_by
-- we want the record of whoever originally created the blackout
.active | ||
|
||
# save and exit | ||
if res.empty? && @blackout.update_attributes(p) |
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.
I'm a little hesitant to do both of these at the same time -- I think we should only attempt to update the blackout if there aren't any conflicting reservations.
This is also a great opportunity to create a service object to handle blackout updating. PR #1624 has an example of a service object for reservation creation.
Essentially, this method should only get the params and pass them to the service object, which will handle the update process and return the appropriate messages (e.g. errors or the updated object)
@@ -143,7 +143,7 @@ | |||
it { is_expected.to render_template(:new) } | |||
it 'should not save the blackout' do | |||
expect { post :create, blackout: attributes }.not_to\ | |||
change { Blackout.all.count } | |||
change { Blackout.all.count } |
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.
Whitespace error!
start_date: Time.zone.today) | ||
|
||
it_behaves_like 'does not update blackout', attributes | ||
end |
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.
These tests are pretty good, but they should probably be replaced with tests of the service object that you'll write. PR #1625 has examples of this.
else | ||
if res.any? |
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.
I don't think we need this if
block -- we want the detailed reservation info always.
@yongjie-lin is this ready for re-review? |
Yeah!
…On Wed, Jan 18, 2017 at 11:42 AM, Sydney Young ***@***.***> wrote:
@yongjie-lin <https://github.com/yongjie-lin> is this ready for re-review?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1651 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AVZNeFjQWHysngnhncJj5uVrxpvT_Yyeks5rTkDwgaJpZM4KwiQi>
.
|
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.
there's a just few small changes to be made! I know you had questions about test coverage, and I think it's fine, but I have to run right now so I will review that more thoroughly tonight.
update_p: blackout_params) | ||
result = updater.update! | ||
|
||
if result[:error] |
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.
I think that you don't need to pull out these methods here (though I realize you were following my example, apologies!)
lib/blackout_updater.rb
Outdated
@@ -0,0 +1,37 @@ | |||
class BlackoutUpdater | |||
# Service Object to update blackouts in the blackouts controller | |||
def initialize(blackout:, update_p:) |
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.
update_p
should be renamed to either params
, new_attrs
, or update_params
, take your pick!
lib/blackout_updater.rb
Outdated
conflicts.each do |conflict| | ||
message += "#{conflict.md_link}, " | ||
end | ||
return message[0, message.length - 2]\ |
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.
why is this array index necessary?
lib/blackout_updater.rb
Outdated
list_conflicts(conflicts) | ||
end | ||
|
||
def list_conflicts(conflicts) |
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.
rename to error_string
lib/blackout_updater.rb
Outdated
end | ||
return message[0, message.length - 2]\ | ||
+ '. Please update their due dates and try again.' | ||
end |
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.
I think the processing of the conflicts should be pulled out to a separate method so that this method reads: "The following reservation(s) will be unable to be returned #{conflicts_string}. Please update their due dates and try again."
before { put :update, id: FactoryGirl.create(:blackout), | ||
blackout: attributes } | ||
|
||
it { is_expected.to set_flash } |
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.
this should specify set_flash[:error]
it 'should not save the blackout' do | ||
expect(attributes).not_to be_nil | ||
expect(assigns(:blackout)).not_to be_nil | ||
expect(assigns(:blackout)[:notice]).not_to eq(attributes[:notice]) |
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.
this should be split up into separate tests / refactored to only have one expect
spec/lib/blackout_updater_spec.rb
Outdated
describe BlackoutUpdater do | ||
describe 'update!' do | ||
let!(:blackout) {instance_spy(Blackout, :update_attributes => nil)} | ||
let!(:cf) {double("conflict", :md_link => "Test")} |
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.
it's not as DRY, but it's preferred to not use let!
and just declare the variables you need inside your tests. This way, every individual test is readable and doesn't depend on things declared elsewhere.
spec/lib/blackout_updater_spec.rb
Outdated
:active).and_return([]) | ||
updater = BlackoutUpdater.new(blackout: blackout, update_p: {}) | ||
@results = updater.update! | ||
end |
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.
same with this before
block.
spec/lib/blackout_updater_spec.rb
Outdated
:active).and_return([cf]) | ||
updater = BlackoutUpdater.new(blackout: blackout, update_p: {}) | ||
@results = updater.update! | ||
end |
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.
and this before
too
I've made the requested changes, please review! Edit: just realized I forgot to implement the conditional |
before I review, I just noticed that you have a merge commit in your branch -- you should revert it and rebase your branch on top of master instead ( |
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.
almost there!
spec/lib/blackout_updater_spec.rb
Outdated
context 'with conflicting blackout' do | ||
it 'does not update blackout' do | ||
blackout = instance_spy(Blackout, update_attributes: nil) | ||
cf = double('conflict', md_link: 'Test') |
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.
this should be either an instance_spy('Reservation')
or a ReservationMock.new
, not just a plain double
spec/lib/blackout_updater_spec.rb
Outdated
end | ||
end | ||
context 'with errors' do | ||
context 'with conflicting blackout' do |
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.
this should be with a conflicting reservation, right?
spec/lib/blackout_updater_spec.rb
Outdated
results = updater.update! | ||
expect(results[:error]).to eq('The following reservation(s) will be'\ | ||
' unable to be returned: Test. Please update their due dates and '\ | ||
'try again.') |
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.
instead of testing the whole string, test if it includes the reservation's md_link
9f2161f
to
b67cf26
Compare
Ready for review! |
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.
mostly small changes, but I found one bug in the error handling
lib/blackout_updater.rb
Outdated
|
||
def error | ||
return 'Invalid blackout parameters. Please try again.' unless | ||
@blackout.validate |
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.
this doesn't work -- this is checking that the blackout is valid before you try to update it
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.
also, this wasn't caught by any tests -- there should be a controller-level test that checks the response to attempting to update a blackout with invalid attributes
lib/blackout_updater.rb
Outdated
return 'Invalid blackout parameters. Please try again.' unless | ||
@blackout.validate | ||
conflicts | ||
return unless @conflicts.any? |
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.
you can get rid of the line above and just have return unless conflicts.any?
lib/blackout_updater.rb
Outdated
|
||
def update_handler | ||
blackout.update_attributes(params) | ||
{ result: 'Blackout was successfully updated.', error: nil } |
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.
in order to check for a successful update and return error messages if necessary, this should read
if blackout.update_attributes(params)
return { success hash }
else
return { result: nil, error: blackout.errors.full_messages }
end
spec/lib/blackout_updater_spec.rb
Outdated
it 'does not update blackout' do | ||
blackout = instance_spy(Blackout, update_attributes: nil, | ||
validate: true) | ||
cf = instance_spy('conflict', md_link: 'Dummy conflict') |
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.
this should be instance_spy('Reservation', ...)
not instance_spy('conflict', ...)
spec/lib/blackout_updater_spec.rb
Outdated
it 'does not return a result' do | ||
blackout = instance_spy(Blackout, update_attributes: nil, | ||
validate: true) | ||
cf = instance_spy('conflict', md_link: 'Dummy conflict') |
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.
this should be instance_spy('Reservation', ...)
not instance_spy('conflict', ...)
0e1e994
to
b67cf26
Compare
Ready for review! |
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.
just a couple of tests to get rid of, then this is ready to squash and merge
it { is_expected.to render_template(:edit) } | ||
it 'should have attributes' do | ||
expect(attributes).not_to be_nil | ||
end |
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.
this test is unnecessary -- you're testing that the shared example wasn't given nil, which you have control over
end | ||
it 'should have a blackout to update' do | ||
expect(assigns(:blackout)).not_to be_nil | ||
end |
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.
this is also unnecessary
cbeb3fc
to
07a3be5
Compare
closes #1645 - change app/controllers/blackouts_controller.rb - add lib/blackout_updater.rb - change spec/controllers/blackouts_controller_spec.rb - add spec/lib/blackout_updater_spec.rb - add service object for updating blackouts - add checking of conflicting reservation when updating blackouts
07a3be5
to
93608aa
Compare
@yongjie-lin don't forget to add this to the changelog (branch |
No description provided.