From 59a28c24dd4158eaffe842a726b4af06608085fe Mon Sep 17 00:00:00 2001 From: takonomura Date: Wed, 11 Apr 2018 10:39:23 +0900 Subject: [PATCH] Record revision at JobDefinition's after_save --- .../kuroko2/api/job_definitions_controller.rb | 2 -- .../kuroko2/job_definitions_controller.rb | 4 ++-- app/models/kuroko2/job_definition.rb | 13 +++++++------ .../job_definition_revisions_controller_spec.rb | 8 +++++--- spec/controllers/job_definitions_controller_spec.rb | 2 -- spec/features/job_definition_revisions_spec.rb | 6 ++++-- 6 files changed, 18 insertions(+), 17 deletions(-) diff --git a/app/controllers/kuroko2/api/job_definitions_controller.rb b/app/controllers/kuroko2/api/job_definitions_controller.rb index 6685f3d0..7f385653 100644 --- a/app/controllers/kuroko2/api/job_definitions_controller.rb +++ b/app/controllers/kuroko2/api/job_definitions_controller.rb @@ -16,7 +16,6 @@ def create_resource user.stars.create(job_definition: definition) if user.google_account? end - definition.record_revision @resource = Kuroko2::Api::JobDefinitionResource.new(definition) else raise HTTP::UnprocessableEntity.new("#{definition.name}: #{definition.errors.full_messages.join()}") @@ -32,7 +31,6 @@ def update_resource definition = Kuroko2::JobDefinition.find(params[:id]) if definition.update(definition_params(params)) - definition.record_revision @resource = Kuroko2::Api::JobDefinitionResource.new(definition) else raise HTTP::UnprocessableEntity.new("#{definition.name}: #{definition.errors.full_messages.join()}") diff --git a/app/controllers/kuroko2/job_definitions_controller.rb b/app/controllers/kuroko2/job_definitions_controller.rb index bf601493..f801ed78 100644 --- a/app/controllers/kuroko2/job_definitions_controller.rb +++ b/app/controllers/kuroko2/job_definitions_controller.rb @@ -50,7 +50,7 @@ def create if @definition.save current_user.stars.create(job_definition: @definition) - @definition.record_revision(user: current_user) + @definition.revisions.first.update(user: current_user) redirect_to @definition, notice: 'Job Definition was successfully created.' else @@ -70,7 +70,7 @@ def update end if success - @definition.record_revision(user: current_user) + @definition.revisions.first.update(user: current_user) redirect_to @definition, notice: 'Job Definition was successfully updated.' else render :edit diff --git a/app/models/kuroko2/job_definition.rb b/app/models/kuroko2/job_definition.rb index 10ea6d95..231516c7 100644 --- a/app/models/kuroko2/job_definition.rb +++ b/app/models/kuroko2/job_definition.rb @@ -33,6 +33,7 @@ module PreventMultiStatus before_destroy :confirm_active_instances after_initialize :set_default_values + after_save :record_revision after_save :create_default_memory_expectancy, on: :create scope :ordered, -> { order(:id) } @@ -99,12 +100,6 @@ def create_instance(script: nil, launched_by:, token: nil ) job_instances.create!(script: script, log_message: message) end - def record_revision(user: nil) - unless revisions.first.try(:script) == script - Kuroko2::JobDefinitionRevision.create(job_definition: self, script: script, user: user, changed_at: Time.current) - end - end - private def confirm_active_instances @@ -133,6 +128,12 @@ def set_default_values EOF end + def record_revision + unless revisions.first.try(:script) == script + Kuroko2::JobDefinitionRevision.create(job_definition: self, script: script, changed_at: Time.current) + end + end + def create_default_memory_expectancy create_memory_expectancy! unless memory_expectancy end diff --git a/spec/controllers/job_definition_revisions_controller_spec.rb b/spec/controllers/job_definition_revisions_controller_spec.rb index a7a37a40..f1d39382 100644 --- a/spec/controllers/job_definition_revisions_controller_spec.rb +++ b/spec/controllers/job_definition_revisions_controller_spec.rb @@ -5,8 +5,10 @@ before { sign_in } - let(:definition) { create(:job_definition) } - let!(:revisions) { create_list(:job_definition_revision, 3, job_definition: definition) } + let!(:definition) { create(:job_definition, script: "noop:\n") } + before do + 1.upto(3) { |i| definition.update(script: "noop:\n" * i) } + end describe '#index' do subject! { get :index, params: { job_definition_id: definition.id } } @@ -16,7 +18,7 @@ expect(response).to render_template('index') expect(assigns(:definition)).to eq definition - expect(assigns(:revisions)).to eq revisions.sort_by(&:id).reverse + expect(assigns(:revisions).size).to eq 3 end end end diff --git a/spec/controllers/job_definitions_controller_spec.rb b/spec/controllers/job_definitions_controller_spec.rb index 1963e183..e6573df9 100644 --- a/spec/controllers/job_definitions_controller_spec.rb +++ b/spec/controllers/job_definitions_controller_spec.rb @@ -128,8 +128,6 @@ end describe '#update' do - before { definition.record_revision } - let(:admin) { create(:user) } let(:script) { "noop:\n" } subject! do diff --git a/spec/features/job_definition_revisions_spec.rb b/spec/features/job_definition_revisions_spec.rb index 675227ed..fe9fb652 100644 --- a/spec/features/job_definition_revisions_spec.rb +++ b/spec/features/job_definition_revisions_spec.rb @@ -3,8 +3,10 @@ describe 'Shows job definition revisions', type: :feature do before { sign_in } - let(:definition) { create(:job_definition) } - let!(:revisions) { create_list(:job_definition_revision, 3, job_definition: definition) } + let!(:definition) { create(:job_definition, script: "noop:\n") } + before do + 1.upto(3) { |i| definition.update(script: "noop:\n" * i) } + end it do visit kuroko2.job_definition_path definition