From ea9f3ad6a008990d4497017fbcddbf740fd58f36 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Tue, 15 Oct 2019 00:00:54 +0100 Subject: [PATCH 1/4] Bring images controller from spree_backend so we can merge it with ofn's decorator --- .../spree/admin/images_controller.rb | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 app/controllers/spree/admin/images_controller.rb diff --git a/app/controllers/spree/admin/images_controller.rb b/app/controllers/spree/admin/images_controller.rb new file mode 100644 index 0000000000..e242483076 --- /dev/null +++ b/app/controllers/spree/admin/images_controller.rb @@ -0,0 +1,35 @@ +module Spree + module Admin + class ImagesController < ResourceController + 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 From 0f128f43f9965784fd1684c350bd152478b3d550 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Tue, 15 Oct 2019 00:02:16 +0100 Subject: [PATCH 2/4] Merge images controller decorator with the controller brought from spree_backend --- .../spree/admin/images_controller.rb | 38 ++++++++++--------- .../admin/images_controller_decorator.rb | 6 --- 2 files changed, 21 insertions(+), 23 deletions(-) delete mode 100644 app/controllers/spree/admin/images_controller_decorator.rb diff --git a/app/controllers/spree/admin/images_controller.rb b/app/controllers/spree/admin/images_controller.rb index e242483076..a506a9231d 100644 --- a/app/controllers/spree/admin/images_controller.rb +++ b/app/controllers/spree/admin/images_controller.rb @@ -1,6 +1,11 @@ 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 @@ -9,27 +14,26 @@ module Spree private - def location_after_save - admin_product_images_url(@product) - end + 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]) + 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 + 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 diff --git a/app/controllers/spree/admin/images_controller_decorator.rb b/app/controllers/spree/admin/images_controller_decorator.rb deleted file mode 100644 index 1f46d9eb54..0000000000 --- a/app/controllers/spree/admin/images_controller_decorator.rb +++ /dev/null @@ -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 From ec3c3aa644f4c332c57898b076733acb5f4198f0 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Tue, 15 Oct 2019 00:02:32 +0100 Subject: [PATCH 3/4] Bring images views from spree_backend --- app/views/spree/admin/images/_form.html.erb | 18 +++++++ app/views/spree/admin/images/edit.html.erb | 28 ++++++++++ app/views/spree/admin/images/index.html.erb | 58 +++++++++++++++++++++ app/views/spree/admin/images/new.html.erb | 15 ++++++ 4 files changed, 119 insertions(+) create mode 100644 app/views/spree/admin/images/_form.html.erb create mode 100644 app/views/spree/admin/images/edit.html.erb create mode 100644 app/views/spree/admin/images/index.html.erb create mode 100644 app/views/spree/admin/images/new.html.erb diff --git a/app/views/spree/admin/images/_form.html.erb b/app/views/spree/admin/images/_form.html.erb new file mode 100644 index 0000000000..51bcf751c4 --- /dev/null +++ b/app/views/spree/admin/images/_form.html.erb @@ -0,0 +1,18 @@ +
+
+
+ <%= f.label Spree.t(:filename) %>
+ <%= f.file_field :attachment %> +
+
+ <%= f.label Spree::Variant.model_name.human %>
+ <%= f.select :viewable_id, @variants, {}, {:class => 'select2 fullwidth'} %> +
+
+
+ <%= f.label Spree.t(:alt_text) %>
+ <%= f.text_area :alt, :rows => 4, :class => 'fullwidth' %> +
+
+ +
\ No newline at end of file diff --git a/app/views/spree/admin/images/edit.html.erb b/app/views/spree/admin/images/edit.html.erb new file mode 100644 index 0000000000..6bbb40c7c8 --- /dev/null +++ b/app/views/spree/admin/images/edit.html.erb @@ -0,0 +1,28 @@ +<%= 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 %> +
  • <%= button_link_to Spree.t(:back_to_images_list), admin_product_images_url(@product), :icon => 'icon-arrow-left' %>
  • +<% end %> + +<%= form_for [:admin, @product, @image], :html => { :multipart => true } do |f| %> +
    + <%= @image.attachment_file_name%> +
    + <%= f.label Spree.t(:thumbnail) %>
    + <%= link_to image_tag(@image.attachment.url(:small)), @image.attachment.url(:product) %> +
    +
    + <%= render :partial => 'form', :locals => { :f => f } %> +
    +
    +
    + <%= button Spree.t('actions.update'), 'icon-refresh' %> + <%= Spree.t(:or) %> + <%= link_to Spree.t('actions.cancel'), admin_product_images_url(@product), :id => 'cancel_link', :class => 'button icon-remove' %> +
    +
    +<% end %> diff --git a/app/views/spree/admin/images/index.html.erb b/app/views/spree/admin/images/index.html.erb new file mode 100644 index 0000000000..e08bb0903a --- /dev/null +++ b/app/views/spree/admin/images/index.html.erb @@ -0,0 +1,58 @@ +<%= render :partial => 'spree/admin/shared/product_sub_menu' %> + +<%= render :partial => 'spree/admin/shared/product_tabs', :locals => {:current => 'Images'} %> + +<% content_for :page_actions do %> +
  • <%= link_to_with_icon('icon-plus', Spree.t(:new_image), new_admin_product_image_url(@product), :id => 'new_image_link', :class => 'button') %>
  • +<% end %> + +
    + +<% unless @product.images.any? || @product.variant_images.any? %> +
    + <%= Spree.t(:no_images_found) %>. +
    +<% else %> + + + + + <% if @product.has_variants? %> + + <% end %> + + + + + + + <% if @product.has_variants? %> + + <% end %> + + + + + + + <% (@product.variant_images).each do |image| %> + + + + <% if @product.has_variants? %> + + <% end %> + + + + <% end %> + +
    <%= Spree.t(:thumbnail) %><%= Spree::Variant.model_name.human %><%= Spree.t(:alt_text) %>
    + + + <%= link_to image_tag(image.attachment.url(:mini)), image.attachment.url(:product) %> + <%= options_text_for(image) %><%= image.alt %> + <%= link_to_with_icon 'icon-edit', Spree.t(: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 } %> +
    +<% end %> diff --git a/app/views/spree/admin/images/new.html.erb b/app/views/spree/admin/images/new.html.erb new file mode 100644 index 0000000000..31841a2684 --- /dev/null +++ b/app/views/spree/admin/images/new.html.erb @@ -0,0 +1,15 @@ +<%= form_for [:admin, @product, @image], :html => { :multipart => true } do |f| %> +
    + <%= Spree.t(:new_image) %> + + <%= render :partial => 'form', :locals => { :f => f } %> + +
    + <%= button Spree.t('actions.update'), 'icon-refresh' %> + <%= Spree.t(:or) %> + <%= link_to_with_icon 'icon-remove', Spree.t('actions.cancel'), admin_product_images_url(@product), :id => 'cancel_link', :class => 'button' %> +
    +
    +<% end %> + +<%= javascript_include_tag 'admin/images/new.js' %> \ No newline at end of file From 04bf5a8742fa0ba9dd1f86dfdd259930e21b4719 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Tue, 15 Oct 2019 00:18:32 +0100 Subject: [PATCH 4/4] Convert images views from erb to haml --- app/views/spree/admin/images/_form.html.erb | 18 ------ app/views/spree/admin/images/_form.html.haml | 16 ++++++ app/views/spree/admin/images/edit.html.erb | 28 ---------- app/views/spree/admin/images/edit.html.haml | 21 +++++++ app/views/spree/admin/images/index.html.erb | 58 -------------------- app/views/spree/admin/images/index.html.haml | 43 +++++++++++++++ app/views/spree/admin/images/new.html.erb | 15 ----- app/views/spree/admin/images/new.html.haml | 10 ++++ spec/features/admin/products_spec.rb | 4 +- 9 files changed, 92 insertions(+), 121 deletions(-) delete mode 100644 app/views/spree/admin/images/_form.html.erb create mode 100644 app/views/spree/admin/images/_form.html.haml delete mode 100644 app/views/spree/admin/images/edit.html.erb create mode 100644 app/views/spree/admin/images/edit.html.haml delete mode 100644 app/views/spree/admin/images/index.html.erb create mode 100644 app/views/spree/admin/images/index.html.haml delete mode 100644 app/views/spree/admin/images/new.html.erb create mode 100644 app/views/spree/admin/images/new.html.haml diff --git a/app/views/spree/admin/images/_form.html.erb b/app/views/spree/admin/images/_form.html.erb deleted file mode 100644 index 51bcf751c4..0000000000 --- a/app/views/spree/admin/images/_form.html.erb +++ /dev/null @@ -1,18 +0,0 @@ -
    -
    -
    - <%= f.label Spree.t(:filename) %>
    - <%= f.file_field :attachment %> -
    -
    - <%= f.label Spree::Variant.model_name.human %>
    - <%= f.select :viewable_id, @variants, {}, {:class => 'select2 fullwidth'} %> -
    -
    -
    - <%= f.label Spree.t(:alt_text) %>
    - <%= f.text_area :alt, :rows => 4, :class => 'fullwidth' %> -
    -
    - -
    \ No newline at end of file diff --git a/app/views/spree/admin/images/_form.html.haml b/app/views/spree/admin/images/_form.html.haml new file mode 100644 index 0000000000..a316040c3c --- /dev/null +++ b/app/views/spree/admin/images/_form.html.haml @@ -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 diff --git a/app/views/spree/admin/images/edit.html.erb b/app/views/spree/admin/images/edit.html.erb deleted file mode 100644 index 6bbb40c7c8..0000000000 --- a/app/views/spree/admin/images/edit.html.erb +++ /dev/null @@ -1,28 +0,0 @@ -<%= 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 %> -
  • <%= button_link_to Spree.t(:back_to_images_list), admin_product_images_url(@product), :icon => 'icon-arrow-left' %>
  • -<% end %> - -<%= form_for [:admin, @product, @image], :html => { :multipart => true } do |f| %> -
    - <%= @image.attachment_file_name%> -
    - <%= f.label Spree.t(:thumbnail) %>
    - <%= link_to image_tag(@image.attachment.url(:small)), @image.attachment.url(:product) %> -
    -
    - <%= render :partial => 'form', :locals => { :f => f } %> -
    -
    -
    - <%= button Spree.t('actions.update'), 'icon-refresh' %> - <%= Spree.t(:or) %> - <%= link_to Spree.t('actions.cancel'), admin_product_images_url(@product), :id => 'cancel_link', :class => 'button icon-remove' %> -
    -
    -<% end %> diff --git a/app/views/spree/admin/images/edit.html.haml b/app/views/spree/admin/images/edit.html.haml new file mode 100644 index 0000000000..9b691479b4 --- /dev/null +++ b/app/views/spree/admin/images/edit.html.haml @@ -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' diff --git a/app/views/spree/admin/images/index.html.erb b/app/views/spree/admin/images/index.html.erb deleted file mode 100644 index e08bb0903a..0000000000 --- a/app/views/spree/admin/images/index.html.erb +++ /dev/null @@ -1,58 +0,0 @@ -<%= render :partial => 'spree/admin/shared/product_sub_menu' %> - -<%= render :partial => 'spree/admin/shared/product_tabs', :locals => {:current => 'Images'} %> - -<% content_for :page_actions do %> -
  • <%= link_to_with_icon('icon-plus', Spree.t(:new_image), new_admin_product_image_url(@product), :id => 'new_image_link', :class => 'button') %>
  • -<% end %> - -
    - -<% unless @product.images.any? || @product.variant_images.any? %> -
    - <%= Spree.t(:no_images_found) %>. -
    -<% else %> - - - - - <% if @product.has_variants? %> - - <% end %> - - - - - - - <% if @product.has_variants? %> - - <% end %> - - - - - - - <% (@product.variant_images).each do |image| %> - - - - <% if @product.has_variants? %> - - <% end %> - - - - <% end %> - -
    <%= Spree.t(:thumbnail) %><%= Spree::Variant.model_name.human %><%= Spree.t(:alt_text) %>
    - - - <%= link_to image_tag(image.attachment.url(:mini)), image.attachment.url(:product) %> - <%= options_text_for(image) %><%= image.alt %> - <%= link_to_with_icon 'icon-edit', Spree.t(: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 } %> -
    -<% end %> diff --git a/app/views/spree/admin/images/index.html.haml b/app/views/spree/admin/images/index.html.haml new file mode 100644 index 0000000000..89124383e2 --- /dev/null +++ b/app/views/spree/admin/images/index.html.haml @@ -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 } diff --git a/app/views/spree/admin/images/new.html.erb b/app/views/spree/admin/images/new.html.erb deleted file mode 100644 index 31841a2684..0000000000 --- a/app/views/spree/admin/images/new.html.erb +++ /dev/null @@ -1,15 +0,0 @@ -<%= form_for [:admin, @product, @image], :html => { :multipart => true } do |f| %> -
    - <%= Spree.t(:new_image) %> - - <%= render :partial => 'form', :locals => { :f => f } %> - -
    - <%= button Spree.t('actions.update'), 'icon-refresh' %> - <%= Spree.t(:or) %> - <%= link_to_with_icon 'icon-remove', Spree.t('actions.cancel'), admin_product_images_url(@product), :id => 'cancel_link', :class => 'button' %> -
    -
    -<% end %> - -<%= javascript_include_tag 'admin/images/new.js' %> \ No newline at end of file diff --git a/app/views/spree/admin/images/new.html.haml b/app/views/spree/admin/images/new.html.haml new file mode 100644 index 0000000000..22a1e01998 --- /dev/null +++ b/app/views/spree/admin/images/new.html.haml @@ -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' diff --git a/spec/features/admin/products_spec.rb b/spec/features/admin/products_spec.rb index 9e1d0a4895..26c0af02ba 100644 --- a/spec/features/admin/products_spec.rb +++ b/spec/features/admin/products_spec.rb @@ -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