Skip to content

Commit

Permalink
RUBY 3212 pafs proposals with duplicate names are not sent to pipeline (
Browse files Browse the repository at this point in the history
#903)

* [RUBY-3212] Add validation for unique project names in `PafsCore::ProjectNameStep` spec

- Added a new context to test for duplicate project names.
- Ensures that the model returns false and sets an error message when a duplicate project name is provided.

* [RUBY-3212] Refactor `name_must_be_unique` method for readability

- Simplified `name_must_be_unique` method using `return unless` to improve readability and reduce nesting.

* [RUBY-3220] Add back shared example to `project_name_step_spec.rb`

* [RUBY-3212] Refactor project name uniqueness validation in spec

- Moved the creation of `existing_project` to a `before` block for better readability and consistency.

* [RUBY-3212] Add unique index to `projects` name

- Created migration to add unique index to `pafs_core_projects` table on the `name` column.
- Updated schema to reflect the new unique index on the `name` column.

* [RUBY-3212] Update migration class to inherit from ActiveRecord::Migration[7.1]

- Changed inheritance of `AddUniqueIndexToProjectsName` from `ActiveRecord::Migration[7.2]` to `ActiveRecord::Migration[7.1]`

* [RUBY-3212] Remove redundant assignment in `project_name_step` uniqueness test
  • Loading branch information
jjromeo authored Sep 6, 2024
1 parent 6e93e31 commit 627d8d8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
10 changes: 10 additions & 0 deletions app/steps/pafs_core/project_name_step.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,20 @@ class ProjectNameStep < BasicStep
message: "The project name must only contain letters, underscores, hyphens and numbers"
}, if: -> { name.present? }

validate :name_must_be_unique

private

def step_params(params)
params.require(:project_name_step).permit(:name)
end

# BasicStep does not have access to the uniqueness validator presumably
# because it's not an ActiveRecord model. So we need to implement our own
def name_must_be_unique
return unless PafsCore::Project.exists?(name: name)

errors.add(:name, "The project name already exists. Your project must have a unique name.")
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddUniqueIndexToProjectsName < ActiveRecord::Migration[7.1]
def change
add_index :pafs_core_projects, :name, unique: true
end
end
4 changes: 2 additions & 2 deletions spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_03_27_113556) do
ActiveRecord::Schema[7.1].define(version: 2024_09_04_104418) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -328,6 +328,7 @@
t.integer "earliest_start_year"
t.string "updated_by_type"
t.bigint "updated_by_id"
t.index ["name"], name: "index_pafs_core_projects_on_name", unique: true
t.index ["reference_number", "version"], name: "index_pafs_core_projects_on_reference_number_and_version", unique: true
t.index ["slug"], name: "index_pafs_core_projects_on_slug", unique: true
t.index ["submitted_to_pol"], name: "index_pafs_core_projects_on_submitted_to_pol"
Expand Down Expand Up @@ -398,5 +399,4 @@
t.index ["reset_password_token"], name: "index_pafs_core_users_on_reset_password_token", unique: true
t.index ["unlock_token"], name: "index_pafs_core_users_on_unlock_token", unique: true
end

end
11 changes: 11 additions & 0 deletions spec/steps/pafs_core/project_name_step_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,16 @@
end
end
end

context "when name already exists" do
let(:duplicate_name_params) { ActionController::Parameters.new({ project_name_step: { name: "Unique Project Name" } }) }

before { create(:project_name_step, name: "Unique Project Name") }

it "returns false and sets the uniqueness error message" do
expect(project_name_step.update(duplicate_name_params)).to be false
expect(project_name_step.errors[:name].join).to eq("The project name already exists. Your project must have a unique name.")
end
end
end
end

0 comments on commit 627d8d8

Please sign in to comment.