File tree Expand file tree Collapse file tree 3 files changed +18
-3
lines changed
controllers/better_together Expand file tree Collapse file tree 3 files changed +18
-3
lines changed Original file line number Diff line number Diff line change @@ -21,10 +21,9 @@ def index
2121 def build_event_hosts # rubocop:disable Metrics/AbcSize
2222 return unless params [ :host_id ] . present? && params [ :host_type ] . present?
2323
24- host_klass = params [ :host_type ] . safe_constantize
25- return unless host_klass
24+ return unless event_host_class
2625
27- policy_scope = Pundit . policy_scope! ( current_user , host_klass )
26+ policy_scope = Pundit . policy_scope! ( current_user , event_host_class )
2827 host_record = policy_scope . find_by ( id : params [ :host_id ] )
2928 return unless host_record
3029
@@ -34,6 +33,14 @@ def build_event_hosts # rubocop:disable Metrics/AbcSize
3433 )
3534 end
3635
36+ def event_host_class
37+ param_type = params [ :host_type ]
38+
39+ # Allow-list only specific classes to be set as host for an event
40+ valid_host_types = BetterTogether ::HostsEvents . included_in_models
41+ valid_host_types . find { |klass | klass . to_s == param_type }
42+ end
43+
3744 def resource_class
3845 ::BetterTogether ::Event
3946 end
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 22
33module BetterTogether
44 # 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
56 module HostsEvents
67 extend ActiveSupport ::Concern
78
89 included do
910 has_many :event_hosts , as : :host
1011 has_many :hosted_events , through : :event_hosts , source : :event
1112 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
1219 end
1320end
You can’t perform that action at this time.
0 commit comments