2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-12 20:03:43 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
|
|
|
|
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
|
2018-10-04 20:29:30 +00:00
|
|
|
module MaintainDedications
|
|
|
|
def self.retrieve_json_dedications
|
2019-07-30 21:29:24 +00:00
|
|
|
Qx.select('id', 'dedication').from(:donations)
|
|
|
|
.where('is_valid_json(dedication)').ex
|
2018-10-04 20:29:30 +00:00
|
|
|
end
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def self.retrieve_non_json_dedications(include_blank = false)
|
2018-10-04 20:29:30 +00:00
|
|
|
temp = Qx.select('id', 'dedication').from(:donations)
|
|
|
|
temp = temp.where("dedication IS NOT NULL AND dedication != ''") unless include_blank
|
2019-07-30 21:29:24 +00:00
|
|
|
temp = temp.and_where('NOT is_valid_json(dedication)')
|
|
|
|
temp.ex
|
2018-10-04 20:29:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.create_json_dedications_from_plain_text(dedications)
|
|
|
|
dedications.map do |i|
|
2019-07-30 21:29:24 +00:00
|
|
|
output = { id: i['id'] }
|
2018-10-04 20:29:30 +00:00
|
|
|
if i['dedication'] =~ /(((in (loving )?)?memory of|in memorium)\:? )(.+)/i
|
2019-07-30 21:29:24 +00:00
|
|
|
output[:dedication] = JSON.generate(type: 'memory', note: $+)
|
2018-10-04 20:29:30 +00:00
|
|
|
elsif i['dedication'] =~ /((in honor of|honor of)\:? )(.+)/i
|
2019-07-30 21:29:24 +00:00
|
|
|
output[:dedication] = JSON.generate(type: 'honor', note: $+)
|
2018-10-04 20:29:30 +00:00
|
|
|
else
|
2019-07-30 21:29:24 +00:00
|
|
|
output[:dedication] = JSON.generate(type: 'honor', note: i['dedication'])
|
2018-10-04 20:29:30 +00:00
|
|
|
end
|
|
|
|
output
|
|
|
|
end.each do |i|
|
2019-07-30 21:29:24 +00:00
|
|
|
Qx.update(:donations).where('id = $id', id: i[:id]).set(dedication: i[:dedication]).ex
|
2018-10-04 20:29:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.add_honor_to_any_json_dedications_without_type(json_dedications)
|
2019-07-30 21:29:24 +00:00
|
|
|
json_dedications.map { |i| { 'id' => i['id'], 'dedication' => JSON.parse(i['dedication']) } }
|
|
|
|
.reject { |i| %w[honor memory].include?(i['dedication']['type']) }
|
|
|
|
.map { |i| i['dedication']['type'] = 'honor'; i }
|
|
|
|
.each do |i|
|
|
|
|
Qx.update(:donations).where('id = $id', id: i['id'])
|
|
|
|
.set(dedication: JSON.generate(i['dedication'])).ex
|
|
|
|
end
|
2018-10-04 20:29:30 +00:00
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|