From 4d0d3d6af0d4fad44eb6dcf19b5b02724984de1a Mon Sep 17 00:00:00 2001 From: Peng DU Date: Tue, 21 Jun 2016 14:49:39 +0200 Subject: [PATCH] optimise load plans json --- .../controllers/admin/plans.coffee.erb | 19 ++++++++++--------- app/assets/javascripts/router.coffee.erb | 6 +++--- app/controllers/api/plans_controller.rb | 9 ++------- app/views/api/plans/_plan.json.jbuilder | 15 --------------- app/views/api/plans/index.json.jbuilder | 8 +++++++- app/views/api/prices/_price.json.jbuilder | 1 + 6 files changed, 23 insertions(+), 35 deletions(-) diff --git a/app/assets/javascripts/controllers/admin/plans.coffee.erb b/app/assets/javascripts/controllers/admin/plans.coffee.erb index c5bf1151b..d7b266f1a 100644 --- a/app/assets/javascripts/controllers/admin/plans.coffee.erb +++ b/app/assets/javascripts/controllers/admin/plans.coffee.erb @@ -175,8 +175,8 @@ Application.Controllers.controller 'NewPlanController', ['$scope', '$uibModal', ## # Controller used in the plan edition form ## -Application.Controllers.controller 'EditPlanController', ['$scope', 'groups', 'plans', 'planPromise', 'machines', 'prices', 'partners', 'CSRF', '$state', '$stateParams', 'growl', '$filter', '_t', '$locale' -, ($scope, groups, plans, planPromise, machines, prices, partners, CSRF, $state, $stateParams, growl, $filter, _t, $locale) -> +Application.Controllers.controller 'EditPlanController', ['$scope', 'groups', 'plans', 'planPromise', 'machines', 'prices', 'partners', 'CSRF', '$state', '$stateParams', 'growl', '$filter', '_t', '$locale', 'Plan' +, ($scope, groups, plans, planPromise, machines, prices, partners, CSRF, $state, $stateParams, growl, $filter, _t, $locale, Plan) -> @@ -207,12 +207,13 @@ Application.Controllers.controller 'EditPlanController', ['$scope', 'groups', 'p ## $scope.copyPricesFromPlan = -> if $scope.plan.parent - parentPlan = $scope.getPlanFromId($scope.plan.parent) - for parentPrice in parentPlan.prices - for childKey, childPrice of $scope.plan.prices - if childPrice.priceable_type == parentPrice.priceable_type and childPrice.priceable_id == parentPrice.priceable_id - $scope.plan.prices[childKey].amount = parentPrice.amount - break + Plan.get {id: $scope.plan.parent}, (parentPlan) -> + for parentPrice in parentPlan.prices + for childKey, childPrice of $scope.plan.prices + if childPrice.priceable_type == parentPrice.priceable_type and childPrice.priceable_id == parentPrice.priceable_id + $scope.plan.prices[childKey].amount = parentPrice.amount + break + # if no plan were selected, unset every prices else for key, price of $scope.plan.prices @@ -257,4 +258,4 @@ Application.Controllers.controller 'EditPlanController', ['$scope', 'groups', 'p ## !!! MUST BE CALLED AT THE END of the controller initialize() -] \ No newline at end of file +] diff --git a/app/assets/javascripts/router.coffee.erb b/app/assets/javascripts/router.coffee.erb index 7f1416510..e3d47ad4c 100644 --- a/app/assets/javascripts/router.coffee.erb +++ b/app/assets/javascripts/router.coffee.erb @@ -330,7 +330,7 @@ angular.module('application.router', ['ui.router']). controller: 'ReserveMachineController' resolve: plansPromise: ['Plan', (Plan)-> - Plan.query(attributes_requested: "['machines_credits']").$promise + Plan.query().$promise ] groupsPromise: ['Group', (Group)-> Group.query().$promise @@ -375,7 +375,7 @@ angular.module('application.router', ['ui.router']). Setting.get(name: 'training_explications_alert').$promise ] plansPromise: ['Plan', (Plan)-> - Plan.query(attributes_requested: "['trainings_credits']").$promise + Plan.query().$promise ] groupsPromise: ['Group', (Group)-> Group.query().$promise @@ -423,7 +423,7 @@ angular.module('application.router', ['ui.router']). Setting.get(name: 'subscription_explications_alert').$promise ] plansPromise: ['Plan', (Plan)-> - Plan.query(shallow: true).$promise + Plan.query().$promise ] groupsPromise: ['Group', (Group)-> Group.query().$promise diff --git a/app/controllers/api/plans_controller.rb b/app/controllers/api/plans_controller.rb index b9b922112..7b7f0d203 100644 --- a/app/controllers/api/plans_controller.rb +++ b/app/controllers/api/plans_controller.rb @@ -2,14 +2,9 @@ before_action :authenticate_user!, except: [:index] def index - @attributes_requested = params[:attributes_requested] - @plans = Plan.all + @plans = Plan.includes(:plan_file) @plans = @plans.where(group_id: params[:group_id]) if params[:group_id] - if params[:shallow] - render :shallow_index - else - render :index - end + render :index end def show diff --git a/app/views/api/plans/_plan.json.jbuilder b/app/views/api/plans/_plan.json.jbuilder index bda984fbe..ca0eb1292 100644 --- a/app/views/api/plans/_plan.json.jbuilder +++ b/app/views/api/plans/_plan.json.jbuilder @@ -6,23 +6,8 @@ json.plan_file_attributes do json.attachment_identifier plan.plan_file.attachment_identifier end if plan.plan_file -json.prices plan.prices do |price| - json.extract! price, :id, :group_id, :plan_id, :priceable_type, :priceable_id - json.amount price.amount / 100.0 - json.priceable_name price.priceable.name -end - json.partners plan.partners do |partner| json.first_name partner.first_name json.last_name partner.last_name json.email partner.email end if plan.respond_to?(:partners) - -json.training_credits plan.training_credits do |tc| - json.training_id tc.creditable_id -end if attribute_requested?(@attributes_requested, 'trainings_credits') - -json.machine_credits plan.machine_credits do |mc| - json.machine_id mc.creditable_id - json.hours mc.hours -end if attribute_requested?(@attributes_requested, 'machines_credits') diff --git a/app/views/api/plans/index.json.jbuilder b/app/views/api/plans/index.json.jbuilder index a63916500..46e7fb9bd 100644 --- a/app/views/api/plans/index.json.jbuilder +++ b/app/views/api/plans/index.json.jbuilder @@ -1 +1,7 @@ -json.partial! 'api/plans/plan', collection: @plans, as: :plan +json.cache! @plans do + json.array!(@plans) do |plan| + json.extract! plan, :id, :base_name, :name, :interval, :interval_count, :group_id, :training_credit_nb, :description, :type, :ui_weight + json.amount (plan.amount / 100.00) + json.plan_file_url plan.plan_file.attachment_url if plan.plan_file + end +end diff --git a/app/views/api/prices/_price.json.jbuilder b/app/views/api/prices/_price.json.jbuilder index 19a0ffc06..ca3aea1c4 100644 --- a/app/views/api/prices/_price.json.jbuilder +++ b/app/views/api/prices/_price.json.jbuilder @@ -1,2 +1,3 @@ json.extract! price, :id, :group_id, :plan_id, :priceable_type, :priceable_id json.amount price.amount / 100.0 +json.priceable_name price.priceable.name