This only makes sense in the context of Products which only have "master" variants, and we removed that option a while back.
Remove #variants? check from ProductStock concern
The old Paperclip configuration was very clever and easy to use but it
was also a complicated implementation building on the complicated Spree
preference system.
I simplified this with Active Storage, storing simple references to blob
ids and default URLs as backup.
Common migrations look for all models with *_file_name attributes but I
found that unreliable in our code base. It finds too many model classes
and doesn't allow us to be more selective in the migration. So I used
our own migration declaration to migrate exactly those attachments
specified.
I chose `has_one_migrating` as method name for two reasons:
1. It reflects Active Storage's method `has_one_attached`.
2. And it has the same length as Paperclip's `has_attached_file`.
Therefore the commits don't need any whitespace changes.
When we change it to `has_one_attached`, we will also remove the
Paperclip options which then don't need whitespace changes either.
There's no need to create an instance of such class for every call to
one of its methods. The balance is computed each time anyway, so it'll
always be up-to-date.
This keeps the `OrderBalance` abstraction but removes the old code now
that the feature is enabled for all users in all instances and there are
no bugs reported. It's become dead code.
This will let us branch by abstraction. All existing calls to
`#outstanding_balance` will go through `OrderBalance` hence, will
check the feature toggle.
Note that by default, `OrderBalance` will end up calling
`#old_outstanding_balance`. As the name states, that's exactly what
`#outstanding_balance` was so far. This means no consumers will see any
change in behavior. We just added on item in the call stack (sort of).
This model concerns helps us put together this related methods. Although
it doesn't provide any encapsulation yet, it makes a bit easier to
consider them all next time we need to change this implementation
somehow. It's a bit of an illusion but it feels like we are making this
God object model a bit smaller.
It also gives more room for documentation that will aid future devs.
Rails 5 introduced some breaking changes to these built-in methods, and the new versions no longer work correctly in relation to decrementing stock with VariantOverrides.
In the case where the variant has not been saved yet, we can use #new_record? here instead of #stock_items.empty?, to avoid an additional query. This can be called a vast number of times per request, in various N+1s. The other case where we need to return here is when a variant has been deleted, so #stock_items will be empty and #stock_item will be nil. Likewise, we can just check that with #deleted? and avoid #stock_items.empty?