Skip to content
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

Feat/add decrypted private body to extra fields #105

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .rubocop_rails.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ Rails/SkipsModelValidations:
Enabled: true
Exclude:
- db/migrate/*.rb
- lib/extends/models/decidim/decidim_awesome/proposal_extra_field_extends.rb
- spec/lib/tasks/decidim_app/set_decrypted_private_body_task_spec.rb

Rails/Validation:
Include:
Expand All @@ -103,4 +105,4 @@ Rails/BulkChangeTable:

RSpec/MultipleMemoizedHelpers:
Exclude:
- spec/**/**
- spec/**/**
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ up: build
@make setup-database

build:
docker build . -f Dockerfile.local -t decidim-app:latest
docker build . -f Dockerfile.local -t decidim-lyon:latest

# Stops containers and remove volumes
teardown:
Expand Down Expand Up @@ -51,4 +51,4 @@ external:
rebuild:
docker compose -f docker-compose.local.yml down
docker volume rm decidim-app_shared-volume || true
@make up
@make up
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class Application < Rails::Application
require "extends/commands/decidim/meetings/admin/create_meeting_extends"
require "extends/commands/decidim/meetings/update_meeting_extends"
require "extends/commands/decidim/meetings/create_meeting_extends"
require "extends/models/decidim/decidim_awesome/proposal_extra_field_extends"

Decidim::GraphiQL::Rails.config.tap do |config|
config.initial_query = "{\n deployment {\n version\n branch\n remote\n upToDate\n currentCommit\n latestCommit\n locallyModified\n }\n}".html_safe
Expand Down
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
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2024_08_19_170611) do
ActiveRecord::Schema.define(version: 2024_10_08_065657) do

# These are extensions that must be enabled in order to support this database
enable_extension "ltree"
Expand Down Expand Up @@ -335,6 +335,7 @@
t.string "private_body"
t.string "decidim_proposal_type", null: false
t.datetime "private_body_updated_at"
t.string "decrypted_private_body"
t.index ["decidim_proposal_id", "decidim_proposal_type"], name: "index_decidim_awesome_proposal_extra_fields_on_decidim_proposal"
end

Expand Down
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)
17 changes: 17 additions & 0 deletions lib/tasks/set_decrypted_private_body.rake
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 spec/lib/tasks/decidim_app/set_decrypted_private_body_task_spec.rb
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
Loading
Loading