Files
grecia/app/controllers/admin/budget_investments_controller.rb
Javi Martín 37e7eeb6e1 Don't redirect when toggling visible to valuators
After upgrading to Turbolinks 5, redirects are followed on AJAX
requests, so we were accidentally redirecting the user after they mark
an investment as visible to valuators.

There was already a system spec failing due to this issue ("Admin budget
investments Mark as visible to valuators Keeps the valuation tags");
however, it only failed in some cases, so we're adding additional tests.

Ideally we would write a system test to check what happens when users
click on the checkbox. However, from the user's point of view, nothing
happens when they do so, and so testing it is hard. There's a usability
issue here (no feedback is provided to the user indicating the
investment is actually updated when they click on the checkbox and so
they might look for a button to send the form), which also results in a
feature which is difficult to test.

So we're writing two tests instead: one checking the controller does not
redirect when using a JSON request, and one checking the form submits a
JSON request.

I've chosen JSON over AJAX because usually requests to the update action
come from the edit form, and we might change the edit form to send an
AJAX request (and, in this case, Turbolinks would handle the redirect as
mentioned above).

Another option would be to send an AJAX request to a different action,
like it's done for the toggle selection action. I don't have a strong
preference for either option, so I'm leaving it the way it was. At some
point we should change the user interface, though; right now in the same
row there are two actions doing basically the same thing (toggling
valuator visibility and toggling selection) but with very different user
interfaces (one is a checkbox and the other one a link changing its
style depending on the state), resulting in a confusing interface.
2020-10-26 15:12:39 +01:00

138 lines
3.8 KiB
Ruby

class Admin::BudgetInvestmentsController < Admin::BaseController
include FeatureFlags
include CommentableActions
include Translatable
feature_flag :budgets
has_orders %w[oldest], only: [:show, :edit]
has_filters %w[all], only: [:index, :toggle_selection]
before_action :load_budget
before_action :load_investment, only: [:show, :edit, :update, :toggle_selection]
before_action :load_ballot, only: [:show, :index]
before_action :parse_valuation_filters
before_action :load_investments, only: [:index, :toggle_selection]
def index
load_tags
respond_to do |format|
format.html
format.js
format.csv do
send_data Budget::Investment::Exporter.new(@investments).to_csv,
filename: "budget_investments.csv"
end
end
end
def show
load_comments
end
def edit
authorize! :admin_update, @investment
load_staff
load_valuator_groups
load_tags
end
def update
authorize! :admin_update, @investment
respond_to do |format|
format.html do
if @investment.update(budget_investment_params)
redirect_to admin_budget_budget_investment_path(@budget,
@investment,
Budget::Investment.filter_params(params).to_h),
notice: t("flash.actions.update.budget_investment")
else
load_staff
load_valuator_groups
load_tags
render :edit
end
end
format.json do
@investment.update!(budget_investment_params)
end
end
end
def toggle_selection
authorize! :toggle_selection, @investment
@investment.toggle :selected
@investment.save!
load_investments
end
private
def load_comments
@commentable = @investment
@comment_tree = CommentTree.new(@commentable, params[:page], @current_order, valuations: true)
set_comment_flags(@comment_tree.comments)
end
def resource_model
Budget::Investment
end
def resource_name
resource_model.parameterize(separator: "_")
end
def load_investments
@investments = Budget::Investment.scoped_filter(params, @current_filter).order_filter(params)
@investments = Kaminari.paginate_array(@investments) if @investments.is_a?(Array)
@investments = @investments.page(params[:page]) unless request.format.csv?
end
def budget_investment_params
attributes = [:external_url, :heading_id, :administrator_id, :tag_list,
:valuation_tag_list, :incompatible, :visible_to_valuators, :selected,
:milestone_tag_list, valuator_ids: [], valuator_group_ids: []]
params.require(:budget_investment).permit(attributes, translation_params(Budget::Investment))
end
def load_budget
@budget = Budget.find_by_slug_or_id! params[:budget_id]
end
def load_investment
@investment = @budget.investments.find(params[:id])
end
def load_staff
@admins = @budget.administrators.includes(:user)
@valuators = @budget.valuators.includes(:user).order(description: :asc).order("users.email ASC")
end
def load_valuator_groups
@valuator_groups = ValuatorGroup.all.order(name: :asc)
end
def load_tags
@tags = Budget::Investment.tags_on(:valuation_tags).order(:name).distinct
end
def load_ballot
query = Budget::Ballot.where(user: current_user, budget: @budget)
@ballot = @budget.balloting? ? query.first_or_create! : query.first_or_initialize
end
def parse_valuation_filters
if params[:valuator_or_group_id]
model, id = params[:valuator_or_group_id].split("_")
if model == "group"
params[:valuator_group_id] = id
else
params[:valuator_id] = id
end
end
end
end