diff --git a/app/controllers/admin/communication/extranets/posts_controller.rb b/app/controllers/admin/communication/extranets/posts_controller.rb index 87675a1b03..6af5cd0965 100644 --- a/app/controllers/admin/communication/extranets/posts_controller.rb +++ b/app/controllers/admin/communication/extranets/posts_controller.rb @@ -34,7 +34,6 @@ def edit end def create - @l10n.add_photo_import params[:photo_import] if @post.save redirect_to admin_communication_extranet_post_path(@post), notice: t('admin.successfully_created_html', model: @post.to_s_in(current_language)) @@ -46,8 +45,6 @@ def create def update if @post.update(post_params) - load_localization - @l10n.add_photo_import params[:photo_import] redirect_to admin_communication_extranet_post_path(@post), notice: t('admin.successfully_updated_html', model: @post.to_s_in(current_language)) else diff --git a/app/controllers/admin/communication/medias/categories_controller.rb b/app/controllers/admin/communication/medias/categories_controller.rb index 54708eb9fb..4ca0abb11b 100644 --- a/app/controllers/admin/communication/medias/categories_controller.rb +++ b/app/controllers/admin/communication/medias/categories_controller.rb @@ -32,7 +32,6 @@ def edit end def create - @l10n.add_photo_import params[:photo_import] if @category.save redirect_to admin_communication_media_category_path(@category), notice: t('admin.successfully_created_html', model: @category.to_s_in(current_language)) @@ -45,8 +44,6 @@ def create def update if @category.update(category_params) - load_localization - @l10n.add_photo_import params[:photo_import] redirect_to admin_communication_media_category_path(@category), notice: t('admin.successfully_updated_html', model: @category.to_s_in(current_language)) else diff --git a/app/controllers/admin/communication/medias/collections_controller.rb b/app/controllers/admin/communication/medias/collections_controller.rb index 21678c0728..2997833915 100644 --- a/app/controllers/admin/communication/medias/collections_controller.rb +++ b/app/controllers/admin/communication/medias/collections_controller.rb @@ -28,7 +28,6 @@ def edit def create if @collection.save - @l10n.add_photo_import params[:photo_import] redirect_to admin_communication_media_collection_path(@collection), notice: t('admin.successfully_created_html', model: @collection.to_s_in(current_language)) else @@ -39,8 +38,6 @@ def create def update if @collection.update(collection_params) - load_localization - @l10n.add_photo_import params[:photo_import] redirect_to admin_communication_media_collection_path(@collection), notice: t('admin.successfully_updated_html', model: @collection.to_s_in(current_language)) else diff --git a/app/controllers/admin/communication/websites/agenda/categories_controller.rb b/app/controllers/admin/communication/websites/agenda/categories_controller.rb index 8505a55aab..3a0788cf45 100644 --- a/app/controllers/admin/communication/websites/agenda/categories_controller.rb +++ b/app/controllers/admin/communication/websites/agenda/categories_controller.rb @@ -32,9 +32,9 @@ def edit def create @category.website = @website - @l10n.add_photo_import params[:photo_import] if @category.save_and_sync - redirect_to admin_communication_website_agenda_category_path(@category), notice: t('admin.successfully_created_html', model: @category.to_s_in(current_language)) + redirect_to admin_communication_website_agenda_category_path(@category), + notice: t('admin.successfully_created_html', model: @category.to_s_in(current_language)) else breadcrumb render :new, status: :unprocessable_entity @@ -42,11 +42,9 @@ def create end def update - if @category.update(category_params) - load_localization - @l10n.add_photo_import params[:photo_import] - @category.sync_with_git - redirect_to admin_communication_website_agenda_category_path(@category), notice: t('admin.successfully_updated_html', model: @category.to_s_in(current_language)) + if @category.update_and_sync(category_params) + redirect_to admin_communication_website_agenda_category_path(@category), + notice: t('admin.successfully_updated_html', model: @category.to_s_in(current_language)) else load_invalid_localization breadcrumb diff --git a/app/controllers/admin/communication/websites/agenda/events_controller.rb b/app/controllers/admin/communication/websites/agenda/events_controller.rb index a3510fd0c0..ce7da24686 100644 --- a/app/controllers/admin/communication/websites/agenda/events_controller.rb +++ b/app/controllers/admin/communication/websites/agenda/events_controller.rb @@ -38,7 +38,6 @@ def edit def create @event.website = @website @event.created_by = current_user - @l10n.add_photo_import params[:photo_import] if @event.save_and_sync redirect_to admin_communication_website_agenda_event_path(@event), notice: t('admin.successfully_created_html', model: @event.to_s_in(current_language)) @@ -50,10 +49,7 @@ def create end def update - if @event.update(event_params) - load_localization - @l10n.add_photo_import params[:photo_import] - @event.sync_with_git + if @event.update_and_sync(event_params) redirect_to admin_communication_website_agenda_event_path(@event), notice: t('admin.successfully_updated_html', model: @event.to_s_in(current_language)) else diff --git a/app/controllers/admin/communication/websites/pages/categories_controller.rb b/app/controllers/admin/communication/websites/pages/categories_controller.rb index ebff6adff0..22f19c4ee4 100644 --- a/app/controllers/admin/communication/websites/pages/categories_controller.rb +++ b/app/controllers/admin/communication/websites/pages/categories_controller.rb @@ -32,9 +32,9 @@ def edit def create @category.website = @website - @l10n.add_photo_import params[:photo_import] if @category.save_and_sync - redirect_to admin_communication_website_page_category_path(@category), notice: t('admin.successfully_created_html', model: @category.to_s_in(current_language)) + redirect_to admin_communication_website_page_category_path(@category), + notice: t('admin.successfully_created_html', model: @category.to_s_in(current_language)) else breadcrumb render :new, status: :unprocessable_entity @@ -42,11 +42,9 @@ def create end def update - if @category.update(category_params) - load_localization - @l10n.add_photo_import params[:photo_import] - @category.sync_with_git - redirect_to admin_communication_website_page_category_path(@category), notice: t('admin.successfully_updated_html', model: @category.to_s_in(current_language)) + if @category.update_and_sync(category_params) + redirect_to admin_communication_website_page_category_path(@category), + notice: t('admin.successfully_updated_html', model: @category.to_s_in(current_language)) else load_invalid_localization breadcrumb diff --git a/app/controllers/admin/communication/websites/pages_controller.rb b/app/controllers/admin/communication/websites/pages_controller.rb index 3b2060ca2d..badf3e1f09 100644 --- a/app/controllers/admin/communication/websites/pages_controller.rb +++ b/app/controllers/admin/communication/websites/pages_controller.rb @@ -97,9 +97,9 @@ def edit def create @page.website = @website - @l10n.add_photo_import params[:photo_import] if @page.save_and_sync - redirect_to admin_communication_website_page_path(@page), notice: t('admin.successfully_created_html', model: @page.to_s_in(current_language)) + redirect_to admin_communication_website_page_path(@page), + notice: t('admin.successfully_created_html', model: @page.to_s_in(current_language)) else @categories = categories breadcrumb @@ -109,11 +109,9 @@ def create end def update - if @page.update(page_params) - load_localization - @l10n.add_photo_import params[:photo_import] - @page.sync_with_git - redirect_to admin_communication_website_page_path(@page), notice: t('admin.successfully_updated_html', model: @page.to_s_in(current_language)) + if @page.update_and_sync(page_params) + redirect_to admin_communication_website_page_path(@page), + notice: t('admin.successfully_updated_html', model: @page.to_s_in(current_language)) else load_invalid_localization @categories = categories diff --git a/app/controllers/admin/communication/websites/portfolio/categories_controller.rb b/app/controllers/admin/communication/websites/portfolio/categories_controller.rb index 8f193ba2bc..9b5f414e21 100644 --- a/app/controllers/admin/communication/websites/portfolio/categories_controller.rb +++ b/app/controllers/admin/communication/websites/portfolio/categories_controller.rb @@ -30,9 +30,9 @@ def edit def create @category.website = @website - @l10n.add_photo_import params[:photo_import] if @category.save_and_sync - redirect_to admin_communication_website_portfolio_category_path(@category), notice: t('admin.successfully_created_html', model: @category.to_s_in(current_language)) + redirect_to admin_communication_website_portfolio_category_path(@category), + notice: t('admin.successfully_created_html', model: @category.to_s_in(current_language)) else breadcrumb render :new, status: :unprocessable_entity @@ -40,11 +40,9 @@ def create end def update - if @category.update(category_params) - load_localization - @l10n.add_photo_import params[:photo_import] - @category.sync_with_git - redirect_to admin_communication_website_portfolio_category_path(@category), notice: t('admin.successfully_updated_html', model: @category.to_s_in(current_language)) + if @category.update_and_sync(category_params) + redirect_to admin_communication_website_portfolio_category_path(@category), + notice: t('admin.successfully_updated_html', model: @category.to_s_in(current_language)) else load_invalid_localization breadcrumb diff --git a/app/controllers/admin/communication/websites/portfolio/projects_controller.rb b/app/controllers/admin/communication/websites/portfolio/projects_controller.rb index ab2c332dc8..19128bc38f 100644 --- a/app/controllers/admin/communication/websites/portfolio/projects_controller.rb +++ b/app/controllers/admin/communication/websites/portfolio/projects_controller.rb @@ -38,7 +38,6 @@ def edit def create @project.website = @website @project.created_by = current_user - @l10n.add_photo_import params[:photo_import] if @project.save_and_sync redirect_to admin_communication_website_portfolio_project_path(@project), notice: t('admin.successfully_created_html', model: @project.to_s_in(current_language)) @@ -50,10 +49,7 @@ def create end def update - if @project.update(project_params) - load_localization - @l10n.add_photo_import params[:photo_import] - @project.sync_with_git + if @project.update_and_sync(project_params) redirect_to admin_communication_website_portfolio_project_path(@project), notice: t('admin.successfully_updated_html', model: @project.to_s_in(current_language)) else diff --git a/app/controllers/admin/communication/websites/posts/categories_controller.rb b/app/controllers/admin/communication/websites/posts/categories_controller.rb index 2696b065a9..4286fb23e2 100644 --- a/app/controllers/admin/communication/websites/posts/categories_controller.rb +++ b/app/controllers/admin/communication/websites/posts/categories_controller.rb @@ -31,9 +31,9 @@ def edit def create @category.website = @website - @l10n.add_photo_import params[:photo_import] if @category.save_and_sync - redirect_to admin_communication_website_post_category_path(@category), notice: t('admin.successfully_created_html', model: @category.to_s_in(current_language)) + redirect_to admin_communication_website_post_category_path(@category), + notice: t('admin.successfully_created_html', model: @category.to_s_in(current_language)) else breadcrumb render :new, status: :unprocessable_entity @@ -41,11 +41,9 @@ def create end def update - if @category.update(category_params) - load_localization - @l10n.add_photo_import params[:photo_import] - @category.sync_with_git - redirect_to admin_communication_website_post_category_path(@category), notice: t('admin.successfully_updated_html', model: @category.to_s_in(current_language)) + if @category.update_and_sync(category_params) + redirect_to admin_communication_website_post_category_path(@category), + notice: t('admin.successfully_updated_html', model: @category.to_s_in(current_language)) else load_invalid_localization breadcrumb diff --git a/app/controllers/admin/communication/websites/posts_controller.rb b/app/controllers/admin/communication/websites/posts_controller.rb index 00606450dc..4bab52c07d 100644 --- a/app/controllers/admin/communication/websites/posts_controller.rb +++ b/app/controllers/admin/communication/websites/posts_controller.rb @@ -58,7 +58,6 @@ def edit def create @post.website = @website if @post.save - @l10n.add_photo_import params[:photo_import] @post.sync_with_git redirect_to admin_communication_website_post_path(@post), notice: t('admin.successfully_created_html', model: @post.to_s_in(current_language)) @@ -70,10 +69,7 @@ def create end def update - if @post.update(post_params) - load_localization - @l10n.add_photo_import params[:photo_import] - @post.sync_with_git + if @post.update_and_sync(post_params) redirect_to admin_communication_website_post_path(@post), notice: t('admin.successfully_updated_html', model: @post.to_s_in(current_language)) else diff --git a/app/controllers/admin/education/programs/categories_controller.rb b/app/controllers/admin/education/programs/categories_controller.rb index f8591ab65e..88db7fa41c 100644 --- a/app/controllers/admin/education/programs/categories_controller.rb +++ b/app/controllers/admin/education/programs/categories_controller.rb @@ -34,7 +34,6 @@ def edit end def create - @l10n.add_photo_import params[:photo_import] if @category.save redirect_to admin_education_program_category_path(@category), notice: t('admin.successfully_created_html', model: @category.to_s_in(current_language)) @@ -47,8 +46,6 @@ def create def update if @category.update(category_params) - load_localization - @l10n.add_photo_import params[:photo_import] redirect_to admin_education_program_category_path(@category), notice: t('admin.successfully_updated_html', model: @category.to_s_in(current_language)) else diff --git a/app/controllers/admin/education/programs_controller.rb b/app/controllers/admin/education/programs_controller.rb index a2a6ffbf33..6b96da1f9e 100644 --- a/app/controllers/admin/education/programs_controller.rb +++ b/app/controllers/admin/education/programs_controller.rb @@ -54,7 +54,6 @@ def edit end def create - @l10n.add_photo_import params[:photo_import] if @program.save redirect_to [:admin, @program], notice: t('admin.successfully_created_html', model: @program.to_s_in(current_language)) @@ -68,9 +67,6 @@ def create def update load_part if @program.update(program_params) - load_localization - @l10n.add_photo_import params[:photo_import] - @program.touch # to ensure it send the photo_import picture redirect_to after_update_path, notice: t('admin.successfully_updated_html', model: @program.to_s_in(current_language)) else diff --git a/app/controllers/admin/research/journals/volumes_controller.rb b/app/controllers/admin/research/journals/volumes_controller.rb index 97769016b4..a6e7a3ddda 100644 --- a/app/controllers/admin/research/journals/volumes_controller.rb +++ b/app/controllers/admin/research/journals/volumes_controller.rb @@ -35,10 +35,7 @@ def edit end def create - @l10n.add_photo_import params[:photo_import] - @volume.assign_attributes( - journal: @journal - ) + @volume.journal = @journal if @volume.save redirect_to admin_research_journal_volume_path(@volume), notice: t('admin.successfully_created_html', model: @volume.to_s_in(current_language)) else @@ -49,9 +46,6 @@ def create def update if @volume.update(volume_params) - load_localization - @l10n.add_photo_import params[:photo_import] - @volume.touch # to ensure it send the photo_import picture redirect_to admin_research_journal_volume_path(@volume), notice: t('admin.successfully_updated_html', model: @volume.to_s_in(current_language)) else load_invalid_localization @@ -77,7 +71,7 @@ def breadcrumb def volume_params params.require(:research_journal_volume) .permit( - :number, + :number, localizations_attributes: [ :id, :language_id, :title, :slug, :keywords, :published, :published_at, :meta_description, :summary, :text, diff --git a/app/controllers/admin/university/organizations/categories_controller.rb b/app/controllers/admin/university/organizations/categories_controller.rb index 4bfc181245..00815d581a 100644 --- a/app/controllers/admin/university/organizations/categories_controller.rb +++ b/app/controllers/admin/university/organizations/categories_controller.rb @@ -31,7 +31,6 @@ def edit end def create - @l10n.add_photo_import params[:photo_import] if @category.save redirect_to admin_university_organization_category_path(@category), notice: t('admin.successfully_created_html', model: @category.to_s_in(current_language)) @@ -44,8 +43,6 @@ def create def update if @category.update(category_params) - load_localization - @l10n.add_photo_import params[:photo_import] redirect_to admin_university_organization_category_path(@category), notice: t('admin.successfully_updated_html', model: @category.to_s_in(current_language)) else diff --git a/app/controllers/admin/university/organizations_controller.rb b/app/controllers/admin/university/organizations_controller.rb index 59fc9834b6..aac1bb3c2b 100644 --- a/app/controllers/admin/university/organizations_controller.rb +++ b/app/controllers/admin/university/organizations_controller.rb @@ -47,7 +47,6 @@ def edit def create if @organization.save - @l10n.add_photo_import params[:photo_import] redirect_to admin_university_organization_path(@organization), notice: t('admin.successfully_created_html', model: @organization.to_s_in(current_language)) else @@ -59,8 +58,6 @@ def create def update if @organization.update(organization_params) - load_localization - @l10n.add_photo_import params[:photo_import] redirect_to admin_university_organization_path(@organization), notice: t('admin.successfully_updated_html', model: @organization.to_s_in(current_language)) else diff --git a/app/controllers/admin/university/people/categories_controller.rb b/app/controllers/admin/university/people/categories_controller.rb index cbfa0c3df7..776e80c770 100644 --- a/app/controllers/admin/university/people/categories_controller.rb +++ b/app/controllers/admin/university/people/categories_controller.rb @@ -33,7 +33,6 @@ def edit end def create - @l10n.add_photo_import params[:photo_import] if @category.save redirect_to admin_university_person_category_path(@category), notice: t('admin.successfully_created_html', model: @category.to_s_in(current_language)) @@ -46,8 +45,6 @@ def create def update if @category.update(category_params) - load_localization - @l10n.add_photo_import params[:photo_import] redirect_to admin_university_person_category_path(@category), notice: t('admin.successfully_updated_html', model: @category.to_s_in(current_language)) else diff --git a/app/controllers/admin/university/people_controller.rb b/app/controllers/admin/university/people_controller.rb index f963589160..f8b8f959ec 100644 --- a/app/controllers/admin/university/people_controller.rb +++ b/app/controllers/admin/university/people_controller.rb @@ -54,7 +54,6 @@ def edit def create if @person.save - @l10n.add_photo_import params[:photo_import] redirect_to admin_university_person_path(@person), notice: t('admin.successfully_created_html', model: @person.to_s_in(current_language)) else @@ -66,8 +65,6 @@ def create def update if @person.update(person_params) - load_localization - @l10n.add_photo_import params[:photo_import] redirect_to admin_university_person_path(@person), notice: t('admin.successfully_updated_html', model: @person.to_s_in(current_language)) else diff --git a/app/models/concerns/with_featured_image.rb b/app/models/concerns/with_featured_image.rb index 8d7ceef223..0778f394a0 100644 --- a/app/models/concerns/with_featured_image.rb +++ b/app/models/concerns/with_featured_image.rb @@ -24,67 +24,4 @@ def best_featured_image_alt def best_featured_image_credit best_featured_image_source.featured_image_credit end - - def add_photo_import(params) - origin = photo_origin(params) - case origin - when :unsplash - photo_import_unsplash(params['unsplash']) - register_featured_image_in_media_library - when :pexels - photo_import_pexels(params['pexels']) - register_featured_image_in_media_library - when :media - photo_import_media(params['media']) - end - end - - protected - - def photo_origin(params) - if params&.dig(:unsplash).present? - :unsplash - elsif params&.dig(:pexels).present? - :pexels - elsif params&.dig(:media).present? - :media - else - :upload - end - end - - def photo_import_unsplash(id) - photo = Unsplash::Photo.find id - url = "#{photo['urls']['full']}&w=2048&fit=max" - filename = "#{photo['id']}.jpg" - ActiveStorage::Utils.attach_from_url(featured_image, url, filename: filename) - photo.track_download - end - - def photo_import_pexels(id) - photo = Pexels::Client.new.photos.find id - url = "#{photo.src['original']}?auto=compress&cs=tinysrgb&w=2048" - filename = "#{photo.id}.png" - ActiveStorage::Utils.attach_from_url(featured_image, url, filename: filename) - end - - def photo_import_media(id) - media = Communication::Media.find(id) - featured_image.attach(media.original_blob) - context = media.contexts.where( - university_id: media.university_id, - active_storage_blob_id: media.original_blob_id, - about: self - ).first_or_create - end - - def register_featured_image_in_media_library - Communication::Media.create_from_blob( - featured_image.blob, - in_context: self, - origin: origin, - alt: featured_image_alt, - credit: featured_image_credit - ) - end end