Fetching Enterprise Fees for order cycle, using new enterprisefee serializer

This commit is contained in:
Rob Harrington
2015-04-06 13:06:21 +10:00
parent 7eb735f87e
commit d516795774
7 changed files with 65 additions and 12 deletions

View File

@@ -2,7 +2,7 @@ angular.module('admin.order_cycles', ['ngResource'])
.controller('AdminCreateOrderCycleCtrl', ['$scope', 'OrderCycle', 'Enterprise', 'EnterpriseFee', 'ocInstance', ($scope, OrderCycle, Enterprise, EnterpriseFee, ocInstance) ->
$scope.enterprises = Enterprise.index(coordinator_id: ocInstance.coordinator_id)
$scope.supplied_products = Enterprise.supplied_products
$scope.enterprise_fees = EnterpriseFee.index()
$scope.enterprise_fees = EnterpriseFee.index(coordinator_id: ocInstance.coordinator_id)
$scope.order_cycle = OrderCycle.new({ coordinator_id: ocInstance.coordinator_id})
@@ -83,7 +83,7 @@ angular.module('admin.order_cycles', ['ngResource'])
order_cycle_id = $location.absUrl().match(/\/admin\/order_cycles\/(\d+)/)[1]
$scope.enterprises = Enterprise.index(order_cycle_id: order_cycle_id)
$scope.supplied_products = Enterprise.supplied_products
$scope.enterprise_fees = EnterpriseFee.index()
$scope.enterprise_fees = EnterpriseFee.index(order_cycle_id: order_cycle_id)
$scope.order_cycle = OrderCycle.load(order_cycle_id)

View File

@@ -3,7 +3,7 @@ angular.module('admin.order_cycles').controller "AdminSimpleCreateOrderCycleCtrl
# TODO: make this a get method, which only fetches one enterprise
$scope.enterprises = Enterprise.index {coordinator_id: ocInstance.coordinator_id}, (enterprises) =>
$scope.init(enterprises)
$scope.enterprise_fees = EnterpriseFee.index()
$scope.enterprise_fees = EnterpriseFee.index(coordinator_id: ocInstance.coordinator_id)
$scope.init = (enterprises) ->
enterprise = enterprises[Object.keys(enterprises)[0]]

View File

@@ -3,7 +3,7 @@ angular.module('admin.order_cycles').controller "AdminSimpleEditOrderCycleCtrl",
$location.absUrl().match(/\/admin\/order_cycles\/(\d+)/)[1]
$scope.enterprises = Enterprise.index(order_cycle_id: $scope.orderCycleId())
$scope.enterprise_fees = EnterpriseFee.index()
$scope.enterprise_fees = EnterpriseFee.index(order_cycle_id: $scope.orderCycleId())
$scope.order_cycle = OrderCycle.load $scope.orderCycleId(), (order_cycle) =>
$scope.init()

View File

@@ -1,18 +1,24 @@
angular.module('admin.order_cycles').factory('EnterpriseFee', ($resource) ->
EnterpriseFee = $resource('/admin/enterprise_fees/:enterprise_fee_id.json', {}, {'index': {method: 'GET', isArray: true}})
EnterpriseFee = $resource('/admin/enterprise_fees/for_order_cycle/:enterprise_fee_id.json', {}, {
'index':
method: 'GET'
isArray: true
params:
order_cycle_id: '@order_cycle_id'
coordinator_id: '@coordinator_id'
})
{
EnterpriseFee: EnterpriseFee
enterprise_fees: {}
loaded: false
index: ->
index: (params={}) ->
service = this
EnterpriseFee.index (data) ->
EnterpriseFee.index params, (data) ->
service.enterprise_fees = data
service.loaded = true
forEnterprise: (enterprise_id) ->
enterprise_fee for enterprise_fee in this.enterprise_fees when enterprise_fee.enterprise_id == enterprise_id
})

View File

@@ -20,6 +20,17 @@ module Admin
end
end
def for_order_cycle
respond_to do |format|
format.html
format.json do
render json: ActiveModel::ArraySerializer.new( @collection,
each_serializer: Api::Admin::EnterpriseFeeSerializer, controller: self
).to_json
end
end
end
def bulk_update
@enterprise_fee_set = EnterpriseFeeSet.new(params[:enterprise_fee_set])
if @enterprise_fee_set.save
@@ -63,9 +74,22 @@ module Admin
end
def collection
collection = EnterpriseFee.managed_by(spree_current_user).order('enterprise_id', 'fee_type', 'name')
collection = collection.for_enterprise(current_enterprise) if current_enterprise
collection
case action
when :for_order_cycle
options = {}
options[:coordinator] = Enterprise.find(params[:coordinator_id]) if params[:coordinator_id]
options[:order_cycle] = OrderCycle.find(params[:order_cycle_id]) if params[:order_cycle_id]
enterprises = OpenFoodNetwork::Permissions.new(spree_current_user).order_cycle_enterprises_for(options)
return EnterpriseFee.for_enterprises(enterprises).order('enterprise_id', 'fee_type', 'name')
else
collection = EnterpriseFee.managed_by(spree_current_user).order('enterprise_id', 'fee_type', 'name')
collection = collection.for_enterprise(current_enterprise) if current_enterprise
collection
end
end
def collection_actions
[:index, :for_order_cycle]
end
def current_enterprise

View File

@@ -0,0 +1,23 @@
class Api::Admin::EnterpriseFeeSerializer < ActiveModel::Serializer
attributes :id, :enterprise_id, :fee_type, :name, :tax_category_id, :calculator_type
attributes :enterprise_name, :calculator_description, :calculator_settings
def enterprise_name
object.enterprise.andand.name
end
def calculator_description
object.calculator.andand.description
end
def calculator_settings
result = nil
options[:controller].send(:with_format, :html) do
result = options[:controller].render_to_string :partial => 'admin/enterprise_fees/calculator_settings', :locals => {:enterprise_fee => object}
end
result.gsub('[0]', '[{{ $index }}]').gsub('_0_', '_{{ $index }}_')
end
end

View File

@@ -396,7 +396,7 @@ describe 'OrderCycle services', ->
inject ($injector, _$httpBackend_)->
EnterpriseFee = $injector.get('EnterpriseFee')
$httpBackend = _$httpBackend_
$httpBackend.whenGET('/admin/enterprise_fees.json').respond [
$httpBackend.whenGET('/admin/enterprise_fees/for_order_cycle.json?').respond [
{id: 1, name: "Yayfee", enterprise_id: 1}
{id: 2, name: "FeeTwo", enterprise_id: 2}
]