-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
release: 0.5.0
- Loading branch information
Showing
9 changed files
with
380 additions
and
3 deletions.
There are no files selected for viewing
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
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
11 changes: 11 additions & 0 deletions
11
db/migrate/20241008065657_add_decrypted_private_body_to_proposal_extra_field.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,11 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddDecryptedPrivateBodyToProposalExtraField < ActiveRecord::Migration[6.1] | ||
class ProposalExtraField < ApplicationRecord | ||
self.table_name = :decidim_awesome_proposal_extra_fields | ||
end | ||
|
||
def change | ||
add_column :decidim_awesome_proposal_extra_fields, :decrypted_private_body, :string | ||
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
18 changes: 18 additions & 0 deletions
18
lib/extends/models/decidim/decidim_awesome/proposal_extra_field_extends.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,18 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_support/concern" | ||
module ProposalExtraFieldExtends | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
after_save :update_decrypted_body | ||
|
||
private | ||
|
||
def update_decrypted_body | ||
update_columns(decrypted_private_body: private_body.to_s) if private_body.present? | ||
end | ||
end | ||
end | ||
|
||
Decidim::DecidimAwesome::ProposalExtraField.include(ProposalExtraFieldExtends) |
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 @@ | ||
# frozen_string_literal: true | ||
|
||
namespace :decidim do | ||
desc "Set decrypted_private_body to existing extra fields" | ||
task set_decrypted_private_body: :environment do | ||
extra_fields = Decidim::DecidimAwesome::ProposalExtraField.where(decrypted_private_body: nil).where.not(private_body: nil) | ||
if extra_fields.any? | ||
p "Extra fields to update: #{extra_fields.size}" | ||
count = 0 | ||
extra_fields.find_each do |extra_field| | ||
extra_field.update(decrypted_private_body: extra_field.private_body.to_s) | ||
count += 1 if extra_field.decrypted_private_body_previous_change.present? | ||
end | ||
p "Extra fields updated: #{count}" | ||
end | ||
end | ||
end |
29 changes: 29 additions & 0 deletions
29
spec/lib/tasks/decidim_app/set_decrypted_private_body_task_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,29 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
describe "rake decidim:set_decrypted_private_body", type: :task do | ||
let(:task) { Rake::Task["decidim:set_decrypted_private_body"] } | ||
let!(:extra_fields) { create(:awesome_proposal_extra_fields, proposal: create(:extended_proposal)) } | ||
|
||
before do | ||
extra_fields.private_body = { "en" => '<xml><dl><dt name="something">Something</dt></dl></xml>' } | ||
extra_fields.save! | ||
end | ||
|
||
it "preloads the Rails environment" do | ||
expect(task.prerequisites).to include "environment" | ||
end | ||
|
||
it "prints nothing if no extra_fields to update" do | ||
expect { task.execute }.not_to output("\"Extra fields to update: 1\"\n\"Extra fields updated: 1\"\n").to_stdout | ||
end | ||
|
||
it "sets the decrypted body correctly" do | ||
# we need an empty decrypted_private_body to test if the task will update it well | ||
extra_fields.update_columns(decrypted_private_body: nil) | ||
expect(extra_fields.decrypted_private_body).to be_nil | ||
expect { task.execute }.to output("\"Extra fields to update: 1\"\n\"Extra fields updated: 1\"\n").to_stdout | ||
expect(extra_fields.reload.decrypted_private_body).to eq('{"en"=>"<xml><dl><dt name=\"something\">Something</dt></dl></xml>"}') | ||
end | ||
end |
Oops, something went wrong.