mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Add api/shops_controller and refactor
This commit is contained in:
@@ -24,7 +24,7 @@ Darkswarm.controller "HubNodeCtrl", ($scope, HashNavigation, CurrentHub, $http,
|
||||
$scope.shopfront_loading = true
|
||||
$scope.toggle_tab(event)
|
||||
|
||||
$http.get("/api/enterprises/" + $scope.hub.id + "/shopfront")
|
||||
$http.get("/api/shops/" + $scope.hub.id)
|
||||
.success (data) ->
|
||||
$scope.shopfront_loading = false
|
||||
$scope.hub = data
|
||||
|
||||
@@ -24,7 +24,7 @@ Darkswarm.controller "ProducerNodeCtrl", ($scope, HashNavigation, $anchorScroll,
|
||||
$scope.shopfront_loading = true
|
||||
$scope.toggle_tab(event)
|
||||
|
||||
$http.get("/api/enterprises/" + $scope.producer.id + "/shopfront")
|
||||
$http.get("/api/shops/" + $scope.producer.id)
|
||||
.success (data) ->
|
||||
$scope.shopfront_loading = false
|
||||
$scope.producer = data
|
||||
|
||||
@@ -5,7 +5,7 @@ Darkswarm.factory "EnterpriseModal", ($modal, $rootScope, $http)->
|
||||
scope = $rootScope.$new(true) # Spawn an isolate to contain the enterprise
|
||||
scope.embedded_layout = window.location.search.indexOf("embedded_shopfront=true") != -1
|
||||
|
||||
$http.get("/api/enterprises/" + enterprise.id + "/shopfront").success (data) ->
|
||||
$http.get("/api/shops/" + enterprise.id).success (data) ->
|
||||
scope.enterprise = data
|
||||
$modal.open(templateUrl: "enterprise_modal.html", scope: scope)
|
||||
.error (data) ->
|
||||
|
||||
@@ -5,8 +5,4 @@ Darkswarm.factory 'EnterpriseResource', ($resource) ->
|
||||
url: '/enterprises/:id/relatives.json'
|
||||
isArray: true
|
||||
cache: true
|
||||
'closed_shops':
|
||||
method: 'GET'
|
||||
isArray: true
|
||||
url: '/api/enterprises/closed_shops.json'
|
||||
})
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
Darkswarm.factory 'Enterprises', (enterprises, EnterpriseResource, CurrentHub, Taxons, Dereferencer, Matcher, Geo, $rootScope) ->
|
||||
Darkswarm.factory 'Enterprises', (enterprises, ShopsResource, CurrentHub, Taxons, Dereferencer, Matcher, Geo, $rootScope) ->
|
||||
new class Enterprises
|
||||
enterprises: []
|
||||
enterprises_by_id: {}
|
||||
@@ -46,7 +46,7 @@ Darkswarm.factory 'Enterprises', (enterprises, EnterpriseResource, CurrentHub, T
|
||||
@enterprises_by_id[enterprise.id] = enterprise
|
||||
|
||||
loadClosedEnterprises: ->
|
||||
request = EnterpriseResource.closed_shops {}, (data) =>
|
||||
request = ShopsResource.closed_shops {}, (data) =>
|
||||
@initEnterprises(data)
|
||||
|
||||
request.$promise
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
Darkswarm.factory 'ShopsResource', ($resource) ->
|
||||
$resource('/api/shops/:id.json', {}, {
|
||||
'closed_shops':
|
||||
method: 'GET'
|
||||
isArray: true
|
||||
url: '/api/shops/closed_shops.json'
|
||||
})
|
||||
@@ -5,7 +5,6 @@ module Api
|
||||
before_filter :override_sells, only: [:create, :update]
|
||||
before_filter :override_visible, only: [:create, :update]
|
||||
respond_to :json
|
||||
skip_authorization_check only: [:shopfront, :closed_shops]
|
||||
|
||||
def create
|
||||
authorize! :create, Enterprise
|
||||
@@ -42,25 +41,6 @@ module Api
|
||||
end
|
||||
end
|
||||
|
||||
def shopfront
|
||||
enterprise = Enterprise.find_by_id(params[:id])
|
||||
|
||||
render text: Api::EnterpriseShopfrontSerializer.new(enterprise).to_json, status: :ok
|
||||
end
|
||||
|
||||
def closed_shops
|
||||
@active_distributor_ids = []
|
||||
@earliest_closing_times = []
|
||||
|
||||
serialized_closed_shops = ActiveModel::ArraySerializer.new(
|
||||
ShopsListService.new.closed_shops,
|
||||
each_serializer: Api::EnterpriseSerializer,
|
||||
data: OpenFoodNetwork::EnterpriseInjectionData.new
|
||||
)
|
||||
|
||||
render json: serialized_closed_shops
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def override_owner
|
||||
|
||||
27
app/controllers/api/shops_controller.rb
Normal file
27
app/controllers/api/shops_controller.rb
Normal file
@@ -0,0 +1,27 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
class ShopsController < BaseController
|
||||
respond_to :json
|
||||
skip_authorization_check only: [:show, :closed_shops]
|
||||
|
||||
def show
|
||||
enterprise = Enterprise.find_by_id(params[:id])
|
||||
|
||||
render text: Api::EnterpriseShopfrontSerializer.new(enterprise).to_json, status: :ok
|
||||
end
|
||||
|
||||
def closed_shops
|
||||
@active_distributor_ids = []
|
||||
@earliest_closing_times = []
|
||||
|
||||
serialized_closed_shops = ActiveModel::ArraySerializer.new(
|
||||
ShopsListService.new.closed_shops,
|
||||
each_serializer: Api::EnterpriseSerializer,
|
||||
data: OpenFoodNetwork::EnterpriseInjectionData.new
|
||||
)
|
||||
|
||||
render json: serialized_closed_shops
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -33,11 +33,9 @@ Openfoodnetwork::Application.routes.draw do
|
||||
|
||||
resource :logo, only: [:destroy]
|
||||
resource :promo_image, only: [:destroy]
|
||||
end
|
||||
|
||||
member do
|
||||
get :shopfront
|
||||
end
|
||||
|
||||
resources :shops, only: [:show] do
|
||||
collection do
|
||||
get :closed_shops
|
||||
end
|
||||
|
||||
@@ -81,41 +81,5 @@ module Api
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "as a non-authenticated user" do
|
||||
let!(:hub) {
|
||||
create(:distributor_enterprise, with_payment_and_shipping: true, name: 'Shopfront Test Hub')
|
||||
}
|
||||
let!(:producer) { create(:supplier_enterprise, name: 'Shopfront Test Producer') }
|
||||
let!(:category) { create(:taxon, name: 'Fruit') }
|
||||
let!(:product) { create(:product, supplier: producer, primary_taxon: category ) }
|
||||
let!(:relationship) { create(:enterprise_relationship, parent: hub, child: producer) }
|
||||
let!(:closed_hub1) { create(:distributor_enterprise) }
|
||||
let!(:closed_hub2) { create(:distributor_enterprise) }
|
||||
|
||||
before do
|
||||
allow(controller).to receive(:spree_current_user) { nil }
|
||||
end
|
||||
|
||||
describe "#shopfront" do
|
||||
it "returns shopfront data for an enterprise" do
|
||||
spree_get :shopfront, id: producer.id, format: :json
|
||||
|
||||
expect(json_response['name']).to eq 'Shopfront Test Producer'
|
||||
expect(json_response['hubs'][0]['name']).to eq 'Shopfront Test Hub'
|
||||
expect(json_response['supplied_taxons'][0]['name']).to eq 'Fruit'
|
||||
end
|
||||
end
|
||||
|
||||
describe "#closed_shops" do
|
||||
it "returns data for all closed shops" do
|
||||
spree_get :closed_shops, nil, format: :json
|
||||
|
||||
expect(json_response).not_to match hub.name
|
||||
expect(json_response[0]['id']).to eq closed_hub1.id
|
||||
expect(json_response[1]['id']).to eq closed_hub2.id
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
46
spec/controllers/api/shops_controller_spec.rb
Normal file
46
spec/controllers/api/shops_controller_spec.rb
Normal file
@@ -0,0 +1,46 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
module Api
|
||||
describe ShopsController, type: :controller do
|
||||
include AuthenticationWorkflow
|
||||
render_views
|
||||
|
||||
context "as a non-authenticated user" do
|
||||
let!(:hub) {
|
||||
create(:distributor_enterprise, with_payment_and_shipping: true, name: 'Shopfront Test Hub')
|
||||
}
|
||||
let!(:producer) { create(:supplier_enterprise, name: 'Shopfront Test Producer') }
|
||||
let!(:category) { create(:taxon, name: 'Fruit') }
|
||||
let!(:product) { create(:product, supplier: producer, primary_taxon: category ) }
|
||||
let!(:relationship) { create(:enterprise_relationship, parent: hub, child: producer) }
|
||||
let!(:closed_hub1) { create(:distributor_enterprise) }
|
||||
let!(:closed_hub2) { create(:distributor_enterprise) }
|
||||
|
||||
before do
|
||||
allow(controller).to receive(:spree_current_user) { nil }
|
||||
end
|
||||
|
||||
describe "#show" do
|
||||
it "returns shopfront data for an enterprise" do
|
||||
spree_get :show, id: producer.id
|
||||
|
||||
expect(json_response['name']).to eq 'Shopfront Test Producer'
|
||||
expect(json_response['hubs'][0]['name']).to eq 'Shopfront Test Hub'
|
||||
expect(json_response['supplied_taxons'][0]['name']).to eq 'Fruit'
|
||||
end
|
||||
end
|
||||
|
||||
describe "#closed_shops" do
|
||||
it "returns data for all closed shops" do
|
||||
spree_get :closed_shops, nil
|
||||
|
||||
expect(json_response).not_to match hub.name
|
||||
expect(json_response[0]['id']).to eq closed_hub1.id
|
||||
expect(json_response[1]['id']).to eq closed_hub2.id
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user