Skip to content

Commit

Permalink
fixed admin results pagination and added sort
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegPhenomenon committed Jul 15, 2024
1 parent 0e0361a commit 288621d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 38 deletions.
33 changes: 20 additions & 13 deletions app/controllers/admin/results_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,10 @@ module Admin
class ResultsController < BaseController
# GET /admin/results
def index
sort_column = params[:sort].presence_in(%w[auctions.domain_name
auctions.ends_at
registration_due_date
status
users.id]) || 'auctions.ends_at'
sort_direction = params[:direction].presence_in(%w[asc desc]) || 'desc'
order_string = "#{allowed_sort_columns[sort_column]} #{allowed_sort_directions}"

@results = Result.includes(:auction, offer: [:billing_profile])
.search(params)
.includes(:user)
.includes(:auction)
.order("#{sort_column} #{sort_direction}")

@pagy, @results = pagy(@results, items: params[:per_page] ||= 15)
@results = result_query.order(Arel.sql(order_string))
@pagy, @results = pagy(@results, items: params[:per_page] ||= 15, link_extra: 'data-turbo-action="advance"')

@auctions_needing_results = Auction.without_result.search(params)
end
Expand Down Expand Up @@ -43,6 +33,23 @@ def show

private

def result_query = Result.includes(:auction, offer: [:billing_profile])
.references(:auction, :offer)
.search(params)
.includes(:user)

def sort_column = params[:sort_by].presence_in(allowed_sort_columns.keys) || 'auctions.ends_at'

def allowed_sort_directions = params[:sort_direction].presence_in(%w[asc desc]) || 'desc'

def allowed_sort_columns = {
'auctions.domain_name' => 'auctions.domain_name',
'auctions.ends_at' => 'auctions.ends_at',
'results.registration_due_date' => 'results.registration_due_date',
'status' => 'results.status',
'billing_profile_id' => 'offers.billing_profile_id'
}

def buyer_name
return @result.offer.billing_profile.name if @result.offer.billing_profile.present?

Expand Down
1 change: 0 additions & 1 deletion app/views/admin/auctions/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@
<%= component 'common/table', header_collection:, options: { class: 'js-table-dt dataTable no-footer' } do %>
<!-- "The body of the table also needs to be in the component block, so the component wraps it with the necessary tags." -->
<%= tag.tbody id: "auctions-table-body", class: 'contents' do %>
<%#= render partial: 'auctions', locals: { auctions: @auctions, form: @form_auction_table } %>
<%= render @auctions, form: @form_auction_table %>
<% end %>
<% end %>
Expand Down
16 changes: 16 additions & 0 deletions app/views/admin/results/_result.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<tr class="results-table-row">
<td class="monospace">
<%= link_to result.auction.domain_name, admin_result_path(result) %>
</td>
<td><%= I18n.l(result.auction.ends_at) %></td>
<td><%= I18n.l(result.registration_due_date) %></td>
<td><%= result.status.humanize %></td>
<td>
<%- unless result.no_bids? %>
<%- unless result.offer.nil? %>
<%= result.offer.billing_profile&.name %>
<% end %>
<% end %>
</td>
<td></td>
</tr>
38 changes: 14 additions & 24 deletions app/views/admin/results/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
</div>
</div>
<% end %>

<% header_collection = [{ column: '', caption: t('auctions.domain_name'), options: { class: "" } },
{ column: '', caption: t('auctions.finished_at'), options: { class: "" } }] %>
<div style='display: flex; justify-content: space-between; align-items: center; margin-top: 2rem; padding: 10px;'>
Expand All @@ -37,35 +38,24 @@
<% end %>
<% end %>

<% result_header_collection = [{ column: '', caption: t('auctions.domain_name'), options: { class: "" } },
{ column: '', caption: t('auctions.finished_at'), options: { class: "" } },
{ column: '', caption: t('results.registration_due_date'), options: { class: "" } },
{ column: '', caption: t('status'), options: { class: "" } },
{ column: '', caption: t('billing_profiles.name'), options: { class: "" } }] %>
<% result_header_collection = [{ column: 'auctions.domain_name', caption: t('auctions.domain_name'), options: { class: "sorting" } },
{ column: 'auctions.finished_at', caption: t('auctions.finished_at'), options: { class: "sorting" } },
{ column: 'results.registration_due_date', caption: t('results.registration_due_date'), options: { class: "sorting" } },
{ column: 'status', caption: t('status'), options: { class: "sorting" } },
{ column: 'billing_profile_id', caption: t('billing_profiles.name'), options: { class: "sorting" } }] %>
<div style='display: flex; justify-content: space-between; align-items: center; margin-top: 2rem; padding: 10px;'>
<h2><%= t('.results') %></h2>
</div>
<%= component 'common/table', header_collection: result_header_collection, options: { class: 'js-table-dt' } do %>

<%= turbo_frame_tag "results" do %>
<%= component 'common/table', header_collection: result_header_collection, options: { class: 'js-table-dt dataTable no-footer' } do %>
<%= tag.tbody class: 'contents' do %>
<% @results.each do |result| %>
<tr class="results-table-row">
<td class="monospace">
<%= link_to result.auction.domain_name, admin_result_path(result) %>
</td>
<td><%= I18n.l(result.auction.ends_at) %></td>
<td><%= I18n.l(result.registration_due_date) %></td>
<td><%= result.status.humanize %></td>
<td>
<%- unless result.no_bids? %>
<%- unless result.offer.nil? %>
<%= result.offer.billing_profile&.name %>
<% end %>
<% end %>
</td>
<td></td>
</tr>
<% end %>
<%= render @results %>
<% end %>
<% end %>

<%= component 'common/pagy', pagy: @pagy %>
<% end %>

</div>
</div>

0 comments on commit 288621d

Please sign in to comment.