Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nullable license field to Projects model #669

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def filter
def project_params
check_for_allow_audio_upload(params.require(:project)).permit(
:description, :image, :name, :notes, :urn, :allow_original_download,
:allow_audio_upload
:allow_audio_upload, :license
)
end

Expand Down
19 changes: 13 additions & 6 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# image_file_name :string
# image_file_size :bigint
# image_updated_at :datetime
# license :text
# name :string not null
# notes :text
# urn :string
Expand Down Expand Up @@ -78,9 +79,11 @@ class Project < ApplicationRecord
validates :name, presence: true, uniqueness: { case_sensitive: false }
#validates :urn, uniqueness: {case_sensitive: false}, allow_blank: true, allow_nil: true
validates_format_of :urn, with: %r{\Aurn:[a-z0-9][a-z0-9-]{0,31}:[a-z0-9()+,\-.:=@;$_!*'%/?#]+\z},
message: 'urn %{value} is not valid, must be in format urn:<name>:<path>', allow_blank: true, allow_nil: true
message: 'urn %<value>s is not valid, must be in format urn:<name>:<path>', allow_blank: true, allow_nil: true
validates_attachment_content_type :image, content_type: %r{\Aimage/(jpg|jpeg|pjpeg|png|x-png|gif)\z},
message: 'file type %{value} is not allowed (only jpeg/png/gif images)'
message: 'file type %<value>s is not allowed (only jpeg/png/gif images)'

validates :license, length: { minimum: 1 }, allow_nil: true

# ensure allow original download is a permission level.
# Do not add predicates to Project ( #reader?, #writer?, #owner? do not make sense when attached directly to Project).
Expand All @@ -99,7 +102,8 @@ def self.filter_settings
:updater_id,
:updated_at,
:deleter_id,
:deleted_at],
:deleted_at,
:license],
render_fields: [:id, :name, :description, :creator_id,
:created_at,
:updater_id,
Expand All @@ -108,7 +112,8 @@ def self.filter_settings
:deleted_at,
:notes,
:allow_original_download,
:allow_audio_upload],
:allow_audio_upload,
:license],
text_fields: [:name, :description],
custom_fields: lambda { |item, user|
# do a query for the attributes that may not be in the projection
Expand Down Expand Up @@ -212,7 +217,8 @@ def self.schema
image_urls: Api::Schema.image_urls,
access_level: Api::Schema.permission_levels,
allow_original_download: Api::Schema.permission_levels,
allow_audio_upload: { type: 'boolean' }
allow_audio_upload: { type: 'boolean' },
license: { type: ['string', 'null'] }
},
required: [
:id,
Expand All @@ -231,7 +237,8 @@ def self.schema
:site_ids,
:region_ids,
:image_urls,
:allow_original_download
:allow_original_download,
:license
]
}.freeze
end
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240110141130_add_license_to_projects.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddLicenseToProjects < ActiveRecord::Migration[7.0]
def change
add_column :projects, :license, :text
end
end
6 changes: 4 additions & 2 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,8 @@ CREATE TABLE public.projects (
created_at timestamp without time zone,
updated_at timestamp without time zone,
allow_original_download character varying,
allow_audio_upload boolean DEFAULT false
allow_audio_upload boolean DEFAULT false,
license text
);


Expand Down Expand Up @@ -3696,6 +3697,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20220704043031'),
('20220808062341'),
('20220825042333'),
('20220930062323');
('20220930062323'),
('20240110141130');


2 changes: 2 additions & 0 deletions spec/factories/project_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# image_file_name :string
# image_file_size :bigint
# image_updated_at :datetime
# license :text
# name :string not null
# notes :text
# urn :string
Expand Down Expand Up @@ -42,6 +43,7 @@
sequence(:urn) { |n| "urn:project:example.org/project/#{n}" }
sequence(:notes) { |n| "note number #{n}" }
allow_audio_upload { false }
license { 'CC-BY-4.0' }

creator

Expand Down
5 changes: 5 additions & 0 deletions spec/models/project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# image_file_name :string
# image_file_size :bigint
# image_updated_at :datetime
# license :text
# name :string not null
# notes :text
# urn :string
Expand Down Expand Up @@ -74,6 +75,10 @@
expect(project_html.description_html).to eq(html)
end

it 'validates license is not an empty string' do
expect(build(:project, license: '')).not_to be_valid
end

# this should pass, but the paperclip implementation of validate_attachment_content_type is buggy.
#it { should validate_attachment_content_type(:image).
# allowing('image/gif', 'image/jpeg', 'image/jpg','image/png').
Expand Down
21 changes: 19 additions & 2 deletions spec/requests/projects_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,25 @@ def form_data(attributes, boundary)
expect(response.body).to match 'projects/new'
end

it 'allows creating a project without a license' do
body = { project: { name: 'test123' } }
post '/projects', params: body, **api_with_body_headers(reader_token)

expect_success
expect(api_data).to match(hash_including({
name: 'test123',
license: nil
}))
end

it 'does not allow creating a project with an empty string license' do
body = { project: { name: 'test123', license: '' } }
post '/projects', params: body, **api_with_body_headers(reader_token)

expect_error(:unprocessable_entity, 'Record could not be saved',
{ license: ['is too short (minimum is 1 character)'] })
end

# can't test empty body with multipart/form-data content type
# because middleware errors when trying to parse it
end
Expand Down Expand Up @@ -171,6 +190,4 @@ def form_data(attributes, boundary)
}))
end
end


end
Loading