Skip to content

Commit

Permalink
User: Show products
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyen.van.hai authored and hainv-2854 committed Jan 10, 2022
1 parent 0d79ec3 commit 78c1138
Show file tree
Hide file tree
Showing 16 changed files with 207 additions and 4 deletions.
Binary file added app/assets/images/products/digital_20.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/assets/images/shop-banner.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class ProductsController < ApplicationController
def index
@pagy, @products = pagy Product.sort_by_price,
items: Settings.length.per_page_12
end

def show; 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
1 change: 1 addition & 0 deletions app/models/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ class Product < ApplicationRecord
belongs_to :category
has_many :order_details, dependent: :destroy
has_many :comments, dependent: :destroy
scope :sort_by_price, ->{order :price}
end
18 changes: 18 additions & 0 deletions app/views/products/_product.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<li class="col-lg-4 col-md-6 col-sm-6 col-xs-6 ">
<div class="product product-style-3 equal-elem ">
<div class="product-thumnail">
<%= link_to product do %>
<figure>
<%= image_tag(product.image, alt: "") %>
</figure>
<% end %>
</div>
<div class="product-info">
<%= link_to product, class: "product-name" do %>
<span><%= product.name %></span>
<% end %>
<div class="wrap-price"><span class="product-price"><span><%= product.price %></span></div>
<%= link_to t(".add_to_cart"), product, class: "btn add-to-cart" %>
</div>
</div>
</li>
59 changes: 59 additions & 0 deletions app/views/products/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<% provide :title, t(".title") %>
<div class="wrap-breadcrumb">
<ul>
<li class="item-link"><%= link_to t(".sub_category"), "#", class: "link" %></li>
<li class="item-link"><span><%= t ".digital_electronic" %></span></li>
</ul>
</div>
<div class="row">
<div class="col-lg-3 col-md-4 col-sm-4 col-xs-12 sitebar">
<div class="widget mercado-widget categories-widget">
<h2 class="widget-title"><%= t ".all_categories" %></h2>
<div class="widget-content">
<ul class="list-category">
<li class="category-item has-child-cate">
<%= link_to t(".category"), "#", class: "cate-link" %>
<span class="toggle-control">+</span>
<ul class="sub-cate">
<li class="category-item"><%= link_to t(".sub_category"), "#", class: "cate-link" %></li>
</ul>
</li>
<li class="category-item">
<%= link_to t(".category"), "#", class: "cate-link" %>
</li>
</ul>
</div>
</div>
<div class="widget mercado-widget filter-widget price-filter">
<h2 class="widget-title"><%= t ".price" %></h2>
<div class="widget-content">
<div id="slider-range"></div>
<p>
<label for="amount"><%= t ".price" %> :</label>
<input type="text" id="amount" readonly>
<button class="filter-submit"><%= t ".filter" %></button>
</p>
</div>
</div>
</div>
<div class="col-lg-9 col-md-8 col-sm-8 col-xs-12 main-content-area">
<div class="banner-shop">
<%= link_to "#", class: "banner-link" do %>
<figure><%= image_tag("shop-banner.jpg", alt: "") %></figure>
<% end %>
</div>
<div class="wrap-shop-control">
<h1 class="shop-title"><%= t ".digital_electronic" %></h1>
</div>
<div class="row">
<ul class="product-list grid-products equal-container">
<%= render @products %>
</ul>
</div>
<div class="wrap-pagination-info">
<ul class="page-numbers">
<%== pagy_bootstrap_nav(@pagy) %>
</ul>
</div>
</div>
</div>
2 changes: 1 addition & 1 deletion app/views/shared/_error_messages.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div id="error_explanation">
<div class="alert alert-danger">
<%= t "the_form_contains" %>
<%= t "errors.error_message", count: pluralize(@user.errors.count, t("errors.error_singular")) %>
<%= t "errors.error_message", count: pluralize(object.errors.count, t("errors.error_singular")) %>
</div>
<ul>
<% object.errors.full_messages.each do |msg| %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/shared/_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<%= link_to t(".about_us"), "#", class: "link-term mercado-item-title" %>
</li>
<li class="menu-item">
<%= link_to t(".shop"), "#", class: "link-term mercado-item-title" %>
<%= link_to t(".shop"), products_path, class: "link-term mercado-item-title" %>
</li>
<li class="menu-item">
<%= link_to t(".cart"), "#", class: "link-term mercado-item-title" %>
Expand Down
13 changes: 13 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,16 @@ en:
forgot_password: "Forgotten password?"
create:
invalid_email_password: "Login fail"
products:
index:
title: "Products"
home: "Home"
digital_electronic: "Digital & Electronics"
all_categories: "All Categories"
price: "Price"
add_to_cart: "Add to cart"
grid: "Grid"
list: "List"
filter: "Filter"
category: "Category"
sub_category: "Sub category"
13 changes: 13 additions & 0 deletions config/locales/vi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,16 @@ vi:
forgot_password: "Quên mật khẩu?"
create:
invalid_email_password: "Đăng nhập thất bại"
products:
index:
title: "Danh sách sản phẩm"
home: "Trang chủ"
digital_electronic: "Kỹ thuật số và công nghệ"
all_categories: "Tất cả danh mục"
price: "Giá"
add_to_cart: "Thêm vào giỏ hàng"
grid: "Lưới"
list: "Danh sách"
filter: "Lọc"
category: "Danh mục"
sub_category: "Danh mục con"
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
get "/login", to: "sessions#new"
post "/login", to: "sessions#create"
delete "/logout", to: "sessions#destroy"

resources :products, only: %i(index show)
end
end
2 changes: 2 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
regex:
email: !ruby/regexp /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
length:
per_page_12: 12
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This migration comes from active_storage (originally 20170806125915)
class CreateActiveStorageTables < ActiveRecord::Migration[5.2]
def change
create_table :active_storage_blobs do |t|
t.string :key, null: false
t.string :filename, null: false
t.string :content_type
t.text :metadata
t.string :service_name, null: false
t.bigint :byte_size, null: false
t.string :checksum, null: false
t.datetime :created_at, null: false

t.index [ :key ], unique: true
end

create_table :active_storage_attachments do |t|
t.string :name, null: false
t.references :record, null: false, polymorphic: true, index: false
t.references :blob, null: false

t.datetime :created_at, null: false

t.index [ :record_type, :record_id, :name, :blob_id ], name: "index_active_storage_attachments_uniqueness", unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end

create_table :active_storage_variant_records do |t|
t.belongs_to :blob, null: false, index: false
t.string :variation_digest, null: false

t.index %i[ blob_id variation_digest ], name: "index_active_storage_variant_records_uniqueness", unique: true
t.foreign_key :active_storage_blobs, column: :blob_id
end
end
end
32 changes: 31 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,35 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2022_01_05_024244) do
ActiveRecord::Schema.define(version: 2022_01_10_025410) do

create_table "active_storage_attachments", charset: "utf8mb4", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
t.bigint "record_id", null: false
t.bigint "blob_id", null: false
t.datetime "created_at", null: false
t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
end

create_table "active_storage_blobs", charset: "utf8mb4", force: :cascade do |t|
t.string "key", null: false
t.string "filename", null: false
t.string "content_type"
t.text "metadata"
t.string "service_name", null: false
t.bigint "byte_size", null: false
t.string "checksum", null: false
t.datetime "created_at", null: false
t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
end

create_table "active_storage_variant_records", charset: "utf8mb4", force: :cascade do |t|
t.bigint "blob_id", null: false
t.string "variation_digest", null: false
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
end

create_table "addresses", charset: "utf8mb4", force: :cascade do |t|
t.text "address_detail"
Expand Down Expand Up @@ -91,6 +119,8 @@
t.datetime "updated_at", precision: 6, null: false
end

add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
add_foreign_key "addresses", "users"
add_foreign_key "comments", "products"
add_foreign_key "comments", "users"
Expand Down
21 changes: 20 additions & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
email: "[email protected]",
password: "user123",
password_confirmation: "user123",
is_role: 1,
role: 1,
activated: true
)
10.times do |n|
Expand All @@ -16,3 +16,22 @@
activated: true
)
end

category = ["Dell", "HP", "Lenovo", "Macbook"]

3.times do |n|
Category.create(name: category[n])
end

50.times do |n|
category_id = 1
category_id = 2 if n > 25
product = Product.create(
name: Faker::Name.first_name,
category_id: category_id,
price: 120000,
quantity: 10,
image: "products/digital_20.jpg",
description: Faker::Lorem.sentence,
)
end

0 comments on commit 78c1138

Please sign in to comment.