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:
Laurel16
2022-11-01 20:45:48 +01:00
committed by Jean-Baptiste Bellet
parent fdf3a0d7b7
commit bfd396e644
7 changed files with 162 additions and 3 deletions

View File

@@ -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

View File

@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2022_11_16_095826) do
ActiveRecord::Schema.define(version: 2022_12_08_150521) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"