[Fix #1543] Strip html tags from textarea champs

This commit is contained in:
Frederic Merizen 2018-03-06 14:37:20 +01:00
parent 3156be4d63
commit 3894fc48f6
2 changed files with 43 additions and 0 deletions

View file

@ -0,0 +1,16 @@
require Rails.root.join("app", "helpers", "html_to_string_helper")
namespace :'2018_03_06_clean_html_textareas' do
task clean: :environment do
include ActionView::Helpers::TextHelper
include HtmlToStringHelper
types_de_champ = TypeDeChamp.joins(:champ)
.where(type_champ: "textarea")
.where("champs.value LIKE '%<%'")
types_de_champ.find_each do |tdc|
tdc.champ.each { |c| c.update_column(:value, html_to_string(c.value)) }
end
end
end

View file

@ -0,0 +1,27 @@
require 'spec_helper'
describe '2018_03_06_clean_html_textareas#clean' do
let(:procedure) { create(:procedure) }
let(:type_champ) { create(:type_de_champ, procedure: procedure, type_champ: :textarea) }
let(:champ) { type_champ.champ.create(value: "<p>Gnahar<br>greu bouahaha</p>") }
let(:champ_date) { Time.local(1995) }
let(:rake_date) { Time.local(2018) }
let(:rake_task) { Rake::Task['2018_03_06_clean_html_textareas:clean'] }
before do
Timecop.freeze(champ_date) { champ }
TPS::Application.load_tasks
Timecop.freeze(rake_date) { rake_task.invoke }
champ.reload
end
after { rake_task.reenable }
it 'cleans up html tags' do
expect(champ.value).to eq("Gnahar\ngreu bouahaha\n")
end
it 'does not change the models dates' do
expect(champ.updated_at).to eq(champ_date)
end
end