class Admin::MetricsController < ApplicationController
  include Concerns::Repository
  include Concerns::Metric

  before_filter :init

  def schedule
    worker = MetricWorker.worker_class(@metric)
    id = worker.send(:perform_async, @repository.id, @metric)
    Job.create!(sidekiq_id: id, repository: @repository.id, metric: @metric)

    render nothing: true
  end

  private
  def init
    load_repositories(:repository_id)
    load_metrics(:metric_id)
  end


end

class Admin::MetricsController < ApplicationController
  include Concerns::Repository
  include Concerns::Metric

  before_filter :init

  def schedule
    worker = MetricWorker.worker_class(@metric)
    job = Job.where(repository: @repository.id, metric: @metric)
    id = worker.send(:perform_async, @repository.id, @metric)

    if job.exists?
      job.update_attributes(sidekiq_id: id)
    else
      Job.create!(sidekiq_id: id, repository: @repository.id, metric: @metric)
    end

    render nothing: true
  end

  private
  def init
    load_repositories(:repository_id)
    load_metrics(:metric_id)
  end


end

Update job document if it already exists
Update job document if it already exists

Apparently, I have not understood yet how the MongoDB, respectively
Mongoid update mechanism works. If I use .create! on attributes, which
theoretically map to the same object, it is not updated to the new
value. What works for now is to try to fetch the job document and use
.update_attributes if it already exists and .create! otherwise.

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