Update nonprofits/payments_controller to Rails 6 and jbuilder

This commit is contained in:
Eric 2020-05-27 10:28:08 -05:00
parent 4b01c2aeb9
commit 2854603d56
4 changed files with 80 additions and 73 deletions

View file

@ -42,6 +42,7 @@ module Nonprofits
def show
@nonprofit = current_nonprofit
@payment = @nonprofit.payments.find(params[:id])
render locals: {payment: @payment}
end # def show
def update

View file

@ -0,0 +1,78 @@
# frozen_string_literal: true
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
json.data do
json.extract! @payment, :id, :gross_amount,
:towards, :net_amount,
:fee_total, :date,
:refund_total, :kind
d_anonymous = @payment.donation.nil? ? false : @payment.donation.anonymous
json.consider_donation_anonymous (!!d_anonymous || !!@payment.supporter.anonymous)
json.charge do
json.extract! @payment.charge, :created_at, :id, :status
end
json.donation do
d = @payment.donation
json.extract! d, :designation, :dedication, :origin_url, :id, :comment
json.campaign do
c = d.campaign
json.extract! c, :name, :id
json.url campaign_locateable_url(c)
end if d.campaign
json.campaign_gift do
json.name(d.campaign_gifts.any? ? d.campaign_gifts.last.campaign_gift_option.name : nil)
end
json.event(d.event, partial: "events/event", as: :event) if d.event
json.recurring_donation do
rd = d.recurring_donation
json.extract! rd, :interval, :time_unit, :created_at
end if d.recurring_donation
end if @payment.donation
json.dispute do
dis =@payment.dispute
json.extract! dis, :id, :status, :reason
end if @payment.dispute
json.refund do
ref = @payment.refund
json.extract! ref, :reason, :comment, :disbursed
end if @payment.refund
json.offsite_payment do
off_p = @payment.offsite_payment
json.extract! off_p, :check_number, :kind
end if @payment.offsite_payment
json.ticket do
event = @payment.tickets.last.event
json.event do
json.extract! event, :name, :id
json.url event_locateable_url(event)
end
json.levels @payment.tickets.map { |t| "#{GetData.chain(t.ticket_level, :name)} (#{t.quantity}x)" }.join(', ')
json.discount @payment.tickets.map { |t| t.event_discount ? "#{t.event_discount.name} (#{t.event_discount.percent}%)" : nil }.compact.join(', ')
end if @payment&.tickets&.last&.event
json.tickets @payment.tickets do |t|
json.id t.id
json.ticket_level t.ticket_level, :name
end if @payment.tickets.any?
json.supporter do
json.extract! @payment.supporter, :name, :email, :city, :state_code, :address, :zip_code, :phone, :id, :country
end
json.nonprofit do
json.id @payment.nonprofit.id
end
end

View file

@ -1,72 +0,0 @@
# frozen_string_literal: true
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
object @payment => :data
attributes :gross_amount, :towards, :net_amount, :fee_total, :id, :date, :refund_total, :kind
node(:consider_donation_anonymous) do |p|
d_anonymous = p.donation.nil? ? false : p.donation.anonymous
!!d_anonymous || !!p.supporter.anonymous
end
child :charge do
attributes :created_at, :id, :status
end
child :donation, object_root: false do
attributes :designation, :dedication, :origin_url, :id, :comment
child :campaign, object_root: false do
attributes :name, :url, :id
end
node(:campaign_gift) { |d| { name: d.campaign_gifts.any? ? d.campaign_gifts.last.campaign_gift_option.name : nil } }
child :event, object_root: false do
attributes :name, :url, :id
end
child :recurring_donation, object_root: false do
attributes :interval, :time_unit, :created_at
end
end
child :dispute, object_root: false do
attributes :id, :status, :reason
end
child :refund do
attributes :reason, :comment, :disbursed
end
child :offsite_payment do
attributes :check_number, :kind
end
node(:ticket) do |payment|
event = payment&.tickets&.last&.event
h = {
event: { name: event&.name, url: event&.url, id: event&.id },
levels: payment.tickets.map { |t| "#{GetData.chain(t.ticket_level, :name)} (#{t.quantity}x)" }.join(', '),
discount: payment.tickets.map { |t| t.event_discount ? "#{t.event_discount.name} (#{t.event_discount.percent}%)" : nil }.compact.join(', ')
}
event ? h : nil
end
child :tickets, object_root: false do
attributes :id
child :ticket_level do
attributes :name
end
end
child :supporter do
attributes :name, :email, :city, :state_code, :address, :zip_code, :phone, :id, :country
end
child :nonprofit do
attributes :id
end

View file

@ -16,7 +16,7 @@ describe Nonprofits::PaymentsController, type: :controller do
end
describe 'show payments' do
include_context :open_to_np_associate, :get, :show, nonprofit_id: :__our_np, id: '1', without_json_view: true
include_context :open_to_np_associate, :get, :show, nonprofit_id: :__our_np, id: '1', with_status: 200
end
describe 'update' do