Skip to content

Commit

Permalink
Adding code from
Browse files Browse the repository at this point in the history
  • Loading branch information
chischaschos committed Jun 10, 2015
1 parent 47839df commit 2093c17
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 7 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

[![Build Status](https://travis-ci.org/crowdint/acts_as_shopping_cart_app.svg?branch=master)](https://travis-ci.org/crowdint/acts_as_shopping_cart_app)

## TODO

- Explain how to use it or where to see it working
## Installation

```bash
git clone https://github.com/crowdint/acts_as_shopping_cart_app
cd acts_as_shopping_cart_app
bundle exec
bundle install
bundle exec rake db:setup
bundle exec rails s
```
69 changes: 69 additions & 0 deletions app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,74 @@
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]

# GET /products
# GET /products.json
def index
@products = Product.all
end

# GET /products/1
# GET /products/1.json
def show
end

# GET /products/new
def new
@product = Product.new
end

# GET /products/1/edit
def edit
end

# POST /products
# POST /products.json
def create
@product = Product.new(product_params)

respond_to do |format|
if @product.save
format.html { redirect_to @product, notice: 'Product was successfully created.' }
format.json { render :show, status: :created, location: @product }
else
format.html { render :new }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /products/1
# PATCH/PUT /products/1.json
def update
respond_to do |format|
if @product.update(product_params)
format.html { redirect_to @product, notice: 'Product was successfully updated.' }
format.json { render :show, status: :ok, location: @product }
else
format.html { render :edit }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end

# DELETE /products/1
# DELETE /products/1.json
def destroy
@product.destroy
respond_to do |format|
format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_product
@product = Product.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def product_params
params.require(:product).permit(:name, :price)
end
end
25 changes: 25 additions & 0 deletions app/views/products/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<%= form_for(@product) do |f| %>
<% if @product.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2>

<ul>
<% @product.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :price %><br>
<%= f.text_field :price %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/products/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Editing product</h1>

<%= render 'form' %>

<%= link_to 'Show', @product %> |
<%= link_to 'Back', products_path %>
29 changes: 27 additions & 2 deletions app/views/products/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
<h1>Products#index</h1>
<h1>Listing products</h1>

<%= render @products %>
<table>
<thead>
<tr>
<th>Name</th>
<th>Price</th>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @products.each do |product| %>
<tr>
<td><%= product.name %></td>
<td><%= product.price %></td>
<td><%= link_to 'Add to cart', shopping_cart_path(:product_id => product), :method => 'POST' %></td>
<td><%= link_to 'Show', product %></td>
<td><%= link_to 'Edit', edit_product_path(product) %></td>
<td><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>

<br>

<%= link_to 'New Product', new_product_path %>
4 changes: 4 additions & 0 deletions app/views/products/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
json.array!(@products) do |product|
json.extract! product, :id, :name, :price
json.url product_url(product, format: :json)
end
5 changes: 5 additions & 0 deletions app/views/products/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New product</h1>

<%= render 'form' %>

<%= link_to 'Back', products_path %>
18 changes: 18 additions & 0 deletions app/views/products/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<p id="notice"><%= notice %></p>

<p>
<strong>Name:</strong>
<%= @product.name %>
</p>

<p>
<strong>Price:</strong>
<%= @product.price %>
</p>

<p>
<%= link_to 'Add to cart', shopping_cart_path(:product_id => @product), :method => 'POST' %>
</p>

<%= link_to 'Edit', edit_product_path(@product) %> |
<%= link_to 'Back', products_path %>
1 change: 1 addition & 0 deletions app/views/products/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.extract! @product, :id, :name, :price, :created_at, :updated_at
6 changes: 4 additions & 2 deletions app/views/shopping_carts/_shopping_cart_item.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<div>
<div><%= shopping_cart_item.item.name %></div>
<div><%= shopping_cart_item.item.price %></div>
<% if shopping_cart_item.item %>
<div><%= shopping_cart_item.item.name %></div>
<div><%= shopping_cart_item.item.price %></div>
<% end %>
</div>
1 change: 1 addition & 0 deletions app/views/shopping_carts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
<div><b>SubTotal:</b><%= number_to_currency @shopping_cart.subtotal %></div>
<div><b>Taxes:</b><%= number_to_currency @shopping_cart.taxes %></div>
<div><b>Total:</b><%= number_to_currency @shopping_cart.total %></div>
</div>
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Rails.application.routes.draw do
resources :products, only: [ :index ]
resources :products
resource :shopping_cart

root 'products#index'
end

0 comments on commit 2093c17

Please sign in to comment.