Skip to content

Commit

Permalink
Merge pull request #487 from h-kataria/cb_reports_startpage_update
Browse files Browse the repository at this point in the history
Chargeback Reports features and startpage data migrations
  • Loading branch information
Fryguy authored Jun 1, 2020
2 parents 7df8031 + 19856e9 commit 3a66ffb
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 0 deletions.
26 changes: 26 additions & 0 deletions db/migrate/20200520210200_update_chargeback_reports_startpage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class UpdateChargebackReportsStartpage < ActiveRecord::Migration[5.2]
class User < ActiveRecord::Base
serialize :settings, Hash
include ActiveRecord::IdRegions
end

def up
say_with_time 'Updating starting page for users who had chargeback reports set' do
User.in_my_region.select(:id, :settings).each do |user|
if user.settings&.dig(:display, :startpage) == 'chargeback_reports/explorer'
user.update!(:settings => user.settings.deep_merge(:display => {:startpage => 'chargeback_report/show_list'}))
end
end
end
end

def down
say_with_time 'Updating starting page for users who had non-explorer chargeback reports pages set' do
User.in_my_region.select(:id, :settings).each do |user|
if user.settings&.dig(:display, :startpage) == 'chargeback_report/show_list'
user.update!(:settings => user.settings.deep_merge(:display => {:startpage => 'chargeback_reports/explorer'}))
end
end
end
end
end
52 changes: 52 additions & 0 deletions db/migrate/20200520211047_adjust_chargeback_reports_features.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class AdjustChargebackReportsFeatures < ActiveRecord::Migration[5.2]
class MiqProductFeature < ActiveRecord::Base; end
class MiqRolesFeature < ActiveRecord::Base; end

FEATURE_MAPPING = {
'chargeback_download_csv' => 'chargeback_reports_download_csv',
'chargeback_download_pdf' => 'chargeback_reports_download_pdf',
'chargeback_download_text' => 'chargeback_reports_download_text',
'chargeback_report_only' => 'chargeback_reports_report_only'
}.freeze

def up
return if MiqProductFeature.none?

say_with_time 'Adjusting chargeback reports features for the non-explorer views' do
new_feature = MiqProductFeature.find_or_create_by!(:identifier => 'chargeback_reports_view')
user_role_ids_with_old_features = MiqRolesFeature.where(:miq_product_feature_id => old_feature_ids).pluck(:miq_user_role_id).uniq
user_role_ids_with_old_features.each do |user_role_id|
MiqRolesFeature.create!(:miq_product_feature_id => new_feature.id, :miq_user_role_id => user_role_id)
end

# Direct renaming of features
FEATURE_MAPPING.each do |from, to|
MiqProductFeature.find_by(:identifier => from)&.update!(:identifier => to)
end
end
end

def down
return if MiqProductFeature.none?

say_with_time 'Adjusting chargeback reports features to explorer views' do
admin_feature = MiqProductFeature.find_or_create_by!(:identifier => 'chargeback_reports')

MiqRolesFeature.where(:miq_product_feature_id => admin_feature.id).each do |feature|
old_feature_ids.each do |id|
MiqRolesFeature.create!(:miq_product_feature_id => id, :miq_user_role_id => feature.miq_user_role_id)
end
end

FEATURE_MAPPING.each do |to, from|
MiqProductFeature.find_by(:identifier => from)&.update!(:identifier => to)
end
end
end

private

def old_feature_ids
FEATURE_MAPPING.keys.map { |identifier| MiqProductFeature.find_or_create_by!(:identifier => identifier) }
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require_migration

describe UpdateChargebackReportsStartpage do
let(:user_stub) { migration_stub :User }

migration_context :up do
describe 'starting page replace' do
it 'replaces user starting page if chargeback_reports/explorer' do
user = user_stub.create!(:settings => {:display => {:startpage => 'chargeback_reports/explorer'}})

migrate
user.reload

expect(user.settings[:display][:startpage]).to eq('chargeback_report/show_list')
end
end

it 'does not affect users without settings' do
user = user_stub.create!

migrate

expect(user_stub.find(user.id)).to eq(user)
end
end

migration_context :down do
describe 'starting page replace' do
it "replaces user starting page to chargeback_reports/explorer if chargeback_report/show_list" do
user = user_stub.create!(:settings => {:display => {:startpage => 'chargeback_report/show_list'}})

migrate
user.reload

expect(user.settings[:display][:startpage]).to eq('chargeback_reports/explorer')
end

it 'does not affect users without settings' do
user = user_stub.create!

migrate

expect(user_stub.find(user.id)).to eq(user)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
require_migration

describe AdjustChargebackReportsFeatures do
let(:user_role_id) { anonymous_class_with_id_regions.id_in_region(1, anonymous_class_with_id_regions.my_region_number) }
let(:feature_stub) { migration_stub :MiqProductFeature }
let(:roles_feature_stub) { migration_stub :MiqRolesFeature }

migration_context :up do
describe 'product features update' do
it 'also sets the chargeback_reports_view feature' do
AdjustChargebackReportsFeatures::FEATURE_MAPPING.keys.each do |identifier|
feature = feature_stub.create!(:identifier => identifier)
roles_feature_stub.create!(:miq_product_feature_id => feature.id, :miq_user_role_id => user_role_id)
end

migrate

assigned = roles_feature_stub.where(:miq_user_role_id => user_role_id)
expect(assigned.count).to eq(5)

feature = feature_stub.find_by(:identifier => 'chargeback_reports_view')
new_roles_feature = roles_feature_stub.where(:miq_user_role_id => user_role_id).where(:miq_product_feature_id => feature.id).first
new_feature = feature_stub.find(new_roles_feature.miq_product_feature_id)
expect(new_feature.identifier).to eq('chargeback_reports_view')
end

it 'renames the features' do
view_feature1 = feature_stub.create!(:identifier => 'chargeback_download_csv')
view_feature2 = feature_stub.create!(:identifier => 'chargeback_download_pdf')
view_feature3 = feature_stub.create!(:identifier => 'chargeback_download_text')
view_feature4 = feature_stub.create!(:identifier => 'chargeback_report_only')

migrate

expect(view_feature1.reload.identifier).to eq('chargeback_reports_download_csv')
expect(view_feature2.reload.identifier).to eq('chargeback_reports_download_pdf')
expect(view_feature3.reload.identifier).to eq('chargeback_reports_download_text')
expect(view_feature4.reload.identifier).to eq('chargeback_reports_report_only')
end
end
end

migration_context :down do
let!(:view_feature1) { feature_stub.create!(:identifier => 'chargeback_reports_download_csv') }
let!(:view_feature2) { feature_stub.create!(:identifier => 'chargeback_reports_download_pdf') }
let!(:view_feature3) { feature_stub.create!(:identifier => 'chargeback_reports_download_text') }
let!(:view_feature4) { feature_stub.create!(:identifier => 'chargeback_reports_report_only') }

describe 'product features update' do
it 'sets the chargeback_reports_view feature' do
feature = feature_stub.create!(:identifier => 'chargeback_reports')
roles_feature_stub.create!(:miq_product_feature_id => feature.id, :miq_user_role_id => user_role_id)

migrate

assigned = roles_feature_stub.where(:miq_user_role_id => user_role_id)
expect(assigned.count).to eq(5)

expect(view_feature1.reload.identifier).to eq('chargeback_download_csv')
expect(view_feature2.reload.identifier).to eq('chargeback_download_pdf')
expect(view_feature3.reload.identifier).to eq('chargeback_download_text')
expect(view_feature4.reload.identifier).to eq('chargeback_report_only')
end

it 'product feature is renamed when no role features are set' do
migrate

assigned = roles_feature_stub.where(:miq_user_role_id => user_role_id)
expect(assigned.count).to eq(0)

expect(view_feature1.reload.identifier).to eq('chargeback_download_csv')
expect(view_feature2.reload.identifier).to eq('chargeback_download_pdf')
expect(view_feature3.reload.identifier).to eq('chargeback_download_text')
expect(view_feature4.reload.identifier).to eq('chargeback_report_only')
end
end
end
end

0 comments on commit 3a66ffb

Please sign in to comment.