Skip to content
This repository was archived by the owner on Dec 8, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/assets/javascripts/products.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/products.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the products controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
71 changes: 71 additions & 0 deletions app/assets/stylesheets/scaffolds.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
body {
background-color: #fff;
color: #333;
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}

p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}

pre {
background-color: #eee;
padding: 10px;
font-size: 11px;
}

a {
color: #000;
&:visited {
color: #666;
}
&:hover {
color: #fff;
background-color: #000;
}
}

div {
&.field, &.actions {
margin-bottom: 10px;
}
}

#notice {
color: green;
}

.field_with_errors {
padding: 2px;
background-color: red;
display: table;
}

#error_explanation {
width: 450px;
border: 2px solid red;
padding: 7px;
padding-bottom: 0;
margin-bottom: 20px;
background-color: #f0f0f0;
h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px;
margin-bottom: 0px;
background-color: #c00;
color: #fff;
}
ul li {
font-size: 12px;
list-style: square;
}
}
*/
89 changes: 89 additions & 0 deletions app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]

# GET /products
# GET /products.json
def index
#puts params[:page], '*'*100
@pages_count = (Product.all.size/10) + (((Product.all.size % 10) > 0) ? 1 : 0)
@current_page = params[:page].to_i
@next_page = if @current_page < (@pages_count - 1)
@current_page + 1
else
@current_page
end

@prev_page = if @current_page > 0
@current_page - 1
else
@current_page
end

@products = Product.limit(10).offset(10 * @current_page) # @tonytonyjan++
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, :description, :price, :quantity)
end
end
2 changes: 2 additions & 0 deletions app/helpers/products_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ProductsHelper
end
2 changes: 2 additions & 0 deletions app/models/product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Product < ActiveRecord::Base
end
23 changes: 23 additions & 0 deletions app/views/layouts/_navbar.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">ˊ_>ˋ</a>
</div>

<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><%= link_to 'Index', products_path %></li>
<li><%= link_to 'New', new_product_path %><li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>

12 changes: 11 additions & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,20 @@
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>

</head>
<body>

<%= yield %>
<%= render 'layouts/navbar' %>

<div class="container">
<%= yield %>
</div>

</body>
</html>
33 changes: 33 additions & 0 deletions app/views/products/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<%= 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 :description %><br>
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :price %><br>
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :quantity %><br>
<%= f.number_field :quantity %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
11 changes: 11 additions & 0 deletions app/views/products/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<h1>Editing product</h1>

<%= render 'form' %>

<br>
<button type="button" class="btn btn-link">
<%= link_to '<< Back', products_path %>
</button>
|<button type="button" class="btn btn-link">
<%= link_to 'Show', @product %>
</button>
48 changes: 48 additions & 0 deletions app/views/products/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<h1>Listing products</h1>

<br>
<button type="button" class="btn btn-default"><%= link_to 'New Product', new_product_path %></button>
<br><br>


<ul class="pagination">
<li><%= link_to '«', page: "#{@prev_page}" %></a></li>
<% @pages_count.times do |i| %>
<% if i == @current_page %>
<li class="active">
<% else %>
<li>
<% end %>
<%= link_to "#{i+1}", page: "#{i}" %></li>
<% end %>
<li><%= link_to '»', page: "#{@next_page}" %></a></li>
</ul>

<table class="table table-striped table-hover">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Price</th>
<th>Quantity</th>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @products.each do |product| %>
<tr>
<td><%= product.name %></td>
<td><%= product.description %></td>
<td><%= product.price %></td>
<td><%= product.quantity %></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>

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, :description, :price, :quantity
json.url product_url(product, format: :json)
end
6 changes: 6 additions & 0 deletions app/views/products/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>New product</h1>

<%= render 'form' %>

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

<p class="bg-success">
<strong>Name:</strong>
<%= @product.name %>
</p>

<p class="bg-info">
<strong>Description:</strong>
<%= @product.description %>
</p>

<p class="bg-warning">
<strong>Price:</strong>
<%= @product.price %>
</p>

<p class="bg-danger">
<strong>Quantity:</strong>
<%= @product.quantity %>
</p>

<button type="button" class="btn btn-link">
<%= link_to '<< Back', products_path %>
</button>
|
<button type="button" class="btn btn-link">
<%= link_to 'Edit', edit_product_path(@product) %>
</button>
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, :description, :price, :quantity, :created_at, :updated_at
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Rails.application.routes.draw do
resources :products

# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20140712191944_create_products.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.text :name
t.text :description
t.decimal :price
t.integer :quantity

t.timestamps
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20140714144837_change_name_type_in_products.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ChangeNameTypeInProducts < ActiveRecord::Migration
def change
change_column :products, :name, :string
end
end
Loading