Skip to content

Commit

Permalink
API v1 financial_transactions endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
wvengen committed Oct 13, 2018
1 parent 554be09 commit 9f700bd
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
20 changes: 20 additions & 0 deletions app/controllers/api/v1/financial_transactions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Api::V1::FinancialTransactionsController < Api::V1::BaseController
include Concerns::CollectionScope

before_action :require_ordergroup

def index
render_collection search_scope
end

def show
render json: scope.find(params.require(:id))
end

private

def scope
current_ordergroup.financial_transactions
end

end
49 changes: 49 additions & 0 deletions app/controllers/concerns/collection_scope.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module Concerns::CollectionScope
extend ActiveSupport::Concern

private

def scope
raise NotImplementedError, 'Please override #scope when you use Concerns::CollectionScope'
end

def default_per_page
20
end

def max_per_page
250
end

def per_page
# allow max_per_page and default_per_page to be nil as well
if params[:per_page]
[params[:per_page].to_i, max_per_page].compact.min
else
default_per_page
end
end

def search_scope
s = scope
s = scope.ransack(params[:q]).result(distinct: true) if params[:q]
s = s.page(params[:page].to_i).per(per_page) if per_page && per_page >= 0
s
end

def render_collection(scope)
render json: scope, meta: collection_meta(scope)
end

def collection_meta(scope, extra = {})
return unless scope.respond_to?(:total_count) && per_page

{
page: params[:page].to_i,
per_page: per_page,
total_pages: (scope.total_count / [1, per_page].max).ceil,
total_count: scope.total_count
}.merge(extra)
end

end
8 changes: 8 additions & 0 deletions app/models/financial_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ class FinancialTransaction < ActiveRecord::Base
initialize_financial_transaction_type
end

def self.ransackable_attributes(auth_object = nil)
%w(id amount note created_on user_id)
end

def self.ransackable_associations(auth_object = nil)
%w() # none, and certainly not user until we've secured that more
end

# Use this save method instead of simple save and after callback
def add_transaction!
ordergroup.add_financial_transaction! amount, note, user, financial_transaction_type
Expand Down
18 changes: 18 additions & 0 deletions app/serializers/financial_transaction_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class FinancialTransactionSerializer < ActiveModel::Serializer
include ApplicationHelper

attributes :id, :user_id, :user_name, :amount, :note, :created_at

def user_name
show_user object.user
end

def amount
object.amount.to_f
end

def created_at
# use Rails convention in API, which is less easier to migrate later
object.created_on
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@
resource :user, only: [:show]
resource :config, only: [:show]
resource :navigation, only: [:show]
resources :financial_transactions, only: [:index, :show]
end
end

Expand Down

0 comments on commit 9f700bd

Please sign in to comment.