Skip to content

Commit

Permalink
Merge pull request #139 from hitobito/supercamp-fixes
Browse files Browse the repository at this point in the history
Prevent supercamp loops
  • Loading branch information
carlobeltrame committed Jan 16, 2020
2 parents 8fab48e + 6578127 commit 1677b6a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
9 changes: 5 additions & 4 deletions app/controllers/supercamps_controller.rb
Expand Up @@ -39,7 +39,7 @@ def available
def query
@found_supercamps = []
if params.key?(:q) && params[:q].size >= 3
@found_supercamps = without_self(matching_supercamps.limit(10))
@found_supercamps = without_self_and_descendants(matching_supercamps.limit(10))
end
end

Expand All @@ -50,16 +50,17 @@ def connect

private

def without_self(supercamps)
supercamps.reject { |supercamp| supercamp.id == camp_id }
def without_self_and_descendants(supercamps)
child_ids = Event::Camp.find(camp_id).self_and_descendants.pluck(:id)
supercamps.reject { |supercamp| child_ids.include?(supercamp.id) }
end

def group
@group ||= Group.find(params[:group_id])
end

def supercamps_on_group_and_above
@supercamps_on_group_and_above = without_self(
@supercamps_on_group_and_above = without_self_and_descendants(
group.decorate.upcoming_supercamps_on_group_and_above)
end

Expand Down
7 changes: 6 additions & 1 deletion app/models/event/camp.rb
Expand Up @@ -278,7 +278,8 @@ def layer_leader_roles

def may_become_sub_camp
if super_camp.present?
if !super_camp.allow_sub_camps || super_camp.state != 'created' || !super_camp.upcoming
if is_ancestor_of(super_camp.id) || !super_camp.allow_sub_camps ||
super_camp.state != 'created' || !super_camp.upcoming
errors.add(:parent_id, :invalid)
end
else
Expand All @@ -289,6 +290,10 @@ def may_become_sub_camp
end
end

def is_ancestor_of(camp_id)
self.self_and_descendants.pluck(:id).include?(camp_id)
end

def assert_contact_attrs_passed_on_to_supercamp_valid
contact_attrs_passed_on_to_supercamp.map(&:to_s).each do |a|
unless valid_contact_attr?(a)
Expand Down
3 changes: 1 addition & 2 deletions app/views/supercamps/_available.html.haml
Expand Up @@ -12,8 +12,7 @@
options: @supercamps_on_group_and_above, prefix: :above

= field_set_tag t('.search_for_supercamp'), id: :supercamp_search do
= form_tag group_query_supercamps_path, method: :get, remote: true do
= hidden_field :selected_supercamp, :id
= form_tag group_query_supercamps_path(camp_id: @camp_id), method: :get, remote: true do
= text_field :selected_supercamp, :q, name: :q
.btn-group
= button_tag t('.search'), class: 'btn btn-secondary'
Expand Down

0 comments on commit 1677b6a

Please sign in to comment.