• Stars
    star
    257
  • Rank 158,728 (Top 4 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 14 years ago
  • Updated almost 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple Shopping Cart implementation, Official repo: https://github.com/dabit/acts_as_shopping_cart

acts_as_shopping_cart

A simple shopping cart implementation.

Build Status Code Climate Test Coverage

You can find an example application here.

Install

Rails 3

As of Version 0.2.0 Rails 3 is no longer supported. Please use the 0-1-x branch if you still need to implement this gem in a Rails 3 app

Include it in your Gemfile

gem 'acts_as_shopping_cart', :github => 'crowdint/acts_as_shopping_cart', :branch => '0-1-x'

And run bundler

bundle install

Rails 4

Just include it in your Gemfile as:

gem 'acts_as_shopping_cart', '~> 0.2.1'

And run bundle install

bundle install

Usage

You need two models, one to hold the Shopping Carts and another to hold the Items

You can use any name for the models, you just have to let each model know about each other.

Examples

For the Shopping Cart:

class Cart < ActiveRecord::Base
  acts_as_shopping_cart_using :cart_item
end

For the items:

class CartItem < ActiveRecord::Base
  acts_as_shopping_cart_item_for :cart
end

or, if you want to use convention over configuration, make sure your models are called ShoppingCart and ShoppingCartItem, then just use the shortcuts:

class ShoppingCart < ActiveRecord::Base
  acts_as_shopping_cart
end

class ShoppingCartItem < ActiveRecord::Base
  acts_as_shopping_cart_item
end

Migrations

In order for this to work, the Shopping Cart Item model should have the following fields:

create_table :cart_items do |t|
  t.shopping_cart_item_fields # Creates the cart items fields
end

Shopping Cart Items

Your ShoppingCart class will have a shopping_cart_items association that returns all the ShoppingCartItem objects in your cart.

Add Items

To add an item to the cart you use the add method. You have to send the object and the price of the object as parameters.

So, if you had a Product class, you would do something like this:

@cart = Cart.create
@product = Product.find(1)

@cart.add(@product, 99.99)

In the case where your product has a price field you could do something like:

@cart.add(@product, @product.price)

I tried to make it independent to the models in case you calculate discounts, sale prices or anything customized.

You can include a quantity parameter too.

@cart.add(@product, 99.99, 5)

In that case, you would add 5 of the same products to the shopping cart. If you don't specify the quantity 1 will be assumed.

Remove Items

To remove an item from the cart you can use the remove method. You just have to send the object and the quantity you want to remove.

@cart.remove(@product, 1)

Empty the cart

To remove all the items in the cart at once, just use the clear method

@cart.clear

Total

You can find out about the total using the total method:

@cart.total # => 99.99

Taxes

Taxes by default are calculated by multiplying subtotal times 8.25

If you want to change the way taxes are calculated, override the taxes method on your class that acts_as_shopping_cart.

Example:

class ShoppingCart < ActiveRecord::Base
  acts_as_shopping_cart

  def taxes
    (subtotal - 10) * 8.3
  end
end

If you just want to update the percentage, just override the tax_pct method.

class ShoppingCart < ActiveRecord::Base
  acts_as_shopping_cart

  def tax_pct
    3.5
  end
end

Shipping Cost

Shipping cost will be added to the total. By default its calculated as 0, but you can just override the shipping_cost method on your cart class depending on your needs.

class ShoppingCart < ActiveRecord::Base
  acts_as_shopping_cart

  def shipping_cost
    5 # defines a flat $5 rate
  end
end

Total unique items

You can find out how many unique items you have on your cart using the total_unique_items method.

So, if you had something like:

@cart.add(@product, 99.99, 5)

Then,

@cart.total_unique_items # => 5

Development

Install the dependencies

bundle install

Test

Run rspec

rspec spec

Run cucumber features

cucumber

Both:

rake

About the author

Crowd Interactive is a leading Ruby and Rails consultancy firm based in Mexico currently doing business with startups in the United States. We specialize in building and growing Rails applications, by increasing your IT crew onsite or offsite. We pick our projects carefully, as we only work with companies we believe in.

More Repositories

1

rails3-jquery-autocomplete

An easy and unobtrusive way to use jQuery's autocomplete with Rails 3
Ruby
926
star
2

rails3-jquery-autocomplete-app

This app is meant to show you how to use the rails3-jquery-autocomplete gem.
Ruby
90
star
3

blog.crowdint.com

Crowd Interactive Tech blog
Ruby
54
star
4

slim_assets

Use Slim with Rails helpers in the asset pipeline
Ruby
29
star
5

fundraiser

Mountable Engine to set up a Crowdfunding app. Uses Amazon Simplepay to collect payments.
Ruby
25
star
6

blog.crowdint.com-jekyll

Crowd Interactive's tech blog
Ruby
24
star
7

prdashboard

CSS
22
star
8

slugoid

Drop-in solution to pretty urls when using Mongoid
Ruby
20
star
9

emojimenu

An Android Library that allows you to easily add emoji support to your Android application
Java
17
star
10

grpc-twitter-example

Example usage of grpc-go
Go
17
star
11

crowdblog

Generic blog engine we use for our blog style websites. Use at own risk
Ruby
12
star
12

acts_as_shopping_cart_app

Example app for acts_as_shopping_cart_gem
Ruby
10
star
13

fundraiser-app

Example app that uses fundraiser gem to create a crowdfunding site
Ruby
10
star
14

keyword_ranking

Ruby library to get the ranking of a url based on a keyword in Bing, Yahoo or Google
Ruby
9
star
15

facturacion_electronica

Ruby
7
star
16

easy-automation

Friendly Automation Testing Framework
Ruby
6
star
17

bamboohr

Consume BambooHR's API with Ruby
Ruby
5
star
18

vagrant-setup

Vagrant / Chef recipes to set up a new dev environment
Ruby
5
star
19

crudspec

Generate specs for CRUD controllers
Ruby
5
star
20

spree_conekta

Ruby
5
star
21

auditrail

Ruby
5
star
22

heroku_mongoid

Easy Mongoid on Heroku
Ruby
4
star
23

gransak

Go
4
star
24

pair-stations

Ruby
4
star
25

rankstar

Ruby gem used to get the ranking of a url based on a keyword in Bing, Yahoo or Google
Ruby
4
star
26

spree_brightpearl

Spree extension for BrightPearl connection
Ruby
3
star
27

associate_by

Allows you to associate two objects using a specified field.
Ruby
3
star
28

spree_store_locator

Store Locator for Spree
Ruby
3
star
29

Doku

Small Mac Os App that keeps track of your Google Docs
Ruby
2
star
30

spree-wrap

Consume a compatible Spree API from Ruby Motion
Ruby
2
star
31

ajax-nested-fields

Ruby
2
star
32

styleguide-deprecated

Create a base styleguide
Ruby
2
star
33

gopher-spree-api

Go
2
star
34

LearningIPhoneMotionExamples

Learning iPhone Programming Ruby Motion examples
Ruby
2
star
35

spree_reviews_rating

Ruby
2
star
36

destroyer

Ruby
2
star
37

rankit

Post stuff, rank it, let others rank, comment
Ruby
1
star
38

pomonit

Pomodoro monitor
Ruby
1
star
39

cucumber-debug

Launch a browser snapshot for cucumber failed scenarios
Ruby
1
star
40

scorecards

Score Cards
Ruby
1
star
41

rspechan_worker

Ruby
1
star
42

memphis

Rails.mx maintenance page
Ruby
1
star
43

find_method

Find a method on a class or an instance using part of the name
Ruby
1
star
44

ruby_on_rails_grupo_1

App que hemos desarrollado en el grupo 1 de los talleres de RoR
Ruby
1
star
45

crowdqna

Q & A Forum we use to get anonymous employee feedback
Ruby
1
star
46

sinatra-view-helpers

Integrates Rails View Helpers in Sinatra
Ruby
1
star
47

spree-mail-chimp

Ruby
1
star
48

hello_mail

Add a rake task to test your mail configuration on Rails 3
Ruby
1
star
49

ci_culture

Ruby
1
star
50

jPlaylist

JavaScript
1
star
51

rpx_regexp

Restrict who authenticates through Janrain with a regexp
Ruby
1
star
52

spree_shipworks

Ruby
1
star
53

simple_caches

Counter caches, as simple as you can imagine
Ruby
1
star
54

harry

VERY lightweight Ruby web applications framework
Ruby
1
star
55

gappster

Quick solution for Rails to authenticate your users with Google Apps' Open Id
Ruby
1
star
56

crowdousel

Small HTML script we use on our public screens
JavaScript
1
star
57

voting_engine

Ruby
1
star
58

mapit

Very, very simple gem that provides a helper to show a map of the specified address using Google Maps.
Ruby
1
star
59

sasha

Add your current Git SHA to your Rails app response headers
Ruby
1
star
60

jfloatable

jfloatable is a jQuery plugin allows you to have floating elements within an area, with a smooth and customizable movement when the window is scrolled. The advantage of using jfloatable is the freedom of move your elements within a container as the page scroll, perfect for content that you want to be visible at all time. It's so easy to install and configure you'll have it working on your site in minutes! Just keep reading and find out how!
JavaScript
1
star
61

magic_form

Ruby
1
star
62

hooker

Rack app to manage your git POST hooks
Ruby
1
star