-
-
Notifications
You must be signed in to change notification settings - Fork 792
Prevent admins to be able to view carousel in shops #14364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
e2db4f8
ac0dfcd
ea26d55
fba566f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class EnsureSingleProductImage < ActiveRecord::Migration[7.1] | ||
| class SpreeImage < ActiveRecord::Base | ||
| self.table_name = "spree_assets" | ||
| self.inheritance_column = :_type_disabled | ||
| end | ||
|
|
||
| def up | ||
| product_ids = SpreeImage | ||
| .where(viewable_type: "Spree::Product") | ||
| .group(:viewable_id) | ||
| .having("COUNT(*) > 1") | ||
| .pluck(:viewable_id) | ||
|
|
||
| product_ids.each do |product_id| | ||
| images = SpreeImage | ||
| .where( | ||
| viewable_type: "Spree::Product", | ||
| viewable_id: product_id | ||
| ) | ||
| .order(:id) | ||
|
|
||
| image_to_keep = images.first | ||
|
|
||
| images.where.not(id: image_to_keep.id).update_all(deleted_at: Time.current) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to run after the other migration, and I am pretty sure currently it's the other way around, based on the migration's timestamp in the name. Could you fix the migration to make sure they run in the correct order ? |
||
| end | ||
| end | ||
|
|
||
| def down | ||
| raise ActiveRecord::IrreversibleMigration | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class AddDeletedAtToAssets < ActiveRecord::Migration[7.1] | ||
| def change | ||
| add_column :spree_assets, :deleted_at, :datetime | ||
| add_index :spree_assets, :deleted_at | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,80 @@ | ||||||||
| # frozen_string_literal: true | ||||||||
|
|
||||||||
| require_relative '../../db/migrate/20260602222924_ensure_single_product_image' | ||||||||
|
|
||||||||
| RSpec.describe EnsureSingleProductImage, type: :migration do | ||||||||
| let(:migration) { described_class.new } | ||||||||
| let(:attachment) { Rack::Test::UploadedFile.new(Rails.root.join('app/webpacker/images/logo-white.png'), "image/png") } | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have a helper for this:
Suggested change
|
||||||||
|
|
||||||||
| describe '#up' do | ||||||||
| let(:product) { create(:product) } | ||||||||
|
|
||||||||
| it 'keeps the first image and soft deletes additional images for a product' do | ||||||||
| first_image = Spree::Image.create(attachment:, | ||||||||
| viewable_id: product.id, | ||||||||
| viewable_type: 'Spree::Product') | ||||||||
|
|
||||||||
| second_image = Spree::Image.create(attachment:, | ||||||||
| viewable_id: product.id, | ||||||||
| viewable_type: 'Spree::Product') | ||||||||
|
|
||||||||
| expect do | ||||||||
| migration.up | ||||||||
| end.to change { | ||||||||
| Spree::Image.where( | ||||||||
| viewable_type: 'Spree::Product', viewable_id: product.id | ||||||||
| ).count | ||||||||
| }.from(2).to(1) | ||||||||
|
|
||||||||
| remaining_ids = Spree::Image.where( | ||||||||
| viewable_type: 'Spree::Product', | ||||||||
| viewable_id: product.id | ||||||||
| ).pluck(:id) | ||||||||
|
|
||||||||
| expect(remaining_ids).to contain_exactly(first_image.id) | ||||||||
| expect(remaining_ids).not_to include(second_image.id) | ||||||||
|
|
||||||||
| # Verify second image is soft deleted, not hard deleted | ||||||||
| soft_deleted_image = Spree::Image.unscoped.find(second_image.id) | ||||||||
| expect(soft_deleted_image.deleted_at).not_to be_nil | ||||||||
| end | ||||||||
|
|
||||||||
| it 'does not remove an image when a product already has a single image' do | ||||||||
| image = Spree::Image.create(attachment:, | ||||||||
| viewable_id: product.id, | ||||||||
| viewable_type: 'Spree::Product') | ||||||||
|
|
||||||||
| expect do | ||||||||
| migration.up | ||||||||
| end.not_to change { | ||||||||
| Spree::Image.where( | ||||||||
| viewable_type: 'Spree::Product', viewable_id: product.id | ||||||||
| ).count | ||||||||
| } | ||||||||
|
|
||||||||
| expect(Spree::Image.exists?(image.id)).to be(true) | ||||||||
| end | ||||||||
|
|
||||||||
| it 'does not remove assets for non-product viewables' do | ||||||||
| variant = create(:variant) | ||||||||
| first_variant_image = Spree::Image.create!(attachment:, | ||||||||
| viewable_id: variant.id, | ||||||||
| viewable_type: 'Spree::Variant') | ||||||||
| second_variant_image = Spree::Image.create!(attachment:, | ||||||||
| viewable_id: variant.id, | ||||||||
| viewable_type: 'Spree::Variant') | ||||||||
|
|
||||||||
| expect do | ||||||||
| migration.up | ||||||||
| end.not_to change { | ||||||||
| Spree::Image.where( | ||||||||
| viewable_type: 'Spree::Variant', viewable_id: variant.id | ||||||||
| ).count | ||||||||
| } | ||||||||
|
|
||||||||
| # Verify both variant images are not deleted | ||||||||
| expect(Spree::Image.unscoped.find(first_variant_image.id).deleted_at).to be_nil | ||||||||
| expect(Spree::Image.unscoped.find(second_variant_image.id).deleted_at).to be_nil | ||||||||
| end | ||||||||
| end | ||||||||
| end | ||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am assuming this refers to :
Could you add a comment to explain what it does and why it's needed ?