Incrementally load products

This commit is contained in:
Rohan Mitchell
2014-11-21 12:44:59 +11:00
parent 001bf999d0
commit 0b030a85ff
7 changed files with 55 additions and 9 deletions

View File

@@ -1,8 +1,27 @@
angular.module("ofn.admin").controller "AdminOverrideVariantsCtrl", ($scope, Indexer, hubs, producers, products) ->
angular.module("ofn.admin").controller "AdminOverrideVariantsCtrl", ($scope, Indexer, SpreeApiAuth, PagedFetcher, hubs, producers) ->
$scope.hubs = hubs
$scope.hub = null
$scope.products = products
$scope.products = []
$scope.producers = Indexer.index producers
$scope.initialise = ->
SpreeApiAuth.authorise()
.then ->
$scope.spree_api_key_ok = true
$scope.fetchProducts()
.catch (message) ->
$scope.api_error_msg = message
$scope.fetchProducts = ->
url = "/api/products/distributable?page=::page::;per_page=100"
PagedFetcher.fetch url, (data) => $scope.addProducts data.products
$scope.addProducts = (products) ->
$scope.products = $scope.products.concat products
$scope.selectHub = ->
$scope.hub = (hub for hub in hubs when hub.id == $scope.hub_id)[0]
$scope.hub = (hub for hub in hubs when hub.id == $scope.hub_id)[0]

View File

@@ -4,7 +4,7 @@ Spree::Admin::ProductsController.class_eval do
include OpenFoodNetwork::SpreeApiKeyLoader
include OrderCyclesHelper
before_filter :load_form_data, :only => [:bulk_edit, :new, :create, :edit, :update]
before_filter :load_spree_api_key, :only => :bulk_edit
before_filter :load_spree_api_key, :only => [:bulk_edit, :override_variants]
alias_method :location_after_save_original, :location_after_save
@@ -52,7 +52,6 @@ Spree::Admin::ProductsController.class_eval do
def override_variants
@hubs = order_cycle_hub_enterprises(without_validation: true)
@producers = order_cycle_producer_enterprises
@products = Spree::Product.not_deleted.where(supplier_id: @producers).by_producer.by_name
end

View File

@@ -9,6 +9,7 @@ Spree::Api::ProductsController.class_eval do
respond_with(@products, default_template: :index)
end
# TODO: This should be named 'managed'. Is the action above used? Maybe we should remove it.
def bulk_products
@products = OpenFoodNetwork::Permissions.new(current_api_user).managed_products.
merge(product_scope).
@@ -19,6 +20,20 @@ Spree::Api::ProductsController.class_eval do
render text: { products: ActiveModel::ArraySerializer.new(@products, each_serializer: Spree::Api::ProductSerializer), pages: @products.num_pages }.to_json
end
def distributable
producers = OpenFoodNetwork::Permissions.new(current_api_user).
order_cycle_enterprises.is_primary_producer.by_name
@products = Spree::Product.scoped.
merge(product_scope).
where(supplier_id: producers).
by_producer.by_name.
ransack(params[:q]).result.
page(params[:page]).per(params[:per_page])
render text: { products: ActiveModel::ArraySerializer.new(@products, each_serializer: Spree::Api::ProductSerializer), pages: @products.num_pages }.to_json
end
def soft_delete
authorize! :delete, Spree::Product
@product = find_product(params[:product_id])

View File

@@ -1,3 +1,3 @@
= admin_inject_spree_api_key
= admin_inject_hubs
= admin_inject_producers
= admin_inject_products

View File

@@ -7,7 +7,7 @@
%th On hand
%tbody
%tr{ng: {repeat: 'product in products'}}
%td {{ producers[product.producer].name }}
%td {{ producers[product.producer_id].name }}
%td {{ product.name }}
%td {{ product.price }}
%td {{ product.on_hand }}

View File

@@ -132,8 +132,11 @@ Spree::Core::Engine.routes.prepend do
end
resources :products do
get :managed, on: :collection
get :bulk_products, on: :collection
collection do
get :managed
get :bulk_products
get :distributable
end
delete :soft_delete
resources :variants do

View File

@@ -7,6 +7,9 @@ describe "OverrideVariantsCtrl", ->
beforeEach ->
module 'ofn.admin'
module ($provide) ->
$provide.value 'SpreeApiKey', 'API_KEY'
null
scope = {}
inject ($controller, Indexer) ->
@@ -16,6 +19,13 @@ describe "OverrideVariantsCtrl", ->
expect(scope.hubs).toEqual hubs
expect(scope.hub).toBeNull
it "adds products", ->
expect(scope.products).toEqual []
scope.addProducts ['a', 'b']
expect(scope.products).toEqual ['a', 'b']
scope.addProducts ['c', 'd']
expect(scope.products).toEqual ['a', 'b', 'c', 'd']
describe "selecting a hub", ->
it "sets the chosen hub", ->
scope.hub_id = 1