Skip to content

Commit 539d970

Browse files
committed
implemented network push for ability updates
1 parent 2201add commit 539d970

2 files changed

Lines changed: 57 additions & 2 deletions

File tree

app/controllers/abilities_controller.rb

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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?

app/views/abilities/_form.html.erb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,23 @@
2222
cur_length: @ability.description.length,
2323
min_length: 15,
2424
max_length: 30_000 %>
25-
<div class="post-preview"></div>
25+
<div class="post-preview"></div>
26+
27+
28+
<% if current_user&.can_push_to_network?(@ability) %>
29+
<div class="form-group">
30+
<div class="checkbox-setting">
31+
<div class="checkbox-setting--desc">
32+
<%= label_tag :network_push, 'Push to network', class: 'form-element' %>
33+
<span class="form-caption">Copy to all communities (where the ability has not been edited)?</span>
34+
</div>
35+
<div class="checkbox-setting--value">
36+
<%= check_box_tag :network_push, true, false, class: 'form-checkbox-element' %>
37+
</div>
38+
</div>
39+
</div>
40+
<% end %>
41+
2642
<%= f.submit 'Save', class: 'button is-filled' %>
2743
<%= link_to t('g.cancel').titlecase,
2844
ability_path(id: @ability.internal_id),

0 commit comments

Comments
 (0)