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

Add validation to line_item.rb #7127

Merged
merged 1 commit into from Jun 8, 2016
Merged

Conversation

ankit1910
Copy link

  1. add order presence validation
  2. fix ensure_proper_currency

1. add order presence validation
2. fix ensure_proper_currency
@damianlegawiec
Copy link
Member

@ankit1910

validate :ensure_proper_currency, if: -> { order.present? }

Why are we checking if order exists? All line items must be associated to order, after adding presence validation it isn't required to have this check.

@ankit1910
Copy link
Author

@damianlegawiec : Thanks for your feedback.
In case when line items are not associated with order. and we call line_item.valid? on any object then it will raise an exception inside ensure_proper_currency method.

@ankit1910 ankit1910 closed this Feb 29, 2016
@ankit1910 ankit1910 reopened this Feb 29, 2016
@damianlegawiec
Copy link
Member

@ankit1910 you've added presence validation, so what's the purpose of even checking for order once again? Record won't be created if order doesn't exist.

@ankit1910
Copy link
Author

Removing this condition will make system run validation ensure_proper_currency even in case when order is nil and this method will call currency on order that is nil at this time. This will raise an exception inside ensure_proper_currency.

NoMethodError:
undefined method `currency' for nil:NilClass

./app/models/spree/line_item.rb:153:in`ensure_proper_currency'

/Users/ankit/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.5.1/lib/active_support/callbacks. rb:432:in `block in make_lambda'

/Users/ankit/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:192:in`call'

/Users/ankit/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:192:in `block in simple'

/Users/ankit/.rvm/gems/ruby-2.1.5/gems/activesupport-4.2.5.1/lib/active_support/callbacks.rb:504:in`call'

@priyank-gupta
Copy link
Contributor

@ankit1910 Agreed!!!
@damianlegawiec This seems fine.

@damianlegawiec damianlegawiec merged commit 2f230ef into spree:master Jun 8, 2016
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.

None yet

3 participants