From 6a7e065ccf0a9a32b70f4eb4e9e516ca91e4ffd8 Mon Sep 17 00:00:00 2001 From: Alan Marques Sousa Date: Wed, 9 Mar 2022 22:57:13 -0300 Subject: [PATCH 1/8] Implement jbuilder expansion on transactions controller Co-authored-by: MaiconMares --- app/controllers/api/api_controller.rb | 1 + .../api/transactions_controller.rb | 2 + .../controllers/api/jbuilder_expansions.rb | 170 ++++++++++++++++++ .../transactions/_transaction.json.jbuilder | 4 +- .../api/transactions/index.json.jbuilder | 2 +- .../object_events/_base.json.jbuilder | 5 + app/views/api/transactions/show.json.jbuilder | 2 +- 7 files changed, 183 insertions(+), 3 deletions(-) create mode 100644 app/controllers/concerns/controllers/api/jbuilder_expansions.rb create mode 100644 app/views/api/transactions/object_events/_base.json.jbuilder diff --git a/app/controllers/api/api_controller.rb b/app/controllers/api/api_controller.rb index bfa3464fa..02276f031 100644 --- a/app/controllers/api/api_controller.rb +++ b/app/controllers/api/api_controller.rb @@ -6,6 +6,7 @@ class Api::ApiController < ActionController::Base # rubocop:disable Rails/Applic # We disable Rails/ApplicationController bec include Controllers::Locale include Controllers::Nonprofit::Authorization + include Controllers::Api::JbuilderExpansions rescue_from ActiveRecord::RecordInvalid, with: :record_invalid_rescue rescue_from AuthenticationError, with: :unauthorized_rescue diff --git a/app/controllers/api/transactions_controller.rb b/app/controllers/api/transactions_controller.rb index f6ae2d574..7b96bb481 100644 --- a/app/controllers/api/transactions_controller.rb +++ b/app/controllers/api/transactions_controller.rb @@ -12,12 +12,14 @@ class Api::TransactionsController < Api::ApiController # Gets the nonprofits supporters # If not logged in, causes a 401 error def index + set_expansions('supporter', 'subtransaction.payments', 'transaction_assignments', 'payments') @transactions = current_nonprofit.transactions.order('created DESC').page(params[:page]).per(params[:per]) end # Gets the a single nonprofit supporter # If not logged in, causes a 401 error def show + set_expansions('supporter', 'subtransaction.payments', 'transaction_assignments', 'payments') @transaction = current_transaction end diff --git a/app/controllers/concerns/controllers/api/jbuilder_expansions.rb b/app/controllers/concerns/controllers/api/jbuilder_expansions.rb new file mode 100644 index 000000000..ee549ecf1 --- /dev/null +++ b/app/controllers/concerns/controllers/api/jbuilder_expansions.rb @@ -0,0 +1,170 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later +# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE +module Controllers::Api::JbuilderExpansions + extend ActiveSupport::Concern + included do + @__expand = Controllers::Api::JbuilderExpansions::ExpansionRequest.new + + def set_expansions(*expansions) + @__expand = Controllers::Api::JbuilderExpansions.set_expansions(*expansions) + end + + def request_expansions(*expansions) + Controllers::Api::JbuilderExpansions.set_expansions(*expansions) + end + + def handle_expansion(attribute, object, opts={}) + opts = opts.deep_symbolize_keys + opts[:__expand] ||= __expand + ExpansionRequestProcessor.handle_expansion(attribute, object, opts) + end + + def handle_array_expansion(attribute, object, opts={}, &block) + opts[:__expand] ||= __expand + ExpansionRequestProcessor.handle_array_expansion(attribute, object, opts, &block) + end + + def handle_item_expansion(object, opts={}) + opts[:__expand] ||= __expand + ExpansionRequestProcessor.handle_item_expansion(object, opts) + end + + helper_method :handle_expansion, :handle_array_expansion, :handle_item_expansion, :request_expansions + end + + def self.set_expansions(*expansions) + request = ExpansionRequest.new + expansions = expansions.flatten + if expansions.count == 1 + if expansions.first.is_a? String + request = ExpansionRequest.new(*expansions) + elsif expansions.first.is_a? ExpansionRequest + request = expansions.first + end + elsif expansions.any? + request = ExpansionRequest.new(*expansions) + end + + request + end + + class ExpansionRequestProcessor + attr_reader :attribute, :object + def initialize(attribute, object, opts) + @attribute = attribute + @object = object + @opts = opts.deep_symbolize_keys + end + + def json + @opts[:json] + end + + def exp_request + @opts[:__expand] + end + + def as + @opts[:as] || attribute + end + + def item_as + @opts[:item_as] || attribute + end + + def handle_expansion + if object.nil? + json.set! attribute, nil + else + if exp_request.expand? attribute + json.set! attribute do + json.partial! object, as: as, __expand: exp_request[attribute] + end + else + json.set! attribute, object&.id + end + end + end + + def handle_array_expansion + json.set! attribute do + if !exp_request.expand? attribute + json.array! object.map(&:id) + else + object.each do |item| + json.child! do + yield(item, {json: json, __expand: exp_request[attribute], as: item_as}) + end + end + end + end + end + + def handle_item_expansion + json.partial! object, as: as, __expand: exp_request + end + + def self.handle_expansion(attribute, object, opts) + ExpansionRequestProcessor.new(attribute, object, opts).handle_expansion + end + + def self.handle_array_expansion(attribute, object, opts, &block) + ExpansionRequestProcessor.new(attribute, object, opts).handle_array_expansion(&block) + end + + def self.handle_item_expansion(object, opts) + opts = opts.deep_symbolize_keys + ExpansionRequestProcessor.new(nil, object, opts).handle_item_expansion + end + end + + class ExpansionRequest + attr_accessor :path_tree + + def initialize(*paths) + @path_tree = Node.new + parse_paths(paths) + end + + def [](path) + unless @path_tree.leaf? + er = ExpansionRequest.create_from(@path_tree[path] || Node.new) + er + else + ExpansionRequest.new + end + end + + def expand?(path) + @path_tree.has_key?(path) + end + + class Node < ActiveSupport::HashWithIndifferentAccess + def leaf? + none? + end + end + + private + + + def parse_paths(paths=[]) + paths.each do |path| + working_tree = @path_tree + path.split('.').each do |path_part| + working_tree[path_part] = Node.new unless working_tree[path_part] + working_tree = working_tree[path_part] + end + end + end + + def self.create_from(tree) + er = ExpansionRequest.new() + er.path_tree = tree + er + end + + end +end \ No newline at end of file diff --git a/app/views/api/transactions/_transaction.json.jbuilder b/app/views/api/transactions/_transaction.json.jbuilder index 32e870df6..186be0bd4 100644 --- a/app/views/api/transactions/_transaction.json.jbuilder +++ b/app/views/api/transactions/_transaction.json.jbuilder @@ -8,7 +8,9 @@ json.object 'transaction' json.created transaction.created.to_i -json.supporter transaction.supporter.id +handle_expansion(:supporter, transaction.supporter, {json: json, __expand: __expand}) + +handle_expansion(:nonprofit, transaction.nonprofit, {json: json, __expand: __expand}) json.nonprofit transaction.nonprofit.id diff --git a/app/views/api/transactions/index.json.jbuilder b/app/views/api/transactions/index.json.jbuilder index 860ca9fb8..32be4f963 100644 --- a/app/views/api/transactions/index.json.jbuilder +++ b/app/views/api/transactions/index.json.jbuilder @@ -2,7 +2,7 @@ # License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later # Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE -json.data @transactions, partial: '/api/transactions/transaction', as: 'transaction' +json.data @transactions, partial: '/api/transactions/transaction', as: 'transaction', __expand: @__expand json.current_page @transactions.current_page json.first_page @transactions.first_page? diff --git a/app/views/api/transactions/object_events/_base.json.jbuilder b/app/views/api/transactions/object_events/_base.json.jbuilder new file mode 100644 index 000000000..03339184c --- /dev/null +++ b/app/views/api/transactions/object_events/_base.json.jbuilder @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later +# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE +json.partial! event_entity, as: :transaction, __expand: request_expansions(%w(subtransaction.payments transaction_assignments)) \ No newline at end of file diff --git a/app/views/api/transactions/show.json.jbuilder b/app/views/api/transactions/show.json.jbuilder index 4505a6023..0c0cbe28e 100644 --- a/app/views/api/transactions/show.json.jbuilder +++ b/app/views/api/transactions/show.json.jbuilder @@ -2,4 +2,4 @@ # License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later # Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE -json.partial! @transaction, as: :transaction +json.partial! @transaction, as: :transaction, __expand: @__expand From 73a49e57d476646116565f787708387440d989b1 Mon Sep 17 00:00:00 2001 From: Alan Marques Sousa Date: Sun, 20 Mar 2022 19:58:45 -0300 Subject: [PATCH 2/8] Adding jbuilder partials and method to handle Amount Co-authored-by: MaiconMares --- app/models/modern_donation.rb | 4 ++++ .../_modern_donation.json.jbuilder | 13 +++++++++++++ .../object_events/_base.json.jbuilder | 12 ++++++++++++ .../object_events/_object_event.json.jbuilder | 15 +++++++++++++++ .../api/object_events/generate.json.jbuilder | 5 +++++ .../api/object_events/index.json.jbuilder | 13 +++++++++++++ .../object_events/_base.json.jbuilder | 12 ++++++++++++ .../_simple_object.json.jbuilder | 16 ++++++++++++++++ .../object_events/_base.json.jbuilder | 6 ++++++ .../api/simple_objects/show.json.jbuilder | 1 + .../_stripe_transaction_charge.json.jbuilder | 18 ++++++++++++++++++ .../object_events/_base.json.jbuilder | 11 +++++++++++ ..._transaction_dispute_reversal.json.jbuilder | 18 ++++++++++++++++++ .../object_events/_base.json.jbuilder | 12 ++++++++++++ .../_stripe_transaction_dispute.json.jbuilder | 18 ++++++++++++++++++ .../object_events/_base.json.jbuilder | 12 ++++++++++++ .../_stripe_transaction_refund.json.jbuilder | 18 ++++++++++++++++++ .../object_events/_base.json.jbuilder | 12 ++++++++++++ .../_stripe_transaction.json.jbuilder | 16 ++++++++++++++++ 19 files changed, 232 insertions(+) create mode 100644 app/views/api/modern_donations/_modern_donation.json.jbuilder create mode 100644 app/views/api/modern_donations/object_events/_base.json.jbuilder create mode 100644 app/views/api/object_events/_object_event.json.jbuilder create mode 100644 app/views/api/object_events/generate.json.jbuilder create mode 100644 app/views/api/object_events/index.json.jbuilder create mode 100644 app/views/api/offline_transaction_charges/object_events/_base.json.jbuilder create mode 100644 app/views/api/simple_objects/_simple_object.json.jbuilder create mode 100644 app/views/api/simple_objects/object_events/_base.json.jbuilder create mode 100644 app/views/api/simple_objects/show.json.jbuilder create mode 100644 app/views/api/stripe_transaction_charges/_stripe_transaction_charge.json.jbuilder create mode 100644 app/views/api/stripe_transaction_charges/object_events/_base.json.jbuilder create mode 100644 app/views/api/stripe_transaction_dispute_reversals/_stripe_transaction_dispute_reversal.json.jbuilder create mode 100644 app/views/api/stripe_transaction_dispute_reversals/object_events/_base.json.jbuilder create mode 100644 app/views/api/stripe_transaction_disputes/_stripe_transaction_dispute.json.jbuilder create mode 100644 app/views/api/stripe_transaction_disputes/object_events/_base.json.jbuilder create mode 100644 app/views/api/stripe_transaction_refunds/_stripe_transaction_refund.json.jbuilder create mode 100644 app/views/api/stripe_transaction_refunds/object_events/_base.json.jbuilder create mode 100644 app/views/api/stripe_transactions/_stripe_transaction.json.jbuilder diff --git a/app/models/modern_donation.rb b/app/models/modern_donation.rb index 3e184ad2f..93a29f9ed 100644 --- a/app/models/modern_donation.rb +++ b/app/models/modern_donation.rb @@ -56,4 +56,8 @@ def publish_updated Houdini.event_publisher.announce(:donation_updated, to_event('donation.updated', :nonprofit, :supporter, :trx).attributes!) Houdini.event_publisher.announce(:trx_assignment_updated, to_event('trx_assignment.updated', :nonprofit, :supporter, :trx).attributes!) end + + def amount_as_money + Amount.new(amount||0, nonprofit.currency) + end end diff --git a/app/views/api/modern_donations/_modern_donation.json.jbuilder b/app/views/api/modern_donations/_modern_donation.json.jbuilder new file mode 100644 index 000000000..ed04374eb --- /dev/null +++ b/app/views/api/modern_donations/_modern_donation.json.jbuilder @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later +# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE + +json.object 'donation' + + +json.(assignable, :designation) + +json.amount do + json.partial! '/api/common/amount', amount: assignable.amount_as_money +end diff --git a/app/views/api/modern_donations/object_events/_base.json.jbuilder b/app/views/api/modern_donations/object_events/_base.json.jbuilder new file mode 100644 index 000000000..9de5b69d1 --- /dev/null +++ b/app/views/api/modern_donations/object_events/_base.json.jbuilder @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later +# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE + +json.partial! 'api_new/transaction_assignments/transaction_assignment', + transaction_assignment: event_entity.transaction_assignment, + __expand: request_expansions( + 'transaction', + 'transaction.transaction_assignments', + 'transaction.subtransaction.payments' + ) \ No newline at end of file diff --git a/app/views/api/object_events/_object_event.json.jbuilder b/app/views/api/object_events/_object_event.json.jbuilder new file mode 100644 index 000000000..9d0ba3c5c --- /dev/null +++ b/app/views/api/object_events/_object_event.json.jbuilder @@ -0,0 +1,15 @@ + +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later +# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE + +json.id object_event.houid +json.created object_event.created.to_i +json.object 'object_event' +json.type object_event.event_type +json.data do + json.object do + json.partial! partial_path, event_entity: object_event.event_entity + end +end \ No newline at end of file diff --git a/app/views/api/object_events/generate.json.jbuilder b/app/views/api/object_events/generate.json.jbuilder new file mode 100644 index 000000000..46fc6ca1e --- /dev/null +++ b/app/views/api/object_events/generate.json.jbuilder @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later +# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE +json.partial! @object_event, as: :object_event, object_as: @object_as, partial_path: @partial_path diff --git a/app/views/api/object_events/index.json.jbuilder b/app/views/api/object_events/index.json.jbuilder new file mode 100644 index 000000000..3fb0e5ebe --- /dev/null +++ b/app/views/api/object_events/index.json.jbuilder @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later +# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE +json.data do + json.array! @object_events.map(&:object_json) +end + +json.current_page @object_events.current_page +json.first_page @object_events.first_page? +json.last_page@object_events.last_page? +json.requested_size @object_events.limit_value +json.total_count @object_events.total_count \ No newline at end of file diff --git a/app/views/api/offline_transaction_charges/object_events/_base.json.jbuilder b/app/views/api/offline_transaction_charges/object_events/_base.json.jbuilder new file mode 100644 index 000000000..80a9b2ca0 --- /dev/null +++ b/app/views/api/offline_transaction_charges/object_events/_base.json.jbuilder @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later +# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE + +json.partial! 'api_new/subtransaction_payments/subtransaction_payment', + subtransaction_payment: event_entity.subtransaction_payment, + __expand: request_expansions( + 'subtransaction', + 'subtransaction.transaction', + 'subtransaction.transaction.transaction_assignments' + ) \ No newline at end of file diff --git a/app/views/api/simple_objects/_simple_object.json.jbuilder b/app/views/api/simple_objects/_simple_object.json.jbuilder new file mode 100644 index 000000000..0688482e8 --- /dev/null +++ b/app/views/api/simple_objects/_simple_object.json.jbuilder @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later +# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE + +json.id object.houid + +json.object 'simple_object' + +handle_expansion(:parent, object.parent, {json: json, as: :object, __expand: __expand}) + +handle_expansion(:nonprofit, object.nonprofit, {json: json, __expand: __expand}) + +handle_array_expansion(:friends, object.friends, {json: json, item_as: :object, __expand: __expand}) do |friend, opts| + handle_item_expansion(friend, opts) +end diff --git a/app/views/api/simple_objects/object_events/_base.json.jbuilder b/app/views/api/simple_objects/object_events/_base.json.jbuilder new file mode 100644 index 000000000..6e63ab363 --- /dev/null +++ b/app/views/api/simple_objects/object_events/_base.json.jbuilder @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later +# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE + +json.partial! event_entity, as: :object, __expand: request_expansions('parent') \ No newline at end of file diff --git a/app/views/api/simple_objects/show.json.jbuilder b/app/views/api/simple_objects/show.json.jbuilder new file mode 100644 index 000000000..85877155c --- /dev/null +++ b/app/views/api/simple_objects/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! @simple_object, as: :object, __expand: @__expand \ No newline at end of file diff --git a/app/views/api/stripe_transaction_charges/_stripe_transaction_charge.json.jbuilder b/app/views/api/stripe_transaction_charges/_stripe_transaction_charge.json.jbuilder new file mode 100644 index 000000000..4c11ebfd5 --- /dev/null +++ b/app/views/api/stripe_transaction_charges/_stripe_transaction_charge.json.jbuilder @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later +# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE + +json.object 'stripe_transaction_charge' + +json.net_amount do + json.partial! '/api_new/common/amount', amount: paymentable.net_amount_as_money +end + +json.gross_amount do + json.partial! '/api_new/common/amount', amount: paymentable.gross_amount_as_money +end + +json.fee_total do + json.partial! '/api_new/common/amount', amount: paymentable.fee_total_as_money +end diff --git a/app/views/api/stripe_transaction_charges/object_events/_base.json.jbuilder b/app/views/api/stripe_transaction_charges/object_events/_base.json.jbuilder new file mode 100644 index 000000000..9c5603a6b --- /dev/null +++ b/app/views/api/stripe_transaction_charges/object_events/_base.json.jbuilder @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later +# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE + +json.partial! 'api_new/subtransaction_payments/subtransaction_payment', + subtransaction_payment: event_entity.subtransaction_payment, + __expand: request_expansions( + 'subtransaction.payments', + 'subtransaction.transaction.transaction_assignments' + ) \ No newline at end of file diff --git a/app/views/api/stripe_transaction_dispute_reversals/_stripe_transaction_dispute_reversal.json.jbuilder b/app/views/api/stripe_transaction_dispute_reversals/_stripe_transaction_dispute_reversal.json.jbuilder new file mode 100644 index 000000000..0f51e753c --- /dev/null +++ b/app/views/api/stripe_transaction_dispute_reversals/_stripe_transaction_dispute_reversal.json.jbuilder @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later +# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE + +json.object 'stripe_transaction_dispute_reversal' + +json.net_amount do + json.partial! '/api_new/common/amount', amount: paymentable.net_amount_as_money +end + +json.gross_amount do + json.partial! '/api_new/common/amount', amount: paymentable.gross_amount_as_money +end + +json.fee_total do + json.partial! '/api_new/common/amount', amount: paymentable.fee_total_as_money +end diff --git a/app/views/api/stripe_transaction_dispute_reversals/object_events/_base.json.jbuilder b/app/views/api/stripe_transaction_dispute_reversals/object_events/_base.json.jbuilder new file mode 100644 index 000000000..80a9b2ca0 --- /dev/null +++ b/app/views/api/stripe_transaction_dispute_reversals/object_events/_base.json.jbuilder @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later +# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE + +json.partial! 'api_new/subtransaction_payments/subtransaction_payment', + subtransaction_payment: event_entity.subtransaction_payment, + __expand: request_expansions( + 'subtransaction', + 'subtransaction.transaction', + 'subtransaction.transaction.transaction_assignments' + ) \ No newline at end of file diff --git a/app/views/api/stripe_transaction_disputes/_stripe_transaction_dispute.json.jbuilder b/app/views/api/stripe_transaction_disputes/_stripe_transaction_dispute.json.jbuilder new file mode 100644 index 000000000..01db54d26 --- /dev/null +++ b/app/views/api/stripe_transaction_disputes/_stripe_transaction_dispute.json.jbuilder @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later +# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE + +json.object 'stripe_transaction_dispute' + +json.net_amount do + json.partial! '/api_new/common/amount', amount: paymentable.net_amount_as_money +end + +json.gross_amount do + json.partial! '/api_new/common/amount', amount: paymentable.gross_amount_as_money +end + +json.fee_total do + json.partial! '/api_new/common/amount', amount: paymentable.fee_total_as_money +end diff --git a/app/views/api/stripe_transaction_disputes/object_events/_base.json.jbuilder b/app/views/api/stripe_transaction_disputes/object_events/_base.json.jbuilder new file mode 100644 index 000000000..80a9b2ca0 --- /dev/null +++ b/app/views/api/stripe_transaction_disputes/object_events/_base.json.jbuilder @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later +# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE + +json.partial! 'api_new/subtransaction_payments/subtransaction_payment', + subtransaction_payment: event_entity.subtransaction_payment, + __expand: request_expansions( + 'subtransaction', + 'subtransaction.transaction', + 'subtransaction.transaction.transaction_assignments' + ) \ No newline at end of file diff --git a/app/views/api/stripe_transaction_refunds/_stripe_transaction_refund.json.jbuilder b/app/views/api/stripe_transaction_refunds/_stripe_transaction_refund.json.jbuilder new file mode 100644 index 000000000..7c1478ac9 --- /dev/null +++ b/app/views/api/stripe_transaction_refunds/_stripe_transaction_refund.json.jbuilder @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later +# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE + +json.object 'stripe_transaction_refund' + +json.net_amount do + json.partial! '/api_new/common/amount', amount: paymentable.net_amount_as_money +end + +json.gross_amount do + json.partial! '/api_new/common/amount', amount: paymentable.gross_amount_as_money +end + +json.fee_total do + json.partial! '/api_new/common/amount', amount: paymentable.fee_total_as_money +end diff --git a/app/views/api/stripe_transaction_refunds/object_events/_base.json.jbuilder b/app/views/api/stripe_transaction_refunds/object_events/_base.json.jbuilder new file mode 100644 index 000000000..80a9b2ca0 --- /dev/null +++ b/app/views/api/stripe_transaction_refunds/object_events/_base.json.jbuilder @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later +# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE + +json.partial! 'api_new/subtransaction_payments/subtransaction_payment', + subtransaction_payment: event_entity.subtransaction_payment, + __expand: request_expansions( + 'subtransaction', + 'subtransaction.transaction', + 'subtransaction.transaction.transaction_assignments' + ) \ No newline at end of file diff --git a/app/views/api/stripe_transactions/_stripe_transaction.json.jbuilder b/app/views/api/stripe_transactions/_stripe_transaction.json.jbuilder new file mode 100644 index 000000000..225ce91fa --- /dev/null +++ b/app/views/api/stripe_transactions/_stripe_transaction.json.jbuilder @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later +# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE + +json.object 'stripe_transaction' + +json.created subtransactable.created.to_i + +json.amount do + json.partial! '/api_new/common/amount', amount: subtransactable.amount_as_money +end + +json.net_amount do + json.partial! '/api_new/common/amount', amount: subtransactable.net_amount_as_money +end From 302946113bc99dc1be9a8cec6465483c01373ef4 Mon Sep 17 00:00:00 2001 From: Alan Marques Sousa Date: Sun, 20 Mar 2022 20:00:06 -0300 Subject: [PATCH 3/8] Expanding subtransaction, transaction and transaction_assignment Co-authored-by: MaiconMares --- .../subtransactions/_subtransaction.json.jbuilder | 4 ++-- .../_transaction_assignment.json.jbuilder | 14 ++++++++++++++ .../api/transactions/_transaction.json.jbuilder | 6 +++--- 3 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 app/views/api/transaction_assignments/_transaction_assignment.json.jbuilder diff --git a/app/views/api/subtransactions/_subtransaction.json.jbuilder b/app/views/api/subtransactions/_subtransaction.json.jbuilder index 5857bd2ba..fdbb79073 100644 --- a/app/views/api/subtransactions/_subtransaction.json.jbuilder +++ b/app/views/api/subtransactions/_subtransaction.json.jbuilder @@ -2,17 +2,17 @@ # License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later # Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE -json.(subtransaction.subtransactable, :id) json.type 'subtransaction' json.supporter subtransaction.supporter.id json.nonprofit subtransaction.nonprofit.id json.transaction subtransaction.trx.id -json.partial! subtransaction.subtransactable, as: :subtransactable json.payments subtransaction.payments do |py| json.partial! py, as: :subtransaction_payment end json.url api_nonprofit_transaction_subtransaction_url(subtransaction.nonprofit, subtransaction.trx) + +json.partial! subtransaction.subtransactable, as: :subtransactable, __expand: __expand \ No newline at end of file diff --git a/app/views/api/transaction_assignments/_transaction_assignment.json.jbuilder b/app/views/api/transaction_assignments/_transaction_assignment.json.jbuilder new file mode 100644 index 000000000..53e8fc68e --- /dev/null +++ b/app/views/api/transaction_assignments/_transaction_assignment.json.jbuilder @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later +# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE +json.type 'trx_assignment' + +json.id transaction_assignment.id + +handle_expansion(:supporter, transaction_assignment.supporter, {json: json, __expand: __expand}) +handle_expansion(:nonprofit, transaction_assignment.nonprofit, {json: json, __expand: __expand}) +handle_expansion(:transaction, transaction_assignment.trx, {json: json, __expand: __expand}) + + +json.partial! transaction_assignment.assignable, as: :assignable, __expand: __expand \ No newline at end of file diff --git a/app/views/api/transactions/_transaction.json.jbuilder b/app/views/api/transactions/_transaction.json.jbuilder index 186be0bd4..6fb027215 100644 --- a/app/views/api/transactions/_transaction.json.jbuilder +++ b/app/views/api/transactions/_transaction.json.jbuilder @@ -18,10 +18,10 @@ json.amount do json.partial! '/api/common/amount', amount: transaction.amount_as_money end -json.subtransaction transaction&.subtransaction&.to_id +handle_expansion(:subtransaction, transaction.subtransaction, {json: json, __expand: __expand}) -json.transaction_assignments transaction.transaction_assignments do |tra| - json.merge! tra.to_id.attributes! +handle_array_expansion(:transaction_assignments, transaction.transaction_assignments, {json: json, __expand: __expand, item_as: :transaction_assignment}) do |tra, opts| + handle_item_expansion(tra, opts) end json.payments transaction.payments do |subt_p| From 805743a38ae912dd98e74bedd806e6f04e7a1ff7 Mon Sep 17 00:00:00 2001 From: Alan Marques Sousa Date: Thu, 24 Mar 2022 09:35:44 -0300 Subject: [PATCH 4/8] Rubocop issue fix Co-authored-by: MaiconMares --- .../controllers/api/jbuilder_expansions.rb | 327 +++++++++--------- .../_modern_donation.json.jbuilder | 1 - .../object_events/_base.json.jbuilder | 12 +- .../object_events/_object_event.json.jbuilder | 9 +- .../api/object_events/index.json.jbuilder | 8 +- .../object_events/_base.json.jbuilder | 12 +- .../_simple_object.json.jbuilder | 8 +- .../object_events/_base.json.jbuilder | 2 +- .../api/simple_objects/show.json.jbuilder | 6 +- .../object_events/_base.json.jbuilder | 10 +- .../object_events/_base.json.jbuilder | 12 +- .../object_events/_base.json.jbuilder | 12 +- .../object_events/_base.json.jbuilder | 12 +- .../_subtransaction.json.jbuilder | 2 +- .../_transaction_assignment.json.jbuilder | 9 +- .../transactions/_transaction.json.jbuilder | 9 +- .../object_events/_base.json.jbuilder | 3 +- 17 files changed, 227 insertions(+), 227 deletions(-) diff --git a/app/controllers/concerns/controllers/api/jbuilder_expansions.rb b/app/controllers/concerns/controllers/api/jbuilder_expansions.rb index ee549ecf1..1bfb69347 100644 --- a/app/controllers/concerns/controllers/api/jbuilder_expansions.rb +++ b/app/controllers/concerns/controllers/api/jbuilder_expansions.rb @@ -3,168 +3,165 @@ # License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later # Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE module Controllers::Api::JbuilderExpansions - extend ActiveSupport::Concern - included do - @__expand = Controllers::Api::JbuilderExpansions::ExpansionRequest.new - - def set_expansions(*expansions) - @__expand = Controllers::Api::JbuilderExpansions.set_expansions(*expansions) - end - - def request_expansions(*expansions) - Controllers::Api::JbuilderExpansions.set_expansions(*expansions) - end - - def handle_expansion(attribute, object, opts={}) - opts = opts.deep_symbolize_keys - opts[:__expand] ||= __expand - ExpansionRequestProcessor.handle_expansion(attribute, object, opts) - end - - def handle_array_expansion(attribute, object, opts={}, &block) - opts[:__expand] ||= __expand - ExpansionRequestProcessor.handle_array_expansion(attribute, object, opts, &block) - end - - def handle_item_expansion(object, opts={}) - opts[:__expand] ||= __expand - ExpansionRequestProcessor.handle_item_expansion(object, opts) - end - - helper_method :handle_expansion, :handle_array_expansion, :handle_item_expansion, :request_expansions - end - - def self.set_expansions(*expansions) - request = ExpansionRequest.new - expansions = expansions.flatten - if expansions.count == 1 - if expansions.first.is_a? String - request = ExpansionRequest.new(*expansions) - elsif expansions.first.is_a? ExpansionRequest - request = expansions.first - end - elsif expansions.any? - request = ExpansionRequest.new(*expansions) - end - - request - end - - class ExpansionRequestProcessor - attr_reader :attribute, :object - def initialize(attribute, object, opts) - @attribute = attribute - @object = object - @opts = opts.deep_symbolize_keys - end - - def json - @opts[:json] - end - - def exp_request - @opts[:__expand] - end - - def as - @opts[:as] || attribute - end - - def item_as - @opts[:item_as] || attribute - end - - def handle_expansion - if object.nil? - json.set! attribute, nil - else - if exp_request.expand? attribute - json.set! attribute do - json.partial! object, as: as, __expand: exp_request[attribute] - end - else - json.set! attribute, object&.id - end - end - end - - def handle_array_expansion - json.set! attribute do - if !exp_request.expand? attribute - json.array! object.map(&:id) - else - object.each do |item| - json.child! do - yield(item, {json: json, __expand: exp_request[attribute], as: item_as}) - end - end - end - end - end - - def handle_item_expansion - json.partial! object, as: as, __expand: exp_request - end - - def self.handle_expansion(attribute, object, opts) - ExpansionRequestProcessor.new(attribute, object, opts).handle_expansion - end - - def self.handle_array_expansion(attribute, object, opts, &block) - ExpansionRequestProcessor.new(attribute, object, opts).handle_array_expansion(&block) - end - - def self.handle_item_expansion(object, opts) - opts = opts.deep_symbolize_keys - ExpansionRequestProcessor.new(nil, object, opts).handle_item_expansion - end - end - - class ExpansionRequest - attr_accessor :path_tree - - def initialize(*paths) - @path_tree = Node.new - parse_paths(paths) - end - - def [](path) - unless @path_tree.leaf? - er = ExpansionRequest.create_from(@path_tree[path] || Node.new) - er - else - ExpansionRequest.new - end - end - - def expand?(path) - @path_tree.has_key?(path) - end - - class Node < ActiveSupport::HashWithIndifferentAccess - def leaf? - none? - end - end - - private - - - def parse_paths(paths=[]) - paths.each do |path| - working_tree = @path_tree - path.split('.').each do |path_part| - working_tree[path_part] = Node.new unless working_tree[path_part] - working_tree = working_tree[path_part] - end - end - end - - def self.create_from(tree) - er = ExpansionRequest.new() - er.path_tree = tree - er - end - - end -end \ No newline at end of file + extend ActiveSupport::Concern + included do + @__expand = Controllers::Api::JbuilderExpansions::ExpansionRequest.new + + def set_expansions(*expansions) + @__expand = Controllers::Api::JbuilderExpansions.set_expansions(*expansions) + end + + def request_expansions(*expansions) + Controllers::Api::JbuilderExpansions.set_expansions(*expansions) + end + + def handle_expansion(attribute, object, opts = {}) + opts = opts.deep_symbolize_keys + opts[:__expand] ||= __expand + ExpansionRequestProcessor.handle_expansion(attribute, object, opts) + end + + def handle_array_expansion(attribute, object, opts = {}, &block) + opts[:__expand] ||= __expand + ExpansionRequestProcessor.handle_array_expansion(attribute, object, opts, &block) + end + + def handle_item_expansion(object, opts = {}) + opts[:__expand] ||= __expand + ExpansionRequestProcessor.handle_item_expansion(object, opts) + end + + helper_method :handle_expansion, :handle_array_expansion, :handle_item_expansion, :request_expansions + end + + def self.set_expansions(*expansions) + request = ExpansionRequest.new + expansions = expansions.flatten + if expansions.count == 1 + if expansions.first.is_a? String + request = ExpansionRequest.new(*expansions) + elsif expansions.first.is_a? ExpansionRequest + request = expansions.first + end + elsif expansions.any? + request = ExpansionRequest.new(*expansions) + end + + request + end + + class ExpansionRequestProcessor + attr_reader :attribute, :object + + def initialize(attribute, object, opts) + @attribute = attribute + @object = object + @opts = opts.deep_symbolize_keys + end + + def json + @opts[:json] + end + + def exp_request + @opts[:__expand] + end + + def as + @opts[:as] || attribute + end + + def item_as + @opts[:item_as] || attribute + end + + def handle_expansion + if object.nil? + json.set! attribute, nil + elsif exp_request.expand? attribute + json.set! attribute do + json.partial! object, as: as, __expand: exp_request[attribute] + end + else + json.set! attribute, object&.id + end + end + + def handle_array_expansion + json.set! attribute do + if exp_request.expand? attribute + object.each do |item| + json.child! do + yield(item, { json: json, __expand: exp_request[attribute], as: item_as }) + end + end + else + json.array! object.map(&:id) + end + end + end + + def handle_item_expansion + json.partial! object, as: as, __expand: exp_request + end + + def self.handle_expansion(attribute, object, opts) + ExpansionRequestProcessor.new(attribute, object, opts).handle_expansion + end + + def self.handle_array_expansion(attribute, object, opts, &block) + ExpansionRequestProcessor.new(attribute, object, opts).handle_array_expansion(&block) + end + + def self.handle_item_expansion(object, opts) + opts = opts.deep_symbolize_keys + ExpansionRequestProcessor.new(nil, object, opts).handle_item_expansion + end + end + + class ExpansionRequest + attr_accessor :path_tree + + def initialize(*paths) + @path_tree = Node.new + parse_paths(paths) + end + + def [](path) + if @path_tree.leaf? + ExpansionRequest.new + else + ExpansionRequest.create_from(@path_tree[path] || Node.new) + + end + end + + def expand?(path) + @path_tree.has_key?(path) + end + + class Node < ActiveSupport::HashWithIndifferentAccess + def leaf? + none? + end + end + + private + + def parse_paths(paths = []) + paths.each do |path| + working_tree = @path_tree + path.split('.').each do |path_part| + working_tree[path_part] = Node.new unless working_tree[path_part] + working_tree = working_tree[path_part] + end + end + end + + def self.create_from(tree) + er = ExpansionRequest.new + er.path_tree = tree + er + end + end +end diff --git a/app/views/api/modern_donations/_modern_donation.json.jbuilder b/app/views/api/modern_donations/_modern_donation.json.jbuilder index ed04374eb..442bb7e99 100644 --- a/app/views/api/modern_donations/_modern_donation.json.jbuilder +++ b/app/views/api/modern_donations/_modern_donation.json.jbuilder @@ -5,7 +5,6 @@ json.object 'donation' - json.(assignable, :designation) json.amount do diff --git a/app/views/api/modern_donations/object_events/_base.json.jbuilder b/app/views/api/modern_donations/object_events/_base.json.jbuilder index 9de5b69d1..52824108a 100644 --- a/app/views/api/modern_donations/object_events/_base.json.jbuilder +++ b/app/views/api/modern_donations/object_events/_base.json.jbuilder @@ -4,9 +4,9 @@ # Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE json.partial! 'api_new/transaction_assignments/transaction_assignment', - transaction_assignment: event_entity.transaction_assignment, - __expand: request_expansions( - 'transaction', - 'transaction.transaction_assignments', - 'transaction.subtransaction.payments' - ) \ No newline at end of file + transaction_assignment: event_entity.transaction_assignment, + __expand: request_expansions( + 'transaction', + 'transaction.transaction_assignments', + 'transaction.subtransaction.payments' + ) diff --git a/app/views/api/object_events/_object_event.json.jbuilder b/app/views/api/object_events/_object_event.json.jbuilder index 9d0ba3c5c..d125d12bc 100644 --- a/app/views/api/object_events/_object_event.json.jbuilder +++ b/app/views/api/object_events/_object_event.json.jbuilder @@ -1,4 +1,3 @@ - # frozen_string_literal: true # License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later @@ -9,7 +8,7 @@ json.created object_event.created.to_i json.object 'object_event' json.type object_event.event_type json.data do - json.object do - json.partial! partial_path, event_entity: object_event.event_entity - end -end \ No newline at end of file + json.object do + json.partial! partial_path, event_entity: object_event.event_entity + end +end diff --git a/app/views/api/object_events/index.json.jbuilder b/app/views/api/object_events/index.json.jbuilder index 3fb0e5ebe..2c1994187 100644 --- a/app/views/api/object_events/index.json.jbuilder +++ b/app/views/api/object_events/index.json.jbuilder @@ -2,12 +2,12 @@ # License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later # Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE -json.data do - json.array! @object_events.map(&:object_json) +json.data do + json.array! @object_events.map(&:object_json) end json.current_page @object_events.current_page json.first_page @object_events.first_page? -json.last_page@object_events.last_page? +json.last_page @object_events.last_page? json.requested_size @object_events.limit_value -json.total_count @object_events.total_count \ No newline at end of file +json.total_count @object_events.total_count diff --git a/app/views/api/offline_transaction_charges/object_events/_base.json.jbuilder b/app/views/api/offline_transaction_charges/object_events/_base.json.jbuilder index 80a9b2ca0..ab81c998d 100644 --- a/app/views/api/offline_transaction_charges/object_events/_base.json.jbuilder +++ b/app/views/api/offline_transaction_charges/object_events/_base.json.jbuilder @@ -4,9 +4,9 @@ # Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE json.partial! 'api_new/subtransaction_payments/subtransaction_payment', - subtransaction_payment: event_entity.subtransaction_payment, - __expand: request_expansions( - 'subtransaction', - 'subtransaction.transaction', - 'subtransaction.transaction.transaction_assignments' - ) \ No newline at end of file + subtransaction_payment: event_entity.subtransaction_payment, + __expand: request_expansions( + 'subtransaction', + 'subtransaction.transaction', + 'subtransaction.transaction.transaction_assignments' + ) diff --git a/app/views/api/simple_objects/_simple_object.json.jbuilder b/app/views/api/simple_objects/_simple_object.json.jbuilder index 0688482e8..810f68d1b 100644 --- a/app/views/api/simple_objects/_simple_object.json.jbuilder +++ b/app/views/api/simple_objects/_simple_object.json.jbuilder @@ -7,10 +7,10 @@ json.id object.houid json.object 'simple_object' -handle_expansion(:parent, object.parent, {json: json, as: :object, __expand: __expand}) +handle_expansion(:parent, object.parent, { json: json, as: :object, __expand: __expand }) -handle_expansion(:nonprofit, object.nonprofit, {json: json, __expand: __expand}) +handle_expansion(:nonprofit, object.nonprofit, { json: json, __expand: __expand }) -handle_array_expansion(:friends, object.friends, {json: json, item_as: :object, __expand: __expand}) do |friend, opts| - handle_item_expansion(friend, opts) +handle_array_expansion(:friends, object.friends, { json: json, item_as: :object, __expand: __expand }) do |friend, opts| + handle_item_expansion(friend, opts) end diff --git a/app/views/api/simple_objects/object_events/_base.json.jbuilder b/app/views/api/simple_objects/object_events/_base.json.jbuilder index 6e63ab363..3216769ca 100644 --- a/app/views/api/simple_objects/object_events/_base.json.jbuilder +++ b/app/views/api/simple_objects/object_events/_base.json.jbuilder @@ -3,4 +3,4 @@ # License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later # Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE -json.partial! event_entity, as: :object, __expand: request_expansions('parent') \ No newline at end of file +json.partial! event_entity, as: :object, __expand: request_expansions('parent') diff --git a/app/views/api/simple_objects/show.json.jbuilder b/app/views/api/simple_objects/show.json.jbuilder index 85877155c..3207a15fc 100644 --- a/app/views/api/simple_objects/show.json.jbuilder +++ b/app/views/api/simple_objects/show.json.jbuilder @@ -1 +1,5 @@ -json.partial! @simple_object, as: :object, __expand: @__expand \ No newline at end of file +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later +# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE +json.partial! @simple_object, as: :object, __expand: @__expand diff --git a/app/views/api/stripe_transaction_charges/object_events/_base.json.jbuilder b/app/views/api/stripe_transaction_charges/object_events/_base.json.jbuilder index 9c5603a6b..019b1b4e4 100644 --- a/app/views/api/stripe_transaction_charges/object_events/_base.json.jbuilder +++ b/app/views/api/stripe_transaction_charges/object_events/_base.json.jbuilder @@ -4,8 +4,8 @@ # Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE json.partial! 'api_new/subtransaction_payments/subtransaction_payment', - subtransaction_payment: event_entity.subtransaction_payment, - __expand: request_expansions( - 'subtransaction.payments', - 'subtransaction.transaction.transaction_assignments' - ) \ No newline at end of file + subtransaction_payment: event_entity.subtransaction_payment, + __expand: request_expansions( + 'subtransaction.payments', + 'subtransaction.transaction.transaction_assignments' + ) diff --git a/app/views/api/stripe_transaction_dispute_reversals/object_events/_base.json.jbuilder b/app/views/api/stripe_transaction_dispute_reversals/object_events/_base.json.jbuilder index 80a9b2ca0..ab81c998d 100644 --- a/app/views/api/stripe_transaction_dispute_reversals/object_events/_base.json.jbuilder +++ b/app/views/api/stripe_transaction_dispute_reversals/object_events/_base.json.jbuilder @@ -4,9 +4,9 @@ # Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE json.partial! 'api_new/subtransaction_payments/subtransaction_payment', - subtransaction_payment: event_entity.subtransaction_payment, - __expand: request_expansions( - 'subtransaction', - 'subtransaction.transaction', - 'subtransaction.transaction.transaction_assignments' - ) \ No newline at end of file + subtransaction_payment: event_entity.subtransaction_payment, + __expand: request_expansions( + 'subtransaction', + 'subtransaction.transaction', + 'subtransaction.transaction.transaction_assignments' + ) diff --git a/app/views/api/stripe_transaction_disputes/object_events/_base.json.jbuilder b/app/views/api/stripe_transaction_disputes/object_events/_base.json.jbuilder index 80a9b2ca0..ab81c998d 100644 --- a/app/views/api/stripe_transaction_disputes/object_events/_base.json.jbuilder +++ b/app/views/api/stripe_transaction_disputes/object_events/_base.json.jbuilder @@ -4,9 +4,9 @@ # Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE json.partial! 'api_new/subtransaction_payments/subtransaction_payment', - subtransaction_payment: event_entity.subtransaction_payment, - __expand: request_expansions( - 'subtransaction', - 'subtransaction.transaction', - 'subtransaction.transaction.transaction_assignments' - ) \ No newline at end of file + subtransaction_payment: event_entity.subtransaction_payment, + __expand: request_expansions( + 'subtransaction', + 'subtransaction.transaction', + 'subtransaction.transaction.transaction_assignments' + ) diff --git a/app/views/api/stripe_transaction_refunds/object_events/_base.json.jbuilder b/app/views/api/stripe_transaction_refunds/object_events/_base.json.jbuilder index 80a9b2ca0..ab81c998d 100644 --- a/app/views/api/stripe_transaction_refunds/object_events/_base.json.jbuilder +++ b/app/views/api/stripe_transaction_refunds/object_events/_base.json.jbuilder @@ -4,9 +4,9 @@ # Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE json.partial! 'api_new/subtransaction_payments/subtransaction_payment', - subtransaction_payment: event_entity.subtransaction_payment, - __expand: request_expansions( - 'subtransaction', - 'subtransaction.transaction', - 'subtransaction.transaction.transaction_assignments' - ) \ No newline at end of file + subtransaction_payment: event_entity.subtransaction_payment, + __expand: request_expansions( + 'subtransaction', + 'subtransaction.transaction', + 'subtransaction.transaction.transaction_assignments' + ) diff --git a/app/views/api/subtransactions/_subtransaction.json.jbuilder b/app/views/api/subtransactions/_subtransaction.json.jbuilder index fdbb79073..fe24cd6bd 100644 --- a/app/views/api/subtransactions/_subtransaction.json.jbuilder +++ b/app/views/api/subtransactions/_subtransaction.json.jbuilder @@ -15,4 +15,4 @@ end json.url api_nonprofit_transaction_subtransaction_url(subtransaction.nonprofit, subtransaction.trx) -json.partial! subtransaction.subtransactable, as: :subtransactable, __expand: __expand \ No newline at end of file +json.partial! subtransaction.subtransactable, as: :subtransactable, __expand: __expand diff --git a/app/views/api/transaction_assignments/_transaction_assignment.json.jbuilder b/app/views/api/transaction_assignments/_transaction_assignment.json.jbuilder index 53e8fc68e..8d6810866 100644 --- a/app/views/api/transaction_assignments/_transaction_assignment.json.jbuilder +++ b/app/views/api/transaction_assignments/_transaction_assignment.json.jbuilder @@ -6,9 +6,8 @@ json.type 'trx_assignment' json.id transaction_assignment.id -handle_expansion(:supporter, transaction_assignment.supporter, {json: json, __expand: __expand}) -handle_expansion(:nonprofit, transaction_assignment.nonprofit, {json: json, __expand: __expand}) -handle_expansion(:transaction, transaction_assignment.trx, {json: json, __expand: __expand}) +handle_expansion(:supporter, transaction_assignment.supporter, { json: json, __expand: __expand }) +handle_expansion(:nonprofit, transaction_assignment.nonprofit, { json: json, __expand: __expand }) +handle_expansion(:transaction, transaction_assignment.trx, { json: json, __expand: __expand }) - -json.partial! transaction_assignment.assignable, as: :assignable, __expand: __expand \ No newline at end of file +json.partial! transaction_assignment.assignable, as: :assignable, __expand: __expand diff --git a/app/views/api/transactions/_transaction.json.jbuilder b/app/views/api/transactions/_transaction.json.jbuilder index 6fb027215..1dae7d63e 100644 --- a/app/views/api/transactions/_transaction.json.jbuilder +++ b/app/views/api/transactions/_transaction.json.jbuilder @@ -8,9 +8,9 @@ json.object 'transaction' json.created transaction.created.to_i -handle_expansion(:supporter, transaction.supporter, {json: json, __expand: __expand}) +handle_expansion(:supporter, transaction.supporter, { json: json, __expand: __expand }) -handle_expansion(:nonprofit, transaction.nonprofit, {json: json, __expand: __expand}) +handle_expansion(:nonprofit, transaction.nonprofit, { json: json, __expand: __expand }) json.nonprofit transaction.nonprofit.id @@ -18,9 +18,10 @@ json.amount do json.partial! '/api/common/amount', amount: transaction.amount_as_money end -handle_expansion(:subtransaction, transaction.subtransaction, {json: json, __expand: __expand}) +handle_expansion(:subtransaction, transaction.subtransaction, { json: json, __expand: __expand }) -handle_array_expansion(:transaction_assignments, transaction.transaction_assignments, {json: json, __expand: __expand, item_as: :transaction_assignment}) do |tra, opts| +handle_array_expansion(:transaction_assignments, transaction.transaction_assignments, + { json: json, __expand: __expand, item_as: :transaction_assignment }) do |tra, opts| handle_item_expansion(tra, opts) end diff --git a/app/views/api/transactions/object_events/_base.json.jbuilder b/app/views/api/transactions/object_events/_base.json.jbuilder index 03339184c..e2223fa93 100644 --- a/app/views/api/transactions/object_events/_base.json.jbuilder +++ b/app/views/api/transactions/object_events/_base.json.jbuilder @@ -2,4 +2,5 @@ # License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later # Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE -json.partial! event_entity, as: :transaction, __expand: request_expansions(%w(subtransaction.payments transaction_assignments)) \ No newline at end of file +json.partial! event_entity, as: :transaction, + __expand: request_expansions(%w[subtransaction.payments transaction_assignments]) From 8961fdb89a892aec0940fe99939c33295834ff10 Mon Sep 17 00:00:00 2001 From: MaiconMares Date: Sat, 23 Apr 2022 18:25:05 -0300 Subject: [PATCH 5/8] feature: Fixing unrelated files Co-authored-by: Alan Marques --- ..._transaction_dispute_reversal.json.jbuilder | 18 ------------------ .../object_events/_base.json.jbuilder | 12 ------------ .../_stripe_transaction_dispute.json.jbuilder | 18 ------------------ .../object_events/_base.json.jbuilder | 12 ------------ .../_stripe_transaction_refund.json.jbuilder | 18 ------------------ .../object_events/_base.json.jbuilder | 12 ------------ .../_stripe_transaction.json.jbuilder | 16 ---------------- 7 files changed, 106 deletions(-) delete mode 100644 app/views/api/stripe_transaction_dispute_reversals/_stripe_transaction_dispute_reversal.json.jbuilder delete mode 100644 app/views/api/stripe_transaction_dispute_reversals/object_events/_base.json.jbuilder delete mode 100644 app/views/api/stripe_transaction_disputes/_stripe_transaction_dispute.json.jbuilder delete mode 100644 app/views/api/stripe_transaction_disputes/object_events/_base.json.jbuilder delete mode 100644 app/views/api/stripe_transaction_refunds/_stripe_transaction_refund.json.jbuilder delete mode 100644 app/views/api/stripe_transaction_refunds/object_events/_base.json.jbuilder delete mode 100644 app/views/api/stripe_transactions/_stripe_transaction.json.jbuilder diff --git a/app/views/api/stripe_transaction_dispute_reversals/_stripe_transaction_dispute_reversal.json.jbuilder b/app/views/api/stripe_transaction_dispute_reversals/_stripe_transaction_dispute_reversal.json.jbuilder deleted file mode 100644 index 0f51e753c..000000000 --- a/app/views/api/stripe_transaction_dispute_reversals/_stripe_transaction_dispute_reversal.json.jbuilder +++ /dev/null @@ -1,18 +0,0 @@ -# frozen_string_literal: true - -# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later -# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE - -json.object 'stripe_transaction_dispute_reversal' - -json.net_amount do - json.partial! '/api_new/common/amount', amount: paymentable.net_amount_as_money -end - -json.gross_amount do - json.partial! '/api_new/common/amount', amount: paymentable.gross_amount_as_money -end - -json.fee_total do - json.partial! '/api_new/common/amount', amount: paymentable.fee_total_as_money -end diff --git a/app/views/api/stripe_transaction_dispute_reversals/object_events/_base.json.jbuilder b/app/views/api/stripe_transaction_dispute_reversals/object_events/_base.json.jbuilder deleted file mode 100644 index ab81c998d..000000000 --- a/app/views/api/stripe_transaction_dispute_reversals/object_events/_base.json.jbuilder +++ /dev/null @@ -1,12 +0,0 @@ -# frozen_string_literal: true - -# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later -# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE - -json.partial! 'api_new/subtransaction_payments/subtransaction_payment', - subtransaction_payment: event_entity.subtransaction_payment, - __expand: request_expansions( - 'subtransaction', - 'subtransaction.transaction', - 'subtransaction.transaction.transaction_assignments' - ) diff --git a/app/views/api/stripe_transaction_disputes/_stripe_transaction_dispute.json.jbuilder b/app/views/api/stripe_transaction_disputes/_stripe_transaction_dispute.json.jbuilder deleted file mode 100644 index 01db54d26..000000000 --- a/app/views/api/stripe_transaction_disputes/_stripe_transaction_dispute.json.jbuilder +++ /dev/null @@ -1,18 +0,0 @@ -# frozen_string_literal: true - -# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later -# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE - -json.object 'stripe_transaction_dispute' - -json.net_amount do - json.partial! '/api_new/common/amount', amount: paymentable.net_amount_as_money -end - -json.gross_amount do - json.partial! '/api_new/common/amount', amount: paymentable.gross_amount_as_money -end - -json.fee_total do - json.partial! '/api_new/common/amount', amount: paymentable.fee_total_as_money -end diff --git a/app/views/api/stripe_transaction_disputes/object_events/_base.json.jbuilder b/app/views/api/stripe_transaction_disputes/object_events/_base.json.jbuilder deleted file mode 100644 index ab81c998d..000000000 --- a/app/views/api/stripe_transaction_disputes/object_events/_base.json.jbuilder +++ /dev/null @@ -1,12 +0,0 @@ -# frozen_string_literal: true - -# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later -# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE - -json.partial! 'api_new/subtransaction_payments/subtransaction_payment', - subtransaction_payment: event_entity.subtransaction_payment, - __expand: request_expansions( - 'subtransaction', - 'subtransaction.transaction', - 'subtransaction.transaction.transaction_assignments' - ) diff --git a/app/views/api/stripe_transaction_refunds/_stripe_transaction_refund.json.jbuilder b/app/views/api/stripe_transaction_refunds/_stripe_transaction_refund.json.jbuilder deleted file mode 100644 index 7c1478ac9..000000000 --- a/app/views/api/stripe_transaction_refunds/_stripe_transaction_refund.json.jbuilder +++ /dev/null @@ -1,18 +0,0 @@ -# frozen_string_literal: true - -# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later -# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE - -json.object 'stripe_transaction_refund' - -json.net_amount do - json.partial! '/api_new/common/amount', amount: paymentable.net_amount_as_money -end - -json.gross_amount do - json.partial! '/api_new/common/amount', amount: paymentable.gross_amount_as_money -end - -json.fee_total do - json.partial! '/api_new/common/amount', amount: paymentable.fee_total_as_money -end diff --git a/app/views/api/stripe_transaction_refunds/object_events/_base.json.jbuilder b/app/views/api/stripe_transaction_refunds/object_events/_base.json.jbuilder deleted file mode 100644 index ab81c998d..000000000 --- a/app/views/api/stripe_transaction_refunds/object_events/_base.json.jbuilder +++ /dev/null @@ -1,12 +0,0 @@ -# frozen_string_literal: true - -# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later -# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE - -json.partial! 'api_new/subtransaction_payments/subtransaction_payment', - subtransaction_payment: event_entity.subtransaction_payment, - __expand: request_expansions( - 'subtransaction', - 'subtransaction.transaction', - 'subtransaction.transaction.transaction_assignments' - ) diff --git a/app/views/api/stripe_transactions/_stripe_transaction.json.jbuilder b/app/views/api/stripe_transactions/_stripe_transaction.json.jbuilder deleted file mode 100644 index 225ce91fa..000000000 --- a/app/views/api/stripe_transactions/_stripe_transaction.json.jbuilder +++ /dev/null @@ -1,16 +0,0 @@ -# frozen_string_literal: true - -# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later -# Full license explanation at https://github.com/houdiniproject/houdini/blob/main/LICENSE - -json.object 'stripe_transaction' - -json.created subtransactable.created.to_i - -json.amount do - json.partial! '/api_new/common/amount', amount: subtransactable.amount_as_money -end - -json.net_amount do - json.partial! '/api_new/common/amount', amount: subtransactable.net_amount_as_money -end From 91442c086a2045b89af94176bc603e06b1c68541 Mon Sep 17 00:00:00 2001 From: Alan Marques Sousa Date: Sat, 23 Apr 2022 18:32:20 -0300 Subject: [PATCH 6/8] Change in index transaction test Co-authored-by: MaiconMares --- .../transactions/index.json.jbuilder_spec.rb | 89 ++++++++++++------- 1 file changed, 55 insertions(+), 34 deletions(-) diff --git a/spec/views/api/transactions/index.json.jbuilder_spec.rb b/spec/views/api/transactions/index.json.jbuilder_spec.rb index 3dba02b05..ea6665dc2 100644 --- a/spec/views/api/transactions/index.json.jbuilder_spec.rb +++ b/spec/views/api/transactions/index.json.jbuilder_spec.rb @@ -5,6 +5,13 @@ require 'rails_helper' RSpec.describe '/api/transactions/index.json.jbuilder', type: :view do + around do |ex| + Timecop.freeze(2020, 5, 4) do + ex.run + end + end + + include Controllers::Api::JbuilderExpansions def base_path(nonprofit_id, transaction_id) "/api/nonprofits/#{nonprofit_id}/transactions/#{transaction_id}" end @@ -14,45 +21,59 @@ def base_url(nonprofit_id, transaction_id) end subject(:json) do + view.lookup_context.prefixes = view.lookup_context.prefixes.drop(1) assign(:transactions, Kaminari.paginate_array([transaction]).page) + assign(:__expand, Controllers::Api::JbuilderExpansions::ExpansionRequest.new('supporter', 'transaction_assignments', 'payments', 'subtransaction.payments')) render - JSON.parse(rendered) + rendered end let(:transaction) { create(:transaction_for_donation) } let(:supporter) { transaction.supporter } let(:nonprofit) { transaction.nonprofit } - it { expect(json['data'].count).to eq 1 } - - describe 'details of the first item' do - subject(:first) do - json['data'].first - end - - include_context 'with json results for transaction_for_donation' - end - - describe 'paging' do - subject(:json) do - transaction - (0..5).each do |_i| - create( - :transaction, - nonprofit: transaction.nonprofit, - supporter: transaction.supporter - ) - end - assign(:transactions, nonprofit.transactions.order('created DESC').page.per(5)) - render - JSON.parse(rendered) - end - - it { is_expected.to include('data' => have_attributes(count: 5)) } - it { is_expected.to include('first_page' => true) } - it { is_expected.to include('last_page' => false) } - it { is_expected.to include('current_page' => 1) } - it { is_expected.to include('requested_size' => 5) } - it { is_expected.to include('total_count' => 7) } - end -end + it { + is_expected.to include_json( + 'first_page' => true, + last_page: true, + current_page: 1, + requested_size: 25, + total_count: 1, + data: [ + attributes_for(:trx, + nonprofit: nonprofit.id, + supporter: attributes_for( + :supporter_expectation, + id: supporter.id + ), + id: transaction.id, + amount_cents: 4000, + subtransaction: attributes_for( + :subtransaction_expectation, + :offline_transaction, + gross_amount_cents: 4000, + payments: [ + attributes_for(:payment_expectation, + :offiline_transaction_charge, + gross_amount_cents: 4000, + fee_total_cents: 0) + ] + ), + payments: [ + attributes_for(:payment_expectation, + :offline_transaction_charge, + gross_amount_cents: 4000, + fee_total_cents: 0) + ], + transaction_assignments: [ + attributes_for(:trx_assignment_expectation, + :donation, + amount_cents:4000, + designation: "Designation 1" + ) + ] + ) + ] + ) + } +end \ No newline at end of file From 7dc00076f1412174439dae204473e36ee05e6448 Mon Sep 17 00:00:00 2001 From: MaiconMares Date: Sun, 24 Apr 2022 18:17:24 -0300 Subject: [PATCH 7/8] feature: Fixing code style problems Co-authored-by: Alan Marques --- .../_subtransaction.json.jbuilder | 1 - .../transactions/index.json.jbuilder_spec.rb | 98 +++++++++---------- 2 files changed, 49 insertions(+), 50 deletions(-) diff --git a/app/views/api/subtransactions/_subtransaction.json.jbuilder b/app/views/api/subtransactions/_subtransaction.json.jbuilder index fe24cd6bd..028bf2520 100644 --- a/app/views/api/subtransactions/_subtransaction.json.jbuilder +++ b/app/views/api/subtransactions/_subtransaction.json.jbuilder @@ -8,7 +8,6 @@ json.supporter subtransaction.supporter.id json.nonprofit subtransaction.nonprofit.id json.transaction subtransaction.trx.id - json.payments subtransaction.payments do |py| json.partial! py, as: :subtransaction_payment end diff --git a/spec/views/api/transactions/index.json.jbuilder_spec.rb b/spec/views/api/transactions/index.json.jbuilder_spec.rb index ea6665dc2..08f200de7 100644 --- a/spec/views/api/transactions/index.json.jbuilder_spec.rb +++ b/spec/views/api/transactions/index.json.jbuilder_spec.rb @@ -5,6 +5,16 @@ require 'rails_helper' RSpec.describe '/api/transactions/index.json.jbuilder', type: :view do + subject(:json) do + view.lookup_context.prefixes = view.lookup_context.prefixes.drop(1) + assign(:transactions, Kaminari.paginate_array([transaction]).page) + assign(:__expand, + Controllers::Api::JbuilderExpansions::ExpansionRequest.new('supporter', 'transaction_assignments', 'payments', + 'subtransaction.payments')) + render + rendered + end + around do |ex| Timecop.freeze(2020, 5, 4) do ex.run @@ -20,60 +30,50 @@ def base_url(nonprofit_id, transaction_id) "http://test.host#{base_path(nonprofit_id, transaction_id)}" end - subject(:json) do - view.lookup_context.prefixes = view.lookup_context.prefixes.drop(1) - assign(:transactions, Kaminari.paginate_array([transaction]).page) - assign(:__expand, Controllers::Api::JbuilderExpansions::ExpansionRequest.new('supporter', 'transaction_assignments', 'payments', 'subtransaction.payments')) - render - rendered - end - let(:transaction) { create(:transaction_for_donation) } let(:supporter) { transaction.supporter } let(:nonprofit) { transaction.nonprofit } it { is_expected.to include_json( - 'first_page' => true, - last_page: true, - current_page: 1, - requested_size: 25, - total_count: 1, - data: [ - attributes_for(:trx, - nonprofit: nonprofit.id, - supporter: attributes_for( - :supporter_expectation, - id: supporter.id - ), - id: transaction.id, - amount_cents: 4000, - subtransaction: attributes_for( - :subtransaction_expectation, - :offline_transaction, + 'first_page' => true, + last_page: true, + current_page: 1, + requested_size: 25, + total_count: 1, + data: [ + attributes_for(:trx, + nonprofit: nonprofit.id, + supporter: attributes_for( + :supporter_expectation, + id: supporter.id + ), + id: transaction.id, + amount_cents: 4000, + subtransaction: attributes_for( + :subtransaction_expectation, + :offline_transaction, + gross_amount_cents: 4000, + payments: [ + attributes_for(:payment_expectation, + :offiline_transaction_charge, + gross_amount_cents: 4000, + fee_total_cents: 0) + ] + ), + payments: [ + attributes_for(:payment_expectation, + :offline_transaction_charge, gross_amount_cents: 4000, - payments: [ - attributes_for(:payment_expectation, - :offiline_transaction_charge, - gross_amount_cents: 4000, - fee_total_cents: 0) - ] - ), - payments: [ - attributes_for(:payment_expectation, - :offline_transaction_charge, - gross_amount_cents: 4000, - fee_total_cents: 0) - ], - transaction_assignments: [ - attributes_for(:trx_assignment_expectation, - :donation, - amount_cents:4000, - designation: "Designation 1" - ) - ] - ) - ] - ) + fee_total_cents: 0) + ], + transaction_assignments: [ + attributes_for(:trx_assignment_expectation, + :donation, + amount_cents: 4000, + designation: 'Designation 1') + ]) + ] + ) } -end \ No newline at end of file +end From b08330bbe57897acd281b1ce5dec252b76ea1a0f Mon Sep 17 00:00:00 2001 From: MaiconMares Date: Sun, 24 Apr 2022 18:36:27 -0300 Subject: [PATCH 8/8] feature: Fixing rubocop semantic problems Co-authored-by: Alan Marques --- .../concerns/controllers/api/jbuilder_expansions.rb | 5 +++-- .../api/transactions/index.json.jbuilder_spec.rb | 13 +++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/app/controllers/concerns/controllers/api/jbuilder_expansions.rb b/app/controllers/concerns/controllers/api/jbuilder_expansions.rb index 1bfb69347..d7f7774ba 100644 --- a/app/controllers/concerns/controllers/api/jbuilder_expansions.rb +++ b/app/controllers/concerns/controllers/api/jbuilder_expansions.rb @@ -38,9 +38,10 @@ def self.set_expansions(*expansions) request = ExpansionRequest.new expansions = expansions.flatten if expansions.count == 1 - if expansions.first.is_a? String + case expansions.first + when String request = ExpansionRequest.new(*expansions) - elsif expansions.first.is_a? ExpansionRequest + when ExpansionRequest request = expansions.first end elsif expansions.any? diff --git a/spec/views/api/transactions/index.json.jbuilder_spec.rb b/spec/views/api/transactions/index.json.jbuilder_spec.rb index 08f200de7..f8d1c07b3 100644 --- a/spec/views/api/transactions/index.json.jbuilder_spec.rb +++ b/spec/views/api/transactions/index.json.jbuilder_spec.rb @@ -35,28 +35,29 @@ def base_url(nonprofit_id, transaction_id) let(:nonprofit) { transaction.nonprofit } it { - is_expected.to include_json( - 'first_page' => true, + is_expected.to include_json(jbuilder_expansions.rb + first_page: => true, last_page: true, current_page: 1, requested_size: 25, total_count: 1, data: [ attributes_for(:trx, - nonprofit: nonprofit.id, + nonprofit: nonprofit.houid, supporter: attributes_for( :supporter_expectation, - id: supporter.id + id: supporter.houid ), - id: transaction.id, + id: transaction.houid, amount_cents: 4000, subtransaction: attributes_for( :subtransaction_expectation, :offline_transaction, gross_amount_cents: 4000, + net_amount_cents: 4000, payments: [ attributes_for(:payment_expectation, - :offiline_transaction_charge, + :offline_transaction_charge, gross_amount_cents: 4000, fee_total_cents: 0) ]