Use normalizes to prepare User
values (#28650)
Co-authored-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
parent
00341c70ff
commit
543d7890fd
2 changed files with 42 additions and 30 deletions
|
@ -118,14 +118,15 @@ class User < ApplicationRecord
|
|||
scope :matches_email, ->(value) { where(arel_table[:email].matches("#{value}%")) }
|
||||
scope :matches_ip, ->(value) { left_joins(:ips).where('user_ips.ip <<= ?', value).group('users.id') }
|
||||
|
||||
before_validation :sanitize_languages
|
||||
before_validation :sanitize_role
|
||||
before_validation :sanitize_time_zone
|
||||
before_validation :sanitize_locale
|
||||
before_create :set_approved
|
||||
after_commit :send_pending_devise_notifications
|
||||
after_create_commit :trigger_webhooks
|
||||
|
||||
normalizes :locale, with: ->(locale) { I18n.available_locales.exclude?(locale.to_sym) ? nil : locale }
|
||||
normalizes :time_zone, with: ->(time_zone) { ActiveSupport::TimeZone[time_zone].nil? ? nil : time_zone }
|
||||
normalizes :chosen_languages, with: ->(chosen_languages) { chosen_languages.compact_blank.presence }
|
||||
|
||||
# This avoids a deprecation warning from Rails 5.1
|
||||
# It seems possible that a future release of devise-two-factor will
|
||||
# handle this itself, and this can be removed from our User class.
|
||||
|
@ -447,25 +448,10 @@ class User < ApplicationRecord
|
|||
@bypass_invite_request_check
|
||||
end
|
||||
|
||||
def sanitize_languages
|
||||
return if chosen_languages.nil?
|
||||
|
||||
chosen_languages.compact_blank!
|
||||
self.chosen_languages = nil if chosen_languages.empty?
|
||||
end
|
||||
|
||||
def sanitize_role
|
||||
self.role = nil if role.present? && role.everyone?
|
||||
end
|
||||
|
||||
def sanitize_time_zone
|
||||
self.time_zone = nil if time_zone.present? && ActiveSupport::TimeZone[time_zone].nil?
|
||||
end
|
||||
|
||||
def sanitize_locale
|
||||
self.locale = nil if locale.present? && I18n.available_locales.exclude?(locale.to_sym)
|
||||
end
|
||||
|
||||
def prepare_new_user!
|
||||
BootstrapTimelineWorker.perform_async(account_id)
|
||||
ActivityTracker.increment('activity:accounts:local')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue