mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-09 23:06:06 +00:00
Merge pull request #4372 from luisramos0/images
Move Images from spree_backend to OFN
This commit is contained in:
39
app/controllers/spree/admin/images_controller.rb
Normal file
39
app/controllers/spree/admin/images_controller.rb
Normal file
@@ -0,0 +1,39 @@
|
||||
module Spree
|
||||
module Admin
|
||||
class ImagesController < ResourceController
|
||||
# This will make resource controller redirect correctly after deleting product images.
|
||||
# This can be removed after upgrading to Spree 2.1.
|
||||
# See here https://github.com/spree/spree/commit/334a011d2b8e16355e4ae77ae07cd93f7cbc8fd1
|
||||
belongs_to 'spree/product', find_by: :permalink
|
||||
|
||||
before_filter :load_data
|
||||
|
||||
create.before :set_viewable
|
||||
update.before :set_viewable
|
||||
destroy.before :destroy_before
|
||||
|
||||
private
|
||||
|
||||
def location_after_save
|
||||
admin_product_images_url(@product)
|
||||
end
|
||||
|
||||
def load_data
|
||||
@product = Product.find_by_permalink(params[:product_id])
|
||||
@variants = @product.variants.collect do |variant|
|
||||
[variant.options_text, variant.id]
|
||||
end
|
||||
@variants.insert(0, [Spree.t(:all), @product.master.id])
|
||||
end
|
||||
|
||||
def set_viewable
|
||||
@image.viewable_type = 'Spree::Variant'
|
||||
@image.viewable_id = params[:image][:viewable_id]
|
||||
end
|
||||
|
||||
def destroy_before
|
||||
@viewable = @image.viewable
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,6 +0,0 @@
|
||||
Spree::Admin::ImagesController.class_eval do
|
||||
# This will make resource controller redirect correctly after deleting product images.
|
||||
# This can be removed after upgrading to Spree 2.1.
|
||||
# See here https://github.com/spree/spree/commit/334a011d2b8e16355e4ae77ae07cd93f7cbc8fd1
|
||||
belongs_to 'spree/product', find_by: :permalink
|
||||
end
|
||||
16
app/views/spree/admin/images/_form.html.haml
Normal file
16
app/views/spree/admin/images/_form.html.haml
Normal file
@@ -0,0 +1,16 @@
|
||||
%div
|
||||
.four.columns.alpha
|
||||
.field
|
||||
= f.label t('spree.filename')
|
||||
%br/
|
||||
= f.file_field :attachment
|
||||
.field
|
||||
= f.label Spree::Variant.model_name.human
|
||||
%br/
|
||||
= f.select :viewable_id, @variants, {}, {class: 'select2 fullwidth'}
|
||||
.field.omega.five.columns
|
||||
= f.label t('spree.alt_text')
|
||||
%br/
|
||||
= f.text_area :alt, rows: 4, class: 'fullwidth'
|
||||
|
||||
.clear
|
||||
21
app/views/spree/admin/images/edit.html.haml
Normal file
21
app/views/spree/admin/images/edit.html.haml
Normal file
@@ -0,0 +1,21 @@
|
||||
= render partial: 'spree/admin/shared/product_sub_menu'
|
||||
= render partial: 'spree/admin/shared/product_tabs', locals: { current: 'Images' }
|
||||
= render partial: 'spree/shared/error_messages', locals: { target: @image }
|
||||
|
||||
- content_for :page_actions do
|
||||
%li= button_link_to t('spree.back_to_images_list'), admin_product_images_url(@product), icon: 'icon-arrow-left'
|
||||
|
||||
= form_for [:admin, @product, @image], html: { multipart: true } do |f|
|
||||
%fieldset
|
||||
%legend{align: "center"}= @image.attachment_file_name
|
||||
.field.alpha.three.columns.align-center
|
||||
= f.label t('spree.thumbnail')
|
||||
%br/
|
||||
= link_to image_tag(@image.attachment.url(:small)), @image.attachment.url(:product)
|
||||
.nine.columns.omega
|
||||
= render partial: 'form', locals: { f: f }
|
||||
.clear
|
||||
.form-buttons.filter-actions.actions
|
||||
= button t('spree.actions.update'), 'icon-refresh'
|
||||
%span.or= t('spree.or')
|
||||
= link_to t('spree.actions.cancel'), admin_product_images_url(@product), id: 'cancel_link', class: 'button icon-remove'
|
||||
43
app/views/spree/admin/images/index.html.haml
Normal file
43
app/views/spree/admin/images/index.html.haml
Normal file
@@ -0,0 +1,43 @@
|
||||
= render partial: 'spree/admin/shared/product_sub_menu'
|
||||
= render partial: 'spree/admin/shared/product_tabs', locals: { current: 'Images'}
|
||||
|
||||
- content_for :page_actions do
|
||||
%li= link_to_with_icon('icon-plus', t('spree.new_image'), new_admin_product_image_url(@product), id: 'new_image_link', class: 'button')
|
||||
|
||||
#images
|
||||
|
||||
- unless @product.images.any? || @product.variant_images.any?
|
||||
.no-objects-found
|
||||
= t('spree.no_images_found')
|
||||
\.
|
||||
- else
|
||||
%table.index.sortable{ "data-sortable-link" => "#{update_positions_admin_product_images_url(@product)}" }
|
||||
%colgroup
|
||||
%col{ style: "width: 5%" }/
|
||||
%col{ style: "width: 10%" }/
|
||||
- if @product.has_variants?
|
||||
%col{ style: "width: 25%" }/
|
||||
%col{ style: "width: 45%" }/
|
||||
%col{ style: "width: 15%" }/
|
||||
%thead
|
||||
%tr
|
||||
%th{:colspan => "2"}= t('spree.thumbnail')
|
||||
- if @product.has_variants?
|
||||
%th= Spree::Variant.model_name.human
|
||||
%th= t('spree.alt_text')
|
||||
%th.actions
|
||||
%tbody
|
||||
- (@product.variant_images).each do |image|
|
||||
- tr_class = cycle('odd', 'even')
|
||||
- tr_id = spree_dom_id(image)
|
||||
%tr{class: tr_class, id: tr_id }
|
||||
%td.no-border
|
||||
%span.handle
|
||||
%td
|
||||
= link_to image_tag(image.attachment.url(:mini)), image.attachment.url(:product)
|
||||
- if @product.has_variants?
|
||||
%td= options_text_for(image)
|
||||
%td= image.alt
|
||||
%td.actions
|
||||
= link_to_with_icon 'icon-edit', t('spree.edit'), edit_admin_product_image_url(@product, image), no_text: true, data: { action: 'edit'}
|
||||
= link_to_delete image, { url: admin_product_image_url(@product, image), no_text: true }
|
||||
10
app/views/spree/admin/images/new.html.haml
Normal file
10
app/views/spree/admin/images/new.html.haml
Normal file
@@ -0,0 +1,10 @@
|
||||
= form_for [:admin, @product, @image], html: { multipart: true } do |f|
|
||||
%fieldset
|
||||
%legend{ align: "center" }= t('spree.new_image')
|
||||
= render partial: 'form', locals: { f: f }
|
||||
.form-buttons.filter-actions.actions
|
||||
= button t('spree.actions.update'), 'icon-refresh'
|
||||
%span.or= t('spree.or')
|
||||
= link_to_with_icon 'icon-remove', t('spree.actions.cancel'), admin_product_images_url(@product), id: 'cancel_link', class: 'button'
|
||||
|
||||
= javascript_include_tag 'admin/images/new.js'
|
||||
@@ -209,14 +209,14 @@ feature '
|
||||
Spree::Image.create(viewable_id: product.master.id, viewable_type: 'Spree::Variant', alt: "position 1", attachment: image, position: 1)
|
||||
|
||||
visit spree.admin_product_images_path(product)
|
||||
expect(page).to have_selector "table[data-hook='images_table'] td img"
|
||||
expect(page).to have_selector "table.index td img"
|
||||
expect(product.reload.images.count).to eq 1
|
||||
|
||||
accept_alert do
|
||||
page.find('a.delete-resource').click
|
||||
end
|
||||
|
||||
expect(page).to_not have_selector "table[data-hook='images_table'] td img"
|
||||
expect(page).to_not have_selector "table.index td img"
|
||||
expect(product.reload.images.count).to eq 0
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user