Commit 52da099b by Tan Phat Nguyen

Merge branch 'feature/load_products' into 'feature/crud_product'

Feature/load products

Add feature list items on venshop
Detail category, products

See merge request !2
parents 99cb5249 8cfa4dc8
......@@ -6,10 +6,7 @@ gem 'mini_magick', '3.8.0'
gem 'fog', '1.23.0'
gem 'bcrypt', '3.1.7'
gem 'faker', '1.4.2'
gem 'will_paginate', '3.0.7'
gem 'bootstrap-will_paginate', '0.0.10'
gem 'bootstrap-sass', '3.2.0.0'
gem 'unicorn', '4.8.3'
gem 'sass-rails', '5.0.0.beta1'
gem 'uglifier', '2.5.3'
gem 'coffee-rails', '4.0.1'
......@@ -20,12 +17,14 @@ gem 'rails-html-sanitizer', '1.0.1'
gem 'sdoc', '0.4.0', group: :doc
gem 'mysql2'
gem 'vacuum'
gem 'kaminari'
group :development, :test do
gem 'sqlite3', '1.3.9'
gem 'byebug', '3.4.0'
gem 'web-console', '2.0.0.beta3'
gem 'spring', '1.1.3'
gem 'pry'
end
group :test do
......
......@@ -44,8 +44,6 @@ GEM
debug_inspector (>= 0.0.1)
bootstrap-sass (3.2.0.0)
sass (~> 3.2)
bootstrap-will_paginate (0.0.10)
will_paginate
builder (3.2.2)
byebug (3.4.0)
columnize (~> 0.8)
......@@ -126,6 +124,9 @@ GEM
railties (>= 4.2.0.beta, < 5.0)
thor (>= 0.14, < 2.0)
json (1.8.1)
kaminari (0.16.1)
actionpack (>= 3.0.0)
activesupport (>= 3.0.0)
kgio (2.9.2)
listen (2.7.11)
celluloid (>= 0.15.2)
......@@ -262,7 +263,6 @@ GEM
binding_of_caller (= 0.7.3.pre1)
railties (~> 4.0)
sprockets-rails (>= 2.0, < 4.0)
will_paginate (3.0.7)
win32console (1.3.2)
PLATFORMS
......@@ -272,7 +272,6 @@ PLATFORMS
DEPENDENCIES
bcrypt (= 3.1.7)
bootstrap-sass (= 3.2.0.0)
bootstrap-will_paginate (= 0.0.10)
byebug (= 3.4.0)
carrierwave (= 0.10.0)
coffee-rails (= 4.0.1)
......@@ -281,10 +280,12 @@ DEPENDENCIES
guard-minitest (= 2.3.1)
jbuilder (= 2.2.3)
jquery-rails (= 4.0.0.beta2)
kaminari
mini_backtrace (= 0.1.3)
mini_magick (= 3.8.0)
minitest-reporters (= 1.0.5)
mysql2
pry
rails (= 4.2.0.beta4)
rails-html-sanitizer (= 1.0.1)
rails_12factor (= 0.0.2)
......@@ -298,4 +299,3 @@ DEPENDENCIES
unicorn (= 4.8.3)
vacuum
web-console (= 2.0.0.beta3)
will_paginate (= 3.0.7)
# 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/
# 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/
# 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/
// Place all the styles related to the admin/products controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
// Place all the styles related to the category controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
......@@ -69,3 +69,15 @@
margin: 0 0 30px 0;
}
}
h4 {
color: #428bca;
}
h2 {
color: #428bca;
}
.price-col {
color: #b12704;
}
\ No newline at end of file
// Place all the styles related to the home controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
class Admin::ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
def index
@products = Product.all
end
def new
@product = Product.new
end
def create
@product = Product.new(product_params)
if @product.save
redirect_to [:admin, @product]
else
render :new
end
end
def show
end
def edit
end
def update
if @product.update_attributes(product_params)
redirect_to [:admin, @product]
else
render :edit
end
end
def destroy
@product.destroy
redirect_to admin_products_path
end
private
def product_params
params.require(:product).permit(:name, :price, :description, :category_id, :photo, :remote_photo_url)
end
def set_product
@product = Product.find(params[:id])
end
end
......@@ -2,4 +2,12 @@ class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found
private
def record_not_found
redirect_to root_path
end
end
class CategoriesController < ApplicationController
def show
@category = Category.find(params[:id])
@products = @category.products.page(params[:page])
end
end
class HomeController < ApplicationController
def index
@categories = Category.all
end
end
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
def index
@products = Product.all
end
def new
@product = Product.new
end
def create
@product = Product.new(product_params)
if @product.save
redirect_to @product
else
render 'new'
end
end
def show
end
def edit
end
def update
if @product.update_attributes(product_params)
redirect_to @product
else
render 'edit'
end
end
def destroy
@product.destroy
redirect_to products_path
end
private
def product_params
params.require(:product).permit(:name, :price, :description, :category_id, :photo)
end
def set_product
@product = Product.find(params[:id])
end
end
module Admin::ProductsHelper
end
module CategoriesHelper
end
module HomeHelper
end
class Category < ActiveRecord::Base
has_many :products, dependent: :destroy
def newest_products
products.newest
end
end
......@@ -8,8 +8,12 @@ class Product < ActiveRecord::Base
validates :description, presence: true
validate :photo_size
scope :newest, -> { order(created_at: :desc).limit(4) }
mount_uploader :photo, ImageUploader
paginates_per 8
private
def photo_size
......
<%= form_for @product, html: {multipart: true} do |f| %>
<%= form_for [:admin, @product], html: { multipart: true } do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="form-group">
<%= image_tag @product.photo.url, width: '140', height: '140', class: 'img-rounded' %>
......@@ -25,6 +25,10 @@
<%= f.label :photo %>
<%= f.file_field :photo, accept: 'image/jpeg,image/gif,image/png' %>
</div>
<div class="form-group">
<%= f.label :remote_photo_url, "or photo URL" %> <br>
<%= f.text_field :remote_photo_url %>
</div>
<%= f.submit class: "btn btn-primary" %>
<%= link_to 'Cancel', products_path, class: "btn btn-primary" %>
<%= link_to 'Cancel', admin_products_path, class: "btn btn-primary" %>
<% end %>
\ No newline at end of file
<tr>
<td><%= product.name %></td>
<td><%= product.price %></td>
<td><%= product.description %></td>
<td><%= product.category.name %></td>
<td><%= image_tag product.photo_url.to_s, width: '140', height: '140', class: 'img-rounded' %></td>
<td><%= link_to 'Show', [:admin, product] %></td>
<td><%= link_to 'Edit', edit_admin_product_path(product) %></td>
<td><%= link_to 'Destroy', [:admin, product], method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
......@@ -13,4 +13,4 @@
<%= render @products %>
</table>
</div>
<%= link_to 'New Product', new_product_path, class: "btn btn-primary" %>
<%= link_to 'New Product', new_admin_product_path, class: "btn btn-primary" %>
\ No newline at end of file
<% provide(:title, @product.name) %>
<h1 class='page-header'>Show Product</h1>
<p>Name: <b><%= @product.name%></b></p>
<p>Price: <b>$<%= @product.price%></b></p>
<p>Description: <b><%= @product.description%></b></p>
<p>Category: <b><%= @product.category.name%></b></p>
<%= image_tag @product.photo.url, width: '140', height: '140', class: 'img-rounded' %> <br/><br>
<%= link_to 'Back', admin_products_path, class: 'btn btn-primary' %>
<%= link_to 'Edit', edit_admin_product_path(@product), class: 'btn btn-primary' %>
\ No newline at end of file
<% if category.products.any? %>
<h2 class='sub-header'><%= category.name %></h2>
<div class='row placeholders'>
<%= render category.newest_products %>
</div>
<div>
<%= link_to '>>View more...', category_path(category) %>
</div>
<% end %>
\ No newline at end of file
<%= provide(:title, "Category #{@category.name if @category}") %>
<h1 class='page-header'>Category <%= @category.name if @category %></h1>
<div class='row placeholders'>
<%= render @products %>
</div>
<%= paginate @products %>
\ No newline at end of file
<%= provide(:title, 'Home') %>
<h1 class='page-header'>VenShop Apps</h1>
<%= render @categories %>
\ No newline at end of file
......@@ -7,6 +7,7 @@
<ul class='nav navbar-nav navbar-right'>
<li><%= link_to 'Home', root_path %></li>
<li><%= link_to 'Log in', '#' %></li>
<li><%= link_to 'Register', '#' %></li>
</ul>
<form class='navbar-form navbar-right'>
<input type='text' class='form-control' placeholder='Search...'>
......
......@@ -14,11 +14,9 @@
<div class='col-sm-3 col-md-2 sidebar'>
<ul class='nav nav-sidebar'>
<li class='active'><a href='#'>Categories</a></li>
<li><a href='#'>Books</a></li>
<li><a href='#'>Toys</a></li>
<li><a href='#'>Clothing</a></li>
<li><a href='#'>DVD</a></li>
<li><a href='#'>Magazine</a></li>
<% Category.all.each do |category| %>
<li><%= link_to category.name, category_path(category) %></li>
<% end %>
</ul>
</div>
<div class='col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main'>
......
<tr>
<td><%= product.name %></td>
<td><%= product.price %></td>
<td><%= product.description %></td>
<td><%= product.category.name %></td>
<td><%= image_tag product.photo_url.to_s, width: '140', height: '140', class: 'img-rounded' %></td>
<td><%= link_to 'Show', product_path(product) %></td>
<td><%= link_to 'Edit', edit_product_path(product) %></td>
<td><%= link_to 'Destroy', product_path(product), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<div class='col-xs-6 col-sm-3 placeholder'>
<div style='height: 234px;'>
<%= image_tag product.photo_url.to_s, width: '140', height: '140', class: 'img-thumbnail' %>
</div>
<h4><%= link_to product.name, product_path(product) %></h4>
<span class='price-col'>$<%= product.price %></span>
</div>
\ No newline at end of file
<% provide(:title, @product.name) %>
<h1 class='page-header'>Show Product</h1>
<p>Name: <b><%= @product.name%></b></p>
<p>Price: <b><%= @product.price%></b></p>
<p>Description: <b><%= @product.description%></b></p>
<p>Category: <b><%= @product.category.name%></b></p>
<%= image_tag @product.photo.url, width: '140', height: '140', class: 'img-rounded' %> <br/><br>
<%= link_to 'Back', products_path, class: 'btn btn-primary' %>
<%= link_to 'Edit', edit_product_path(@product), class: 'btn btn-primary' %>
\ No newline at end of file
<% provide(:title, "Item detail") %>
<% if @product %>
<h1 class='page-header'><%= @product.name %></h1>
<div class='page-header row'>
<div class='col-xs-4'>
<%= image_tag @product.photo_url.to_s, width: '349', height: '500', class: 'img-rounded' %>
</div>
<div class='col-xs-8'>
Item: <h4><%= @product.name %></h4>
Price: <p class='price-col'>$<%= @product.price %></p>
Date: <p><%= @product.created_at.strftime('%d-%m-%Y') %></p>
</div>
</div>
<div class='row'>
<b>Description:</b>
<p></p>
<p><%= @product.description %></p>
</div>
<div class='row'>
<%= link_to 'Buy', '#', class: 'btn btn-primary' %>
</div>
<% end %>
\ No newline at end of file
Rails.application.routes.draw do
namespace :admin do
resources :products
end
get 'amazon/welcome'
get 'amazon/index'
resources :products
root 'products#index'
get '/category/:id', to: 'categories#show', as: 'category'
resources :products, only: [:show]
get 'home' => 'home#index'
root 'home#index'
end
require 'test_helper'
class Admin::ProductsControllerTest < ActionController::TestCase
test "should get index" do
get :index
assert_response :success
end
test "should get show" do
get :show
assert_response :success
end
test "should get new" do
get :new
assert_response :success
end
test "should get create" do
get :create
assert_response :success
end
test "should get edit" do
get :edit
assert_response :success
end
test "should get update" do
get :update
assert_response :success
end
test "should get destroy" do
get :destroy
assert_response :success
end
end
require 'test_helper'
class CategoriesControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end
require 'test_helper'
class HomeControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment