Files
timeoverflow/app/models/transfer_sources_options.rb

38 lines
925 B
Ruby

# Wraps the passed sources as a collection of HTML <option>'s value and text
# pairs as expected by Rails' #options_for_select.
class TransferSourcesOptions
# Constructor
#
# @param sources [Array<Account>]
def initialize(sources)
@sources = sources
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
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
[
"#{accountable.display_id} #{accountable.class} #{accountable}",
account.id
]
end
end