|  | 
|  | 1 | +# frozen_string_literal: true | 
|  | 2 | + | 
|  | 3 | +class Event | 
|  | 4 | +  class ScopedTagsController < ApplicationController | 
|  | 5 | +    include SetEvent | 
|  | 6 | + | 
|  | 7 | +    before_action :set_scoped_tag, except: :create | 
|  | 8 | +    before_action :set_event | 
|  | 9 | + | 
|  | 10 | +    def create | 
|  | 11 | +      @scoped_tag = @event.subevent_scoped_tags.build(name: params[:name]) | 
|  | 12 | + | 
|  | 13 | +      authorize @scoped_tag | 
|  | 14 | + | 
|  | 15 | +      if @scoped_tag.save | 
|  | 16 | +        if params[:subevent_id] | 
|  | 17 | +          subevent = Event.find(params[:subevent_id]) | 
|  | 18 | + | 
|  | 19 | +          if @scoped_tag.parent_event.subevents.include?(subevent) | 
|  | 20 | +            subevent.scoped_tags << @scoped_tag | 
|  | 21 | +          end | 
|  | 22 | +        end | 
|  | 23 | + | 
|  | 24 | +        respond_to do |format| | 
|  | 25 | +          format.turbo_stream { render turbo_stream: turbo_stream.append_all(".scoped_tag_results", partial: "events/scoped_tags/scoped_tag_option", locals: { scoped_tag: @scoped_tag }) } | 
|  | 26 | +          format.any do | 
|  | 27 | +            flash[:success] = "Successfully created new sub-organization tag" | 
|  | 28 | +            redirect_back fallback_location: event_sub_organizations_path(@event) | 
|  | 29 | +          end | 
|  | 30 | +        end | 
|  | 31 | +      else | 
|  | 32 | +        flash[:error] = "Failed to create new sub-organization tag" | 
|  | 33 | +        redirect_to event_sub_organizations_path(@event) | 
|  | 34 | +      end | 
|  | 35 | +    end | 
|  | 36 | + | 
|  | 37 | +    def destroy | 
|  | 38 | +      authorize @scoped_tag | 
|  | 39 | + | 
|  | 40 | +      @scoped_tag.destroy! | 
|  | 41 | + | 
|  | 42 | +      respond_to do |format| | 
|  | 43 | +        format.turbo_stream do | 
|  | 44 | +          streams = [turbo_stream.remove_all("[data-scoped-tag='#{@scoped_tag.id}']")] | 
|  | 45 | +          streams << turbo_stream.remove_all(".scoped-tags__divider") if @event.subevent_scoped_tags.none? | 
|  | 46 | +          render turbo_stream: streams | 
|  | 47 | +        end | 
|  | 48 | +        format.any { redirect_back fallback_location: event_sub_organizations_path(@scoped_tag.parent_event) } | 
|  | 49 | +      end | 
|  | 50 | +    end | 
|  | 51 | + | 
|  | 52 | +    def toggle_tag | 
|  | 53 | +      authorize @scoped_tag | 
|  | 54 | + | 
|  | 55 | +      unless @scoped_tag.parent_event.subevents.include?(@event) | 
|  | 56 | +        flash[:error] = "Cannot add tag of a different parent organization to this organization" | 
|  | 57 | +        return redirect_to event_sub_organizations_path(@scoped_tag.parent_event) | 
|  | 58 | +      end | 
|  | 59 | + | 
|  | 60 | +      if @event.scoped_tags.exists?(@scoped_tag.id) | 
|  | 61 | +        removed = true | 
|  | 62 | +        @event.scoped_tags.destroy(@scoped_tag) | 
|  | 63 | +      else | 
|  | 64 | +        @event.scoped_tags << @scoped_tag | 
|  | 65 | +      end | 
|  | 66 | + | 
|  | 67 | +      respond_to do |format| | 
|  | 68 | +        format.turbo_stream do | 
|  | 69 | +          if removed | 
|  | 70 | +            render partial: "events/scoped_tags/destroy", locals: { scoped_tag: @scoped_tag, subevent: @event } | 
|  | 71 | +          else | 
|  | 72 | +            render partial: "events/scoped_tags/create", locals: { scoped_tag: @scoped_tag, subevent: @event } | 
|  | 73 | +          end | 
|  | 74 | +        end | 
|  | 75 | +        format.any { redirect_back fallback_location: event_sub_organizations_path(@scoped_tag.parent_event) } | 
|  | 76 | +      end | 
|  | 77 | +    end | 
|  | 78 | + | 
|  | 79 | +    private | 
|  | 80 | + | 
|  | 81 | +    def set_scoped_tag | 
|  | 82 | +      @scoped_tag = Event::ScopedTag.find(params[:id]) | 
|  | 83 | +    end | 
|  | 84 | + | 
|  | 85 | +  end | 
|  | 86 | + | 
|  | 87 | +end | 
0 commit comments