Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
After Cart expiry reserved products are released, expired Carts are read-only
- Loading branch information
Showing
7 changed files
with
36 additions
and
3 deletions.
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
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 |
---|---|---|
@@ -1,8 +1,34 @@ | ||
class Cart < ActiveRecord::Base | ||
EXPIRE_AFTER = 300 # seconds | ||
|
||
belongs_to :user | ||
has_many :cart_items, dependent: :destroy, autosave: true | ||
|
||
validate :ensure_unexpired | ||
|
||
scope :unexpired, -> { where unexpired_condition } | ||
|
||
def self.unexpired_condition | ||
arel_table[:updated_at].gteq(EXPIRE_AFTER.seconds.ago) | ||
end | ||
|
||
def total_price | ||
cart_items.map(&:total_price).reduce(:+) | ||
end | ||
|
||
def expires_at | ||
(updated_at || DateTime.now) + EXPIRE_AFTER.seconds | ||
end | ||
|
||
def expired? | ||
DateTime.now > expires_at | ||
end | ||
|
||
private | ||
|
||
def ensure_unexpired | ||
if expired? | ||
errors.add :expires_at, "can't be in the past" | ||
end | ||
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