Skip to content

Commit

Permalink
added sorting service, added selector on view
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriyAndriyovuch committed Sep 18, 2023
1 parent f8acd86 commit 7913b62
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app/controllers/account/products_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Account::ProductsController < Account::BaseController
def index
@products = collection
@products = Sort.new(collection, params).sort
end

def show
Expand Down Expand Up @@ -56,7 +56,7 @@ def resource
end

def collection
Product.ordered_by_title
Product.all
end

def product_params
Expand Down
10 changes: 10 additions & 0 deletions app/helpers/account/products_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Account::ProductsHelper
def collection
[
[I18n.t("sort.id_asc"), "id asc"],
[I18n.t("sort.id_desc"), "id desc"],
[I18n.t("sort.title_asc"), "title asc"],
[I18n.t("sort.title_desc"), "title desc"]
]
end
end
2 changes: 0 additions & 2 deletions app/models/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ class Product < ApplicationRecord

validates :title, presence: true, length: { in: 2..50 }

scope :ordered_by_title, -> { order(:title) }

accepts_nested_attributes_for :prices, reject_if: :blank_prices, allow_destroy: true

def self.diaper
Expand Down
18 changes: 18 additions & 0 deletions app/services/sort.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Sort
attr_accessor :collection, :params

def initialize(collection, params)
@collection = collection
@params = params
end

def sort
collection.order(sorting_params)
end

private

def sorting_params
params.nil? ? "id asc" : (params[:sort].presence || "id asc")
end
end
7 changes: 7 additions & 0 deletions app/views/account/products/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
i.fa.fa-plus.mr-2
span =t('.add_product_button')

p.d-inline.mr-3
= t('sort.sort_by')
div.d-inline-block
= form_with method: :get do |form|

= form.select :sort, collection, { selected: params[:sort], include_blank: true }, class: 'form-control mr-5', onchange: 'this.form.submit()'

table.table
thead
tr
Expand Down
6 changes: 6 additions & 0 deletions config/locales/en/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -699,3 +699,9 @@ en:
long: "%B %d, %Y %H:%M"
short: "%d %b %H:%M"
pm: pm
sort:
sort_by: Sort by
id_asc: ID, ascending
id_desc: ID, descending
title_asc: Title, ascending
title_desc: Title, descending
6 changes: 6 additions & 0 deletions config/locales/uk/uk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -755,3 +755,9 @@ uk:
long: "%d %B %Y, %H:%M"
short: "%d %b, %H:%M"
pm: по полудні
sort:
sort_by: Сортувати за
id_asc: ID, за зростанням
id_desc: ID, за спаданням
title_asc: Назва, за зростанням
title_desc: Назва, за спаданням

0 comments on commit 7913b62

Please sign in to comment.