From f5a35baa1bc3ee28246cd6721127feac4c26146f Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Mon, 31 Jan 2022 12:50:20 +0530 Subject: [PATCH] simplified logic --- extensions/guardian.rb | 62 +++----------- lib/custom_wizard/action.rb | 1 - plugin.rb | 4 - spec/extensions/guardian_extension_spec.rb | 94 +++++++--------------- 4 files changed, 40 insertions(+), 121 deletions(-) diff --git a/extensions/guardian.rb b/extensions/guardian.rb index 96ceda93..bbfe6f41 100644 --- a/extensions/guardian.rb +++ b/extensions/guardian.rb @@ -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 diff --git a/lib/custom_wizard/action.rb b/lib/custom_wizard/action.rb index 0c2532ef..1e1c410c 100644 --- a/lib/custom_wizard/action.rb +++ b/lib/custom_wizard/action.rb @@ -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 } } diff --git a/plugin.rb b/plugin.rb index dbdf8cd5..94f9059b 100644 --- a/plugin.rb +++ b/plugin.rb @@ -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 diff --git a/spec/extensions/guardian_extension_spec.rb b/spec/extensions/guardian_extension_spec.rb index 50e2ebe4..d779fe11 100644 --- a/spec/extensions/guardian_extension_spec.rb +++ b/spec/extensions/guardian_extension_spec.rb @@ -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