Friendly urls for tracks
Don't use tracks ids in urls Add short_name to tracks and use that in urls as the identifier
This commit is contained in:
parent
882a1d20c9
commit
95fbd10153
9 changed files with 61 additions and 8 deletions
39
db/migrate/20170629162450_add_short_name_to_tracks.rb
Normal file
39
db/migrate/20170629162450_add_short_name_to_tracks.rb
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
class AddShortNameToTracks < ActiveRecord::Migration
|
||||
class TmpProgram < ActiveRecord::Base
|
||||
self.table_name = 'programs'
|
||||
end
|
||||
|
||||
class TmpTrack < ActiveRecord::Base
|
||||
self.table_name = 'tracks'
|
||||
end
|
||||
|
||||
def change
|
||||
add_column :tracks, :short_name, :string
|
||||
|
||||
TmpTrack.reset_column_information
|
||||
|
||||
TmpProgram.find_each do |program|
|
||||
# Keeps count of how many times we've encountered a short_name
|
||||
track_name_counter = {}
|
||||
|
||||
TmpTrack.where(program_id: program.id).find_each do |track|
|
||||
# Replace spaces with undercores and remove the non alphanumeric characters that aren't underscores or dashes
|
||||
short_name = track.name.tr(' ', '_').tr('^a-zA-Z0-9_-', '')
|
||||
|
||||
# If we've seen that short_name before then add the counter in the end to avoid collisions
|
||||
if track_name_counter[short_name]
|
||||
track_name_counter[short_name] += 1
|
||||
short_name += "_#{track_name_counter[short_name]}"
|
||||
else
|
||||
# Initialize the counter
|
||||
track_name_counter[short_name] = 0 unless track_name_counter[short_name]
|
||||
end
|
||||
|
||||
track.short_name = short_name
|
||||
track.save!
|
||||
end
|
||||
end
|
||||
|
||||
change_column_null :tracks, :short_name, false
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue