AO3-3176 Prevent 500 error when editing chapter you didn't coauthor #2627

Merged
merged 25 commits into from May 10, 2017

Conversation

Projects
None yet
4 participants
Owner

sarken commented Nov 23, 2016

Issue

https://otwarchive.atlassian.net/browse/AO3-3176

Purpose

Prevents a 500 error that happens when trying to edit a chapter you didn't co-author on a work you did co-author.

The problem here is you have to include a pseud when posting or editing a chapter. If you're posting a new chapter or editing a chapter you're already a co-author of, one or more pseuds are selected for you and everything works smoothly. However, when you're editing a chapter you're not already a co-author of, none of your pseuds are selected. (If you had multiple pseuds and knew the cause of the error, this wasn't a huge problem: there was a visible field to select the pseuds you wanted to add to the chapter. Once you chose one or more pseuds, everything worked great! But if you only had a default pseud, the field was hidden.)

So what we're doing here is saying, "Okay, if we're editing a chapter this person's not already a co-author on, select and add all the pseuds they have used on this work so far."

References

Starts with #2515 to avoid merge conflicts and take advantage of test changes. 2666aca is the commit with the relevant changes.

This should be merged with the other two chapter co-authoring pull requests, #2515 and #2518.

sarken added some commits Aug 21, 2016

@sarken sarken AO3-3752 Let user posting a chapter set the co-creators 2a8f28d
@sarken sarken AO3-3752 Don't add co-creators by default edf5618
@sarken sarken AO3-3176 Have to use type rather than @chapter to avoid affecting wor…
…k form
e7ad9f5
@sarken sarken AO3-3752 Only show co-authors when there are co-authors 4d0e7ee
@sarken sarken AO3-3752 Tests for co-authored chapters, co-author test step changes
78d281a
@sarken sarken AO3-3752 Make the step for posting (draft) chapters work again 86418b4
@sarken sarken AO3-3752 Fix spacing in chapter edit tests 5a5f333
@sarken sarken AO3-3176 Add test showing you can now edit a chapter you are not a co…
…-author of
a227f6c
@sarken sarken AO3-3176 Add steps to prove we are editing the right chapter
c84542d
@sarken sarken AO3-3752 Merge with master 9ea3419
@sarken sarken AO3-3752 Remove test for unrelated issue as this no longer fixes it 5d7819a
@sarken sarken AO3-3752 Use byline format for cocreator checkbox labels ae760fb
@sarken sarken AO3-3752 Use double quotes in the view
d96a7c1
@sarken sarken Merge branch 'AO3-3752' into AO3-3176 54b37dc
app/helpers/pseuds_helper.rb
+ if item_type == "chapter" && @chapter.posted?
+ to_select = @to_select.select { |pseud| pseud.user.id == current_user.id }
+ pseuds = to_select.empty? ? @work.pseuds : to_select
+ pseuds.collect { |pseud| pseud.id.to_i }
@houndci-bot

houndci-bot Nov 23, 2016

Prefer map over collect.

@@ -1,3 +1,3 @@
module ChaptersHelper
-
+
@houndci-bot

houndci-bot Nov 23, 2016

Extra empty line detected at module body beginning.

- @selected_pseuds = to_select.collect {|pseud| pseud.id.to_i }
+ @coauthors = @allpseuds.select{ |p| p.user.id != current_user.id }
+ @to_select = @chapter.authors.blank? ? @chapter.pseuds.blank? ? @work.pseuds : @chapter.pseuds : @chapter.authors
+ @selected_pseuds = @to_select.collect { |pseud| pseud.id.to_i }
@houndci-bot

houndci-bot Nov 23, 2016

Prefer map over collect.

- to_select = @chapter.authors.blank? ? @chapter.pseuds.blank? ? @work.pseuds : @chapter.pseuds : @chapter.authors
- @selected_pseuds = to_select.collect {|pseud| pseud.id.to_i }
+ @coauthors = @allpseuds.select{ |p| p.user.id != current_user.id }
+ @to_select = @chapter.authors.blank? ? @chapter.pseuds.blank? ? @work.pseuds : @chapter.pseuds : @chapter.authors
@houndci-bot

houndci-bot Nov 23, 2016

Ternary operators must not be nested. Prefer if or else constructs instead.

@zz9pzza

zz9pzza Nov 23, 2016

Contributor

I agree with the hound here.

@sarken

sarken Nov 23, 2016

Owner

Yeah, so do I -- it took me hours to figure out what it was doing. However, I'm not too keen on refactoring it here, partially because I'd have to update two or three other pull requests, and partially because the corresponding code in the works controller should also be updated if we change this, and that's getting beyond the scope of this issue.

- @coauthors = @allpseuds.select{ |p| p.user.id != current_user.id}
- to_select = @chapter.authors.blank? ? @chapter.pseuds.blank? ? @work.pseuds : @chapter.pseuds : @chapter.authors
- @selected_pseuds = to_select.collect {|pseud| pseud.id.to_i }
+ @coauthors = @allpseuds.select{ |p| p.user.id != current_user.id }
@houndci-bot

houndci-bot Nov 23, 2016

Space missing to the left of {.

sarken changed the title from AO3-3176 Let co-authors edit all chapters to AO3-3176 Prevent 500 error when editing chapter you didn't coauthor Nov 23, 2016

Contributor

zz9pzza commented Apr 17, 2017

This has gone grey btw.

Owner

sarken commented Apr 17, 2017

Yeah, this goes with #2515 and #2518 -- they're all grey because they're all building off each other. I'll update them all when we're thinking about merging them; it's just too much to keep updating them every time they go grey.

sarken added some commits May 2, 2017

@sarken sarken Merge branch 'master' into AO3-3752
93a7b5b
@sarken sarken AO3-3752 Use [author_attributes][ids] rather than [coauthors] because…
… strong parameters

We originally stuffed the coauthors params into the ids params in the controller, but we can't do that anymore with strong parameters.  We can possibly eliminate the stuffing step and use ids to begin with.
f989f6e
@sarken sarken AO3-3752 Stop concatenating author attributes
We can't concatenate params[:chapter][:author_attributes][:ids] and params[:chapter][:author_attributes] because strong parameters aren't mutable. This code was no longer working, which was a good thing because it was originally responsible for every existing co-author being added to new chapters of a work, regardless of whether you wanted that.
200dea4
@sarken sarken AO3-3752 Fix mistake from merging with master
a6056b8
@sarken sarken AO3-35752 More tests relating to chapter coauthoring
656e82c
@sarken sarken AO3-3752 Remove coauthor field from hidden fields and don't allow the…
… parameter
a2368fc
@sarken sarken AO3-3752 ACTUALLY remove coauthors for chapters allowed parameters
400fecb
@sarken sarken AO3-3176 Let any work co-author edit any chapter of the work
AO3-3176 Add editing user as a co-author to the chapter they are editing

AO3-3176 Don't override selected_pseuds in the view

AO3-3176 Consolidate the logic in the pseuds helper

AO3-3176 Better name for helper method
a7cb061
@sarken sarken Merge branch 'AO3-3752' into AO3-3176
f995e21
@bingeling

Only nitpicking

- @selected_pseuds = to_select.collect {|pseud| pseud.id.to_i }
+ @coauthors = @allpseuds.select{ |p| p.user.id != current_user.id }
+ @to_select = @chapter.authors.blank? ? @chapter.pseuds.blank? ? @work.pseuds : @chapter.pseuds : @chapter.authors
+ @selected_pseuds = @to_select.collect { |pseud| pseud.id.to_i }
@bingeling

bingeling May 8, 2017

Contributor

I think we can forego the conversion to integer here

app/helpers/pseuds_helper.rb
+ # Controls which of current_user's pseuds is selected when posting or editing
+ # an item. It used to be handled with just @selected_pseuds from the item
+ # type's controller (chapter, series, work), but needs a special case when
+ # current_user is editing a chapter they didn't co-author. When that happens, # we want to select all pseuds current_user has listed on the work itself.
@bingeling

bingeling May 8, 2017

Contributor

I think a line-break gone missing in this comment block

@sarken

sarken May 8, 2017

Owner

I'll probably need to update this once we merge #2515 -- will fix this then!

app/helpers/pseuds_helper.rb
+ if item_type == "chapter" && @chapter.posted?
+ to_select = @to_select.select { |pseud| pseud.user.id == current_user.id }
+ pseuds = to_select.empty? ? @work.pseuds : to_select
+ pseuds.collect { |pseud| pseud.id.to_i }
@bingeling

bingeling May 8, 2017

Contributor

Another redundant conversion to integer

app/views/pseuds/_byline.html.erb
- options_from_collection_for_select(@pseuds, :id, :name, @selected_pseuds), :multiple => true %>
+ options_from_collection_for_select(@pseuds, :id, :name,
+ selected_pseuds(type)),
+ multiple: true %>
@bingeling

bingeling May 8, 2017

Contributor

indentation missing there

sarken added some commits May 9, 2017

@sarken sarken AO3-3176 Merge conflicts b1f40a3
@sarken sarken AO3-3176 Don't add more unnecssary interger conversion for pseud.id, …
…and fix a line break in a code comment
5f6c60d
+ if item_type == "chapter" && @chapter.posted?
+ to_select = @to_select.select { |pseud| pseud.user.id == current_user.id }
+ pseuds = to_select.empty? ? @work.pseuds : to_select
+ pseuds.collect { |pseud| pseud.id }
@houndci-bot

houndci-bot May 9, 2017

Prefer map over collect.
Pass &:id as an argument to collect instead of a block.

@bingeling

👍 from me

@bingeling bingeling merged commit adf5f9d into otwcode:master May 10, 2017

2 checks passed

Scrutinizer 1 updated code elements
Details
hound 6 violations found.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment