@@ -7,17 +7,19 @@ class Event < ApplicationRecord
77 include Categorizable
88 include Creatable
99 include FriendlySlug
10+ include Identifier
1011 include Geography ::Geospatial ::One
1112 include Geography ::Locatable ::One
12- include Identifier
13+ include Metrics :: Viewable
1314 include Privacy
1415 include TrackedActivity
15- include Viewable
1616
1717 attachable_cover_image
1818
1919 categorizable ( class_name : 'BetterTogether::EventCategory' )
2020
21+ has_many :event_hosts
22+
2123 # belongs_to :address, -> { where(physical: true, primary_flag: true) }
2224 # accepts_nested_attributes_for :address, allow_destroy: true, reject_if: :blank?
2325 # delegate :geocoding_string, to: :address, allow_nil: true
@@ -27,11 +29,14 @@ class Event < ApplicationRecord
2729 translates :description , backend : :action_text
2830
2931 validates :name , presence : true
30- validates :starts_at , presence : true
3132 validates :registration_url , format : { with : URI ::DEFAULT_PARSER . make_regexp ( %w[ http https ] ) } , allow_blank : true ,
3233 allow_nil : true
3334 validate :ends_at_after_starts_at
3435
36+ before_validation :set_host
37+
38+ accepts_nested_attributes_for :event_hosts , reject_if : :all_blank
39+
3540 scope :draft , lambda {
3641 start_query = arel_table [ :starts_at ] . eq ( nil )
3742 where ( start_query )
@@ -52,11 +57,18 @@ def self.permitted_attributes(id: false, destroy: false)
5257 starts_at ends_at registration_url
5358 ] + [
5459 {
55- address_attributes : BetterTogether ::Address . permitted_attributes ( id : true )
60+ address_attributes : BetterTogether ::Address . permitted_attributes ( id : true ) ,
61+ event_hosts_attributes : BetterTogether ::EventHost . permitted_attributes ( id : true )
5662 }
5763 ]
5864 end
5965
66+ def set_host
67+ return if event_hosts . any?
68+
69+ event_hosts . build ( host : creator )
70+ end
71+
6072 def schedule_address_geocoding
6173 return unless should_geocode?
6274
0 commit comments