-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix any existing rate cadences to reflect new enums
- Loading branch information
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
lib/engines/content_block_manager/lib/tasks/update_rates_cadence.rake
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,17 @@ | ||
namespace :content_block_manager do | ||
desc "Update Content Block Manager Version Diffs" | ||
task update_rates_cadence: :environment do | ||
ContentBlockManager::ContentBlock::Document.where(block_type: "pension").find_each do |document| | ||
document.editions.each do |edition| | ||
edition.details.dig("rates")&.each do |key, value| | ||
if value["cadence"] == "weekly" | ||
edition.details.dig("rates")[key]["cadence"] = "a week" | ||
elsif value["cadence"] == "monthly" | ||
edition.details.dig("rates")[key]["cadence"] = "a month" | ||
end | ||
end | ||
edition.save! | ||
end | ||
end | ||
end | ||
end |
65 changes: 65 additions & 0 deletions
65
lib/engines/content_block_manager/test/unit/lib/tasks/update_rates_cadence_test.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,65 @@ | ||
require "test_helper" | ||
require "rake" | ||
|
||
class UpdateRatesCadenceTest < ActiveSupport::TestCase | ||
extend Minitest::Spec::DSL | ||
|
||
teardown do | ||
Rake::Task["content_block_manager:update_rates_cadence"].reenable | ||
end | ||
|
||
let(:schema) { build(:content_block_schema, block_type: "content_block_pension", body: { }) } | ||
Check failure on line 11 in lib/engines/content_block_manager/test/unit/lib/tasks/update_rates_cadence_test.rb
|
||
|
||
before do | ||
ContentBlockManager::ContentBlock::Schema.expects(:find_by_block_type).returns(schema).at_least_once | ||
end | ||
|
||
|
||
it "updates the rate cadences" do | ||
document = create(:content_block_document, :pension) | ||
|
||
edition_1 = create(:content_block_edition, document:, title: "Edition 1", details: { | ||
"description": "Edition 1", | ||
"rates": { | ||
"rate1": | ||
{ "name": "rate1", "amount": "£100.5", "cadence": "weekly", "description": "" }, | ||
}, | ||
}) | ||
|
||
edition_2 = create(:content_block_edition, document:, title: "Edition 2", details: { | ||
"description": "Edition 2", | ||
"rates": { | ||
"rate1": | ||
{ "name": "rate3", "amount": "£100.5", "cadence": "monthly", "description": "" }, | ||
}, | ||
}) | ||
|
||
document_2 = create(:content_block_document, :pension) | ||
|
||
edition_3 = create(:content_block_edition, document: document_2, title: "Edition 3", details: { | ||
"description": "Edition 3", | ||
"rates": { | ||
"rate1": | ||
{ "name": "rate3", "amount": "£100.5", "cadence": "a month", "description": "" }, | ||
}, | ||
}) | ||
|
||
edition_4 = create(:content_block_edition, document: document_2, title: "Edition 4", details: { | ||
"description": "Edition 4", | ||
"rates": { | ||
"rate1": | ||
{ "name": "rate1", "amount": "£100.5", "cadence": "weekly", "description": "" }, | ||
"rate2": | ||
{ "name": "rate2", "amount": "£100.5", "cadence": "monthly", "description": "" }, | ||
}, | ||
}) | ||
|
||
Rake.application.invoke_task("content_block_manager:update_rates_cadence") | ||
|
||
assert_equal "a week", edition_1.reload.details.dig("rates", "rate1", "cadence") | ||
assert_equal "a month", edition_2.reload.details.dig("rates", "rate1", "cadence") | ||
assert_equal "a month", edition_3.reload.details.dig("rates", "rate1", "cadence") | ||
assert_equal "a week", edition_4.reload.details.dig("rates", "rate1", "cadence") | ||
assert_equal "a month", edition_4.reload.details.dig("rates", "rate2", "cadence") | ||
end | ||
end |