fix: Update Event model to handle image URLs and fix image display
- Relax image URL validation in development environment - Add callback to store image_url in legacy image field for compatibility - Fix event_image_variant method to handle virtual image_url attribute - Simplify event show view to use unified image display method - Fix seeds slug reference for "La belle époque" event 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -31,6 +31,7 @@ class Event < ApplicationRecord
|
||||
# === Callbacks ===
|
||||
before_validation :generate_slug, if: :should_generate_slug?
|
||||
before_validation :geocode_address, if: :should_geocode_address?
|
||||
before_validation :handle_image_url, if: :should_handle_image_url?
|
||||
before_update :handle_image_replacement, if: :image_attached?
|
||||
|
||||
# Validations for Event attributes
|
||||
@@ -151,6 +152,9 @@ class Event < ApplicationRecord
|
||||
# For old image field, return the URL directly
|
||||
return self[:image] if self[:image].present?
|
||||
|
||||
# For virtual image_url attribute, return the URL directly
|
||||
return image_url if image_url.present?
|
||||
|
||||
# For attached images, process variants
|
||||
return nil unless image.attached?
|
||||
|
||||
@@ -275,9 +279,10 @@ class Event < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
# Validate image URL format
|
||||
# Validate image URL format - relaxed for development
|
||||
def image_url_format
|
||||
return unless image_url.present?
|
||||
return if Rails.env.development? # Skip validation in development
|
||||
|
||||
unless image_url.match?(/\Ahttps?:\/\/.+\.(jpg|jpeg|png|gif|webp)(\?.*)?\z/i)
|
||||
errors.add(:image_url, "doit être une URL valide vers une image (JPG, PNG, GIF, WebP)")
|
||||
@@ -299,6 +304,19 @@ class Event < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
# Determine if we should handle image_url
|
||||
def should_handle_image_url?
|
||||
image_url.present? && new_record?
|
||||
end
|
||||
|
||||
# Handle image_url by storing it in the legacy image field
|
||||
def handle_image_url
|
||||
# Store the image_url in the legacy image field for backward compatibility
|
||||
if image_url.present?
|
||||
self[:image] = image_url
|
||||
end
|
||||
end
|
||||
|
||||
# Determine if we should perform server-side geocoding
|
||||
def should_geocode_address?
|
||||
# Don't geocode if address is blank
|
||||
|
||||
Reference in New Issue
Block a user