-
-
Notifications
You must be signed in to change notification settings - Fork 724
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12930 from chahmedejaz/task/12878-add-variant-nam…
…e-in-od-report Report Orders and Distributors should display variant
- Loading branch information
Showing
6 changed files
with
147 additions
and
32 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
db/migrate/20241011071014_update_item_name_to_product_in_od_report.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
class UpdateItemNameToProductInOdReport < ActiveRecord::Migration[7.0] | ||
class ReportRenderingOptions < ActiveRecord::Base | ||
self.belongs_to_required_by_default = false | ||
|
||
belongs_to :user, class_name: "Spree::User" | ||
serialize :options, Hash, coder: YAML | ||
end | ||
|
||
# OD: Orders and Distributors | ||
def up | ||
# adding subtype filter just to be safe | ||
options = ReportRenderingOptions.where(report_type: 'orders_and_distributors', report_subtype: nil) | ||
|
||
options.find_each do |option| | ||
begin | ||
fields_to_show = option.options[:fields_to_show] | ||
next if fields_to_show&.exclude?('item_name') | ||
|
||
fields_to_show.delete('item_name') | ||
fields_to_show << 'product' | ||
option.save | ||
rescue StandardError => e | ||
puts "Failed to update rendering option with id: #{option.id}" | ||
puts "Error: #{e.message}" | ||
end | ||
end | ||
end | ||
|
||
def down | ||
options = ReportRenderingOptions.where(report_type: 'orders_and_distributors', report_subtype: nil) | ||
|
||
options.find_each do |option| | ||
begin | ||
fields_to_show = option.options[:fields_to_show] | ||
next if fields_to_show&.exclude?('product') | ||
|
||
fields_to_show.delete('product') | ||
fields_to_show << 'item_name' | ||
option.update(options: option.options) | ||
rescue StandardError => e | ||
puts "Failed to revert rendering option with id: #{option.id}" | ||
puts "Error: #{e.message}" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :orders_and_distributors_options, class: ReportRenderingOptions do | ||
report_type { "orders_and_distributors" } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
spec/migrations/20241011071014_update_item_name_to_product_in_od_report_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
require_relative '../../db/migrate/20241011071014_update_item_name_to_product_in_od_report' | ||
|
||
RSpec.describe UpdateItemNameToProductInOdReport, type: :migration do | ||
let!(:report_option_without_item_name_product) do | ||
create( | ||
:orders_and_distributors_options, | ||
options: { fields_to_show: ['other_field'] } | ||
) | ||
end | ||
|
||
describe '#up' do | ||
let!(:report_option_with_item_name) do | ||
create( | ||
:orders_and_distributors_options, | ||
options: { fields_to_show: ['item_name', 'other_field'] } | ||
) | ||
end | ||
before { subject.up } | ||
|
||
it 'updates fields_to_show from item_name to product only if options have item_name' do | ||
report_option_with_item_name.reload | ||
expect(fields_to_show(report_option_with_item_name)).to eq(['other_field', 'product']) | ||
expect(fields_to_show(report_option_without_item_name_product)).to eq(['other_field']) | ||
end | ||
end | ||
|
||
describe '#down' do | ||
let!(:report_option_with_product) do | ||
create( | ||
:orders_and_distributors_options, | ||
options: { fields_to_show: ['product', 'other_field'] } | ||
) | ||
end | ||
before { subject.down } | ||
|
||
it 'reverts fields_to_show from product to item_name only if options have product' do | ||
report_option_with_product.reload | ||
expect(fields_to_show(report_option_with_product)).to eq(['other_field', 'item_name']) | ||
expect(fields_to_show(report_option_without_item_name_product)).to eq(['other_field']) | ||
end | ||
end | ||
|
||
def fields_to_show(report_options) | ||
report_options.options[:fields_to_show] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters