diff --git a/app/controllers/api/v1/events_controller.rb b/app/controllers/api/v1/events_controller.rb index 2a9398c..f57db19 100755 --- a/app/controllers/api/v1/events_controller.rb +++ b/app/controllers/api/v1/events_controller.rb @@ -88,6 +88,7 @@ module Api latitude: event.latitude, longitude: event.longitude, featured: event.featured, + image_url: event.display_image_url, created_at: event.created_at, updated_at: event.updated_at, user: { diff --git a/app/controllers/promoter/events_controller.rb b/app/controllers/promoter/events_controller.rb index 72af78d..e86b8bb 100644 --- a/app/controllers/promoter/events_controller.rb +++ b/app/controllers/promoter/events_controller.rb @@ -45,6 +45,8 @@ class Promoter::EventsController < ApplicationController if @event.update(event_params) redirect_to promoter_event_path(@event), notice: "Event mis à jour avec succès!" else + # If validation fails and a new image was attached, purge it + @event.image.purge if @event.image.attached? && @event.changed.include?('image') render :edit, status: :unprocessable_entity end end diff --git a/app/models/event.rb b/app/models/event.rb index 5038539..d4be2c9 100755 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -27,6 +27,7 @@ class Event < ApplicationRecord # === Callbacks === before_validation :geocode_address, if: :should_geocode_address? + before_update :handle_image_replacement, if: :image_attached? # Validations for Event attributes # Basic information @@ -61,8 +62,26 @@ class Event < ApplicationRecord # === Instance Methods === + # Get image URL prioritizing old image field if it exists + def display_image_url + # First check if old image field exists and has a value + return self[:image] if self[:image].present? + + # Fall back to attached image + return nil unless image.attached? + + # Return the URL for the attached image + Rails.application.routes.url_helpers.rails_blob_url(image, only_path: true) + end + # Get image variants for different display sizes def event_image_variant(size = :medium) + # For old image field, return the URL directly + return self[:image] if self[:image].present? + + # For attached images, process variants + return nil unless image.attached? + case size when :large image.variant(resize_to_limit: [1200, 630]) @@ -75,6 +94,11 @@ class Event < ApplicationRecord end end + # Check if event has any image (old field or attached) + def has_image? + self[:image].present? || image.attached? + end + # Check if coordinates were successfully geocoded or are fallback coordinates def geocoding_successful? coordinates_look_valid? @@ -169,6 +193,19 @@ class Event < ApplicationRecord private + # Check if image is attached for the callback + def image_attached? + image.attached? + end + + # Handle image replacement when a new image is uploaded + def handle_image_replacement + # Clear the old image field if a new image is being attached + if image.attached? + self[:image] = nil + end + end + # Determine if we should perform server-side geocoding def should_geocode_address? # Don't geocode if address is blank diff --git a/app/views/components/_event_item.html.erb b/app/views/components/_event_item.html.erb index 4fa25f1..3ae2a3f 100755 --- a/app/views/components/_event_item.html.erb +++ b/app/views/components/_event_item.html.erb @@ -1,7 +1,7 @@ <%= link_to event_path(event.slug, event), class: "group block p-4 rounded-lg border border-slate-200 dark:border-slate-700 hover:border-purple-300 dark:hover:border-purple-600 hover:shadow-md transition-all duration-200" do %>
- <%= image_tag event.event_image_variant(:small), alt: event.name, class: "w-full h-full object-cover" if event.image.attached? %> + <%= image_tag event.event_image_variant(:small), alt: event.name, class: "w-full h-full object-cover" if event.has_image? %>

diff --git a/app/views/events/index.html.erb b/app/views/events/index.html.erb index 768511a..7f19b3f 100755 --- a/app/views/events/index.html.erb +++ b/app/views/events/index.html.erb @@ -22,7 +22,7 @@ <% @events.each do |event| %>
<%= link_to event_path(event.slug, event), class: "block" do %> - <% if event.image.attached? %> + <% if event.has_image? %>
<%= image_tag event.event_image_variant(:medium), alt: event.name, class: "w-full h-full object-cover group-hover:scale-105 transition-transform duration-300" %> diff --git a/app/views/events/show.html.erb b/app/views/events/show.html.erb index ad7c236..69e8028 100755 --- a/app/views/events/show.html.erb +++ b/app/views/events/show.html.erb @@ -10,7 +10,7 @@
- <% if @event.image.attached? %> + <% if @event.has_image? %>
<%= image_tag @event.event_image_variant(:large), class: "w-full h-full object-cover" %>
@@ -88,12 +88,8 @@ %> <% map_providers.each do |name, url| %> - <%= link_to url, target: "_blank", rel: "noopener", - class: "inline-flex items-center px-3 py-2 text-xs font-medium text-gray-700 bg-white border border-gray-300 rounded-lg hover:bg-gray-50 transition-colors" do %> - <%= icons[name] %> - <%= name %> + <%= link_to "#{icons[name]} #{name}".html_safe, url, target: "_blank", rel: "noopener", class: "inline-flex items-center px-3 py-2 text-xs font-medium text-gray-700 bg-white border border-gray-300 rounded-lg hover:bg-gray-50 transition-colors" %> <% end %> - <% end %>
<% end %> @@ -131,14 +127,7 @@
- <%= form_with url: event_order_new_path(@event.slug, @event.id), method: :get, id: "checkout_form", local: true, data: { - controller: "ticket-selection", - ticket_selection_target: "form", - ticket_selection_event_slug_value: @event.slug, - ticket_selection_event_id_value: @event.id, - ticket_selection_order_new_url_value: event_order_new_path(@event.slug, @event.id), - ticket_selection_store_cart_url_value: api_v1_store_cart_path - } do |form| %> + <%= form_with url: event_order_new_path(@event.slug, @event.id), method: :get, id: "checkout_form", local: true, data: { controller: "ticket-selection", ticket_selection_target: "form", ticket_selection_event_slug_value: @event.slug, ticket_selection_event_id_value: @event.id, ticket_selection_order_new_url_value: event_order_new_path(@event.slug, @event.id), ticket_selection_store_cart_url_value: api_v1_store_cart_path } do |form| %>
diff --git a/app/views/pages/home.html.erb b/app/views/pages/home.html.erb index f663a9a..82b946a 100755 --- a/app/views/pages/home.html.erb +++ b/app/views/pages/home.html.erb @@ -89,7 +89,7 @@
- <% if event.image.attached? %> + <% if event.has_image? %> <%= image_tag event.event_image_variant(:medium), alt: event.name, class: "w-full h-full object-cover group-hover:scale-105 transition-transform duration-300" %> <% else %>
diff --git a/app/views/promoter/events/edit.html.erb b/app/views/promoter/events/edit.html.erb index 58ba4c6..90b05c0 100644 --- a/app/views/promoter/events/edit.html.erb +++ b/app/views/promoter/events/edit.html.erb @@ -70,12 +70,23 @@ <%= form.label :image, "Image de couverture", class: "block text-sm font-medium text-gray-700 mb-2" %>
- <% if @event.image.attached? %> -
- <%= image_tag @event.image.variant(resize_to_limit: [400, 225]), class: "w-full h-48 object-cover rounded-lg border border-gray-200" %> -
-
@@ -86,7 +97,7 @@ <%= form.file_field :image, class: "w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-transparent file:mr-4 file:py-2 file:px-4 file:rounded-lg file:border-0 file:text-sm file:font-semibold file:bg-purple-50 file:text-purple-700 hover:file:bg-purple-100", accept: "image/png,image/jpeg,image/jpg,image/webp", data: { action: "change->event-form#previewImage" } %>
Formats acceptés : PNG, JPG, JPEG, WebP (max 5MB) - <% if @event.image.attached? %> + <% if @event.has_image? %>
Laissez vide pour conserver l'image actuelle <% end %>
diff --git a/app/views/promoter/events/new.html.erb b/app/views/promoter/events/new.html.erb index 7d6ef4d..84bb73b 100644 --- a/app/views/promoter/events/new.html.erb +++ b/app/views/promoter/events/new.html.erb @@ -63,7 +63,7 @@ <%= form.label :image, "Image de couverture", class: "block text-sm font-medium text-gray-700 mb-2" %>
- <% if @event.image.attached? %> + <% if @event.has_image? %>
<%= image_tag @event.image.variant(resize_to_limit: [400, 225]), class: "w-full h-48 object-cover rounded-lg border border-gray-200" %>
diff --git a/app/views/promoter/events/show.html.erb b/app/views/promoter/events/show.html.erb index 6596c20..75d129c 100644 --- a/app/views/promoter/events/show.html.erb +++ b/app/views/promoter/events/show.html.erb @@ -174,7 +174,7 @@
- <% if @event.image.attached? %> + <% if @event.has_image? %>
<%= image_tag @event.event_image_variant(:large), alt: @event.name, class: "w-full h-full object-cover" %>