From e503d6edd8bdb00865c5e8c98dc5d2ec29cb6655 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Tue, 14 Jun 2016 15:42:28 +0200 Subject: [PATCH] optimize trainings API --- CHANGELOG.md | 1 + .../controllers/admin/pricing.coffee.erb | 32 +++++++------------ app/assets/javascripts/router.coffee.erb | 14 +++++++- app/controllers/api/trainings_controller.rb | 1 + app/views/api/trainings/index.json.jbuilder | 2 +- 5 files changed, 27 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4fff6eff6..2ff69ae42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - User public profile: UI re-design with possible admin's customization - Admin: Invoices list and users list are now loaded per 10 items to improve pages load time - Admin: select member (eg. to buy a subscription for a member) is now loading the user's list dynamically when you type +- API: GET /api/trainings do not send the associated availabilities until they are requested - [TODO DEPLOY] Regenerate the theme stylesheet (easy way: Customization/General/Main colour -> "Save") - [TODO DEPLOY] `bundle install` and `rake db:migrate` diff --git a/app/assets/javascripts/controllers/admin/pricing.coffee.erb b/app/assets/javascripts/controllers/admin/pricing.coffee.erb index 7a60717df..c6b959c4a 100644 --- a/app/assets/javascripts/controllers/admin/pricing.coffee.erb +++ b/app/assets/javascripts/controllers/admin/pricing.coffee.erb @@ -3,8 +3,8 @@ ## # Controller used in the prices edition page ## -Application.Controllers.controller "EditPricingController", ["$scope", "$state", '$uibModal', 'Training', 'TrainingsPricing', 'Machine', '$filter', 'Credit', 'Pricing', 'Plan', 'plans', 'groups', 'growl', 'machinesPricesPromise', 'Price', 'dialogs', 'trainingsPricingsPromise', '_t' -, ($scope, $state, $uibModal, Training, TrainingsPricing, Machine, $filter, Credit, Pricing, Plan, plans, groups, growl, machinesPricesPromise, Price, dialogs, trainingsPricingsPromise, _t) -> +Application.Controllers.controller "EditPricingController", ["$scope", "$state", '$uibModal', 'TrainingsPricing', '$filter', 'Credit', 'Pricing', 'Plan', 'plans', 'groups', 'growl', 'machinesPricesPromise', 'Price', 'dialogs', 'trainingsPricingsPromise', 'trainingsPromise', 'machineCreditsPromise', 'machinesPromise', 'trainingCreditsPromise', '_t' +, ($scope, $state, $uibModal, TrainingsPricing, $filter, Credit, Pricing, Plan, plans, groups, growl, machinesPricesPromise, Price, dialogs, trainingsPricingsPromise, trainingsPromise, machineCreditsPromise, machinesPromise, trainingCreditsPromise, _t) -> ### PUBLIC SCOPE ### ## List of machines prices (not considering any plan) @@ -20,19 +20,19 @@ Application.Controllers.controller "EditPricingController", ["$scope", "$state", $scope.groups = groups ## Associate free machine hours with subscriptions - $scope.machineCredits = [] + $scope.machineCredits = machineCreditsPromise ## Array of associations (plan <-> training) - $scope.trainingCredits = [] + $scope.trainingCredits = trainingCreditsPromise ## Associate a plan with all its trainings ids $scope.trainingCreditsGroups = {} ## List of trainings - $scope.trainings = [] + $scope.trainings = trainingsPromise ## List of machines - $scope.machines = [] + $scope.machines = machinesPromise ## The plans list ordering. Default: by group $scope.orderPlans = 'group_id' @@ -320,23 +320,13 @@ Application.Controllers.controller "EditPricingController", ["$scope", "$state", ## initialize = -> - Credit.query({creditable_type: 'Training'}).$promise.then (data)-> - $scope.trainingCredits = data - $scope.trainingCreditsGroups = groupCreditsByPlan(data) + $scope.trainingCreditsGroups = groupCreditsByPlan($scope.trainingCredits) - ## adds empty array for plan which hasn't any credits yet - for plan in $scope.plans - unless $scope.trainingCreditsGroups[plan.id]? - $scope.trainingCreditsGroups[plan.id] = [] + ## adds empty array for plan which hasn't any credits yet + for plan in $scope.plans + unless $scope.trainingCreditsGroups[plan.id]? + $scope.trainingCreditsGroups[plan.id] = [] - Credit.query({creditable_type: 'Machine'}).$promise.then (data)-> - $scope.machineCredits = data - - Training.query().$promise.then (data)-> - $scope.trainings = data - - Machine.query().$promise.then (data)-> - $scope.machines = data ## diff --git a/app/assets/javascripts/router.coffee.erb b/app/assets/javascripts/router.coffee.erb index cc8fff873..cf1485624 100644 --- a/app/assets/javascripts/router.coffee.erb +++ b/app/assets/javascripts/router.coffee.erb @@ -515,7 +515,7 @@ angular.module('application.router', ['ui.router']). controller: 'TrainingsController' resolve: trainingsPromise: ['Training', (Training)-> - Training.query().$promise + Training.query({requested_attributes:'[availabilities]'}).$promise ] machinesPromise: ['Machine', (Machine)-> Machine.query().$promise @@ -601,6 +601,18 @@ angular.module('application.router', ['ui.router']). translations: [ 'Translations', (Translations) -> Translations.query('app.admin.pricing').$promise ] + trainingsPromise: ['Training', (Training) -> + Training.query().$promise + ] + machineCreditsPromise: ['Credit', (Credit) -> + Credit.query({creditable_type: 'Machine'}).$promise + ] + machinesPromise: ['Machine', (Machine) -> + Machine.query().$promise + ] + trainingCreditsPromise: ['Credit', (Credit) -> + Credit.query({creditable_type: 'Training'}).$promise + ] # plans .state 'app.admin.plans', diff --git a/app/controllers/api/trainings_controller.rb b/app/controllers/api/trainings_controller.rb index b06944bf5..87275549a 100644 --- a/app/controllers/api/trainings_controller.rb +++ b/app/controllers/api/trainings_controller.rb @@ -3,6 +3,7 @@ class API::TrainingsController < API::ApiController before_action :set_training, only: [:show, :update, :destroy] def index + @requested_attributes = params[:requested_attributes] @trainings = policy_scope(Training) end diff --git a/app/views/api/trainings/index.json.jbuilder b/app/views/api/trainings/index.json.jbuilder index 95f0b6e27..72ff1ff85 100644 --- a/app/views/api/trainings/index.json.jbuilder +++ b/app/views/api/trainings/index.json.jbuilder @@ -12,7 +12,7 @@ json.array!(@trainings) do |training| json.full_name slot.reservation.user.profile.full_name json.is_valid slot.reservation.user.trainings.include?(training) end - end + end if attribute_requested?(@requested_attributes, 'availabilities') json.nb_total_places training.nb_total_places json.plan_ids training.plan_ids if current_user and current_user.is_admin?