-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Add amount_remaining for Spree::StoreCreditEvent #1512
Add amount_remaining for Spree::StoreCreditEvent #1512
Conversation
Thanks for taking this on Matteo. Is there anything that we could do to fill in the amount remaining on all of the existing store credits? We should have enough of a history to come up with something based upon the StoreCreditEvent. If not, I would suggest we leave it |
I've changed the migration to add About the data migration: I've manually tested the various cases and it seems to be working for me (it correctly recalculated all the remaining amounts for all the store credit events I had in the sandbox). Can't say if there are some edge cases I'm not considering... Please take a look at the comments on the migration and tell me if you think it could be right. I noticed there is a |
This commit backports work from an open [Solidus PR][1] [1]: solidusio#1512 This migration adds the `amount_remaining` column to the `spree_store_credit_events` table, and iterates over existing Store Credit Events to calculate historical data for this column. This value is also displayed in the admin, instead of the User's total remaining store credit.
I've been using this change successfully in my store for more than six months. I believe it's time to get this one merged. 👍 cc: @gmacdougall |
This commit backports work from an open [Solidus PR][1] [1]: solidusio#1512 This migration adds the `amount_remaining` column to the `spree_store_credit_events` table, and iterates over existing Store Credit Events to calculate historical data for this column. This value is also displayed in the admin, instead of the User's total remaining store credit.
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.
LGTM, @gmacdougall could you please review as well?
@mtylty would you mind to rebase this with latest |
3d8045e
to
1f92c97
Compare
|
||
credit.store_credit_events.chronological.each do |event| | ||
case event.action | ||
when Spree::StoreCredit::ALLOCATION_ACTION, |
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.
Avoid when branches without a body.
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 not sure... is this a false positive?
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.
@mtylty I think hound is right. This when
is missing its content, maybe it should just be credit_amount
? Or are you intentionally leaving it without content, just for documentation?
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.
@kennyadsl you're right I didn't notice the when
doesn't actually have code in it. Thanks!
11cce75
to
ee1ee3c
Compare
def up | ||
add_column :spree_store_credit_events, :amount_remaining, :decimal, precision: 8, scale: 2, default: nil, null: true | ||
|
||
Spree::StoreCredit.all.each do |credit| |
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 need to avoid referencing the Spree::StoreCredit
model and the constants here.
Instead we can define our own "AddAmountRemainingToStoreCreditEvents::StoreCredit" model here and use that instead.
ee1ee3c
to
994519a
Compare
994519a
to
ee8a77b
Compare
@@ -0,0 +1,38 @@ | |||
class AddAmountRemainingToStoreCreditEvents < ActiveRecord::Migration[5.0] |
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.
Missing magic comment # frozen_string_literal: true.
ee8a77b
to
41682aa
Compare
@kennyadsl this should be good to go, I made the requested changes. I think it needs to be re-reviewed because I added a couple of changes that are probably worth talking about. I talked this through with @kennyadsl and we decided to go for |
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 @mtylty
@mtylty can you rebase now please? |
This additional field will make sure that credit events will record the actual unused amount for a particular Spree::StoreCredit. This also hides the user_total_amount in the backend because it makes no sense to display the total credit a user has at her/his disposal when visiting the store credit show route in the backend.
This adds data migration capabilities to the migration that adds the amount_remaining column to Spree::StoreCreditEvent. The migration will re-calculate the amount_remaining based on previous store credit events and the initial credit amount.
41682aa
to
38a4a5f
Compare
@kennyadsl done! |
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 good to me.
My only concern is that the migration might be slow
This additional field will make sure that credit events will record the actual unused amount for a particular
Spree::StoreCredit
.This also hides the
user_total_amount
in the backend because it makes no sense to display the total credit a user has at her/his disposal when visiting the store credit show route in the backend.This should close #1473. It's a temporary fix waiting for a bigger refactor of
Spree::StoreCreditEvent
.