Skip to content

Commit

Permalink
add sorting for categories, add rescue for exeption if sorting params…
Browse files Browse the repository at this point in the history
… are bad
  • Loading branch information
AndriyAndriyovuch committed Sep 19, 2023
1 parent 92961c8 commit be90c6a
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 2 deletions.
4 changes: 4 additions & 0 deletions app/controllers/account/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ class Account::BaseController < ApplicationController
format.html { redirect_to root_path, alert: exception.message }
end
end

rescue_from ActiveRecord::StatementInvalid, ActiveRecord::UnknownAttributeReference do |exception|
redirect_back(fallback_location: root_path, alert: I18n.t("sort.sort_error"))

Check warning on line 15 in app/controllers/account/base_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/account/base_controller.rb#L15

Added line #L15 was not covered by tests
end
end
2 changes: 1 addition & 1 deletion app/controllers/account/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Account::CategoriesController < Account::BaseController
load_and_authorize_resource

def index
@categories = collection
@categories = SortService.new(collection, params).call
end

def new
Expand Down
11 changes: 11 additions & 0 deletions app/helpers/account/sort_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,15 @@ def users_criterias
[I18n.t("sort.last_sign_in_at_desc"), "last_sign_in_at desc"]
]
end

def categories_criterias
[
[I18n.t("sort.id_asc"), "id asc"],
[I18n.t("sort.id_desc"), "id desc"],
[I18n.t("sort.name_asc"), "name asc"],
[I18n.t("sort.name_desc"), "name desc"],
[I18n.t("sort.priority_asc"), "priority asc"],
[I18n.t("sort.priority_desc"), "priority desc"]
]
end
end
3 changes: 2 additions & 1 deletion app/views/account/categories/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
span =t('.add_category_button')

div class="container home"
= render 'account/shared/sort_selector', criterias: categories_criterias

table.table
table.table.mt-2
thead
tr
th scope="col" #
Expand Down
3 changes: 3 additions & 0 deletions config/locales/en/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -713,3 +713,6 @@ en:
email_desc: Email, descending
last_sign_in_at_asc: Last sign in, ascending
last_sign_in_at_desc: Last sign in, descending
priority_asc: Priority, ascending
priority_desc: Priority, descending
sort_error: Sorting by this parameter is not available
3 changes: 3 additions & 0 deletions config/locales/uk/uk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -769,3 +769,6 @@ uk:
email_desc: Електронна адреса, за спаданням
last_sign_in_at_asc: Час останнього візиту, за зростанням
last_sign_in_at_desc: Час останнього візиту, за спаданням
priority_asc: Пріоритет, за зростанням
priority_desc: Пріоритет, за спаданням
sort_error: Сортування за цими параметрами недоступне

0 comments on commit be90c6a

Please sign in to comment.