@@ -17,7 +17,18 @@ def show
1717 def edit ; end
1818
1919 def update
20- if @ability . update ( ability_update_params )
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 )
2132 do_delete_draft ( current_user , URI ( request . referer || '' ) . path )
2233 flash [ :success ] = I18n . t ( 'abilities.success.update_generic' )
2334 redirect_to ability_path ( id : @ability . internal_id )
@@ -37,6 +48,34 @@ def ability_update_params
3748 params . require ( :ability ) . permit ( :description )
3849 end
3950
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+
4079 def set_ability
4180 @ability = Ability . where ( internal_id : params [ :id ] ) . first
4281 not_found! unless @ability . present?
0 commit comments