Created
November 19, 2019 19:00
-
-
Save Fishy49/06b1fcf098bf0db8165d30235aadafc5 to your computer and use it in GitHub Desktop.
STRING_AGG implementation of orders datatable - breaks the dang COUNT calls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Datatables::Orders < Datatables::Model[Order] | |
include Routeable | |
column :row_toggle, nil, title: '', class: 'text-center cursor-pointer' | |
column :line_item_search, nil, visible: false | |
column :invoice_number, ->(r) { link_to(r.invoice_number, order_path(r), remote: true) }, title: 'Order #' | |
column :entry_date, ->(r) { r.entry_date&.strftime('%m/%d/%Y') }, title: 'Order Date' | |
column :order_type | |
column :status_description, title: 'Status' | |
column :no_of_lines, title: 'Line Items' | |
column :customer_po, title: 'PO' | |
column :sidemark | |
column :ship_to_city, title: 'Ship to City' | |
column :ship_to_state, title: 'Ship to State' | |
column :order_total, ->(r) { number_to_currency(r.order_total) } | |
column :tracking_number, ->(r) { link_to_tracking_number(r.carrier, r.tracking_number) }, title: 'Tracking' | |
searchable :line_item_search | |
searchable :entry_date | |
searchable :order_type | |
searchable :status_description | |
searchable :no_of_lines | |
searchable :customer_po | |
searchable :sidemark | |
searchable :ship_to_city | |
searchable :ship_to_state | |
orderable :entry_date | |
orderable :order_type | |
orderable :status_description | |
orderable :no_of_lines | |
orderable :customer_po | |
orderable :sidemark | |
orderable :ship_to_city | |
orderable :ship_to_state | |
orderable :order_total | |
initial_order :entry_date, :desc | |
page_length 25 | |
SEPARATOR = Arel::Nodes.build_quoted(' ') | |
def initialize(params, account_id, filter = nil) | |
@orders = Order.arel_table | |
order_items_join = model.arel_table.join(@order_items, Arel::Nodes::OuterJoin).on( | |
@order_items[:order_id].eq(model.arel_table[:id]) | |
) | |
line_item_concat = Arel::Nodes::NamedFunction.new( | |
'concat', | |
[ | |
@order_items[:pattern_product], | |
SEPARATOR, | |
@order_items[:color_model], | |
SEPARATOR, | |
@order_items[:generic_description], | |
SEPARATOR, | |
@order_items[:product_sub_group], | |
SEPARATOR, | |
@order_items[:line_sidemark] | |
] | |
) | |
@line_item_agg = Arel::Nodes::NamedFunction.new( | |
'string_agg', | |
[ | |
line_item_concat, | |
SEPARATOR | |
] | |
) | |
books = model.where(account_id: account_id) | |
.joins(order_items_join.join_sources) | |
.select(model.arel_table[Arel.star], @line_item_agg) | |
.group('orders.id') | |
books = filter.apply(books) if filter.present? | |
super(params, books) | |
end | |
def render_row_at_column(view_context, row, column) | |
return super unless column.name == :row_toggle | |
view_context.tag.span(class: 'row-expand-toggle order-item-table-toggle btn btn-sm btn-light px-1 py-0 closed', data: { 'child-link': order_items_table_path(row.id) }) do | |
html = view_context.tag.i(class: 'fa fa-plus') | |
html << view_context.tag.i(class: 'fa fa-minus') | |
html | |
end | |
end | |
def search_line_item_search(search) | |
default_search_for_node(@line_item_agg, search) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment