Potential fix for one of the issues mentioned in #49

Reduces the number of SQL queries used on group_orders/index and
group_orders/archive, but adds extra  complexity to the code
This commit is contained in:
Florian Lentsch 2020-07-12 10:26:36 +02:00 committed by Patrick Gansterer
parent 602f663245
commit 5eb8abf431
7 changed files with 45 additions and 9 deletions

View file

@ -9,6 +9,8 @@ class GroupOrdersController < ApplicationController
# Index page. # Index page.
def index def index
@closed_orders_including_group_order = Order.closed.limit(5).ordergroup_group_orders_map(@ordergroup)
@finished_not_closed_orders_including_group_order = Order.finished_not_closed.ordergroup_group_orders_map(@ordergroup)
end end
def new def new
@ -56,6 +58,8 @@ class GroupOrdersController < ApplicationController
def archive def archive
# get only orders belonging to the ordergroup # get only orders belonging to the ordergroup
@closed_orders = Order.closed.page(params[:page]).per(10) @closed_orders = Order.closed.page(params[:page]).per(10)
@closed_orders_including_group_order = @closed_orders.ordergroup_group_orders_map(@ordergroup)
@finished_not_closed_orders_including_group_order = Order.finished_not_closed.ordergroup_group_orders_map(@ordergroup)
respond_to do |format| respond_to do |format|
format.html # archive.html.haml format.html # archive.html.haml

View file

@ -125,6 +125,20 @@ class Order < ApplicationRecord
self self
end end
# fetch current Order scope's records and map the current user's GroupOrders in (if any)
# (performance enhancement as opposed to fetching each GroupOrder separately from the view)
def self.ordergroup_group_orders_map(ordergroup)
orders = includes(:supplier)
group_orders = GroupOrder.where(ordergroup_id: ordergroup.id, order_id: orders.map(&:id))
group_orders_hash = Hash[group_orders.collect {|go| [go.order_id, go]}]
orders.map do |order|
{
order: order,
group_order: group_orders_hash[order.id]
}
end
end
# search GroupOrder of given Ordergroup # search GroupOrder of given Ordergroup
def group_order(ordergroup) def group_order(ordergroup)
group_orders.where(:ordergroup_id => ordergroup.id).first group_orders.where(:ordergroup_id => ordergroup.id).first

View file

@ -6,8 +6,9 @@
%th= heading_helper Order, :ends %th= heading_helper Order, :ends
%th= heading_helper GroupOrder, :price %th= heading_helper GroupOrder, :price
%tbody %tbody
- for order in orders - for order_hash in orders
- group_order = order.group_order(@ordergroup) # Get GroupOrder if possible - order = order_hash[:order]
- group_order = order_hash[:group_order]
- order_class = group_order ? "" : "color:grey" - order_class = group_order ? "" : "color:grey"
%tr{:class=> cycle('even', 'odd', :name => 'orders'), :style => order_class} %tr{:class=> cycle('even', 'odd', :name => 'orders'), :style => order_class}
%td= group_order.present? ? link_to(order.name, group_order_path(group_order)) : order.name %td= group_order.present? ? link_to(order.name, group_order_path(group_order)) : order.name

View file

@ -5,9 +5,9 @@
.row-fluid .row-fluid
.span6 .span6
%h2= t '.title_open' %h2= t '.title_open'
= render :partial => "orders", :locals => {:orders => Order.finished_not_closed, :pagination => false} = render partial: 'orders', locals: {orders: @finished_not_closed_orders_including_group_order, pagination: false}
.span6 .span6
%h2= t '.title_closed' %h2= t '.title_closed'
#closed_orders #closed_orders
= render :partial => "orders", :locals => {:orders => @closed_orders, :pagination => true} = render partial: 'orders', locals: {orders: @closed_orders_including_group_order, pagination: true}

View file

@ -1 +1 @@
$('#closed_orders').html('#{escape_javascript(render("orders", orders: @closed_orders, pagination: true))}'); $('#closed_orders').html('#{escape_javascript(render("orders", orders: @closed_orders_including_group_order, pagination: true))}');

View file

@ -21,19 +21,19 @@
= render :partial => "shared/open_orders", :locals => {:ordergroup => @ordergroup} = render :partial => "shared/open_orders", :locals => {:ordergroup => @ordergroup}
// finished orders // finished orders
- unless Order.finished_not_closed.empty? - unless @finished_not_closed_orders_including_group_order.empty?
%section %section
%h2= t '.finished_orders.title' %h2= t '.finished_orders.title'
= render :partial => "orders", :locals => {:orders => Order.finished_not_closed, :pagination => false} = render partial: 'orders', locals: {orders: @finished_not_closed_orders_including_group_order, pagination: false}
- if @ordergroup.value_of_finished_orders > 0 - if @ordergroup.value_of_finished_orders > 0
%p %p
= t('.finished_orders.total_sum') + ':' = t('.finished_orders.total_sum') + ':'
%b= number_to_currency(@ordergroup.value_of_finished_orders) %b= number_to_currency(@ordergroup.value_of_finished_orders)
// closed orders // closed orders
- unless Order.closed.empty? - unless @closed_orders_including_group_order.empty?
%section %section
%h2= t '.closed_orders.title' %h2= t '.closed_orders.title'
= render :partial => "orders", :locals => {:orders => Order.closed.limit(5), :pagination => false} = render partial: 'orders', locals: {orders: @closed_orders_including_group_order, pagination: false}
%br/ %br/
= link_to t('.closed_orders.more'), archive_group_orders_path = link_to t('.closed_orders.more'), archive_group_orders_path

View file

@ -83,4 +83,21 @@ describe Order do
end end
describe 'mapped to GroupOrders' do
let!(:user) { create :user, groups: [create(:ordergroup)] }
let!(:order) { create :order }
let!(:order2) { create :order }
let!(:go) { create :group_order, order: order, ordergroup: user.ordergroup }
it 'to map a user\'s GroupOrders to a list of Orders' do
orders = Order.ordergroup_group_orders_map(user.ordergroup)
expect(orders.length).to be 2
expect(orders[0][:order]).to have_attributes(id: order.id)
expect(orders[0][:group_order]).to have_attributes(id: go.id)
expect(orders[1][:order]).to have_attributes(id: order2.id)
expect(orders[1][:group_order]).to be_nil
end
end
end end