|
1 | 1 | class AbilitiesController < ApplicationController |
| 2 | + include DraftManagement |
| 3 | + |
| 4 | + before_action :authenticate_user!, except: [:index, :show] |
| 5 | + before_action :set_ability, only: [:show, :edit, :update] |
2 | 6 | before_action :set_user |
3 | | - before_action :verify_moderator, only: [:recalc] |
| 7 | + before_action :verify_moderator, only: [:edit, :recalc, :update] |
4 | 8 |
|
5 | 9 | def index |
6 | 10 | @abilities = Ability.all |
7 | 11 | end |
8 | 12 |
|
9 | 13 | def show |
10 | | - @ability = Ability.where(internal_id: params[:id]).first |
11 | | - return not_found! if @ability.nil? |
12 | | - |
13 | 14 | @your_ability = @user&.community_user&.privilege @ability.internal_id |
14 | 15 | end |
15 | 16 |
|
| 17 | + def edit; end |
| 18 | + |
| 19 | + def update |
| 20 | + if push_to_network?(@ability) |
| 21 | + abilities = Ability.unscoped.where(internal_id: @ability.internal_id, |
| 22 | + description: @ability.description) |
| 23 | + |
| 24 | + if do_update_network(@ability, abilities) |
| 25 | + do_delete_draft(current_user, URI(request.referer || '').path) |
| 26 | + flash[:success] = "#{helpers.pluralize(abilities.to_a.size, 'ability')} updated." |
| 27 | + redirect_to ability_path(id: @ability.internal_id) |
| 28 | + else |
| 29 | + render :edit, status: :bad_request |
| 30 | + end |
| 31 | + elsif @ability.update(ability_update_params) |
| 32 | + do_delete_draft(current_user, URI(request.referer || '').path) |
| 33 | + flash[:success] = I18n.t('abilities.success.update_generic') |
| 34 | + redirect_to ability_path(id: @ability.internal_id) |
| 35 | + else |
| 36 | + render :edit, status: :bad_request |
| 37 | + end |
| 38 | + end |
| 39 | + |
16 | 40 | def recalc |
17 | 41 | @user.community_user.recalc_privileges! |
18 | 42 | redirect_to user_privileges_url(@user.id) |
19 | 43 | end |
20 | 44 |
|
21 | 45 | private |
22 | 46 |
|
| 47 | + def ability_update_params |
| 48 | + params.require(:ability).permit(:description, :name) |
| 49 | + end |
| 50 | + |
| 51 | + # Actually update a given set of abilities network-wide |
| 52 | + # @param ability [Ability] ability from which the push is initiated |
| 53 | + # @param abilities [ActiveRecord::Relation<Ability>] network abilities to update |
| 54 | + # @return [Boolean] status of the operation |
| 55 | + def do_update_network(ability, abilities) |
| 56 | + Ability.transaction do |
| 57 | + abilities.each do |network_ability| |
| 58 | + unless network_ability.update(ability_update_params) |
| 59 | + ability.errors.merge!(network_ability.errors) |
| 60 | + raise ActiveRecord::Rollback |
| 61 | + end |
| 62 | + end |
| 63 | + true |
| 64 | + rescue |
| 65 | + false |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + # Should push to network? |
| 70 | + # @param ability [Ability] ability to check |
| 71 | + # @return [Boolean] check result |
| 72 | + def push_to_network?(ability) |
| 73 | + return false unless params[:network_push] == 'true' |
| 74 | + return false unless current_user.present? |
| 75 | + |
| 76 | + current_user.can_push_to_network?(ability) |
| 77 | + end |
| 78 | + |
| 79 | + def set_ability |
| 80 | + @ability = Ability.where(internal_id: params[:id]).first |
| 81 | + not_found! unless @ability.present? |
| 82 | + end |
| 83 | + |
23 | 84 | def set_user |
24 | 85 | @user = if params[:for].present? |
25 | 86 | User.where(id: params[:for]).first || @user |
|
0 commit comments