Merge pull request #2859 from albarnaz/instagram_pattern

Instagram pattern
This commit is contained in:
Maikel
2018-10-30 15:10:27 +11:00
committed by GitHub
2 changed files with 74 additions and 0 deletions

View File

@@ -80,6 +80,8 @@ class Enterprise < ActiveRecord::Base
before_validation :set_unused_address_fields
after_validation :geocode_address
validates :instagram, format: /\A@[a-zA-Z0-9._]{1,30}\z/, allow_blank: true
after_touch :touch_distributors
after_create :set_default_contact
after_create :relate_to_owners_enterprises
@@ -330,6 +332,10 @@ class Enterprise < ActiveRecord::Base
abn.present?
end
def instagram=(value)
write_attribute(:instagram, value.try(:gsub, instagram_regex, '@\1'))
end
protected
def devise_mailer
@@ -338,6 +344,10 @@ class Enterprise < ActiveRecord::Base
private
def instagram_regex
%r{\A(?:https?://)?(?:www\.)?instagram\.com/([a-zA-Z0-9._]{1,30})/?\z}
end
def name_is_unique
dups = Enterprise.where(name: name)
dups = dups.where('id != ?', id) unless new_record?

View File

@@ -147,6 +147,70 @@ describe Enterprise do
it "sets the enterprise contact to the owner by default" do
enterprise.contact.should eq enterprise.owner
end
context "prevent an wrong instagram link pattern" do
it "expects to be invalid the instagram attribute @my-user" do
e = build(:enterprise, instagram: '@my-user')
expect(e).to_not be_valid
end
it "expects to be invalid the instagram attribute https://facebook.com/user" do
e = build(:enterprise, instagram: 'https://facebook.com/user')
expect(e).to_not be_valid
end
it "expects to be invalid the instagram attribute tagram.com/user" do
e = build(:enterprise, instagram: 'tagram.com/user')
expect(e).to_not be_valid
end
it "expects to be invalid the instagram attribute https://instagram.com/user/preferences" do
e = build(:enterprise, instagram: 'https://instagram.com/user/preferences')
expect(e).to_not be_valid
end
end
context "accepted pattern" do
it "expects to be valid empty instagram attribute" do
e = build(:enterprise)
expect(e).to be_valid
end
it "expects be valid the instagram attribute @user" do
e = build(:enterprise, instagram: '@user')
expect(e).to be_valid
end
it "expects be valid the instagram attribute @my_www5.example" do
e = build(:enterprise, instagram: '@my_www5.example')
expect(e).to be_valid
end
it "expects be valid the instagram attribute http://instagram.com/user" do
e = build(:enterprise, instagram: 'http://instagram.com/user')
expect(e).to be_valid
end
it "expects be valid the instagram attribute https://instagram.com/user/" do
e = build(:enterprise, instagram: 'https://instagram.com/user/')
expect(e).to be_valid
end
it "expects be valid the instagram attribute https://www.instagram.com/user" do
e = build(:enterprise, instagram: 'https://www.instagram.com/user')
expect(e).to be_valid
end
it "expects be valid the instagram attribute instagram.com/user" do
e = build(:enterprise, instagram: 'instagram.com/user')
expect(e).to be_valid
end
it "renders the expected pattern" do
e = build(:enterprise, instagram: 'instagram.com/user')
expect(e.instagram).to eq('@user')
end
end
end
describe "preferred_shopfront_taxon_order" do