module BeetleReporter class ReportsController < BeetleReporter::ApplicationController before_action :load_report, only: %w[show edit update destroy ignore undo suspend hide remove mute forbid] def index # authorize Report load_reports # skip_policy_scope end def show # authorize @report end def new # authorize Report @report = Report.new end def create # authorize Report create_params = report_params.to_hash.deep_symbolize_keys @report = report_scope.new(create_params) @report.user = current_user respond_to do |format| if @report.save format.turbo_stream { render turbo_stream: turbo_stream.replace(dom_id(@report), partial: "report", locals: {report: @report}) } format.html { redirect_to reports_path, notice: "report was successfully created." } else format.html { render :edit, status: :unprocessable_entity } end end end def edit # authorize @report end def update # authorize @report update_params = report_params.to_hash.deep_symbolize_keys respond_to do |format| if @report.update(update_params) format.turbo_stream { render turbo_stream: turbo_stream.replace(dom_id(@report), partial: "report", locals: {report: @report}) } format.html { redirect_to reports_path, notice: "report was successfully updated." } else format.html { render :edit, status: :unprocessable_entity } end end end def destroy # authorize @report @report.discard respond_to do |format| format.turbo_stream end end def ignore @report.ignore! redirect_to reports_url, notice: 'report was successfully ignored.' end def suspend @report.suspend! redirect_to reports_url, notice: 'report was successfully ignored.' end def hide @report.beetle_hide("fooobar hide") redirect_to reports_url, notice: 'report was successfully hidden.' end def remove @report.beetle_remove("fooobar remove") redirect_to reports_url, notice: 'report was successfully removed.' end def mute @report.beetle_mute("fooobar mute") redirect_to reports_url, notice: 'report was successfully muted.' end def forbid @report.beetle_forbid("fooobar forbid") redirect_to reports_url, notice: 'report was successfully forbiden.' end def undo @report.undo! redirect_to reports_url, notice: 'report was successfully ignored.' end private def load_reports builder = report_scope.includes(:record) builder = builder.where("reason ilike ?", "%#{params[:q]}%") if params[:q].present? if params[:filter] != 'all' builder = builder.pending builder = builder.unscope(where: :aasm_state).where(aasm_state: params[:state]) if params[:state].present? end builder = builder.where(record_type: params[:type]&.classify) if params[:type] builder = builder.order(id: :desc, reported_count: :desc) builder = builder.reorder(updated_at: :desc) if params[:order] == 'last_updated' builder = builder.reorder(created_at: :desc) if params[:order] == 'last_created' builder = builder.reorder(updated_at: :asc) if params[:order] == 'oldest_updated' builder = builder.reorder(created_at: :asc) if params[:order] == 'oldest_created' @pagy, @reports = pagy(builder) end def load_report @report = report_scope.find(params[:id] || params[:report_id]) end def report_scope BeetleReporter::Report.kept end def report_params params.require(:report).permit(:id, :record_id, :record_type, :reason, :user_id, :device_id) end end end