Skip to content

Commit

Permalink
Make optional route time params work with rails 7
Browse files Browse the repository at this point in the history
  • Loading branch information
codez committed Nov 12, 2022
1 parent f4694c5 commit 6087911
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 18 deletions.
6 changes: 2 additions & 4 deletions app/controllers/audio_files_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class AudioFilesController < ListController

include TimeFilterable
NOT_FOUND_PATH = Rails.public_path.join('system', 'not_found.mp3')
THE_FUTURE_PATH = Rails.public_path.join('system', 'the_future.mp3')

Expand Down Expand Up @@ -192,10 +193,7 @@ def detect_codec
end

def timestamp
@timestamp ||=
Time.zone.local(*params.values_at(:year, :month, :day, :hour, :min, :sec))
rescue ArgumentError
not_found
@timestamp ||= get_timestamp(param_time_parts)
end

def access
Expand Down
9 changes: 8 additions & 1 deletion app/controllers/concerns/time_filterable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ module TimeFilterable
private

def start_finish
parts = params.values_at(*TIME_PARTS).compact
parts = param_time_parts
start = get_timestamp(parts)
finish = start + range(parts)
[start, finish]
end

def param_time_parts
parts = params.values_at(*TIME_PARTS).compact
time = params[:time].to_s.match(/^(\d{2})(\d{2})?(\d{2})?$/)
parts += time[1..3].compact if time && parts.size == 3
parts
end

def range(parts)
range = TIME_PARTS[parts.size - 1]
case range
Expand Down
9 changes: 6 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@
resources :tracks, except: :index

constraints(year: /\d{4}/, month: /\d{2}/, day: /\d{2}/,
hour: /\d{2}/, min: /\d{2}/, sec: /\d{2}/) do
get '(/shows/:show_id)/broadcasts(/:year(/:month(/:day(/:hour(:min(:sec))))))',
hour: /\d{2}/, min: /\d{2}/, sec: /\d{2}/,
time: /(\d{2})(\d{2})?(\d{2})?/) do
# Does not work anymore with rails 7:
# get '(/shows/:show_id)/broadcasts(/:year(/:month(/:day(/:hour(:min(:sec))))))',
get '(/shows/:show_id)/broadcasts(/:year(/:month(/:day(/:time))))',
to: 'broadcasts#index',
as: :broadcasts

get 'audio_files/:year/:month/:day/:hour:min(:sec)_:playback_format.:format',
to: 'audio_files#show',
as: :audio_file

get '(/shows/:show_id)/tracks(/:year(/:month(/:day(/:hour(:min(:sec))))))',
get '(/shows/:show_id)/tracks(/:year(/:month(/:day(/:time))))',
to: 'tracks#index'
end

Expand Down
14 changes: 14 additions & 0 deletions test/controllers/audio_files_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,20 @@ class AudioFilesControllerTest < ActionController::TestCase
assert_response 404
end

test 'GET index with time parts up to minutes resolves params correctly' do
assert_routing({ path: 'audio_files/2013/05/20/2015_high.mp3', method: :get },
controller: 'audio_files', action: 'show', format: 'mp3',
year: '2013', month: '05', day: '20', hour: '20', min: '15',
playback_format: 'high')
end

test 'GET index with time parts up to seconds resolves params correctly' do
assert_routing({ path: 'audio_files/2013/05/20/201542_high.mp3', method: :get },
controller: 'audio_files', action: 'show', format: 'mp3',
year: '2013', month: '05', day: '20', hour: '20', min: '15', sec: '42',
playback_format: 'high')
end

private

def file
Expand Down
22 changes: 17 additions & 5 deletions test/controllers/broadcasts_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,30 @@ class BroadcastsControllerTest < ActionController::TestCase
assert_equal [true, false, true, false], json_attrs(:audio_access)
end

test 'GET index with hour time range returns filtered list' do
test 'GET index with hour range returns filtered list' do
get :index, params: { year: 2013, month: 5, day: 20, hour: 11 }
assert_equal ['Info Mai', 'Klangbecken'],
json_attrs(:label)
end

test 'GET index with minute time range returns filtered list' do
test 'GET index with hour time range returns filtered list' do
get :index, params: { year: 2013, month: 5, day: 20, time: 11 }
assert_equal ['Info Mai', 'Klangbecken'],
json_attrs(:label)
end

test 'GET index with hour and minute range returns filtered list' do
get :index, params: { year: 2013, month: 5, day: 20, hour: 21, min: 0 }
assert_equal ['G9S Shizzle Edition'],
json_attrs(:label)
end

test 'GET index with hourminute time range returns filtered list' do
get :index, params: { year: 2013, month: 5, day: 20, time: '2100' }
assert_equal ['G9S Shizzle Edition'],
json_attrs(:label)
end

test 'GET index with show_id and time parts resolves params correctly' do
assert_routing({ path: 'shows/42/broadcasts/2013/05/20', method: :get },
controller: 'broadcasts', action: 'index', show_id: '42',
Expand Down Expand Up @@ -99,19 +111,19 @@ class BroadcastsControllerTest < ActionController::TestCase
test 'GET index with time parts up to hour resolves params correctly' do
assert_routing({ path: 'broadcasts/2013/05/20/20', method: :get },
controller: 'broadcasts', action: 'index', format: :json,
year: '2013', month: '05', day: '20', hour: '20')
year: '2013', month: '05', day: '20', time: '20')
end

test 'GET index with time parts up to minute resolves params correctly' do
assert_routing({ path: 'broadcasts/2013/05/20/2015', method: :get },
controller: 'broadcasts', action: 'index', format: :json,
year: '2013', month: '05', day: '20', hour: '20', min: '15')
year: '2013', month: '05', day: '20', time: '2015')
end

test 'GET index with time parts up to seconds resolves params correctly' do
assert_routing({ path: 'broadcasts/2013/05/20/201534', method: :get },
controller: 'broadcasts', action: 'index', format: :json,
year: '2013', month: '05', day: '20', hour: '20', min: '15', sec: '34')
year: '2013', month: '05', day: '20', time: '201534')
end

test 'GET show returns entry' do
Expand Down
20 changes: 15 additions & 5 deletions test/controllers/tracks_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,26 @@ class TracksControllerTest < ActionController::TestCase
assert_equal %w[Shakira Jay-Z Jay-Z Chocolococolo], json_attrs(:artist)
end

test 'GET index with hour time range returns filtered list' do
test 'GET index with hour range returns filtered list' do
get :index, params: { year: 2013, month: 5, day: 20, hour: 11 }
assert_equal ['Shakira'], json_attrs(:artist)
end

test 'GET index with minute time range returns filtered list' do
test 'GET index with time range returns filtered list' do
get :index, params: { year: 2013, month: 5, day: 20, time: 11 }
assert_equal ['Shakira'], json_attrs(:artist)
end

test 'GET index with hour and minute range returns filtered list' do
get :index, params: { year: 2013, month: 5, day: 20, hour: 20, min: 12 }
assert_equal ['Chocolococolo'], json_attrs(:artist)
end

test 'GET index with hourminute time range returns filtered list' do
get :index, params: { year: 2013, month: 5, day: 20, time: '2012' }
assert_equal ['Chocolococolo'], json_attrs(:artist)
end

test 'GET index with show_id and time parts resolves params correctly' do
assert_routing({ path: 'shows/42/tracks/2013/05/20', method: :get },
controller: 'tracks', action: 'index', show_id: '42',
Expand Down Expand Up @@ -94,19 +104,19 @@ class TracksControllerTest < ActionController::TestCase
test 'GET index with time parts up to hour resolves params correctly' do
assert_routing({ path: 'tracks/2013/05/20/20', method: :get },
controller: 'tracks', action: 'index', format: :json,
year: '2013', month: '05', day: '20', hour: '20')
year: '2013', month: '05', day: '20', time: '20')
end

test 'GET index with time parts up to minute resolves params correctly' do
assert_routing({ path: 'tracks/2013/05/20/2015', method: :get },
controller: 'tracks', action: 'index', format: :json,
year: '2013', month: '05', day: '20', hour: '20', min: '15')
year: '2013', month: '05', day: '20', time: '2015')
end

test 'GET index with time parts up to seconds resolves params correctly' do
assert_routing({ path: 'tracks/2013/05/20/201534', method: :get },
controller: 'tracks', action: 'index', format: :json,
year: '2013', month: '05', day: '20', hour: '20', min: '15', sec: '34')
year: '2013', month: '05', day: '20', time: '201534')
end

test 'GET show returns entry' do
Expand Down

0 comments on commit 6087911

Please sign in to comment.