class OwnersController < ApplicationController
  before_filter :authenticate_user!, except: :show
  before_filter :load_resource, except: :settings_redirect
  authorize_resource
  skip_authorize_resource only: :settings_redirect

  def show
    # Whether this user has just become a supporter
    @new_supporter = session[:new_supporter]
    # Only do this once
    session[:new_supporter] = false if @new_supporter

    @scrapers = @owner.scrapers.includes(:last_run)

    # Split out scrapers into different groups
    @running_scrapers, @erroring_scrapers, @other_scrapers = [], [], []
    @scrapers.each do |scraper|
      if scraper.running?
        @running_scrapers << scraper
      elsif scraper.requires_attention?
        @erroring_scrapers << scraper
      else
        @other_scrapers << scraper
      end
    end
  end

  def settings_redirect
    redirect_to settings_owner_url(current_user)
  end

  def settings
  end

  def update
    if @owner.user?
      @owner.update_attributes(
        see_downloads: params[:user][:see_downloads])
    end
    redirect_to @owner
  end

  def reset_key
    @owner.set_api_key
    @owner.save!
    redirect_to settings_owner_url(@owner)
  end

  # Toggle whether we're watching this user / organization
  def watch
    current_user.toggle_watch(@owner)
    redirect_to :back
  end

  private

  def load_resource
    @owner = Owner.friendly.find(params[:id])
  end
end

class OwnersController < ApplicationController
  before_filter :authenticate_user!, except: :show
  before_filter :load_resource, except: :settings_redirect
  authorize_resource
  skip_authorize_resource only: :settings_redirect

  def show
    # Whether this user has just become a supporter
    @new_supporter = session[:new_supporter]
    # Only do this once
    session[:new_supporter] = false if @new_supporter

    @scrapers = @owner.scrapers

    # Split out scrapers into different groups
    @running_scrapers, @erroring_scrapers, @other_scrapers = [], [], []
    @scrapers.each do |scraper|
      if scraper.running?
        @running_scrapers << scraper
      elsif scraper.requires_attention?
        @erroring_scrapers << scraper
      else
        @other_scrapers << scraper
      end
    end
  end

  def settings_redirect
    redirect_to settings_owner_url(current_user)
  end

  def settings
  end

  def update
    if @owner.user?
      @owner.update_attributes(
        see_downloads: params[:user][:see_downloads])
    end
    redirect_to @owner
  end

  def reset_key
    @owner.set_api_key
    @owner.save!
    redirect_to settings_owner_url(@owner)
  end

  # Toggle whether we're watching this user / organization
  def watch
    current_user.toggle_watch(@owner)
    redirect_to :back
  end

  private

  def load_resource
    @owner = Owner.friendly.find(params[:id])
  end
end

Revert "Preload the last_run so rest will run faster"
Revert "Preload the last_run so rest will run faster"

Sure enough looking at an owner with lots of runs the performance
problem mentioned in the original commit is in fact a problem. So,
reverting this until we come up with a better way of preloading
the last_runs which doesn't involve loading all the runs.

This reverts commit 0df0361f338fae5c9d5bc3704d1904e55490f225.
