Skip to content

Commit e0052ed

Browse files
authored
Merge branch 'main' into feature/exchange
Signed-off-by: Robert Smith <[email protected]>
2 parents fdd1618 + 1453e90 commit e0052ed

File tree

29 files changed

+266
-70
lines changed

29 files changed

+266
-70
lines changed

app/controllers/better_together/events_controller.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class EventsController < FriendlyResourceController
88
Rails.application.eager_load!
99
end
1010

11+
before_action :build_event_hosts, only: :new
12+
1113
def index
1214
@draft_events = @events.draft
1315
@upcoming_events = @events.upcoming
@@ -43,6 +45,29 @@ def rsvp_cancel
4345

4446
protected
4547

48+
def build_event_hosts # rubocop:disable Metrics/AbcSize
49+
return unless params[:host_id].present? && params[:host_type].present?
50+
51+
return unless event_host_class
52+
53+
policy_scope = Pundit.policy_scope!(current_user, event_host_class)
54+
host_record = policy_scope.find_by(id: params[:host_id])
55+
return unless host_record
56+
57+
resource_instance.event_hosts.build(
58+
host_id: params[:host_id],
59+
host_type: params[:host_type]
60+
)
61+
end
62+
63+
def event_host_class
64+
param_type = params[:host_type]
65+
66+
# Allow-list only specific classes to be set as host for an event
67+
valid_host_types = BetterTogether::HostsEvents.included_in_models
68+
valid_host_types.find { |klass| klass.to_s == param_type }
69+
end
70+
4671
def resource_class
4772
::BetterTogether::Event
4873
end

app/helpers/better_together/events_helper.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,12 @@
33
module BetterTogether
44
# View helpers for events
55
module EventsHelper
6+
# Return hosts for an event that the current user is authorized to view.
7+
# Keeps view markup small and centralizes the policy logic for testing.
8+
def visible_event_hosts(event)
9+
return [] unless event.respond_to?(:event_hosts)
10+
11+
event.event_hosts.map { |eh| eh.host if policy(eh.host).show? }.compact
12+
end
613
end
714
end

app/models/better_together/community.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ module BetterTogether
44
# A gathering
55
class Community < ApplicationRecord
66
include Contactable
7-
include Host
7+
include HostsEvents
88
include Identifier
99
include Infrastructure::BuildingConnections
1010
include Joinable
11+
include Permissible
12+
include PlatformHost
1113
include Protected
1214
include Privacy
13-
include Permissible
1415
include Metrics::Viewable
1516

1617
belongs_to :creator,

app/models/better_together/event.rb

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ class Event < ApplicationRecord
88
include Categorizable
99
include Creatable
1010
include FriendlySlug
11+
include Identifier
1112
include Geography::Geospatial::One
1213
include Geography::Locatable::One
13-
include Identifier
14+
include Metrics::Viewable
1415
include Privacy
1516
include TrackedActivity
16-
include Viewable
1717

1818
attachable_cover_image
1919

@@ -25,6 +25,8 @@ class Event < ApplicationRecord
2525

2626
categorizable(class_name: 'BetterTogether::EventCategory')
2727

28+
has_many :event_hosts
29+
2830
# belongs_to :address, -> { where(physical: true, primary_flag: true) }
2931
# accepts_nested_attributes_for :address, allow_destroy: true, reject_if: :blank?
3032
# delegate :geocoding_string, to: :address, allow_nil: true
@@ -38,6 +40,10 @@ class Event < ApplicationRecord
3840
allow_nil: true
3941
validate :ends_at_after_starts_at
4042

43+
before_validation :set_host
44+
45+
accepts_nested_attributes_for :event_hosts, reject_if: :all_blank
46+
4147
scope :draft, lambda {
4248
start_query = arel_table[:starts_at].eq(nil)
4349
where(start_query)
@@ -63,11 +69,18 @@ def self.permitted_attributes(id: false, destroy: false)
6369
starts_at ends_at registration_url
6470
] + [
6571
{
66-
address_attributes: BetterTogether::Address.permitted_attributes(id: true)
72+
address_attributes: BetterTogether::Address.permitted_attributes(id: true),
73+
event_hosts_attributes: BetterTogether::EventHost.permitted_attributes(id: true)
6774
}
6875
]
6976
end
7077

78+
def set_host
79+
return if event_hosts.any?
80+
81+
event_hosts.build(host: creator)
82+
end
83+
7184
def schedule_address_geocoding
7285
return unless should_geocode?
7386

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
module BetterTogether
4+
# Join an event to its host
5+
class EventHost < ApplicationRecord
6+
belongs_to :event, class_name: 'BetterTogether::Event'
7+
belongs_to :host, polymorphic: true
8+
9+
def self.permitted_attributes(id: false, destroy: false)
10+
super + %i[
11+
host_id host_type event_id
12+
]
13+
end
14+
end
15+
end

app/models/better_together/person.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def self.primary_community_delegation_attrs
1212
include Author
1313
include Contactable
1414
include FriendlySlug
15+
include HostsEvents
1516
include Identifier
1617
include Identity
1718
include Member
@@ -107,6 +108,10 @@ def description_html(locale: I18n.locale)
107108
super || description
108109
end
109110

111+
def valid_event_host_ids
112+
[id] + member_communities.pluck(:id)
113+
end
114+
110115
def handle
111116
slug
112117
end

app/models/better_together/platform.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
module BetterTogether
66
# Represents the host application and it's peers
77
class Platform < ApplicationRecord
8-
include Host
8+
include PlatformHost
99
include Identifier
1010
include Joinable
1111
include Permissible
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
module BetterTogether
4+
# Concern that when included gives the model access to events through event_host records
5+
# This module must be included in a model to permit assigning instances as an event host
6+
module HostsEvents
7+
extend ActiveSupport::Concern
8+
9+
included do
10+
has_many :event_hosts, as: :host
11+
has_many :hosted_events, through: :event_hosts, source: :event
12+
end
13+
14+
def self.included_in_models
15+
included_module = self
16+
Rails.application.eager_load! if Rails.env.development? # Ensure all models are loaded
17+
ActiveRecord::Base.descendants.select { |model| model.included_modules.include?(included_module) }
18+
end
19+
end
20+
end

app/models/concerns/better_together/host.rb renamed to app/models/concerns/better_together/platform_host.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module BetterTogether
44
# Concern that when included allows model to be set as host
5-
module Host
5+
module PlatformHost
66
extend ActiveSupport::Concern
77

88
included do

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

0 commit comments

Comments
 (0)