TheCart is an implementation of a cart that utilizes Redis’s awesomeness.
Add this line to your application’s Gemfile:
And then execute:
Or install it yourself as:
$ gem install the_cart
create an initializer in
TheCart.configure do |c| #... end
The following options exist:
- redis: an existing Redis connection, defaults to a
TheCart consists of two main actors,
To add shopping abilities to a class, add the following:
cart_expires_in lets TheCart know in how many hours the cart key in redis should be expired, defaults to 24 hours.
To allow the shopper to add items to the cart, all classes that can be added should include the following:
and the following configuration:
track fields/attributes/methods return values will be cached in Redis after everytime you save the item instance, note that
your ORM needs to implement
after_save since TheCart utilizes those callbacks to update the cached data from Redis.
id attribute will automatically be cached.
price_field is the name of the attribute / method that returns the item price, defaults to
Listing items in a cart
@user.cart # => returns a hash of the tracked item fields currently cached, with quantity
Adding an item
Removing an item
Count items in cart
@user.cart_count #=> will return the actual item count, quantity considered.
Total Cost of items in cart
@user.cart_total #= 3000.0
Contributing and Source Code
As always, Pull requests and forks are always welcome over @ Github