From e87075aed70980f5902302a9f7532174b7e85d63 Mon Sep 17 00:00:00 2001 From: "fabricio.albarnaz" Date: Thu, 11 Oct 2018 15:27:46 -0300 Subject: [PATCH 1/8] Add before save to check the instagram link pattern --- app/models/enterprise.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index ea1724f20a..024bc04d9a 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -80,6 +80,8 @@ class Enterprise < ActiveRecord::Base before_validation :set_unused_address_fields after_validation :geocode_address + before_save :check_instagram_pattern + after_touch :touch_distributors after_create :set_default_contact after_create :relate_to_owners_enterprises @@ -425,4 +427,9 @@ class Enterprise < ActiveRecord::Base where('enterprises.id != ?', self.id). each(&:touch) end + + def check_instagram_pattern + return if self.instagram.blank? || self.instagram.exclude?('www.instagram.com') + self.instagram = "@#{self.instagram.split('/').last}" + end end From 48720c835cbfd9bec679449cf6c9005de0e4c993 Mon Sep 17 00:00:00 2001 From: "fabricio.albarnaz" Date: Thu, 11 Oct 2018 16:30:17 -0300 Subject: [PATCH 2/8] Add spec to instagram pattern --- spec/models/enterprise_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/models/enterprise_spec.rb b/spec/models/enterprise_spec.rb index 73cd3e1c5b..28ce82b103 100644 --- a/spec/models/enterprise_spec.rb +++ b/spec/models/enterprise_spec.rb @@ -147,6 +147,14 @@ 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 + let!(:e) { create(:enterprise, instagram: 'www.instagram.com/my_user') } + + it "expects the instagram attribute to be in the correct pattern" do + expect(e.instagram).to eq('@my_user') + end + end end describe "preferred_shopfront_taxon_order" do From e46b3566c3c71a7d155715406772b3a14ab882ba Mon Sep 17 00:00:00 2001 From: "fabricio.albarnaz" Date: Thu, 11 Oct 2018 16:51:43 -0300 Subject: [PATCH 3/8] Remove the check for www.instagram.com --- app/models/enterprise.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 024bc04d9a..a92e617303 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -429,7 +429,7 @@ class Enterprise < ActiveRecord::Base end def check_instagram_pattern - return if self.instagram.blank? || self.instagram.exclude?('www.instagram.com') + return if self.instagram.blank? self.instagram = "@#{self.instagram.split('/').last}" end end From 6ad32fb66df762f3b760226b8d3df5f5e3ab0e40 Mon Sep 17 00:00:00 2001 From: "fabricio.albarnaz" Date: Thu, 11 Oct 2018 18:05:18 -0300 Subject: [PATCH 4/8] Add check for instagram.com --- app/models/enterprise.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index a92e617303..7d4650ad0e 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -429,7 +429,7 @@ class Enterprise < ActiveRecord::Base end def check_instagram_pattern - return if self.instagram.blank? + return if self.instagram.blank? || self.instagram.exclude?('instagram.com') self.instagram = "@#{self.instagram.split('/').last}" end end From 02bc134c593ad9617ac8137bfc0fa6b9aa85c087 Mon Sep 17 00:00:00 2001 From: "fabricio.albarnaz" Date: Mon, 15 Oct 2018 17:51:33 -0300 Subject: [PATCH 5/8] Change befor save check to format validation --- app/models/enterprise.rb | 11 +++++------ spec/models/enterprise_spec.rb | 6 +++--- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 7d4650ad0e..4f62d9f2f1 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -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 diff --git a/spec/models/enterprise_spec.rb b/spec/models/enterprise_spec.rb index 28ce82b103..fa4877ff3b 100644 --- a/spec/models/enterprise_spec.rb +++ b/spec/models/enterprise_spec.rb @@ -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 From 93a051bf5a3a21bd80aef23ca41e2377f5350989 Mon Sep 17 00:00:00 2001 From: "fabricio.albarnaz" Date: Tue, 16 Oct 2018 15:33:10 -0300 Subject: [PATCH 6/8] Regex ajust --- app/models/enterprise.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 4f62d9f2f1..c91187a325 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -80,7 +80,7 @@ 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 + validates :instagram, format: /\A@[a-zA-Z0-9._]{1,30}\z/, allow_blank: true after_touch :touch_distributors after_create :set_default_contact @@ -333,7 +333,8 @@ class Enterprise < ActiveRecord::Base end def instagram=(value) - write_attribute(:instagram, value.try(:gsub, %r{\Ahttps?://(?:www.)?instagram.com/([a-zA-Z0-9._]{1,30})/?\z}, '@\1')) + regex = %r{\A(?:https?://)?(?:www\.)?instagram\.com/([a-zA-Z0-9._]{1,30})/?\z} + write_attribute(:instagram, value.try(:gsub, regex, '@\1')) end protected From 8e4adbd23a2dcb6cabda5edee4c91e307a94b8e1 Mon Sep 17 00:00:00 2001 From: "fabricio.albarnaz" Date: Tue, 16 Oct 2018 15:34:46 -0300 Subject: [PATCH 7/8] Add more test cases --- spec/models/enterprise_spec.rb | 62 ++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 3 deletions(-) diff --git a/spec/models/enterprise_spec.rb b/spec/models/enterprise_spec.rb index fa4877ff3b..3c489ec413 100644 --- a/spec/models/enterprise_spec.rb +++ b/spec/models/enterprise_spec.rb @@ -149,11 +149,67 @@ describe Enterprise do end context "prevent an wrong instagram link pattern" do - let(:e) { build(:enterprise, instagram: 'instagram.com/my_user') } - - it "expects the instagram attribute to not be valid" 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 From 419493f1534d2e1781c0ac2c10c0e6547874d207 Mon Sep 17 00:00:00 2001 From: "fabricio.albarnaz" Date: Tue, 16 Oct 2018 16:09:15 -0300 Subject: [PATCH 8/8] Move instagram regex to an method to fix codeclimate --- app/models/enterprise.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index c91187a325..7ea1b069fa 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -333,8 +333,7 @@ class Enterprise < ActiveRecord::Base end def instagram=(value) - regex = %r{\A(?:https?://)?(?:www\.)?instagram\.com/([a-zA-Z0-9._]{1,30})/?\z} - write_attribute(:instagram, value.try(:gsub, regex, '@\1')) + write_attribute(:instagram, value.try(:gsub, instagram_regex, '@\1')) end protected @@ -345,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?