Skip to content

Commit f5a35ba

Browse files
committed
simplified logic
1 parent 5d882d6 commit f5a35ba

File tree

4 files changed

+40
-121
lines changed

4 files changed

+40
-121
lines changed

extensions/guardian.rb

Lines changed: 10 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,17 @@
11
# frozen_string_literal: true
2-
module CustomWizardGuardian
3-
def can_see_topic?(topic, hide_deleted = true)
4-
wizard_user_can_create_topic_on_category?(topic) || super
5-
end
62

3+
module CustomWizardGuardian
74
def can_edit_topic?(topic)
8-
wizard_user_can_create_topic_on_category?(topic) || super
9-
end
10-
11-
def can_create_post?(parent)
12-
result = parent.present? ? wizard_user_can_create_topic_on_category?(parent) : false
13-
result || super
5+
wizard_can_edit_topic?(topic) || super
146
end
157

16-
private
17-
18-
def wizard_user_can_create_topic_on_category?(topic)
19-
wizard = creating_wizard(topic)
20-
(wizard.present? && wizard.permitted? && wizard_can_create_topic_on_category?(wizard, topic))
21-
end
22-
23-
def creating_wizard(topic)
24-
wizard_id = topic.wizard_id.presence
25-
wizard = CustomWizard::Builder.new(wizard_id, @user).build if wizard_id
26-
wizard.presence
27-
end
28-
29-
def wizard_can_create_topic_on_category?(wizard, topic)
30-
return false unless topic.category.present?
31-
32-
wizard_actions = wizard.actions
33-
return false if wizard_actions.empty?
34-
35-
create_topic_actions = wizard_actions.select do |action|
36-
action['type'] === 'create_topic'
37-
end
38-
39-
submission_data = begin
40-
submissions = CustomWizard::Submission.list(wizard)
41-
submissions.find { |sub| sub.id == topic.wizard_submission_id }&.fields_and_meta
42-
end
43-
44-
categories = wizard_actions.map do |action|
45-
category = CustomWizard::Mapper.new(
46-
inputs: action['category'],
47-
data: submission_data,
48-
user: @user
49-
).perform
50-
51-
category
52-
end
53-
54-
categories.flatten!
55-
56-
return true if categories.include?(topic.category.id)
57-
false
8+
def wizard_can_edit_topic?(topic)
9+
created_by_wizard = !!topic.wizard_submission_id
10+
(
11+
is_my_own?(topic) &&
12+
created_by_wizard &&
13+
can_see_topic?(topic) &&
14+
can_create_post_on_topic?(topic)
15+
)
5816
end
5917
end

lib/custom_wizard/action.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,6 @@ def basic_topic_params
517517
skip_validations: true,
518518
topic_opts: {
519519
custom_fields: {
520-
wizard_id: @wizard.id,
521520
wizard_submission_id: @wizard.current_submission.id
522521
}
523522
}

plugin.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,6 @@ def process_require_tree_discourse_directive(path = ".")
126126

127127
Liquid::Template.register_filter(::CustomWizard::LiquidFilter::FirstNonEmpty)
128128

129-
add_to_class(:topic, :wizard_id) do
130-
custom_fields['wizard_id']
131-
end
132-
133129
add_to_class(:topic, :wizard_submission_id) do
134130
custom_fields['wizard_submission_id']
135131
end
Lines changed: 30 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2+
23
require_relative '../plugin_helper'
34

45
describe ::Guardian do
@@ -14,82 +15,47 @@
1415
)
1516
}
1617

18+
def create_topic_by_wizard(wizard)
19+
wizard.create_updater(
20+
wizard.steps.first.id,
21+
step_1_field_1: "Topic Title",
22+
step_1_field_2: "topic body"
23+
).update
24+
wizard.create_updater(wizard.steps.second.id, {}).update
25+
wizard.create_updater(wizard.steps.last.id,
26+
step_3_field_3: category.id
27+
).update
28+
29+
topic = Topic.where(
30+
title: "Topic Title",
31+
category_id: category.id
32+
).first
33+
34+
topic
35+
end
36+
1737
before do
1838
CustomWizard::Template.save(wizard_template, skip_jobs: true)
1939
@template = CustomWizard::Template.find('super_mega_fun_wizard')
2040
end
2141

22-
context "the user has access to creating wizard" do
42+
context "topic created by user using wizard" do
2343
it "allows editing the topic first post" do
2444
wizard = CustomWizard::Builder.new(@template[:id], user).build
25-
26-
wizard.create_updater(
27-
wizard.steps.first.id,
28-
step_1_field_1: "Topic Title",
29-
step_1_field_2: "topic body"
30-
).update
31-
wizard.create_updater(wizard.steps.second.id, {}).update
32-
wizard.create_updater(wizard.steps.last.id,
33-
step_3_field_3: category.id
34-
).update
35-
36-
topic = Topic.where(
37-
title: "Topic Title",
38-
category_id: category.id
39-
).first
40-
41-
expect(user.guardian.send(:wizard_user_can_create_topic_on_category?, topic)).to be_truthy
42-
end
43-
end
44-
45-
context "the user doesn't have access to creating wizard" do
46-
it "restricts editing the topic first post" do
47-
wizard = CustomWizard::Builder.new(@template[:id], user).build
48-
CustomWizard::Wizard.any_instance.stubs(:permitted?).returns(false)
49-
50-
wizard.create_updater(
51-
wizard.steps.first.id,
52-
step_1_field_1: "Topic Title",
53-
step_1_field_2: "topic body"
54-
).update
55-
wizard.create_updater(wizard.steps.second.id, {}).update
56-
wizard.create_updater(wizard.steps.last.id,
57-
step_3_field_3: category.id
58-
).update
59-
60-
topic = Topic.where(
61-
title: "Topic Title",
62-
category_id: category.id
63-
).first
64-
65-
expect(user.guardian.send(:wizard_user_can_create_topic_on_category?, topic)).to be_falsey
45+
topic = create_topic_by_wizard(wizard)
46+
expect(user.guardian.wizard_can_edit_topic?(topic)).to be_truthy
6647
end
6748
end
6849

69-
context "the wizard can't create a topic in the category" do
50+
context "topic created by user without wizard" do
7051
it "restricts editing the topic first post" do
71-
wizard = CustomWizard::Builder.new(@template[:id], user).build
72-
wizard.create_updater(
73-
wizard.steps.first.id,
74-
step_1_field_1: "Topic Title",
75-
step_1_field_2: "topic body"
76-
).update
77-
wizard.create_updater(wizard.steps.second.id, {}).update
78-
wizard.create_updater(wizard.steps.last.id,
79-
step_3_field_3: category.id
80-
).update
81-
82-
topic = Topic.where(
52+
topic_params = {
8353
title: "Topic Title",
84-
category_id: category.id
85-
).first
86-
87-
new_category = Fabricate(:category)
88-
topic.category_id = new_category.id
89-
topic.save
90-
topic.reload
91-
92-
expect(user.guardian.send(:wizard_user_can_create_topic_on_category?, topic)).to be_falsey
54+
raw: "Topic body",
55+
skip_validations: true
56+
}
57+
post = PostCreator.new(user, topic_params).create
58+
expect(user.guardian.wizard_can_edit_topic?(post.topic)).to be_falsey
9359
end
9460
end
9561
end

0 commit comments

Comments
 (0)