-
-
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
Remove state and mandatory columns from adjustments #279
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f5f6ba3
Remove spree_adjustments.mandatory
jhawthorn d33254d
Replace adjustment.state with adjustment.finalized
jhawthorn 61a25ef
Add deprecation warnings
jhawthorn 148d154
Replace use of deprecated adjustment methods
jhawthorn a590cc6
Add non-bang versions of Adjustment#{,un}finalize!
jhawthorn 38854ad
Added extra space in finalized??
jhawthorn 9885387
Switch to using finalized? in backend
jhawthorn File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,16 +7,7 @@ module Spree | |
# | ||
# == Boolean attributes | ||
# | ||
# 1. *mandatory* | ||
# | ||
# If this flag is set to true then it means the the charge is required and | ||
# will not be removed from the order, even if the amount is zero. In other | ||
# words a record will be created even if the amount is zero. This is | ||
# useful for representing things such as shipping and tax charges where | ||
# you may want to make it explicitly clear that no charge was made for | ||
# such things. | ||
# | ||
# 2. *eligible?* | ||
# 1. *eligible?* | ||
# | ||
# This boolean attributes stores whether this adjustment is currently | ||
# eligible for its order. Only eligible adjustments count towards the | ||
|
@@ -35,21 +26,11 @@ class Adjustment < Spree::Base | |
validates :amount, numericality: true | ||
validates :promotion_code, presence: true, if: :require_promotion_code? | ||
|
||
state_machine :state, initial: :open do | ||
event :close do | ||
transition from: :open, to: :closed | ||
end | ||
|
||
event :open do | ||
transition from: :closed, to: :open | ||
end | ||
end | ||
|
||
after_create :update_adjustable_adjustment_total | ||
after_destroy :update_adjustable_adjustment_total | ||
|
||
scope :open, -> { where(state: 'open') } | ||
scope :closed, -> { where(state: 'closed') } | ||
scope :open, -> { where(finalized: false) } | ||
scope :closed, -> { where(finalized: true) } | ||
scope :cancellation, -> { where(source_type: 'Spree::UnitCancel') } | ||
scope :tax, -> { where(source_type: 'Spree::TaxRate') } | ||
scope :non_tax, -> do | ||
|
@@ -58,7 +39,6 @@ class Adjustment < Spree::Base | |
end | ||
scope :price, -> { where(adjustable_type: 'Spree::LineItem') } | ||
scope :shipping, -> { where(adjustable_type: 'Spree::Shipment') } | ||
scope :optional, -> { where(mandatory: false) } | ||
scope :eligible, -> { where(eligible: true) } | ||
scope :charge, -> { where("#{quoted_table_name}.amount >= 0") } | ||
scope :credit, -> { where("#{quoted_table_name}.amount < 0") } | ||
|
@@ -72,9 +52,55 @@ class Adjustment < Spree::Base | |
extend DisplayMoney | ||
money_methods :amount | ||
|
||
def finalize! | ||
update_attributes!(finalized: true) | ||
end | ||
|
||
def unfinalize! | ||
update_attributes!(finalized: false) | ||
end | ||
|
||
|
||
# Deprecated methods | ||
def state | ||
ActiveSupport::Deprecation.warn "Adjustment#state is deprecated. Instead use Adjustment#finalized?", caller | ||
finalized?? "closed" : "open" | ||
end | ||
|
||
def state=(new_state) | ||
ActiveSupport::Deprecation.warn "Adjustment#state= is deprecated. Instead use Adjustment#finalized=", caller | ||
case new_state | ||
when "open" | ||
self.finalized = false | ||
when "closed" | ||
self.finalized = true | ||
else | ||
raise "invaliid adjustment state #{new_state}" | ||
end | ||
end | ||
|
||
def open? | ||
ActiveSupport::Deprecation.warn "Adjustment#open? is deprecated. Instead use Adjustment#finalized?", caller | ||
!closed? | ||
end | ||
|
||
def closed? | ||
state == "closed" | ||
ActiveSupport::Deprecation.warn "Adjustment#closed? is deprecated. Instead use Adjustment#finalized?", caller | ||
finalized? | ||
end | ||
|
||
def open | ||
ActiveSupport::Deprecation.warn "Adjustment#open is deprecated. Instead use Adjustment#unfinalize!", caller | ||
unfinalize! | ||
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. is it worth having 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 think it is. |
||
end | ||
alias_method :open!, :open | ||
|
||
def close | ||
ActiveSupport::Deprecation.warn "Adjustment#close is deprecated. Instead use Adjustment#finalize!", caller | ||
finalize! | ||
end | ||
alias_method :close!, :close | ||
# End deprecated methods | ||
|
||
def currency | ||
adjustable ? adjustable.currency : Spree::Config[:currency] | ||
|
@@ -107,7 +133,7 @@ def update!(target = nil) | |
if target | ||
ActiveSupport::Deprecation.warn("Passing a target to Adjustment#update! is deprecated. The adjustment will use the correct target from it's adjustable association.", caller) | ||
end | ||
return amount if closed? | ||
return amount if finalized? | ||
|
||
# If the adjustment has no source, do not attempt to re-calculate the amount. | ||
# Chances are likely that this was a manually created adjustment in the admin backend. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
core/db/migrate/20150811210350_remove_mandatory_from_adjustments.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class RemoveMandatoryFromAdjustments < ActiveRecord::Migration | ||
def change | ||
remove_column :spree_adjustments, :mandatory, :boolean | ||
end | ||
end |
7 changes: 7 additions & 0 deletions
7
core/db/migrate/20150811211025_add_finalized_to_spree_adjustments.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class AddFinalizedToSpreeAdjustments < ActiveRecord::Migration | ||
def change | ||
add_column :spree_adjustments, :finalized, :boolean | ||
execute %q(UPDATE spree_adjustments SET finalized=('open' = state)) | ||
remove_column :spree_adjustments, :state, :string | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
not important, but did you mean to omit the space between the question marks? it jumped out at me like "finalized???" :)