Skip to content

Commit 4c5e4ba

Browse files
committed
Allow events to be created within communities by community members.
Adjust event policy to ensure that checks are correct. Don't show draft or private events to people who aren't connected to them.
1 parent 5131007 commit 4c5e4ba

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

app/models/better_together/person.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def description_html(locale: I18n.locale)
102102
end
103103

104104
def valid_event_host_ids
105-
[id]
105+
[id] + member_communities.pluck(:id)
106106
end
107107

108108
def handle

app/policies/better_together/community_policy.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ def update?
2222
user.present? && (permitted_to?('manage_platform') || permitted_to?('update_community', record))
2323
end
2424

25+
def create_events?
26+
update? &&
27+
BetterTogether::EventPolicy.new(user, BetterTogether::Event.new).create?
28+
end
29+
2530
def edit?
2631
update?
2732
end

app/policies/better_together/event_policy.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ def index?
88
end
99

1010
def show?
11-
record.privacy_public? || creator_or_manager || event_host_member?
11+
(record.privacy_public? && record.starts_at.present?) || creator_or_manager || event_host_member?
1212
end
1313

1414
def update?
1515
creator_or_manager || event_host_member?
1616
end
1717

1818
def create?
19-
permitted_to?('manage_platform')
19+
permitted_to?('manage_platform') || event_host_member?
2020
end
2121

2222
def destroy?
@@ -56,15 +56,20 @@ def permitted_query # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
5656
query = query.or(
5757
events_table[:creator_id].eq(agent.id)
5858
)
59+
5960
if agent.valid_event_host_ids.any?
60-
event_ids = event_ids
61+
event_ids = event_hosts_table
6162
.where(event_hosts_table[:host_id].in(agent.valid_event_host_ids))
6263
.project(:event_id)
6364
query = query.or(
6465
events_table[:id].in(event_ids)
6566
)
6667
end
68+
6769
query
70+
else
71+
# Events must have a start time to be shown to people who aren't conencted to the event
72+
query = query.and(events_table[:starts_at].not_eq(nil))
6873
end
6974

7075
query

app/views/better_together/communities/show.html.erb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,14 @@
4949
<%= content_tag :div, id: 'communityTabs', class: 'nav nav-tabs card-header-tabs', role: 'tablist', aria_label: 'Community Sections' do %>
5050
<%= link_to t('globals.tabs.about'), '#about', class: 'nav-link active', id: 'about-tab', data: { bs_toggle: 'tab', bs_target: '#about', bs_parent: '#profileSections' }, role: 'tab', aria_controls: 'about', aria_selected: 'true', tabindex: '0' %>
5151
<%= link_to t('globals.tabs.members'), '#members', class: 'nav-link', id: 'members-tab', data: { bs_toggle: 'tab', bs_target: '#members', bs_parent: '#communitySections' }, role: 'tab', aria_controls: 'members', aria_selected: 'false', tabindex: '-1' %>
52+
<%= link_to t('globals.tabs.events'), '#events', class: 'nav-link', id: 'events-tab', data: { bs_toggle: 'tab', bs_target: '#events', bs_parent: '#profileSections' }, role: 'tab', aria_controls: 'events', aria_selected: 'true', tabindex: '0' %>
5253
<% end %>
5354
</div>
5455

5556
<%# Accordion content with accessible attributes and flexbox layout %>
5657
<div class="card-body" id="communitySections" role="tabpanel">
5758
<!-- Person About Section -->
58-
<section id="about" class="row collapse show" aria-labelledby="about-tab" aria-expanded="true" data-bs-parent="#platformTabs">
59+
<section id="about" class="row collapse show" aria-labelledby="about-tab" aria-expanded="true" data-bs-parent="#communityTabs">
5960
<div class="col-md-12">
6061
<!-- Name Section -->
6162
<h2 class="card-title profile-name text-center mb-3">
@@ -121,6 +122,15 @@
121122
<%= render partial: 'better_together/person_community_memberships/person_community_membership_member', collection: @resource.person_community_memberships, as: :membership %>
122123
</div>
123124
</div>
125+
126+
<div id="events" class="collapse" aria-labelledby="events-tab" aria-expanded="true" data-bs-parent="#communityTabs">
127+
<% if policy(@resource).create_events? %>
128+
<%= link_to t('.create_event', default: 'Create an Event'), new_event_path(host_id: @resource.id, host_type: resource_class), class: 'btn btn-primary' %>
129+
<% end %>
130+
<div id="events_list" class="mt-3 row row-cols-1 row-cols-md-2 row-cols-lg-3 row-cols-xl-4">
131+
<%= render(policy_scope(@resource.hosted_events)) || render('better_together/events/none') %>
132+
</div>
133+
</div>
124134
</div>
125135
</section>
126136
</div>

0 commit comments

Comments
 (0)