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.
This commit is contained in:
Javi Martín
2019-10-31 15:36:00 +01:00
parent 7b89dd6a5f
commit 6c323eaf3e
5 changed files with 8 additions and 28 deletions

View File

@@ -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({

View File

@@ -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

View File

@@ -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

View File

@@ -2,8 +2,6 @@
<%= translatable_form_for [:admin, @budget] do |f| %>
<div class="js-budget-id" id="<%= @budget.id %>"></div>
<%= render "shared/errors", resource: @budget %>
<div class="row">
@@ -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}") } %>
</div>
<% end %>
</div>

View File

@@ -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