1
0
Fork 0

Working version contained within template model

Dieser Commit ist enthalten in:
Angus McLeod 2022-08-02 13:54:57 +01:00
Ursprung 2cec01ba2c
Commit 1e8b667e3f
5 geänderte Dateien mit 92 neuen und 37 gelöschten Zeilen

Datei anzeigen

@ -44,7 +44,7 @@
imageUrl=field.image imageUrl=field.image
onUploadDone=(action "imageUploadDone") onUploadDone=(action "imageUploadDone")
onUploadDeleted=(action "imageUploadDeleted") onUploadDeleted=(action "imageUploadDeleted")
type=(concat "wizard-" wizard.id) type="wizard-field-image"
class="no-repeat contain-image" class="no-repeat contain-image"
id=(concat "wizard-field-" field.id "-image-upload")}} id=(concat "wizard-field-" field.id "-image-upload")}}
</div> </div>

Datei anzeigen

@ -18,7 +18,7 @@
imageUrl=step.banner imageUrl=step.banner
onUploadDone=(action "bannerUploadDone") onUploadDone=(action "bannerUploadDone")
onUploadDeleted=(action "bannerUploadDeleted") onUploadDeleted=(action "bannerUploadDeleted")
type=(concat "wizard-" wizard.id) type="wizard-step-banner"
class="no-repeat contain-image" class="no-repeat contain-image"
id=(concat "wizard-step-" step.id "-banner-upload")}} id=(concat "wizard-step-" step.id "-banner-upload")}}
</div> </div>

Datei anzeigen

@ -29,10 +29,10 @@ class CustomWizard::Template
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
schedule_save_jobs unless opts[:skip_jobs] schedule_save_jobs unless opts[:skip_jobs]
PluginStore.set(CustomWizard::PLUGIN_NAME, @data[:id], @data) PluginStore.set(CustomWizard::PLUGIN_NAME, @data[:id], @data)
ensure_wizard_upload_references!
end end
self.class.clear_cache_keys self.class.clear_cache_keys
remove_unused_wizard_upload_references
@data[:id] @data[:id]
end end
@ -53,17 +53,21 @@ class CustomWizard::Template
PluginStore.get(CustomWizard::PLUGIN_NAME, wizard_id) PluginStore.get(CustomWizard::PLUGIN_NAME, wizard_id)
end end
def self.find_record(wizard_id)
PluginStoreRow.find_by(plugin_name: CustomWizard::PLUGIN_NAME, key: wizard_id)
end
def self.remove(wizard_id) def self.remove(wizard_id)
wizard = CustomWizard::Wizard.create(wizard_id) wizard = CustomWizard::Wizard.create(wizard_id)
return false if !wizard return false if !wizard
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
ensure_wizard_upload_references!(wizard_id)
PluginStore.remove(CustomWizard::PLUGIN_NAME, wizard.id) PluginStore.remove(CustomWizard::PLUGIN_NAME, wizard.id)
clear_user_wizard_redirect(wizard_id, after_time: !!wizard.after_time) clear_user_wizard_redirect(wizard_id, after_time: !!wizard.after_time)
end end
clear_cache_keys clear_cache_keys
remove_wizard_upload_references
true true
end end
@ -125,6 +129,18 @@ class CustomWizard::Template
CustomWizard::Cache.new(AFTER_TIME_CACHE_KEY).delete CustomWizard::Cache.new(AFTER_TIME_CACHE_KEY).delete
end end
def self.ensure_wizard_upload_references!(wizard_id, wizard_upload_ids = [])
wizard_record = find_record(wizard_id)
if wizard_record
UploadReference.ensure_exist!(
upload_ids: wizard_upload_ids,
target_type: "PluginStoreRow",
target_id: wizard_record.id
)
end
end
private private
def normalize_data def normalize_data
@ -179,7 +195,7 @@ class CustomWizard::Template
end end
end end
def collect_upload_ids def ensure_wizard_upload_references!
upload_ids = [] upload_ids = []
@data[:steps].each do |step| @data[:steps].each do |step|
@ -190,21 +206,7 @@ class CustomWizard::Template
end end
end end
upload_ids upload_ids = upload_ids.select { |upload_id| Upload.exists?(upload_id) }
end self.class.ensure_wizard_upload_references!(@data[:id], upload_ids)
def wizard_upload_references
@wizard_upload_references ||= begin
record = PluginStoreRow.find_by(plugin_name: CustomWizard::PLUGIN_NAME, key: @data[:id])
UploadReference.where(target_type: "CustomWizard", target_id: custom_wizard_record.id)
end
end
def remove_unused_wizard_upload_references
wizard_upload_references.where.not(upload_id: collect_upload_ids).delete_all
end
def remove_wizard_upload_references
wizard_upload_references.delete_all
end end
end end

Datei anzeigen

@ -226,21 +226,5 @@ after_initialize do
::DiscourseTagging.singleton_class.prepend CustomWizardDiscourseTagging ::DiscourseTagging.singleton_class.prepend CustomWizardDiscourseTagging
end end
on(:after_upload_creation) do |upload, opts|
from_wizard = opts[:type].include?("wizard-")
wizard_id = opts[:type].split("wizard-").last
wizard_record = PluginStoreRow.find_by(plugin_name: CustomWizard::PLUGIN_NAME, key: wizard_id)
if wizard_record
UploadReference.create(
upload_id: upload.id,
target_type: "CustomWizard",
target_id: wizard_record.id,
created_at: Time.zone.now,
updated_at: Time.zone.now
)
end
end
DiscourseEvent.trigger(:custom_wizard_ready) DiscourseEvent.trigger(:custom_wizard_ready)
end end

Datei anzeigen

@ -2,6 +2,7 @@
describe CustomWizard::Template do describe CustomWizard::Template do
fab!(:user) { Fabricate(:user) } fab!(:user) { Fabricate(:user) }
fab!(:upload) { Fabricate(:upload) }
let(:template_json) { let(:template_json) {
JSON.parse(File.open( JSON.parse(File.open(
@ -54,6 +55,74 @@ describe CustomWizard::Template do
).to eq(true) ).to eq(true)
end end
context "upload references" do
it "are added if a wizard has a step banner" do
template_json['steps'][0]['banner'] = upload.url
template_json['steps'][0]['banner_upload_id'] = upload.id
CustomWizard::Template.save(template_json, skip_jobs: true)
wizard_record = CustomWizard::Template.find_record(template_json["id"])
expect(
UploadReference.exists?(
upload_id: upload.id,
target_type: "PluginStoreRow",
target_id: wizard_record.id
)
).to eq(true)
end
it "are added if a wizard has a field image" do
template_json['steps'][0]["fields"][0]['image'] = upload.url
template_json['steps'][0]["fields"][0]['image_upload_id'] = upload.id
CustomWizard::Template.save(template_json, skip_jobs: true)
wizard_record = CustomWizard::Template.find_record(template_json["id"])
expect(
UploadReference.exists?(
upload_id: upload.id,
target_type: "PluginStoreRow",
target_id: wizard_record.id
)
).to eq(true)
end
it "are removed if a wizard step banner is removed" do
template_json['steps'][0]['banner'] = upload.url
template_json['steps'][0]['banner_upload_id'] = upload.id
CustomWizard::Template.save(template_json, skip_jobs: true)
template_json['steps'][0]['banner'] = nil
template_json['steps'][0]['banner_upload_id'] = nil
CustomWizard::Template.save(template_json, skip_jobs: true)
wizard_record = CustomWizard::Template.find_record(template_json["id"])
expect(
UploadReference.exists?(target_type: "PluginStoreRow")
).to eq(false)
end
it "are removed if a wizard field image is removed" do
template_json['steps'][0]["fields"][0]['image'] = upload.url
template_json['steps'][0]["fields"][0]['image_upload_id'] = upload.id
CustomWizard::Template.save(template_json, skip_jobs: true)
template_json['steps'][0]["fields"][0]['image'] = nil
template_json['steps'][0]["fields"][0]['image_upload_id'] = nil
CustomWizard::Template.save(template_json, skip_jobs: true)
wizard_record = CustomWizard::Template.find_record(template_json["id"])
expect(
UploadReference.exists?(target_type: "PluginStoreRow")
).to eq(false)
end
it "are removed if a wizard is removed" do
template_json['steps'][0]["fields"][0]['image'] = upload.url
template_json['steps'][0]["fields"][0]['image_upload_id'] = upload.id
CustomWizard::Template.save(template_json, skip_jobs: true)
CustomWizard::Template.remove(template_json["id"])
expect(
UploadReference.exists?(target_type: "PluginStoreRow")
).to eq(false)
end
end
context "wizard template list" do context "wizard template list" do
before do before do
template_json_2 = template_json.dup template_json_2 = template_json.dup