module RepositoryManager
  extend ActiveSupport::Concern

  included do
    before_filter :repository, :snapshot, :repositories
  end

  def repository
    id = params[:repository_id] || params[:id]

    if id.nil?
      @repository = Repository.all.first
    else
      @repository = Repository.find(id)
    end

    gon.repository = @repository
  end

  def snapshot
    date = params[:snapshot_id] || params[:id]

    if not params[:repository_id].nil? and not date.nil?
      @snapshot = @repository.snapshots.where(date: date).first
      gon.snapshot = @snapshot
    end
  end

  def repositories
    @repositories = Repository.all.to_a
    gon.repositories = @repositories
  end

end

module RepositoryManager
  extend ActiveSupport::Concern

  included do
    before_filter :repository, :snapshot, :repositories
  end

  def repository
    id = params[:repository_id] || params[:id]

    if id.nil?
      @repository = Repository.all.first
    else
      @repository = Repository.find(id)
    end

    gon.repository = @repository
  end

  def snapshot
    date = params[:snapshot_id] || params[:id]

    if not params[:repository_id].nil? and not date.nil?
      @snapshot = @repository.snapshots.where(date: date)

      if @snapshot.exists?
        @snapshot = @snapshot.first
      else
        @snapshot = @repository.snapshots.last
      end

      gon.snapshot = @snapshot
    end
  end

  def repositories
    @repositories = Repository.all.to_a
    gon.repositories = @repositories
  end

end

Use latest snapshot if snapshot cannot be found
Use latest snapshot if snapshot cannot be found

A snapshot might be selected through the URL that is not available.
Instead of failing all together, the latest snapshot of the repository
is selected. What is still missing is an automated redirection.

Signed-off-by: Konrad Reiche <4ff0213f56e2082fcd8c882d04c546881c579ce4@gmail.com>
