-
-
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
Verify ownership of payment_source when creating WalletPaymentSource #3007
Changes from all commits
4a8fa76
efd9f5f
3835f9d
28c66c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
class Spree::WalletPaymentSource < ActiveRecord::Base | ||
belongs_to :user, class_name: Spree::UserClassHandle.new, foreign_key: 'user_id', inverse_of: :wallet_payment_sources | ||
belongs_to :payment_source, polymorphic: true, inverse_of: :wallet_payment_sources | ||
module Spree | ||
class WalletPaymentSource < Spree::Base | ||
belongs_to :user, class_name: Spree::UserClassHandle.new, foreign_key: 'user_id', inverse_of: :wallet_payment_sources | ||
belongs_to :payment_source, polymorphic: true, inverse_of: :wallet_payment_sources | ||
|
||
validates_presence_of :user | ||
validates_presence_of :payment_source | ||
validates_presence_of :user | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer |
||
validates_presence_of :payment_source | ||
validates :user_id, uniqueness: { | ||
scope: [:payment_source_type, :payment_source_id], | ||
message: :payment_source_already_exists | ||
kennyadsl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
validate :check_for_payment_source_class | ||
validate :check_for_payment_source_class | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm guessing the reasoning for this is to mimic what happens in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @skukx agree but these validations were already there, they have just been indented due to the module change. I think that if we want to change them we need to open another PR and deprecate existing methods. |
||
validate :validate_payment_source_ownership | ||
|
||
private | ||
private | ||
|
||
def check_for_payment_source_class | ||
if !payment_source.is_a?(Spree::PaymentSource) | ||
errors.add(:payment_source, :has_to_be_payment_source_class) | ||
def check_for_payment_source_class | ||
if !payment_source.is_a?(Spree::PaymentSource) | ||
errors.add(:payment_source, :has_to_be_payment_source_class) | ||
end | ||
end | ||
|
||
def validate_payment_source_ownership | ||
return unless payment_source.present? | ||
|
||
if payment_source.respond_to?(:user_id) && | ||
payment_source.user_id != user_id | ||
errors.add(:payment_source, :not_owned_by_user) | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -488,6 +488,9 @@ en: | |
attributes: | ||
payment_source: | ||
has_to_be_payment_source_class: has to be a Spree::PaymentSource | ||
not_owned_by_user: does not belong to user | ||
user_id: | ||
payment_source_already_exists: already has the Spree::PaymentSource in their wallet | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not a very human friendly error message. Could we use "already has this payment source in their wallet"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm assuming @ericsaupe took inspiration from the message above. I think we could fix both in this PR. |
||
models: | ||
spree/address: | ||
one: Address | ||
|
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.
can you please change the namespace/inheritance stuff into one or more other commits, so they are independently documented? Also, why are these change needed?
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.
It's to follow the format used in the other model files.