Commit 5bd4bee0 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'remove-callback' into 'master'

This MR removes AR before_validation callback in favor setter.

## Why was this MR needed?

Because setters is good practice to normalize model attributes instead AR callbacks. Because new object should be valid right after initialization. 
If it MR interested I can try to find other places where we can use setters.

See merge request !6763
parents ed256037 b1ce2eb1
...@@ -10,6 +10,7 @@ Please view this file on the master branch, on stable branches it's out of date. ...@@ -10,6 +10,7 @@ Please view this file on the master branch, on stable branches it's out of date.
- Use MergeRequestsClosingIssues cache data on Issue#closed_by_merge_requests method - Use MergeRequestsClosingIssues cache data on Issue#closed_by_merge_requests method
- Fix documents and comments on Build API `scope` - Fix documents and comments on Build API `scope`
- Refactor email, use setter method instead AR callbacks for email attribute (Semyon Pupkov)
## 8.13.1 (unreleased) ## 8.13.1 (unreleased)
- Fix error in generating labels - Fix error in generating labels
......
...@@ -7,10 +7,8 @@ class Email < ActiveRecord::Base ...@@ -7,10 +7,8 @@ class Email < ActiveRecord::Base
validates :email, presence: true, uniqueness: true, email: true validates :email, presence: true, uniqueness: true, email: true
validate :unique_email, if: ->(email) { email.email_changed? } validate :unique_email, if: ->(email) { email.email_changed? }
before_validation :cleanup_email def email=(value)
write_attribute(:email, value.downcase.strip)
def cleanup_email
self.email = self.email.downcase.strip
end end
def unique_email def unique_email
......
...@@ -6,4 +6,9 @@ describe Email, models: true do ...@@ -6,4 +6,9 @@ describe Email, models: true do
subject { build(:email) } subject { build(:email) }
end end
end end
it 'normalize email value' do
expect(described_class.new(email: ' inFO@exAMPLe.com ').email)
.to eq 'info@example.com'
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment