Best Minitest_ruby code snippet using Reportable.o
manage_moderations_examples.rb
Source:manage_moderations_examples.rb
1# frozen_string_literal: true2shared_examples "sorted moderations" do3 let!(:moderations) do4 reportables.first(reportables.length - 1).map do |reportable|5 moderation = create(:moderation, reportable: reportable, report_count: 1, reported_content: reportable.reported_searchable_content_text)6 create(:report, moderation: moderation)7 moderation8 end9 end10 let!(:moderation) { moderations.first }11 let(:moderations_link_text) { "Moderations" }12 before do13 visit participatory_space_path14 click_link moderations_link_text15 end16 it "sorts the most recent first" do17 within ".pagination" do18 click_link "Last"19 end20 all("tbody tr").each_with_index do |row, _index|21 expect(row.find("td:first-child")).to have_content(reportables.first.id)22 end23 end24end25shared_examples "manage moderations" do26 let!(:moderations) do27 reportables.first(reportables.length - 1).map do |reportable|28 moderation = create(:moderation, reportable: reportable, report_count: 1, reported_content: reportable.reported_searchable_content_text)29 create(:report, moderation: moderation)30 moderation31 end32 end33 let!(:moderation) { moderations.first }34 let!(:hidden_moderations) do35 reportables.last(1).map do |reportable|36 moderation = create(:moderation, reportable: reportable, report_count: 3, reported_content: reportable.reported_searchable_content_text, hidden_at: Time.current)37 create_list(:report, 3, moderation: moderation, reason: :spam)38 moderation39 end40 end41 let(:moderations_link_text) { "Moderations" }42 before do43 visit participatory_space_path44 click_link moderations_link_text45 end46 context "when listing moderations" do47 it "only lists moderations for the current organization" do48 external_reportable = create :dummy_resource49 external_moderation = create(:moderation, reportable: external_reportable, report_count: 1, reported_content: external_reportable.reported_searchable_content_text)50 create(:report, moderation: external_moderation)51 visit current_path52 expect(page).to have_no_selector("tr[data-id=\"#{external_moderation.id}\"]")53 end54 it "user can review them" do55 moderations.each do |moderation|56 within "tr[data-id=\"#{moderation.id}\"]" do57 expect(page).to have_css("a[href='#{moderation.reportable.reported_content_url}']")58 expect(page).to have_content "Spam"59 end60 end61 end62 it "user can un-report a resource" do63 within "tr[data-id=\"#{moderation.id}\"]" do64 click_link "Unreport"65 end66 expect(page).to have_admin_callout("Resource successfully unreported")67 end68 it "user can hide a resource" do69 within "tr[data-id=\"#{moderation.id}\"]" do70 click_link "Hide"71 end72 expect(page).to have_admin_callout("Resource successfully hidden")73 expect(page).to have_no_content(moderation.reportable.reported_content_url)74 end75 it "user can sort by report count" do76 moderations.each_with_index { |moderation, index| moderation.update(report_count: index + 1) }77 moderations_ordered_by_report_count_asc = moderations.sort_by(&:report_count)78 within "table" do79 click_link "Count"80 all("tbody tr").each_with_index do |row, index|81 reportable_id = moderations_ordered_by_report_count_asc[index].reportable.id82 expect(row.find("td:first-child")).to have_content(reportable_id)83 end84 end85 end86 it "user can filter by reportable type" do87 reportable_type = moderation.reportable.class.name.demodulize88 within ".filters__section" do89 find(:xpath, "//a[contains(text(), 'Filter')]").hover90 find(:xpath, "//a[contains(text(), 'Type')]").hover91 click_link reportable_type92 end93 expect(page).to have_selector("tbody tr", count: moderations.length)94 end95 it "user can filter by reported content" do96 search = moderation.reportable.id97 within ".filters__section" do98 fill_in("Search Moderation by reportable id or content.", with: search)99 find(:xpath, "//button[@type='submit']").click100 end101 expect(page).to have_selector("tbody tr", count: 1)102 end103 it "user can see moderation details" do104 within "tr[data-id=\"#{moderation.id}\"]" do105 click_link "Expand"106 end107 reported_content_slice = moderation.reportable.reported_searchable_content_text.split("\n").first108 expect(page).to have_content(reported_content_slice)109 end110 context "when the reported content does not exist" do111 it "still renders the page" do112 moderation.reportable.destroy113 visit current_path114 expect(page).to have_no_selector("tr[data-id=\"#{moderation.id}\"]")115 end116 end117 end118 context "when listing hidden resources" do119 it "user can review them" do120 within ".card-title" do121 click_link "Hidden"122 end123 hidden_moderations.each do |moderation|124 within "tr[data-id=\"#{moderation.id}\"]" do125 expect(page).to have_css("a[href='#{moderation.reportable.reported_content_url}']")126 end127 end128 end129 end130end...
reports_helper_spec.rb
Source:reports_helper_spec.rb
1# frozen_string_literal: true2require "spec_helper"3require "decidim/proposals/test/factories"4module Decidim5 module Admin6 module Moderations7 describe ReportsHelper do8 subject do9 Nokogiri::HTML(10 helper.reportable_author_name(reportable)11 )12 end13 before do14 allow(helper).to receive(:current_or_new_conversation_path_with).and_return("/conversations/1")15 end16 context "with different reportable authors types" do17 describe "when it's a dummy resource author " do18 let(:reportable) { create(:dummy_resource, title: { "en" => "<p>Dummy<br> Title</p>" }) }19 it "returns the author's name" do20 expect(helper.reportable_author_name(reportable)).to include("reportable-authors")21 expect(helper.reportable_author_name(reportable)).to include(reportable.author.name)22 end23 end24 describe "when it's a user author" do25 let!(:proposal) { create(:proposal) }26 let(:reportable) { proposal }27 it "returns the user's name" do28 expect(helper.reportable_author_name(reportable)).to include("reportable-authors")29 expect(helper.reportable_author_name(reportable)).to include(reportable.authors.first.name)30 end31 end32 describe "when it's a meeting author" do33 let!(:proposal) { create(:proposal, :official_meeting) }34 let(:reportable) { proposal }35 it "returns the meeting's title" do36 expect(helper.reportable_author_name(reportable)).to include("reportable-authors")37 expect(helper.reportable_author_name(reportable)).to include(translated_attribute(reportable.authors.first.title))38 end39 end40 end41 end42 end43 end44end...
hide_resource.rb
Source:hide_resource.rb
1# frozen_string_literal: true2module Decidim3 module Admin4 # A command with all the business logic when a user hides a resource.5 class HideResource < Decidim::Command6 # Public: Initializes the command.7 #8 # reportable - A Decidim::Reportable9 # current_user - the user that performs the action10 def initialize(reportable, current_user)11 @reportable = reportable12 @current_user = current_user13 end14 # Executes the command. Broadcasts these events:15 #16 # - :ok when everything is valid, together with the resource.17 # - :invalid if the resource is already hidden18 #19 # Returns nothing.20 def call21 return broadcast(:invalid) unless hideable?22 hide!23 send_hide_notification_to_author24 broadcast(:ok, @reportable)25 end26 private27 def hideable?28 !@reportable.hidden? && @reportable.reported?29 end30 def hide!31 Decidim.traceability.perform_action!(32 "hide",33 @reportable.moderation,34 @current_user,35 extra: {36 reportable_type: @reportable.class.name37 }38 ) do39 @reportable.moderation.update!(hidden_at: Time.current)40 @reportable.try(:touch)41 end42 end43 def send_hide_notification_to_author44 data = {45 event: "decidim.events.reports.resource_hidden",46 event_class: Decidim::ResourceHiddenEvent,47 resource: @reportable,48 extra: {49 report_reasons: report_reasons50 },51 affected_users: @reportable.try(:authors) || [@reportable.try(:normalized_author)]52 }53 Decidim::EventsManager.publish(data)54 end55 def report_reasons56 @reportable.moderation.reports.pluck(:reason).uniq57 end58 end59 end60end...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!