33module BetterTogether
44 # A Schedulable Event
55 class Event < ApplicationRecord
6+ include Attachments ::Images
7+ include Categorizable
68 include Creatable
79 include FriendlySlug
810 include Geography ::Geospatial ::One
@@ -11,6 +13,10 @@ class Event < ApplicationRecord
1113 include Privacy
1214 include Viewable
1315
16+ attachable_cover_image
17+
18+ categorizable ( class_name : 'BetterTogether::EventCategory' )
19+
1420 # belongs_to :address, -> { where(physical: true, primary_flag: true) }
1521 # accepts_nested_attributes_for :address, allow_destroy: true, reject_if: :blank?
1622 # delegate :geocoding_string, to: :address, allow_nil: true
@@ -21,33 +27,7 @@ class Event < ApplicationRecord
2127 translates :name
2228 translates :description , backend : :action_text
2329
24- has_one_attached :cover_image do |attachable |
25- attachable . variant :optimized_jpeg , resize_to_limit : [ 2400 , 1200 ] ,
26- # rubocop:todo Layout/LineLength
27- saver : { strip : true , quality : 85 , interlace : true , optimize_coding : true , trellis_quant : true , quant_table : 3 } , format : 'jpg'
28- # rubocop:enable Layout/LineLength
29- attachable . variant :optimized_png , resize_to_limit : [ 2400 , 1200 ] ,
30- saver : { strip : true , quality : 85 , optimize_coding : true } , format : 'png'
31-
32- attachable . variant :optimized_card_jpeg , resize_to_limit : [ 1200 , 300 ] ,
33- # rubocop:todo Layout/LineLength
34- saver : { strip : true , quality : 90 , interlace : true , optimize_coding : true , trellis_quant : true , quant_table : 3 } , format : 'jpg'
35- # rubocop:enable Layout/LineLength
36- attachable . variant :optimized_card_png , resize_to_limit : [ 1200 , 300 ] ,
37- saver : { strip : true , quality : 90 , optimize_coding : true } , format : 'png'
38- end
39-
40- CONTENT_TYPES = %w[ image/jpeg image/png image/gif image/webp image/svg+xml ] . freeze
41-
42- validates :cover_image ,
43- content_type : CONTENT_TYPES ,
44- size : { less_than : 100 . megabytes , message : 'is too large' }
45-
46- alias card_image cover_image
47-
48- attr_accessor :remove_cover_image
49-
50- before_save :purge_cover_image , if : -> { remove_cover_image == '1' }
30+ validates :name , presence : true
5131
5232 scope :draft , lambda {
5333 start_query = arel_table [ :starts_at ] . eq ( nil )
@@ -74,36 +54,6 @@ def self.permitted_attributes(id: false, destroy: false)
7454 ]
7555 end
7656
77- def optimized_card_image
78- if card_image . content_type == 'image/svg+xml'
79- # If SVG, return the original without transformation
80- card_image
81-
82- # For other formats, analyze to determine transparency
83- elsif card_image . content_type == 'image/png'
84- # If PNG with transparency, return the optimized PNG variant
85- card_image . variant ( :optimized_card_png ) . processed
86- else
87- # Otherwise, use the optimized JPG variant
88- card_image . variant ( :optimized_card_jpeg ) . processed
89- end
90- end
91-
92- def optimized_cover_image
93- if cover_image . content_type == 'image/svg+xml'
94- # If SVG, return the original without transformation
95- cover_image
96-
97- # For other formats, analyze to determine transparency
98- elsif cover_image . content_type == 'image/png'
99- # If PNG with transparency, return the optimized PNG variant
100- cover_image . variant ( :optimized_png ) . processed
101- else
102- # Otherwise, use the optimized JPG variant
103- cover_image . variant ( :optimized_jpeg ) . processed
104- end
105- end
106-
10757 def schedule_address_geocoding
10858 return unless should_geocode?
10959
@@ -122,6 +72,6 @@ def to_s
12272 name
12373 end
12474
125- include :: BetterTogether :: RemoveableAttachment
75+ configure_attachment_cleanup
12676 end
12777end
0 commit comments