TheCart is an implementation of a cart that utilizes Redis's awesomeness.
Add this line to your application's Gemfile:
gem 'the_cart'
And then execute:
$ bundle
Or install it yourself as:
$ gem install the_cart
create an initializer in config/initializers/the_cart.rb
:
TheCart.configure do |c|
#...
end
The following options exist:
- redis: an existing Redis connection, defaults to a
Redis.new
instance.
TheCart consists of two main actors, Shopper
and Item
.
To add shopping abilities to a class, add the following:
include TheCart::Shopper
and the cartify
directive:
cartify cart_expires_in: 12
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:
include TheCart::Item
and the following configuration:
cartify_item track: [field1, field2,...], price_field: :price
Those 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.
The id
attribute will automatically be cached.
price_field
is the name of the attribute / method that returns the item price, defaults to :price
@user.cart # => returns a hash of the tracked item fields currently cached, with quantity
@user.add_item_to_cart(@product)
@user.remove_item_from_cart(@product)
@user.cart_count #=> will return the actual item count, quantity considered.
@user.cart_total #= 3000.0
- Fork it ( http://github.com//the_cart/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request