<commit_before>class VenuesController < ApplicationController
  include SquashManyDuplicatesMixin # Provides squash_many_duplicates

  # GET /venues
  # GET /venues.xml
  def index
    scoped_venues = Venue.non_duplicates

    # Pick a subset of venues (we want in_business by default)
    if params[:include_closed]
      scoped_venues = scoped_venues
    elsif params[:closed]
      scoped_venues = scoped_venues.out_of_business
    else
      scoped_venues = scoped_venues.in_business
    end

    # Support old ajax autocomplete parameter name
    params[:term] = params[:val] if params[:val]

    @tag = nil
    if params[:tag].present? # searching by tag
      @tag = params[:tag]
      @venues = scoped_venues.tagged_with(@tag)
    elsif params.has_key?(:query) || params.has_key?(:term) || params[:all] == '1' # searching by query
      scoped_venues = scoped_venues.with_public_wifi if params[:wifi]

      if params[:term].present? # for the ajax autocomplete widget
        conditions = ["title LIKE ?", "%#{params[:term]}%"]
        @venues = scoped_venues.where(conditions).order('LOWER(title)')
      elsif params[:query].present?
        @venues = Venue.search(params[:query], :include_closed => params[:include_closed], :wifi => params[:wifi])
      else
        @venues = scoped_venues.all
      end
    else # default view
      @most_active_venues = scoped_venues.limit(10).order('events_count DESC')
      @newest_venues = scoped_venues.limit(10).order('created_at DESC')
    end

    @page_title = "Venues"

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml  => @venues || scoped_venues }
      format.json { render :json => @venues || scoped_venues, :callback => params[:callback] }
      format.js   { render :json => @venues || scoped_venues, :callback => params[:callback] }
      format.kml  { @venues ||= scoped_venues; render } # index.kml.erb
    end
  end

  # GET /venues/map
  def map
    @venues = Venue.non_duplicates.in_business
  end

  # GET /venues/1
  # GET /venues/1.xml
  def show
    @venue = Venue.find(params[:id], include: :source)

    return redirect_to @venue.duplicate_of if @venue.duplicate?

    @page_title = @venue.title

    respond_to do |format|
      format.html
      format.xml  { render xml: @venue }
      format.json { render json: @venue, callback: params[:callback] }
      format.ics  { ical_export(@venue) }
    end

  rescue ActiveRecord::RecordNotFound => e
    flash[:failure] = e.to_s
    redirect_to venues_path
  end

  # GET /venues/new
  # GET /venues/new.xml
  def new
    @venue = Venue.new
    @page_title = "Add a Venue"

    render layout: params[:layout] != "false"
  end

  # GET /venues/1/edit
  def edit
    @venue = Venue.find(params[:id])
    @page_title = "Editing '#{@venue.title}'"
  end

  # POST /venues
  # POST /venues.xml
  def create
    @venue = Venue.new
    create_or_update
  end

  # PUT /venues/1
  # PUT /venues/1.xml
  def update
    params[:venue][:latitude] = params[:venue][:longitude] = nil if params[:venue][:force_geocoding]=="1" unless params[:venue].blank?
    @venue = Venue.find(params[:id])
    create_or_update
  end

  def create_or_update
    @venue.attributes = params[:venue]

    if evil_robot = params[:trap_field].present?
      flash[:failure] = "<h3>Evil Robot</h3> We didn't save this venue because we think you're an evil robot. If you're really not an evil robot, look at the form instructions more carefully. If this doesn't work please file a bug report and let us know."
    end

    respond_to do |format|
      if !evil_robot && @venue.save
        format.html do
          flash[:success] = 'Venue was successfully saved.'
          if params[:from_event].present?
            redirect_to event_url(params[:from_event])
          else
            redirect_to @venue
          end
        end
        format.xml  { render xml: @venue, status: :created, location: @venue }
      else
        format.html { render action: @venue.new_record? ? "new" : "edit" }
        format.xml  { render xml: @venue.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /venues/1
  # DELETE /venues/1.xml
  def destroy
    @venue = Venue.find(params[:id])

    if @venue.events.any?
      message = "Cannot destroy venue that has associated events, you must reassociate all its events first."
      respond_to do |format|
        format.html { redirect_to @venue, flash: { failure: message } }
        format.xml  { render xml: message, status: :unprocessable_entity }
      end
    else
      @venue.destroy
      respond_to do |format|
        format.html { redirect_to venues_path, flash: { success: %("#{@venue.title}" has been deleted) } }
        format.xml  { head :ok }
      end
    end
  end

protected

  def ical_export(venue)
    events = venue.events.order("start_time ASC").non_duplicates
    render(:text => Event.to_ical(events, :url_helper => lambda{|event| event_url(event)}), :mime_type => 'text/calendar')
  end
end<commit_msg>extract out some private methods from VenuesController#create_or_update.<commit_after>class VenuesController < ApplicationController
  include SquashManyDuplicatesMixin # Provides squash_many_duplicates

  # GET /venues
  # GET /venues.xml
  def index
    scoped_venues = Venue.non_duplicates

    # Pick a subset of venues (we want in_business by default)
    if params[:include_closed]
      scoped_venues = scoped_venues
    elsif params[:closed]
      scoped_venues = scoped_venues.out_of_business
    else
      scoped_venues = scoped_venues.in_business
    end

    # Support old ajax autocomplete parameter name
    params[:term] = params[:val] if params[:val]

    @tag = nil
    if params[:tag].present? # searching by tag
      @tag = params[:tag]
      @venues = scoped_venues.tagged_with(@tag)
    elsif params.has_key?(:query) || params.has_key?(:term) || params[:all] == '1' # searching by query
      scoped_venues = scoped_venues.with_public_wifi if params[:wifi]

      if params[:term].present? # for the ajax autocomplete widget
        conditions = ["title LIKE ?", "%#{params[:term]}%"]
        @venues = scoped_venues.where(conditions).order('LOWER(title)')
      elsif params[:query].present?
        @venues = Venue.search(params[:query], :include_closed => params[:include_closed], :wifi => params[:wifi])
      else
        @venues = scoped_venues.all
      end
    else # default view
      @most_active_venues = scoped_venues.limit(10).order('events_count DESC')
      @newest_venues = scoped_venues.limit(10).order('created_at DESC')
    end

    @page_title = "Venues"

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml  => @venues || scoped_venues }
      format.json { render :json => @venues || scoped_venues, :callback => params[:callback] }
      format.js   { render :json => @venues || scoped_venues, :callback => params[:callback] }
      format.kml  { @venues ||= scoped_venues; render } # index.kml.erb
    end
  end

  # GET /venues/map
  def map
    @venues = Venue.non_duplicates.in_business
  end

  # GET /venues/1
  # GET /venues/1.xml
  def show
    @venue = Venue.find(params[:id], include: :source)

    return redirect_to @venue.duplicate_of if @venue.duplicate?

    @page_title = @venue.title

    respond_to do |format|
      format.html
      format.xml  { render xml: @venue }
      format.json { render json: @venue, callback: params[:callback] }
      format.ics  { ical_export(@venue) }
    end

  rescue ActiveRecord::RecordNotFound => e
    flash[:failure] = e.to_s
    redirect_to venues_path
  end

  # GET /venues/new
  # GET /venues/new.xml
  def new
    @venue = Venue.new
    @page_title = "Add a Venue"

    render layout: params[:layout] != "false"
  end

  # GET /venues/1/edit
  def edit
    @venue = Venue.find(params[:id])
    @page_title = "Editing '#{@venue.title}'"
  end

  # POST /venues
  # POST /venues.xml
  def create
    @venue = Venue.new
    create_or_update
  end

  # PUT /venues/1
  # PUT /venues/1.xml
  def update
    params[:venue][:latitude] = params[:venue][:longitude] = nil if params[:venue][:force_geocoding]=="1" unless params[:venue].blank?
    @venue = Venue.find(params[:id])
    create_or_update
  end

  def create_or_update
    @venue.attributes = params[:venue]
    respond_to do |format|
      if !evil_robot? && @venue.save
        format.html { redirect_to from_event || @venue, flash: { success: "Venue was successfully saved." } }
        format.xml  { render xml: @venue, status: :created, location: @venue }
      else
        format.html { render action: @venue.new_record? ? "new" : "edit" }
        format.xml  { render xml: @venue.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /venues/1
  # DELETE /venues/1.xml
  def destroy
    @venue = Venue.find(params[:id])

    if @venue.events.any?
      message = "Cannot destroy venue that has associated events, you must reassociate all its events first."
      respond_to do |format|
        format.html { redirect_to @venue, flash: { failure: message } }
        format.xml  { render xml: message, status: :unprocessable_entity }
      end
    else
      @venue.destroy
      respond_to do |format|
        format.html { redirect_to venues_path, flash: { success: %("#{@venue.title}" has been deleted) } }
        format.xml  { head :ok }
      end
    end
  end

  private

  def evil_robot?
    if params[:trap_field].present?
      flash[:failure] = "<h3>Evil Robot</h3> We didn't save this venue because we think you're an evil robot. If you're really not an evil robot, look at the form instructions more carefully. If this doesn't work please file a bug report and let us know."
    end
  end

  def from_event
    Event.find_by_id(params[:from_event])
  end

  def ical_export(venue)
    events = venue.events.order("start_time ASC").non_duplicates
    render(:text => Event.to_ical(events, :url_helper => lambda{|event| event_url(event)}), :mime_type => 'text/calendar')
  end
end
{'fraction_non_alphanumeric': 0.11741293532338308, 'fraction_numerical': 0.0029850746268656717, 'mean_word_length': 3.0220088035214085, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 34}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3851384', 'n_tokens_mistral': 3358, 'n_tokens_neox': 3264, 'n_words': 833}