<commit_before>### app/controllers/songs_controller.rb ###

class SongsController < ApplicationController
  before_action :find_song, :find_artist, only: [:show]

  def show
    render status: 404 if @song == nil
    @song.sticky_tabs = @song.get_chords_from_api(@song.sticky_tabs)
    if session[:user_id] == nil
      @my_favorite = "♡"
    else
      @my_favorite = is_favorite(@song)
    end
  end

  def new
    @song = Song.new()
    @post_method = :post
  end

  def create
    @song = Song.new
    @song.website = params["song"]["website"]
    if Song.where(website: @song.website).length == 0
      @chords, @song_tab_string, @header_array = @song.scrape_song(params["song"]["website"])

      @song.name = @header_array[0]
      if Artist.find_by(name: @header_array[1]) == nil
        Artist.create(name: @header_array[1])
      end
      @song.artist_id = Artist.find_by(name: @header_array[1]).id
      @song.lyrics_tabs = @song_tab_string
      @song.key = @chords.split(",").first
      @song.sticky_tabs = @chords

      if @song.save
        redirect_to song_path(@song.id)
      else
        @error = "did not successfully save, please try again."
        @post_path = new_song_path
        @post_method = :post
        render :new
      end
    else
      existing_song = Song.find_by(website: @song.website)
      redirect_to song_path( existing_song.id )
    end
  end

  def index
    @songs = Song.all.order(:name)
  end

  private
  def find_song
    if Song.exists?(params[:id].to_i)
      return @song = Song.find_by(id: params[:id].to_i)
    else
      render :status => 404
    end
  end

  def find_artist
    if Artist.exists?(@song.artist_id)<commit_msg>now save sticky tabs when creating song to make render load faster<commit_after>### app/controllers/songs_controller.rb ###

class SongsController < ApplicationController
  before_action :find_song, :find_artist, only: [:show]

  def show
    render status: 404 if @song == nil
    # puts Time.now
    # @song.sticky_tabs = @song.get_chords_from_api(@song.sticky_tabs)
    # puts Time.now
    if session[:user_id] == nil
      @my_favorite = "♡"
    else
      @my_favorite = is_favorite(@song)
    end
  end

  def new
    @song = Song.new()
    @post_method = :post
  end

  def create
    @song = Song.new
    @song.website = params["song"]["website"]
    if Song.where(website: @song.website).length == 0
      @chords, @song_tab_string, @header_array = @song.scrape_song(params["song"]["website"])

      @song.name = @header_array[0]
      if Artist.find_by(name: @header_array[1]) == nil
        Artist.create(name: @header_array[1])
      end
      @song.artist_id = Artist.find_by(name: @header_array[1]).id
      @song.lyrics_tabs = @song_tab_string
      @song.key = @chords.split(",").first
      @song.sticky_tabs = @song.get_chords_from_api(@chords)


      if @song.save
        redirect_to song_path(@song.id)
      else
        @error = "did not successfully save, please try again."
        @post_path = new_song_path
        @post_method = :post
        render :new
      end
    else
      existing_song = Song.find_by(website: @song.website)
      redirect_to song_path( existing_song.id )
    end
  end

  def index
    @songs = Song.all.order(:name)
  end

  private
  def find_song
    if Song.exists?(params[:id].to_i)
      return @song = Song.find_by(id: params[:id].to_i)
    else
      render :status => 404
    end
  end

  def find_artist
    if Artist.exists?(@song.artist_id)
{'fraction_non_alphanumeric': 0.12396221013455483, 'fraction_numerical': 0.006298310907529345, 'mean_word_length': 2.8437843784378436, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '1668532', 'n_tokens_mistral': 1303, 'n_tokens_neox': 1209, 'n_words': 258}