-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/add method handling expansion child objects #1063
Draft
alan-ms
wants to merge
8
commits into
houdiniproject:main
Choose a base branch
from
FGA-GCES:feature/add-method-handling-expansion-child-objects
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6a7e065
Implement jbuilder expansion on transactions controller
alan-ms 73a49e5
Adding jbuilder partials and method to handle Amount
alan-ms 3029461
Expanding subtransaction, transaction and transaction_assignment
alan-ms 805743a
Rubocop issue fix
alan-ms 8961fdb
feature: Fixing unrelated files
MaiconMares 91442c0
Change in index transaction test
alan-ms 7dc0007
feature: Fixing code style problems
MaiconMares b08330b
feature: Fixing rubocop semantic problems
MaiconMares File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
168 changes: 168 additions & 0 deletions
168
app/controllers/concerns/controllers/api/jbuilder_expansions.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
# 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 | ||
case expansions.first | ||
when String | ||
request = ExpansionRequest.new(*expansions) | ||
when 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 |
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
12 changes: 12 additions & 0 deletions
12
app/views/api/modern_donations/_modern_donation.json.jbuilder
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.object 'donation' | ||
|
||
json.(assignable, :designation) | ||
|
||
json.amount do | ||
json.partial! '/api/common/amount', amount: assignable.amount_as_money | ||
end |
12 changes: 12 additions & 0 deletions
12
app/views/api/modern_donations/object_events/_base.json.jbuilder
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.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 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
12 changes: 12 additions & 0 deletions
12
app/views/api/offline_transaction_charges/object_events/_base.json.jbuilder
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
6 changes: 6 additions & 0 deletions
6
app/views/api/simple_objects/object_events/_base.json.jbuilder
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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! @simple_object, as: :object, __expand: @__expand |
18 changes: 18 additions & 0 deletions
18
app/views/api/stripe_transaction_charges/_stripe_transaction_charge.json.jbuilder
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
11 changes: 11 additions & 0 deletions
11
app/views/api/stripe_transaction_charges/object_events/_base.json.jbuilder
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' | ||
) |
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
13 changes: 13 additions & 0 deletions
13
app/views/api/transaction_assignments/_transaction_assignment.json.jbuilder
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.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 |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be kept for now.