Skip to content

Commit

Permalink
product_outstanding
Browse files Browse the repository at this point in the history
  • Loading branch information
khungking909 committed Mar 29, 2024
1 parent cd4ff68 commit 19eb17a
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ Rails/SkipsModelValidations:
Style/InlineComment:
Enabled: true

Style/InlineComment:
Enabled: false

Style/IpAddresses:
Enabled: true

Expand Down
1 change: 1 addition & 0 deletions app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class ProductsController < ApplicationController

def index
@pagy, @products = pagy(filter_products, items: Settings.PAGE_9)
@product_outstandings = Product.product_outstanding
end

def new
Expand Down
7 changes: 7 additions & 0 deletions app/javascript/controllers/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@ application.debug = false
window.Stimulus = application

export { application }
const swiper = new Swiper('.swiper', {
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
slidesPerView: 3,
});
9 changes: 9 additions & 0 deletions app/models/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ class Product < ApplicationRecord
where("price >= ? AND price <= ?", min_price, max_price) if min_price.present? && max_price.present?
}
scope :newest, -> { order(created_at: :desc) }
scope :product_outstanding, (lambda do
select("products.*, SUM(order_histories.quantity) AS total_quantity")
.joins(:order_histories)
.group("products.id")
.order("total_quantity DESC")
.joins("INNER JOIN orders ON orders.id = order_histories.order_id")
.where(orders: { status: Order.statuses[:approved] })
.limit(Settings.DIGIT_10)
end)

validates :name, presence: true, length: { maximum: Settings.DIGIT_255 }
validates :price, presence: true
Expand Down
6 changes: 6 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
<%= javascript_importmap_tags %>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/swiper@11/swiper-bundle.min.css"
/>
<script src="https://cdn.jsdelivr.net/npm/swiper@11/swiper-bundle.min.js"></script>

</head>

<body>
Expand Down
18 changes: 18 additions & 0 deletions app/views/products/_outstanding_product.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<div class="swiper">
<div class="swiper-wrapper">
<% @product_outstandings.each do |product| %>
<div class="swiper-slide d-flex justify-content-center">
<div class="col-md-4 w-100">
<div class="product-item-container">
<div class="product-item-info">
<%= product_image_tag(product) %>
<h4><%= link_to product.name, product_path(product) %></h4>
<%= render "shared/rating" %>
<p><%= number_to_currency(product.price) %></p>
</div>
</div>
</div>
</div>
<% end %>
</div>
</div>
2 changes: 1 addition & 1 deletion app/views/products/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<%= render "layouts/flash" %>
<% end %>
<% if @slider %>
<%= render "layouts/slider" %>
<%= render "outstanding_product" %>
<% end %>
<div class="row">
<%= render "shared/list_categories" %>
Expand Down
1 change: 1 addition & 0 deletions config/importmap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
pin_all_from "app/javascript/controllers", under: "controllers"
pin_all_from "app/javascript/custom", under: "custom"
pin "bootstrap", to: "bootstrap.min.js"
pin "swiper" # @11.0.7
1 change: 1 addition & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ DIGIT_50: 50
DIGIT_255: 255
DIGIT_5: 5
DIGIT_2: 2
DIGIT_10: 10
DIGIT_1000: 1000
SIZE_400x400: [400, 400]
range_1_5: [1, 2, 3, 4, 5]
Expand Down
21 changes: 20 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"bootstrap-icons": "^1.11.3",
"postcss": "^8.4.38",
"postcss-cli": "^11.0.0",
"sass": "^1.72.0"
"sass": "^1.72.0",
"swiper": "^11.1.0"
},
"scripts": {
"build:css:compile": "sass ./app/assets/stylesheets/application.bootstrap.scss:./app/assets/builds/application.css --no-source-map --load-path=node_modules",
Expand Down
2 changes: 2 additions & 0 deletions vendor/javascript/swiper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export{S as Swiper,S as default}from"./_/a2UuPJCF.js";import"./_/MVv-lhcB.js";

5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,11 @@ strip-ansi@^6.0.0, strip-ansi@^6.0.1:
dependencies:
ansi-regex "^5.0.1"

swiper@^11.1.0:
version "11.1.0"
resolved "https://registry.npmjs.org/swiper/-/swiper-11.1.0.tgz"
integrity sha512-Pm78CgU7Wvg+w/MgIL/1NwMSLgx0XqFof25EnVyX8iZFCYJv5CppH7LHkBj43qW2k5Cp3Iz7FcdBxrox7XX2AQ==

thenby@^1.3.4:
version "1.3.4"
resolved "https://registry.npmjs.org/thenby/-/thenby-1.3.4.tgz"
Expand Down

0 comments on commit 19eb17a

Please sign in to comment.