Skip to content

Commit dc75e7c

Browse files
committed
Set provider prefix on creation and when editing a provider with no topics
Resolves #607 Each provider has a `file_name_prefix`. This value is added to the file name when the provider uploads a file. This `file_name_prefix` is necessary to coordinate between our file storage and Azure's. However, if a provider was to change their `file_name_prefix` when they had uploaded files, it would break management of those existing uploaded files. Before, we got around this by simply removing the field from the provider's form. This meant that users could not set the value of `file_name_prefix` when making a new provider or when editing a provider who had no uploaded files. This PR adds the ability to set the `file_name_prefix` value on the provider form. The field will show on the form when the provider is... - A new instance - One who has no topics, and no uploaded files We also update the logic in the Provider model so it will check that `file_name_prefix` may be changed (condition being the provider does not have topics) Other additional changes include... - A new `disabled` styling. This is used to signify to the user that a file name prefix can't be changed when the provider has topics, as that means file names are established. This can be seen on the form of a provider you're editing who has topics associated with them. There is also a notice on the page. - Updated layout on a provider's overview page so that the File name prefix display spans all its column in the grid. This is because file name prefixes could be long, and could overflow its display label otherwise `file_name_prefix` editing restricted on model level
1 parent fc9dc7f commit dc75e7c

8 files changed

Lines changed: 160 additions & 8 deletions

File tree

app/assets/tailwind/application.css

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,14 @@
6666
box-shadow: 0 0 0 4px rgba(59, 130, 246, 0.1);
6767
}
6868

69-
.input-field:hover:not(:focus) {
69+
.input-field:hover:not(:focus):not(:disabled) {
7070
@apply border-gray-400;
7171
}
7272

73+
.input-field:disabled {
74+
@apply bg-gray-100 text-gray-500;
75+
}
76+
7377
.select-field {
7478
@apply w-full border border-gray-300 rounded-lg text-sm bg-white transition-all duration-200 cursor-pointer;
7579
padding: 0.625rem 2.5rem 0.625rem 0.875rem;

app/models/provider.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,21 @@ class Provider < ApplicationRecord
2626

2727
validates :name, :provider_type, presence: true
2828
validates :name, uniqueness: true
29+
30+
validate :file_name_prefix_may_be_changed, on: :update, if: :file_name_prefix_changed?
31+
32+
def topics?
33+
topics.any?
34+
end
35+
36+
private
37+
38+
def file_name_prefix_may_be_changed
39+
if topics?
40+
errors.add(
41+
:file_name_prefix,
42+
"can't be changed as provider has associated topics and so file names are established"
43+
)
44+
end
45+
end
2946
end

app/views/providers/_form.html.erb

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@
22
<div class="space-y-6">
33
<%= render "shared/errors", errors: provider.errors.full_messages, resource_name: provider.class.name %>
44

5+
<!-- File name prefix uneditable notice -->
6+
<% if @provider.topics? %>
7+
<div id="file-name-prefix-uneditable-notice" class="bg-blue-50 border border-blue-200 rounded-lg p-4">
8+
<div class="flex">
9+
<div class="flex-shrink-0">
10+
<svg class="h-5 w-5 text-blue-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
11+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
12+
</svg>
13+
</div>
14+
<div class="ml-3">
15+
<p class="text-sm text-blue-700 m-0">
16+
There are topics associated with this provider, so the file names are established and the file name prefix can't be edited.
17+
</p>
18+
</div>
19+
</div>
20+
</div>
21+
<% end %>
22+
23+
524
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
625

726
<!-- Provider Name -->
@@ -11,7 +30,7 @@
1130
<span class="text-red-500">*</span>
1231
<% end %>
1332
<%= form.text_field :name,
14-
placeholder: "Enter provider name (e.g., Johns Hopkins, Mayo Clinic)",
33+
placeholder: "e.g., Johns Hopkins, Mayo Clinic",
1534
autofocus: true,
1635
class: "input-field bg-gray-50" %>
1736
<p class="help-text">The official name of the healthcare provider or organization.</p>
@@ -29,6 +48,31 @@
2948
<p class="help-text">The type or category of healthcare provider.</p>
3049
</div>
3150

51+
<!-- File name prefix -->
52+
<% if @provider.topics? %>
53+
<div>
54+
<%= form.label :file_name_prefix, class: "input-label" do %>
55+
File name prefix
56+
<% end %>
57+
<%= form.text_field :file_name_prefix,
58+
disabled: true,
59+
value: @provider.file_name_prefix,
60+
class: "input-field bg-gray-50"
61+
%>
62+
<p class="help-text">The prefix to use for this provider's uploads.</p>
63+
</div>
64+
<% else %>
65+
<div>
66+
<%= form.label :file_name_prefix, class: "input-label" do %>
67+
File name prefix
68+
<% end %>
69+
<%= form.text_field :file_name_prefix,
70+
placeholder: "e.g., 123_who_guidelines",
71+
class: "input-field bg-gray-50" %>
72+
<p class="help-text">The prefix to use for this provider's uploads.</p>
73+
</div>
74+
<% end %>
75+
3276
<!-- Regions -->
3377
<div>
3478
<%= form.label :region, class: "input-label" do %>

app/views/providers/show.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
</div>
8282

8383
<div class="card-body">
84-
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8">
84+
<div class="grid grid-cols-1 md:grid-cols-2 gap-8">
8585

8686
<div>
8787
<label class="input-label">Provider Name</label>
@@ -97,7 +97,7 @@
9797
</div>
9898
</div>
9999

100-
<div>
100+
<div class="col-span-full">
101101
<label class="input-label">File Name Prefix</label>
102102
<div class="bg-gray-50 p-4 rounded-lg border border-gray-200">
103103
<% if @provider.file_name_prefix.present? %>

spec/jobs/documents_sync_job_spec.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
require "rails_helper"
22

33
RSpec.describe DocumentsSyncJob, type: :job do
4-
let(:topic) { create(:topic, :with_documents) }
5-
let(:provider) { topic.provider }
4+
let(:provider) { create(:provider, file_name_prefix: "MyPrefix") }
5+
let(:topic) { create(:topic, :with_documents, provider:) }
66
let(:document) { topic.documents.first }
77
let(:file_name) { document.filename }
88

9-
before { provider.update(file_name_prefix: "MyPrefix") }
10-
119
describe "#perform" do
1210
let(:file_worker) { instance_double(FileWorker) }
1311

spec/models/provider_spec.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# == Schema Information
2+
#
3+
# Table name: providers
4+
# Database name: primary
5+
#
6+
# id :bigint not null, primary key
7+
# file_name_prefix :string
8+
# name :string
9+
# provider_type :string
10+
# created_at :datetime not null
11+
# updated_at :datetime not null
12+
# old_id :integer
13+
#
14+
# Indexes
15+
#
16+
# index_providers_on_old_id (old_id) UNIQUE
17+
#
18+
require "rails_helper"
19+
20+
RSpec.describe Provider, type: :model do
21+
let(:provider) { create(:provider) }
22+
subject { provider }
23+
24+
describe "validations" do
25+
it { should validate_presence_of(:provider_type) }
26+
it { should validate_presence_of(:name) }
27+
it { should validate_uniqueness_of(:name) }
28+
29+
describe "#file_name_prefix_may_be_changed" do
30+
context "when a provider has topics" do
31+
let!(:topic) { create(:topic, provider: provider) }
32+
33+
it "returns an error" do
34+
provider.file_name_prefix = "updated_prefix"
35+
expect(provider).not_to be_valid
36+
expect(provider.errors[:file_name_prefix]).to match_array(
37+
"can't be changed as provider has associated topics and so file names are established"
38+
)
39+
end
40+
end
41+
42+
context "when a provider has no topics" do
43+
it "does not return an error" do
44+
provider.file_name_prefix = "updated_prefix"
45+
expect(provider).to be_valid
46+
end
47+
end
48+
end
49+
end
50+
51+
describe "#topics?" do
52+
subject { provider.topics? }
53+
54+
context "when a provider has topics" do
55+
let!(:topic) { create(:topic, provider: provider) }
56+
57+
it { is_expected.to be(true) }
58+
end
59+
60+
context "when a provider has no topics" do
61+
it { is_expected.to be(false) }
62+
end
63+
end
64+
end

spec/views/providers/edit.html.erb_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,28 @@
1616
assert_select "input[type='submit'][value='Update Provider']"
1717
end
1818
end
19+
20+
context "when the provider has associated topics" do
21+
before { create(:topic, provider: provider) }
22+
23+
it "tells the user that the File name prefix can't be changed" do
24+
render
25+
26+
assert_select "form[action=?][method=?]", provider_path(provider), "post" do
27+
assert_select "input[name=?][disabled]", "provider[file_name_prefix]"
28+
end
29+
30+
assert_select "div#file-name-prefix-uneditable-notice"
31+
end
32+
end
33+
34+
context "when the provider that has no associated topics" do
35+
it "has the File name prefix field" do
36+
render
37+
38+
assert_select "form[action=?][method=?]", provider_path(provider), "post" do
39+
assert_select "input[name=?]", "provider[file_name_prefix]"
40+
end
41+
end
42+
end
1943
end

spec/views/providers/new.html.erb_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
assert_select "form[action=?][method=?]", providers_path, "post" do
1515
assert_select "input[name=?]", "provider[name]"
1616
assert_select "input[name=?]", "provider[provider_type]"
17+
assert_select "input[name=?]", "provider[file_name_prefix]"
1718
assert_select "input[type='submit'][value='Create Provider']"
1819
end
1920
end

0 commit comments

Comments
 (0)