Skip to content
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

Fix dup with multiple mounters or not cached #2645

Merged
merged 1 commit into from Jan 20, 2023

Conversation

BrianHawley
Copy link
Contributor

@BrianHawley BrianHawley commented Jan 4, 2023

  • Don't call each if @_mounters is unitialized or nil. It will be nil for calls to later mounters, or if not referenced or saved.
  • Don't interpolate column when it should be a local variable.
  • Don't define global functions in specs. Make them local to the block.
  • Cover more missing scenarios.
  • Fix some rubocop failures.

[Fixes #1962]

@mshibuya
Copy link
Member

mshibuya commented Jan 4, 2023

Thanks for the PR. Where's the fix for this point?

The initialize_dup is defined in the wrong context, as it refers to the overall mounters collection for the object, but is defined in a per-mounter module.

#1962 (comment)

t.column :textfile, :string
t.column :textfiles, :json
t.column :foo, :string
describe CarrierWave::ActiveRecord, :aggregate_failures do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove this :aggregate_failures ? While it can be useful for local development, I don't see any benefit for permanently enabling this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could remove it, but there's no harm in keeping it.

The benefit is that it's also useful in CI for future developers to debug future test failures. It gives better errors when things fail, and has no overhead in the success case. It really helps on multi-person teams where only one person has to know the feature exists, but everyone gets the benefit of better results.

Still, your call, so I removed it.

old_mounters.each do |column, mounter|
_mounter(:#{column}).cache(mounter.uploaders)
old_mounters&.each do |column, mounter|
_mounter(column).cache(mounter.uploaders)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weirdly enough, these two lines have the side effect of fixing the method being defined in the wrong context.

You don't want this method defined unless you have at least one mount field in the model, so it should be added by this method builder somehow. The problem is, the method gets redefined for every mount field, and these two lines apply to all fields and you only want them to run once.

I could have tried to make another module for this and reload that only gets included once, and then check whether it's there before including it again. But, that would add a module name, because you'd need a name to see if it was included before, and that inclusion check is a little tricky to do compatibly across all the Ruby versions supported.

However, I noticed that if this method is defined and called repeatedly in the same inheritance chain, @_mounters is nil already for every later call, due to it having been assigned nil in the earlier call on line 97, so old_mounters will always be nil for later calls. When old_mounters is nil, old_mounters&.each doesn't do anything. This means that the later calls are safe to run because they don't do anything significant that can't be repeated. And by changing :#{column} to column, that makes the first method called do the work for all the defined fields properly.

This is the same trick that makes the reload method above safe to repeat, as it only assigns a nil to the same variable once per field. I don't know if that was intentional, but it works.

So with this trick, we don't have to make a new context, and can just live with these methods being repeated. It works well enough, doesn't have a lot of overhead, and I made sure that the tests verify it works (the be_a(@uploader) vs be_a(@uploader1) assertions).

If you prefer, we could make another named module and use ActiveSupport::Concern. That way the module could be included every time in the prepended module defined above and ActiveSupport::Concern would handle the multiple inclusion in a compatible way.

@mshibuya FYI.

@BrianHawley
Copy link
Contributor Author

BrianHawley commented Jan 4, 2023

@mshibuya I've used this fix in my app at work (to fix after updating carrierwave yesterday) and it solves the issues I ran into: NoMethodError on nil, fortunately caught by our test suite. I verified it there before making the PR here.

private def foo=(value); raise; end
private

def foo=(value); raise; end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was just to get a fully passing rubocop run.

Copy link
Member

@mshibuya mshibuya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we were to solve the 'wrong context' issue, the change will look like this. What do you think?

Comment on lines 96 to 97
old_mounters = @_mounters if instance_variable_defined?(:@_mounters)
@_mounters = nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
old_mounters = @_mounters if instance_variable_defined?(:@_mounters)
@_mounters = nil
old_uploaders = _mounter(:"#{column}").uploaders
@_mounters[:"#{column}"] = nil

Comment on lines 99 to 101
old_mounters&.each do |column, mounter|
_mounter(column).cache(mounter.uploaders)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
old_mounters&.each do |column, mounter|
_mounter(column).cache(mounter.uploaders)
end
_mounter(:"#{column}").cache(old_uploaders)

- Don't call `each` if `@_mounters` is unitialized or nil. It will be
  nil for calls to later mounters, or if not referenced or saved.
- Don't interpolate `column` when it should be a local variable.
- Don't define global functions in specs. Make them local to the block.
- Cover more missing scenarios.
- Fix some rubocop failures.

[Fixes carrierwaveuploader#1962]
@BrianHawley
Copy link
Contributor Author

If we were to solve the 'wrong context' issue, the change will look like this. What do you think?

Sure, that will work. Updated with your changes, @mshibuya.

@mshibuya mshibuya merged commit ba986b7 into carrierwaveuploader:master Jan 20, 2023
@mshibuya
Copy link
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for ActiveRecord dup method
2 participants