-
Notifications
You must be signed in to change notification settings - Fork 11
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
6064 – Refactor duplicated media creation methods #2223
Conversation
Some notes on the current state of things
I think this is the bulk of it, though for sure I'm forgetting something. |
app/models/media.rb
Outdated
if original_claim | ||
case media_type | ||
when 'UploadedImage', 'UploadedVideo', 'UploadedAudio' | ||
m = find_or_create_uploaded_file_media(original_claim, media_type, true) | ||
when 'Claim' | ||
m = find_or_create_claim_media(original_claim, nil, true) | ||
when 'Link' | ||
m = find_or_create_link_media(original_claim, team, true) | ||
when 'Blank' | ||
m = Blank.create! | ||
end | ||
else | ||
find_or_create_claim_media_from_original_claim(claim) | ||
case media_type | ||
when 'UploadedImage', 'UploadedVideo', 'UploadedAudio' | ||
m = find_or_create_uploaded_file_media(project_media.file, media_type) | ||
when 'Claim' | ||
m = find_or_create_claim_media(project_media.quote, project_media.quote_attributions) | ||
when 'Link' | ||
m = find_or_create_link_media(project_media.url, team) | ||
when 'Blank' | ||
m = Blank.create! | ||
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.
Let's try to resolve this duplication by trying to send the media information as an argument. Suggested signature:
find_or_create_media_from_content(type, content, additional_args = [])
I moved the methods to Media so it's easier to compare them side-by-side, and to make sure this works from Media. I also made a small change to the create_media method in ProjectAssociation, so its behavior is closer to create_original_claim. Since those two have differences, but are both basically creating a Media associated to the ProjectMedia. Hopefully those two will be turned into one method.
failing tests: rails test test/models/project_group_test.rb:39 rails test test/models/project_media_5_test.rb:799 rails test test/controllers/graphql_controller_7_test.rb:370 rails test test/controllers/graphql_controller_5_test.rb:200 - we make sure original_claim is not nil before trying to match - after that was fixed, project_media_5_test.rb:799 was still failing: - we werent passing team - so it failed to get the pender_key, since that expects a team - questions: - why was this working before? we expected team before as well - do we need to have team?
One big difference between how we create Media is in one case we were sending the entire project_media, but when creating from original_claim we weren't. I think since this is Media it makes more sense only to receive what we need, instead of the whole project_media. So this is a step in that direction. There is still a lot of duplication, and I think there should be better ways to access the values we want.
The method signature changed, so I updated the tests. however: should it change? is there a better way to deal with this?
create_media checked if media_type.blank? before running, however, the graphql request might send original_claim and media_type set to Blank. Which means in the case of original_claim present we do want to check and re-set media_type if needed.
The issue here, I think, is create_project_media creates a bunch of stuff by default. So it was creating an extra Link Media, which caused some confusion on when debugging a different issue. So maybe it's a good idea to be more conservative in using sample_data's methods?
They are now both doing the same thing. And I think conceptually they both create media, even if it is a media from an original claim.
To make clear this method mutates the original object.
Depending on the type of Media we try to create, it needs different data from the ProjectMedia object. Still, Media shouldn't need to know too much about ProjectMedia. So, we are deciding on what to send in ProjectMediaCreators, and only sending what is necessary: - I created a method that gets those media_arguments - Moved that and set_media_type under protected - Inside media it only gets that data and forwards it - There were a few changes to method's signatures (so tests were also updated) - Because there are a few differences between arguments needed, we are sending those as an additional_args hash
create_link is no longer in ProjectMediaCreators, only in Media. I think these two tests are already covered in media_test.rb
bd631f1
to
09625ae
Compare
Code Climate has analyzed commit 09625ae and detected 0 issues on this pull request. The test coverage on the diff in this pull request is 100.0% (100% is the threshold). This pull request will bring the total coverage in the repository to 100.0% (0.0% change). View more on Code Climate. |
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.
Thanks Manu for addressing the things we discussed before! I left a couple of comments, but they are not blockers.
app/models/media.rb
Outdated
# we set it to UploadedImage by default, should we? | ||
# def self.create_with_file(project_media, media_type = 'UploadedImage') |
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 can remove these comments now, right?
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.
Yes! But should we set media_type = 'UploadedImage'
by default?
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.
No, I don't think it makes sense to have any default.
@@ -870,7 +870,7 @@ def teardown | |||
WebMock.stub_request(:get, pender_url).with({ query: { url: url } }).to_return(body: response_body) | |||
|
|||
t = create_team | |||
pm = create_project_media team: t, set_original_claim: url | |||
pm = ProjectMedia.create(team: t, set_original_claim: url) | |||
cd = create_claim_description project_media: pm |
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 suggest you replace ProjectMedia.create
by ProjectMedia.create!
- just to make it more verbose if it fails.
To make it more verbose if it fails.
m = self.create_media | ||
self.media_id = m.id unless m.nil? | ||
self.create_media! | ||
self.media_id unless self.media_id.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.
Q: this line should return self.media_id
or assign a value to self.media_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.
The create_media
method changes the object and assigns the value, if I'm not mistaken. So I think we should be able to just return self.media_id
.
Description
The
Media
class and theProjectMediaCreators
concern, in Check API, have some duplicated logic around creating different types of media. The main differences were:Media
class handleoriginal_claim
ProjectMediaCreators
expected aProjectMedia
object,Media
class expected the claim/url/fileReferences: CV2-6064
How to test?
Because Media is such an important class, we should run the whole suite. But while working on this I mainly ran:
Checklist