From 6c323eaf3ef6ec4390a1fa2e3d5509f8b236e641 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 31 Oct 2019 15:36:00 +0100 Subject: [PATCH] Use JavaScript pluralize for budget staff We were using two different systems to set translations in JavaScript: to set the text for languages, we were using data attributes, and to set the text for staff members, we were using AJAX calls. I find data attributes keep the code more simple, since there's no need to define an extra route and controller action. Furthermore, the user experience is better because response times are faster. So now both places use data attributes. --- .../javascripts/budget_edit_associations.js | 22 +++++-------------- app/controllers/admin/budgets_controller.rb | 7 ------ app/models/abilities/administrator.rb | 2 +- app/views/admin/budgets/_form.html.erb | 4 +--- config/routes/admin.rb | 1 - 5 files changed, 8 insertions(+), 28 deletions(-) diff --git a/app/assets/javascripts/budget_edit_associations.js b/app/assets/javascripts/budget_edit_associations.js index 213ce14ab..e37c6e27a 100644 --- a/app/assets/javascripts/budget_edit_associations.js +++ b/app/assets/javascripts/budget_edit_associations.js @@ -1,28 +1,18 @@ (function() { "use strict"; App.BudgetEditAssociations = { - set_text: function(response) { - $(".js-budget-show-administrators-list").text(response.administrators); - $(".js-budget-show-valuators-list").text(response.valuators); - $(".js-budget-show-trackers-list").text(response.trackers); - }, initialize: function() { $(".js-budget-list-checkbox-user").on({ click: function() { - var admin_count, budget, params, tracker_count, url, valuator_count; + var admin_count, tracker_count, valuator_count; + admin_count = $(".js-budget-list-checkbox-administrators:checkbox:checked").length; valuator_count = $(".js-budget-list-checkbox-valuators:checkbox:checked").length; tracker_count = $(".js-budget-list-checkbox-trackers:checkbox:checked").length; - budget = $(".js-budget-id").attr("id"); - url = "/admin/budgets/" + budget + "/assigned_users_translation.json"; - params = { - administrators: admin_count, - valuators: valuator_count, - trackers: tracker_count - }; - $.get(url, params, function(response) { - App.BudgetEditAssociations.set_text(response, "json"); - }); + + App.I18n.set_pluralize($(".js-budget-show-administrators-list"), admin_count); + App.I18n.set_pluralize($(".js-budget-show-valuators-list"), valuator_count); + App.I18n.set_pluralize($(".js-budget-show-trackers-list"), tracker_count); } }); $(".js-budget-show-users-list").on({ diff --git a/app/controllers/admin/budgets_controller.rb b/app/controllers/admin/budgets_controller.rb index 39475de36..40bb1727f 100644 --- a/app/controllers/admin/budgets_controller.rb +++ b/app/controllers/admin/budgets_controller.rb @@ -72,13 +72,6 @@ class Admin::BudgetsController < Admin::BaseController end end - def assigned_users_translation - render json: { administrators: t("admin.budgets.edit.administrators", count: params[:administrators].to_i), - valuators: t("admin.budgets.edit.valuators", count: params[:valuators].to_i), - trackers: t("admin.budgets.edit.trackers", count: params[:trackers].to_i) - } - end - private def budget_params diff --git a/app/models/abilities/administrator.rb b/app/models/abilities/administrator.rb index 05b66c78f..07238f548 100644 --- a/app/models/abilities/administrator.rb +++ b/app/models/abilities/administrator.rb @@ -59,7 +59,7 @@ module Abilities can :manage, Dashboard::Action - can [:index, :read, :new, :create, :update, :destroy, :calculate_winners, :assigned_users_translation], Budget + can [:index, :read, :new, :create, :update, :destroy, :calculate_winners], Budget can [:read, :create, :update, :destroy], Budget::Group can [:read, :create, :update, :destroy], Budget::Heading can [:hide, :admin_update, :toggle_selection], Budget::Investment diff --git a/app/views/admin/budgets/_form.html.erb b/app/views/admin/budgets/_form.html.erb index 484927d7a..ea8624a2a 100644 --- a/app/views/admin/budgets/_form.html.erb +++ b/app/views/admin/budgets/_form.html.erb @@ -2,8 +2,6 @@ <%= translatable_form_for [:admin, @budget] do |f| %> -
- <%= render "shared/errors", resource: @budget %>
@@ -29,7 +27,7 @@ <%= link_to t("admin.budgets.edit.#{staff}", count: @budget.send(staff).count), "#", class: "button expanded hollow js-budget-show-#{staff}-list js-budget-show-users-list", - data: { toggle: "#{staff}_list" } %> + data: { toggle: "#{staff}_list", texts: t("admin.budgets.edit.#{staff}") } %>
<% end %> diff --git a/config/routes/admin.rb b/config/routes/admin.rb index 79f8242ac..972f15842 100644 --- a/config/routes/admin.rb +++ b/config/routes/admin.rb @@ -54,7 +54,6 @@ namespace :admin do resources :budgets do member do put :calculate_winners - get :assigned_users_translation end resources :groups, except: [:show], controller: "budget_groups" do