Skip to content

Instantly share code, notes, and snippets.

@meskallito
Created December 8, 2014 17:49
Show Gist options
  • Select an option

  • Save meskallito/9597c0f563cab2a79290 to your computer and use it in GitHub Desktop.

Select an option

Save meskallito/9597c0f563cab2a79290 to your computer and use it in GitHub Desktop.
module V1
class ChequeTransactionsController < TransactionsController
before_filter :check_if_selected, only: :set_cheque_number
def show
authorize Transaction
transactions = ChequeTransactionsIndex.query(params[:query]) { |scope|
paginated_collection(filtered_transactions(scope))
}.preload(:funding_processing, { real_transaction: :transaction_transitions })
respond_with transactions, each_serializer: ChequeShortTransactionSerializer, root: :transactions
end
def set_cheque_number
authorize Transaction
service = SetChequeNumber.new(transactions: transactions_group, cheque_number: params[:transaction][:cheque_number])
service.on(:error) { |error| unprocessable!(error_codes: error.codes) }
service.on(:success) { |transactions| respond_with(transactions, root: :transactions, each_serializer: ChequeShortTransactionSerializer) }
service.call
end
private
def check_if_selected
bad_request! if params[:id].blank? and params[:originator_id].blank?
end
def transactions_group
transactions = params[:id] && Transaction.where(id: params[:id].split(',')) || Transaction.all
transactions = transactions.where(originator_id: params[:originator_id].split(',')) unless params[:originator_id].blank?
transactions
end
end
end
class SetChequeNumber
include Wisper::Publisher
include Virtus.model
include DomainService
attribute :transactions
attribute :cheque_number, Integer
success_message :transactions
def perform
error!(:not_allowed_state) unless transactions.all? { |transaction| transaction.state == 'submitting' }
progressive_update
end
private
def progressive_update
funding_processings.inject(cheque_number) do |number, transaction|
unless transaction.cheque_number
transaction.update_attribute(:cheque_number, number)
number + 1
end
end
end
def funding_processings
ChequeFundingProcessing.for_transactions(transactions).reorder('cheque_order')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment