-
Notifications
You must be signed in to change notification settings - Fork 69
Move give time action to transfers controller #264
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
Changes from all commits
72b7bad
9a1d6b0
808ed91
69982ab
0aa4073
0fa4668
fa3adf4
24e5d6a
96f36b1
b50d662
bf6223b
a624dbc
fc902af
14eaad8
be04d97
e97d467
5f4d70e
7b51848
95230dc
b319ed9
b13b38e
b71dfb7
47812a9
e40a0ff
545ea44
70efd91
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,9 @@ | ||
module TransfersHelper | ||
def accountable_path(accountable) | ||
if accountable.is_a?(Organization) | ||
organization_path(accountable) | ||
else | ||
user_path(accountable.user) | ||
end | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any reason why this can't be an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure. I'd just do whatever it takes to follow Rails convention so that we don't have to define any path helper. But because we made this standout, we can work on it in an upcoming iteration, not here. |
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
class TransferFactory | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the first try at encapsulating all this transfer building logic. It's more of a patch to let us move code around until we figure out how the data model really works. |
||
def initialize(current_organization, current_user, offer_id, destination_account_id) | ||
@current_organization = current_organization | ||
@current_user = current_user | ||
@offer_id = offer_id | ||
@destination_account_id = destination_account_id | ||
end | ||
|
||
# Returns the offer that is the subject of the transfer | ||
# | ||
# @return [Maybe<Offer>] | ||
def offer | ||
current_organization.offers.find_by_id(offer_id) | ||
end | ||
|
||
# Returns a new instance of Transfer with the data provided | ||
# | ||
# @return [Transfer] | ||
def build_transfer | ||
transfer = Transfer.new(source: source, destination: destination_account.id) | ||
transfer.post = offer unless for_organization? | ||
transfer | ||
end | ||
|
||
def transfer_sources | ||
if admin? | ||
[current_organization.account] + | ||
current_organization.member_accounts.merge(Member.active) | ||
else | ||
[] | ||
end | ||
end | ||
|
||
def accountable | ||
@accountable ||= destination_account.accountable | ||
end | ||
|
||
private | ||
|
||
attr_reader :current_organization, :current_user, :offer_id, | ||
:destination_account_id | ||
|
||
# Returns the id of the account that acts as source of the transfer. | ||
# Either the account of the organization or the account of the current user. | ||
# | ||
# @return [Maybe<Integer>] | ||
def source | ||
organization = if accountable.is_a?(Organization) | ||
accountable | ||
else | ||
current_organization | ||
end | ||
|
||
current_user.members.find_by(organization: organization).account.id | ||
end | ||
|
||
# Checks whether the destination account is an organization | ||
# | ||
# @return [Boolean] | ||
def for_organization? | ||
destination_account.accountable.class == Organization | ||
end | ||
|
||
def admin? | ||
current_user.try :manages?, current_organization | ||
end | ||
|
||
# TODO: this method implements authorization by scoping the destination | ||
# account in all the accounts of the current organization. If the specified | ||
# destination account does not belong to it, the request will simply faily. | ||
# | ||
# Returns the account the time will be transfered to | ||
# | ||
# @return [Account] | ||
def destination_account | ||
@destination_account ||= current_organization | ||
.all_accounts | ||
.find(destination_account_id) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Wraps the passed sources as a collection of HTML <option>'s value and text | ||
# pairs as expected by Rails' #options_for_select. | ||
class TransferSourcesOptions | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doc please 😬 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. true There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
# Constructor | ||
# | ||
# @param sources [Array<Account>] | ||
# @param destination_accountable [Member | Organization] | ||
def initialize(sources, destination_accountable) | ||
@sources = sources | ||
@destination_accountable = destination_accountable | ||
end | ||
|
||
# Returns the collection as an Array containing pairs of <option>'s text and | ||
# value sorted by accountable type and member_uid | ||
# | ||
# @return [Array<String, Integer>] | ||
def to_a | ||
sources | ||
.sort_by { |account| to_accountable_type_and_uid(account) } | ||
.map { |account| to_text_and_value(account) } | ||
end | ||
|
||
private | ||
|
||
attr_reader :sources, :destination_accountable | ||
|
||
def to_accountable_type_and_uid(account) | ||
[account.accountable_type, account.accountable.try(:member_uid)] | ||
end | ||
|
||
def to_text_and_value(account) | ||
accountable = account.accountable | ||
|
||
[ | ||
"#{display_id(accountable)} #{accountable.class} #{accountable}", | ||
account.id | ||
] | ||
end | ||
|
||
def display_id(accountable) | ||
accountable.display_id(destination_accountable) | ||
end | ||
end |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<% if offer %> | ||
<%= form.input :post, readonly: true %> | ||
<%= form.input :post_id, as: :hidden %> | ||
<% else %> | ||
<%= form.input :post_id, collection: accountable.offers.active %> | ||
<% end %> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<% if offer %> | ||
<%= form.input :post, as: :hidden %> | ||
<% end %> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<div class="form-group"> | ||
<%= form.label :source %> | ||
<%= form.select :source, | ||
options_for_select( | ||
TransferSourcesOptions.new(sources, accountable).to_a, | ||
selected: current_user.member(current_organization).account.id | ||
), {}, id: "select2-time", class: "form-control" | ||
%> | ||
</div> |
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.
The term
destination_account
would be better off as payee. Seems more appropriate to this domain.