Change befor save check to format validation

This commit is contained in:
fabricio.albarnaz
2018-10-15 17:51:33 -03:00
parent 6ad32fb66d
commit 02bc134c59
2 changed files with 8 additions and 9 deletions

View File

@@ -80,7 +80,7 @@ class Enterprise < ActiveRecord::Base
before_validation :set_unused_address_fields
after_validation :geocode_address
before_save :check_instagram_pattern
validates :instagram, format: /\A@[a-zA-Z0-9._]{1,30}\z/, :allow_blank => true
after_touch :touch_distributors
after_create :set_default_contact
@@ -332,6 +332,10 @@ class Enterprise < ActiveRecord::Base
abn.present?
end
def instagram=(value)
write_attribute(:instagram, value.try(:gsub, %r{\Ahttps?://(?:www.)?instagram.com/([a-zA-Z0-9._]{1,30})/?\z}, '@\1'))
end
protected
def devise_mailer
@@ -427,9 +431,4 @@ class Enterprise < ActiveRecord::Base
where('enterprises.id != ?', self.id).
each(&:touch)
end
def check_instagram_pattern
return if self.instagram.blank? || self.instagram.exclude?('instagram.com')
self.instagram = "@#{self.instagram.split('/').last}"
end
end

View File

@@ -149,10 +149,10 @@ describe Enterprise do
end
context "prevent an wrong instagram link pattern" do
let!(:e) { create(:enterprise, instagram: 'www.instagram.com/my_user') }
let(:e) { build(:enterprise, instagram: 'instagram.com/my_user') }
it "expects the instagram attribute to be in the correct pattern" do
expect(e.instagram).to eq('@my_user')
it "expects the instagram attribute to not be valid" do
expect(e).to_not be_valid
end
end
end