-
Notifications
You must be signed in to change notification settings - Fork 4
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
Add has_many
method to Ruby Unit
#23
Add has_many
method to Ruby Unit
#23
Conversation
098657a
to
85435b6
Compare
a1175ca
to
d78aa30
Compare
d711154
to
7bec1eb
Compare
7bec1eb
to
e420f1d
Compare
@@ -19,6 +19,7 @@ class DepositAccount < APIResource | |||
attribute :close_reason, Types::String, readonly: true # Optional. The reason the account was closed, either ByCustomer or Fraud. | |||
|
|||
belongs_to :customer, class_name: 'Unit::IndividualCustomer' | |||
has_many :customers, class_name: 'Unit::IndividualCustomer' |
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.
Might be worth calling out in a comment that this is for joint accounts
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.
@ianyamey thanks for commenting on this - I'd actually appreciate your thoughts on this specific instance. I thought it would be strange for the user of ruby gem to have to know whether the particular resource belongs to customer
or to customers
, so in my implementation of adding customers
enabled it to also work when there was only one particular customer added to the account.
ie if .customers
is called on a deposit account with only one customer, we'll return an array with one customer. Do you agree that that's the correct approach to use?
@@ -11,6 +11,21 @@ def find(id) | |||
|
|||
build_resource_from_json_api(located_resource) | |||
end | |||
|
|||
def find_all(ids) |
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.
Could we use the List operation here instead?
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.
sorry, do you mean update the existing def list
so that it's able to determine whether the resource is a single resource or a plural resource?
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.
Also I realized that my original implementation could be simplified, what's currently reflected is the more streamlined version.
lib/unit-ruby/util/api_resource.rb
Outdated
end | ||
end | ||
|
||
define_method("#{resource_name}=") do |resource| |
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.
Does this need to accept an array of resources?
I would have expected usage to look like:
deposit_account = DepositAccount.find(...)
deposit_account.customers = [customer_1, customer_2]
deposit_account.save
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.
Possibly? In taking a look at this comment & playing around with this a little, I'm not a little unsure what this is doing. Would you be up for pairing on this a little tomorrow AM?
lib/unit-ruby/util/api_resource.rb
Outdated
|
||
relationship_id = relationships.dig(singular_resource_name, :data, :id) | ||
|
||
return nil unless relationship_id |
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.
Since this is for the plural resource, should we return []
instead of nil?
(same on line L130)
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.
Good suggestion!
e420f1d
to
d146595
Compare
d146595
to
6449a0f
Compare
lib/unit-ruby/util/api_resource.rb
Outdated
if relationships.keys.include?(singular_resource_name) | ||
|
||
relationship_id = relationships.dig(singular_resource_name, :data, :id) | ||
|
||
return [] unless relationship_id | ||
|
||
return [relationship_id].map { |id| Kernel.const_get(class_name).find(id) } | ||
else | ||
relationship_ids = relationships.dig(resource_name.to_sym, :data).map { |x| x[:id] } | ||
|
||
return [] if relationship_ids.empty? | ||
|
||
return [relationship_ids].map { |id| Kernel.const_get(class_name).find(id) } | ||
end | ||
end |
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 adds a lot of complexity trying to get around Unit's design decision to allow a singular and plural relationship.
My suggestion would just be to keep this narrow, and allow apps that use this SDK to handle those cases when there are mix of singular/plural entities themselves.
if relationships.keys.include?(singular_resource_name) | |
relationship_id = relationships.dig(singular_resource_name, :data, :id) | |
return [] unless relationship_id | |
return [relationship_id].map { |id| Kernel.const_get(class_name).find(id) } | |
else | |
relationship_ids = relationships.dig(resource_name.to_sym, :data).map { |x| x[:id] } | |
return [] if relationship_ids.empty? | |
return [relationship_ids].map { |id| Kernel.const_get(class_name).find(id) } | |
end | |
end | |
resource_class = Kernel.const_get(class_name) | |
relationships.dig(resource_name.to_sym, :data).map do |data| | |
resource_class.build_resource_from_json_api(id) | |
end | |
end |
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.
@ianyamey apologies, I thought I had responded to this. I guess my question would be how is the user of this service supposed to determine whether or not a particular deposit account has one or many users without throwing an error. If we're only providing a mapping of what's sent from them to us, calling customers
on a deposit account that only has one will result in an error since the relationship
returned from unit will only have a customer
property.
e6ff60f
to
8fae143
Compare
lib/unit-ruby/util/api_resource.rb
Outdated
Kernel.const_get(class_name).find(resource.id) | ||
end | ||
elsif defined?(singular_resource_name) | ||
[send(singular_resource_name)] |
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.
@ianyamey .to_a
isn't a method on Unit::IndividualCustomer
add has many method
8fae143
to
a7eed46
Compare
…count-owners-endpoint-to Add Ability to Add Customer to Deposit Account
This PR adds a
has_many
method to the Ruby gem in order to return all customers that belong to adeposit_account.
One question - Unit returns
customer
as a key onresources
when there is only one customer &customers
as a key when there is more than one customer. In this PR I made it so thatcustomers
is always returned, regardless of whether there is one or more customers. Do you think this is the appropriate solution?Please let me know if walking through this together would be helpful!