diff --git a/Gemfile b/Gemfile index 344a6f4a..6699cb0b 100644 --- a/Gemfile +++ b/Gemfile @@ -5,6 +5,7 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby '3.2.2' +gem 'active_storage_validations', '~> 1.0' gem 'authentication-zero', '~> 2.16' gem 'aws-sdk-s3' gem 'bcrypt', '~> 3.1.7' diff --git a/Gemfile.lock b/Gemfile.lock index 0a0e56ea..854713d1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -46,6 +46,11 @@ GEM erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.1, >= 1.2.0) + active_storage_validations (1.0.4) + activejob (>= 5.2.0) + activemodel (>= 5.2.0) + activestorage (>= 5.2.0) + activesupport (>= 5.2.0) activejob (7.0.6) activesupport (= 7.0.6) globalid (>= 0.3.6) @@ -304,6 +309,7 @@ PLATFORMS x86_64-linux DEPENDENCIES + active_storage_validations (~> 1.0) authentication-zero (~> 2.16) aws-sdk-s3 bcrypt (~> 3.1.7) diff --git a/app/controllers/tracks_controller.rb b/app/controllers/tracks_controller.rb index 371898fc..2c9436d9 100644 --- a/app/controllers/tracks_controller.rb +++ b/app/controllers/tracks_controller.rb @@ -49,6 +49,6 @@ def artist # Only allow a list of trusted parameters through. def track_params - params.require(:track).permit(:title) + params.require(:track).permit(:title, :original) end end diff --git a/app/models/track.rb b/app/models/track.rb index 91e5d1a9..bb070ab1 100644 --- a/app/models/track.rb +++ b/app/models/track.rb @@ -3,4 +3,7 @@ class Track < ApplicationRecord belongs_to :album delegate :artist, to: :album + + has_one_attached :original + validates :original, attached: true, content_type: { in: 'audio/x-wav', message: 'must be a WAV file' } end diff --git a/app/views/tracks/_form.html.erb b/app/views/tracks/_form.html.erb index 42375e5e..102244c7 100644 --- a/app/views/tracks/_form.html.erb +++ b/app/views/tracks/_form.html.erb @@ -1,5 +1,11 @@ <%= form_for([track.artist, track.album, track], builder: TailwindFormBuilder) do |form| %> <%= render('shared/errors', model: track) %> <%= form.text_field :title, class: 'w-full mb-6' %> + <% if track.original.present? %> + <%= form.label :original, "File (#{form.object.original.filename})", class: 'text-slate-600 mb-1 md:mb-0 pr-4' %> + <% else %> + <%= form.label :original, "File", class: 'text-slate-600 mb-1 md:mb-0 pr-4' %> + <% end %> + <%= form.file_field :original, class: 'block border border-slate-200 mb-6 file:mr-3 file:px-3 file:py-3 w-full file:border-0 file:bg-amber-600 hover:file:bg-amber-500 file:text-white' %> <%= form.submit "Save", class: 'w-full' %> <% end %> diff --git a/test/controllers/tracks_controller_test.rb b/test/controllers/tracks_controller_test.rb index 808d425a..2d037469 100644 --- a/test/controllers/tracks_controller_test.rb +++ b/test/controllers/tracks_controller_test.rb @@ -16,7 +16,7 @@ class TracksControllerTest < ActionDispatch::IntegrationTest test 'should create track' do assert_difference('Track.count') do post artist_album_tracks_url(@track.artist, @track.album), params: { - track: { title: @track.title } + track: { title: @track.title, original: fixture_file_upload('track.wav', 'audio/x-wav') } } end diff --git a/test/factories/tracks.rb b/test/factories/tracks.rb index b2e9c23c..2bb41322 100644 --- a/test/factories/tracks.rb +++ b/test/factories/tracks.rb @@ -5,5 +5,13 @@ album sequence(:title) { |n| "Never gonna give you up #{n}" } sequence(:position) { |n| n } + + after(:build) do |track| + track.original.attach( + io: Rails.root.join('test/fixtures/files/track.wav').open, + filename: 'track.wav', + content_type: 'audio/x-wav' + ) + end end end diff --git a/test/fixtures/files/track.wav b/test/fixtures/files/track.wav new file mode 100644 index 00000000..17e9e6cb Binary files /dev/null and b/test/fixtures/files/track.wav differ diff --git a/test/system/tracks_test.rb b/test/system/tracks_test.rb index 4ab18988..0fe0ecd0 100644 --- a/test/system/tracks_test.rb +++ b/test/system/tracks_test.rb @@ -13,6 +13,7 @@ class TracksTest < ApplicationSystemTestCase click_on 'Add track' fill_in 'Title', with: @track.title + attach_file 'File', Rails.root.join('test/fixtures/files/track.wav') click_on 'Save' assert_text "2. #{@track.title}"