1
0
Fork 0

simplified logic

Dieser Commit ist enthalten in:
Faizaan Gagan 2022-01-31 12:50:20 +05:30
Ursprung 5d882d69a2
Commit f5a35baa1b
4 geänderte Dateien mit 40 neuen und 121 gelöschten Zeilen

Datei anzeigen

@ -1,59 +1,17 @@
# frozen_string_literal: true
module CustomWizardGuardian
def can_see_topic?(topic, hide_deleted = true)
wizard_user_can_create_topic_on_category?(topic) || super
end
def can_edit_topic?(topic)
wizard_user_can_create_topic_on_category?(topic) || super
wizard_can_edit_topic?(topic) || super
end
def can_create_post?(parent)
result = parent.present? ? wizard_user_can_create_topic_on_category?(parent) : false
result || super
end
private
def wizard_user_can_create_topic_on_category?(topic)
wizard = creating_wizard(topic)
(wizard.present? && wizard.permitted? && wizard_can_create_topic_on_category?(wizard, topic))
end
def creating_wizard(topic)
wizard_id = topic.wizard_id.presence
wizard = CustomWizard::Builder.new(wizard_id, @user).build if wizard_id
wizard.presence
end
def wizard_can_create_topic_on_category?(wizard, topic)
return false unless topic.category.present?
wizard_actions = wizard.actions
return false if wizard_actions.empty?
create_topic_actions = wizard_actions.select do |action|
action['type'] === 'create_topic'
end
submission_data = begin
submissions = CustomWizard::Submission.list(wizard)
submissions.find { |sub| sub.id == topic.wizard_submission_id }&.fields_and_meta
end
categories = wizard_actions.map do |action|
category = CustomWizard::Mapper.new(
inputs: action['category'],
data: submission_data,
user: @user
).perform
category
end
categories.flatten!
return true if categories.include?(topic.category.id)
false
def wizard_can_edit_topic?(topic)
created_by_wizard = !!topic.wizard_submission_id
(
is_my_own?(topic) &&
created_by_wizard &&
can_see_topic?(topic) &&
can_create_post_on_topic?(topic)
)
end
end

Datei anzeigen

@ -517,7 +517,6 @@ class CustomWizard::Action
skip_validations: true,
topic_opts: {
custom_fields: {
wizard_id: @wizard.id,
wizard_submission_id: @wizard.current_submission.id
}
}

Datei anzeigen

@ -126,10 +126,6 @@ after_initialize do
Liquid::Template.register_filter(::CustomWizard::LiquidFilter::FirstNonEmpty)
add_to_class(:topic, :wizard_id) do
custom_fields['wizard_id']
end
add_to_class(:topic, :wizard_submission_id) do
custom_fields['wizard_submission_id']
end

Datei anzeigen

@ -1,4 +1,5 @@
# frozen_string_literal: true
require_relative '../plugin_helper'
describe ::Guardian do
@ -14,82 +15,47 @@ describe ::Guardian do
)
}
def create_topic_by_wizard(wizard)
wizard.create_updater(
wizard.steps.first.id,
step_1_field_1: "Topic Title",
step_1_field_2: "topic body"
).update
wizard.create_updater(wizard.steps.second.id, {}).update
wizard.create_updater(wizard.steps.last.id,
step_3_field_3: category.id
).update
topic = Topic.where(
title: "Topic Title",
category_id: category.id
).first
topic
end
before do
CustomWizard::Template.save(wizard_template, skip_jobs: true)
@template = CustomWizard::Template.find('super_mega_fun_wizard')
end
context "the user has access to creating wizard" do
context "topic created by user using wizard" do
it "allows editing the topic first post" do
wizard = CustomWizard::Builder.new(@template[:id], user).build
wizard.create_updater(
wizard.steps.first.id,
step_1_field_1: "Topic Title",
step_1_field_2: "topic body"
).update
wizard.create_updater(wizard.steps.second.id, {}).update
wizard.create_updater(wizard.steps.last.id,
step_3_field_3: category.id
).update
topic = Topic.where(
title: "Topic Title",
category_id: category.id
).first
expect(user.guardian.send(:wizard_user_can_create_topic_on_category?, topic)).to be_truthy
topic = create_topic_by_wizard(wizard)
expect(user.guardian.wizard_can_edit_topic?(topic)).to be_truthy
end
end
context "the user doesn't have access to creating wizard" do
context "topic created by user without wizard" do
it "restricts editing the topic first post" do
wizard = CustomWizard::Builder.new(@template[:id], user).build
CustomWizard::Wizard.any_instance.stubs(:permitted?).returns(false)
wizard.create_updater(
wizard.steps.first.id,
step_1_field_1: "Topic Title",
step_1_field_2: "topic body"
).update
wizard.create_updater(wizard.steps.second.id, {}).update
wizard.create_updater(wizard.steps.last.id,
step_3_field_3: category.id
).update
topic = Topic.where(
topic_params = {
title: "Topic Title",
category_id: category.id
).first
expect(user.guardian.send(:wizard_user_can_create_topic_on_category?, topic)).to be_falsey
end
end
context "the wizard can't create a topic in the category" do
it "restricts editing the topic first post" do
wizard = CustomWizard::Builder.new(@template[:id], user).build
wizard.create_updater(
wizard.steps.first.id,
step_1_field_1: "Topic Title",
step_1_field_2: "topic body"
).update
wizard.create_updater(wizard.steps.second.id, {}).update
wizard.create_updater(wizard.steps.last.id,
step_3_field_3: category.id
).update
topic = Topic.where(
title: "Topic Title",
category_id: category.id
).first
new_category = Fabricate(:category)
topic.category_id = new_category.id
topic.save
topic.reload
expect(user.guardian.send(:wizard_user_can_create_topic_on_category?, topic)).to be_falsey
raw: "Topic body",
skip_validations: true
}
post = PostCreator.new(user, topic_params).create
expect(user.guardian.wizard_can_edit_topic?(post.topic)).to be_falsey
end
end
end