Skip to content

Instantly share code, notes, and snippets.

@Nerian
Created June 12, 2017 15:21
Show Gist options
  • Save Nerian/d1391d2773acfbffb3b0b131fc986e41 to your computer and use it in GitHub Desktop.
Save Nerian/d1391d2773acfbffb3b0b131fc986e41 to your computer and use it in GitHub Desktop.
class ChargesDatatable < Effective::Datatable
datatable do
order :date
col :description do |charge|
content_tag(:p, charge.description) +
content_tag(:p, link_to('Invoice', manager_accounting.invoice_path(current_space, charge.invoice)))
end
unless attributes[:member_id].present?
col :member_id, sort: false, label: 'Member', search: { as: :select, collection: members_collection } do |charge|
link_to charge.member.name, manager_accounting.member_path(current_space, charge.member)
end.search do |collection, term, column, sql_column|
collection.joins(:invoice).where(invoices: { member_id: term })
end
end
col :date, search: false do |charge|
l(charge.date, format: :long)
end
col :amount_cents, label: 'Amount' do |charge|
m(charge.amount)
end
col :payment_method, search: { as: :select, collection: Proc.new { current_space.payment_methods.joins(:charges).distinct } } do |charge|
charge.payment_method.name
end
col :status, sort: false, search: { as: :select, collection: status_options } do |charge|
charge.status.humanize
end
actions_col partial: 'datatables/charges/controls'
end
# def search_column(collection, table_column, search_term, sql_column)
# if table_column[:name] == 'member_id'
# collection.joins(:invoice).where(invoices: { member_id: search_term })
# elsif table_column[:name] == 'amount_cents'
# collection.where('(charges.amount_cents/100)::text LIKE ?', "%#{search_term}%")
# elsif table_column[:name] == 'status'
# case search_term
# when 'collected'
# collection.collected
# when 'pending'
# collection.pending
# when 'failed'
# collection.failed
# end
# else
# super
# end
# end
def members_collection
current_space.members.joins(invoices: :charges).distinct.map { |i| [i.name, i.id]}
end
collection do
charges = current_space.charges.includes(:payment_method, :invoice, invoice: :member)
if attributes[:member_id]
charges = charges.joins(:invoice).where(invoices: { member_id: attributes[:member_id] })
end
if attributes[:invoice_id]
charges = charges.joins(:invoice).where(invoices: { id: attributes[:invoice_id] })
end
charges
end
def status_options
[['collected', :collected], ['pending', :pending], ['failed', :failed]]
end
def current_space
@space ||= Space.find(attributes[:space_id].to_i)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment