Skip to content

Commit

Permalink
added sorting to users and calculators
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriyAndriyovuch committed Sep 19, 2023
1 parent 7913b62 commit 0ef8a09
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 19 deletions.
8 changes: 8 additions & 0 deletions app/controllers/account/calculators_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ class Account::CalculatorsController < Account::BaseController
before_action :calculator, only: [:edit, :update, :destroy]
load_and_authorize_resource

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

Check warning on line 8 in app/controllers/account/calculators_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/account/calculators_controller.rb#L8

Added line #L8 was not covered by tests
end

def show
# TODO: fill it
end
Expand Down Expand Up @@ -40,6 +44,10 @@ def destroy

private

def collection
Calculator.all

Check warning on line 48 in app/controllers/account/calculators_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/account/calculators_controller.rb#L48

Added line #L48 was not covered by tests
end

def calculator
@calculator = Calculator.friendly.find(params[:slug])
end
Expand Down
2 changes: 1 addition & 1 deletion 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 = Sort.new(collection, params).sort
@products = SortService.new(collection, params).call
end

def show
Expand Down
6 changes: 5 additions & 1 deletion app/controllers/account/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Account::UsersController < Account::BaseController
load_and_authorize_resource

def index
@users = User.all
@users = SortService.new(collection, params).call

respond_to do |format|
format.html
Expand Down Expand Up @@ -70,6 +70,10 @@ def user_params
prms
end

def collection
User.all
end

def resource
User.find(params[:id])
end
Expand Down
10 changes: 0 additions & 10 deletions app/helpers/account/products_helper.rb

This file was deleted.

32 changes: 32 additions & 0 deletions app/helpers/account/sort_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Account::SortHelper
def products_criterias
[
[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

def calculators_criterias
[
[I18n.t("sort.id_asc"), "id asc"],

Check warning on line 13 in app/helpers/account/sort_helper.rb

View check run for this annotation

Codecov / codecov/patch

app/helpers/account/sort_helper.rb#L13

Added line #L13 was not covered by tests
[I18n.t("sort.id_desc"), "id desc"],
[I18n.t("sort.name_asc"), "name asc"],
[I18n.t("sort.name_desc"), "name desc"],
[I18n.t("sort.slug_asc"), "slug asc"],
[I18n.t("sort.slug_desc"), "slug desc"]
]
end

def users_criterias
[
[I18n.t("sort.id_asc"), "id asc"],
[I18n.t("sort.id_desc"), "id desc"],
[I18n.t("sort.email_asc"), "email asc"],
[I18n.t("sort.email_desc"), "email desc"],
[I18n.t("sort.last_sign_in_at_asc"), "last_sign_in_at asc"],
[I18n.t("sort.last_sign_in_at_desc"), "last_sign_in_at desc"]
]
end
end
4 changes: 2 additions & 2 deletions app/services/sort.rb → app/services/sort_service.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
class Sort
class SortService
attr_accessor :collection, :params

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

def sort
def call
collection.order(sorting_params)
end

Expand Down
6 changes: 6 additions & 0 deletions app/views/account/calculators/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
i.fa.fa-plus.mr-2
span =t('.add_calculator_button')

p.d-inline.mr-3
= t('sort.sort_by')
div.d-inline-block
= form_with method: :get do |form|
= form.select :sort, calculators_criterias, { selected: params[:sort], include_blank: true }, class: 'form-control mr-5', onchange: 'this.form.submit()'

table.table.admin-table
thead
tr
Expand Down
3 changes: 1 addition & 2 deletions app/views/account/products/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
= 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()'
= form.select :sort, products_criterias, { selected: params[:sort], include_blank: true }, class: 'form-control mr-5', onchange: 'this.form.submit()'

table.table
thead
Expand Down
15 changes: 12 additions & 3 deletions app/views/account/users/index.html.slim
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
div class="container home"


.row
.col
h1 = t '.main_header'
h3 = link_to icon('fa-solid', 'download'), account_users_path(format: "csv")
table.table
p.d-inline.mr-3
= t('sort.sort_by')
div.d-inline-block
= form_with method: :get do |form|
= form.select :sort, users_criterias, { selected: params[:sort], include_blank: true }, class: 'form-control mr-5', onchange: 'this.form.submit()'

h3.d-inline.float-right.mt-3
= link_to icon('fa-solid', 'download'), account_users_path(format: "csv")

table.table.mt-2
thead
tr
td = t '.table.email_col'
Expand Down
8 changes: 8 additions & 0 deletions config/locales/en/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -705,3 +705,11 @@ en:
id_desc: ID, descending
title_asc: Title, ascending
title_desc: Title, descending
name_asc: Name, ascending
name_desc: Name, descending
slug_asc: Slug, ascending
slug_desc: Slug, descending
email_asc: Email, ascending
email_desc: Email, descending
last_sign_in_at_asc: Last sign in, ascending
last_sign_in_at_desc: Last sign in, descending
8 changes: 8 additions & 0 deletions config/locales/uk/uk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -761,3 +761,11 @@ uk:
id_desc: ID, за спаданням
title_asc: Назва, за зростанням
title_desc: Назва, за спаданням
name_asc: Назва, за зростанням
name_desc: Назва, за спаданням
slug_asc: Шлях, за зростанням
slug_desc: Шлях, за спаданням
email_asc: Електронна адреса, за зростанням
email_desc: Електронна адреса, за спаданням
last_sign_in_at_asc: Час останнього візиту, за зростанням
last_sign_in_at_desc: Час останнього візиту, за спаданням

0 comments on commit 0ef8a09

Please sign in to comment.