mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-14 04:04:23 +00:00
Add instagram handler validator
update entreprise model to match the correct pattern for instagram attribute & add migration to correct wrong links in db + test
This commit is contained in:
committed by
Jean-Baptiste Bellet
parent
fdf3a0d7b7
commit
bfd396e644
@@ -0,0 +1,29 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class UpdateEnterpriseInstagramLinks < ActiveRecord::Migration[6.1]
|
||||
class Enterprise < ActiveRecord::Base
|
||||
def strip_url(url)
|
||||
url&.sub(%r{(https?://)?}, '')
|
||||
end
|
||||
|
||||
def correct_instagram_url(url)
|
||||
url && strip_url(url.downcase).sub(%r{www.instagram.com/}, '').sub(%r{instagram.com/},
|
||||
'').delete("@")
|
||||
end
|
||||
|
||||
def instagram
|
||||
correct_instagram_url self[:instagram]
|
||||
end
|
||||
end
|
||||
|
||||
def up
|
||||
Enterprise.where.not(instagram: nil).find_each do |enterprise|
|
||||
enterprise.update!(instagram: enterprise.instagram)
|
||||
enterprise.save!
|
||||
end
|
||||
end
|
||||
|
||||
def down
|
||||
raise ActiveRecord::IrreversibleMigration
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user