Skip to content

Commit

Permalink
Introduce staging namespace for Workflow active record
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisBr committed Nov 13, 2018
1 parent 0f26b2e commit 27c9635
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def destroy
private

def set_staging_workflow
@staging_workflow = StagingWorkflow.find(params[:staging_workflow_id])
@staging_workflow = Staging::Workflow.find(params[:staging_workflow_id])
return if @staging_workflow

redirect_back(fallback_location: root_path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def set_bootstrap_views
end

def set_staging_workflow
@staging_workflow = StagingWorkflow.find_by(id: params[:id])
@staging_workflow = Staging::Workflow.find_by(id: params[:id])
return if @staging_workflow

redirect_back(fallback_location: root_path)
Expand Down
4 changes: 2 additions & 2 deletions src/api/app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ def autocomplete(search)
has_many :target_of_bs_request_actions, class_name: 'BsRequestAction', foreign_key: 'target_project_id'
has_many :target_of_bs_requests, through: :target_of_bs_request_actions, source: :bs_request

has_one :staging, class_name: 'StagingWorkflow', inverse_of: :project
belongs_to :staging_workflow, inverse_of: :staging_projects
has_one :staging, class_name: 'Staging::Workflow', inverse_of: :project
belongs_to :staging_workflow, inverse_of: :staging_projects, class_name: 'Staging::Workflow'
has_many :staged_requests, class_name: 'BsRequest', foreign_key: 'staging_project_id', dependent: :nullify

default_scope { where('projects.id not in (?)', Relationship.forbidden_project_ids) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
class StagingWorkflow < ApplicationRecord
class Staging::Workflow < ApplicationRecord
def self.table_name_prefix
'staging_'
end

belongs_to :project, inverse_of: :staging
has_many :staging_projects, class_name: 'Project', inverse_of: :staging_workflow, dependent: :nullify do
has_many :staging_projects, class_name: 'Project', inverse_of: :staging_workflow, dependent: :nullify, foreign_key: 'staging_workflow_id' do
def without_staged_requests
includes(:staged_requests).where(bs_requests: { id: nil })
end
end

has_many :target_of_bs_requests, through: :project do
has_many :target_of_bs_requests, through: :project, foreign_key: 'staging_workflow_id' do
def stageable
in_states(['new', 'review'])
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
get :new, params: { project: project.name }
end

it { expect(StagingWorkflow.count).to eq(0) }
it { expect(assigns[:staging_workflow].class).to be(StagingWorkflow) }
it { expect(Staging::Workflow.count).to eq(0) }
it { expect(assigns[:staging_workflow].class).to be(Staging::Workflow) }
it { expect(response).to render_template(:new) }
end

Expand All @@ -25,7 +25,7 @@
get :new, params: { project: project.name }
end

it { expect(StagingWorkflow.count).to eq(1) }
it { expect(Staging::Workflow.count).to eq(1) }
it { expect(response).to redirect_to(staging_workflow_path(project.staging)) }
end
end
Expand All @@ -38,7 +38,7 @@

subject { project.staging }

it { expect(StagingWorkflow.count).to eq(1) }
it { expect(Staging::Workflow.count).to eq(1) }
it { expect(subject.staging_projects.map(&:name)).to match_array(['home:tom:Staging:A', 'home:tom:Staging:B']) }
it { expect(response).to redirect_to(staging_workflow_path(project.staging)) }
it { expect(flash[:success]).not_to be_nil }
Expand All @@ -54,19 +54,19 @@

subject { project.staging }

it { expect(StagingWorkflow.count).to eq(1) }
it { expect(Staging::Workflow.count).to eq(1) }
it { expect(subject.staging_projects.map(&:name)).to match_array(['home:tom:Staging:A', 'home:tom:Staging:B']) }
it { expect(response).to redirect_to(staging_workflow_path(project.staging)) }
it { expect(flash[:success]).not_to be_nil }
end

context 'when it fails to save' do
before do
allow_any_instance_of(StagingWorkflow).to receive(:save).and_return(false)
allow_any_instance_of(Staging::Workflow).to receive(:save).and_return(false)
post :create, params: { project: project.name }
end

it { expect(StagingWorkflow.count).to eq(0) }
it { expect(Staging::Workflow.count).to eq(0) }
it { expect(response).to render_template(:new) }
it { expect(flash[:error]).not_to be_nil }
end
Expand Down
2 changes: 1 addition & 1 deletion src/api/spec/factories/staging_workflow.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FactoryBot.define do
factory :staging_workflow do
factory :staging_workflow, class: 'Staging::Workflow' do
factory :staging_workflow_with_staging_projects do
initialize_with { new(attributes) }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'rails_helper'

RSpec.describe StagingWorkflow, type: :model do
RSpec.describe Staging::Workflow, type: :model do
let(:project) { create(:project_with_package) }
let(:staging_workflow) { create(:staging_workflow_with_staging_projects, project: project) }
let(:staging_project) { staging_workflow.staging_projects.first }
Expand Down

0 comments on commit 27c9635

Please sign in to comment.