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
This commit is contained in:
Rémy Coutable 2016-10-24 10:58:03 +00:00
commit 5bd4bee00f
3 changed files with 8 additions and 4 deletions

View file

@ -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
- 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)
- Fix error in generating labels

View file

@ -7,10 +7,8 @@ class Email < ActiveRecord::Base
validates :email, presence: true, uniqueness: true, email: true
validate :unique_email, if: ->(email) { email.email_changed? }
before_validation :cleanup_email
def cleanup_email
self.email = self.email.downcase.strip
def email=(value)
write_attribute(:email, value.downcase.strip)
end
def unique_email

View file

@ -6,4 +6,9 @@ describe Email, models: true do
subject { build(:email) }
end
end
it 'normalize email value' do
expect(described_class.new(email: ' inFO@exAMPLe.com ').email)
.to eq 'info@example.com'
end
end