From e8474bd72572c8a92f043958200835b7c570ec9f Mon Sep 17 00:00:00 2001 From: Nick Borromeo <ngborromeo@gmail.com> Date: Mon, 20 Jan 2014 14:58:02 -0800 Subject: [PATCH] Change user update to use #fetch for defaults This changes the use of the || operator when setting the default of the user attribute if the key does not exist to #fetch. This will make sure that if false is passed in as the value it will still fail and use the default setting. This also adds constants to define and array and a hash of symbols which is iterated over to set the users attributes. --- app/services/user_updater.rb | 57 +++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/app/services/user_updater.rb b/app/services/user_updater.rb index 7130c2d19..40801d664 100644 --- a/app/services/user_updater.rb +++ b/app/services/user_updater.rb @@ -1,27 +1,33 @@ class UserUpdater + + CATEGORY_IDS = { + watched_category_ids: :watching, + tracked_category_ids: :tracking, + muted_category_ids: :muted + } + + USER_ATTR = [ + :email_digests, + :email_always, + :email_direct, + :email_private_messages, + :external_links_in_new_tab, + :enable_quoting, + :dynamic_favicon, + :watch_new_topics + ] + def initialize(actor, user) @user = user @guardian = Guardian.new(actor) end def update(attributes = {}) - user.website = format_url(attributes[:website]) || user.website + user.website = format_url(attributes.fetch(:website) { user.website }) - user.bio_raw = attributes[:bio_raw] || user.bio_raw - user.name = attributes[:name] || user.name - user.digest_after_days = attributes[:digest_after_days] || user.digest_after_days - - if ids = attributes[:watched_category_ids] - CategoryUser.batch_set(user, :watching, ids) - end - - if ids = attributes[:tracked_category_ids] - CategoryUser.batch_set(user, :tracking, ids) - end - - if ids = attributes[:muted_category_ids] - CategoryUser.batch_set(user, :muted, ids) - end + user.bio_raw = attributes.fetch(:bio_raw) { user.bio_raw } + user.name = attributes.fetch(:name) { user.name } + user.digest_after_days = attributes.fetch(:digest_after_days) { user.digest_after_days } if attributes[:auto_track_topics_after_msecs] user.auto_track_topics_after_msecs = attributes[:auto_track_topics_after_msecs].to_i @@ -32,19 +38,16 @@ class UserUpdater end if guardian.can_grant_title?(user) - user.title = attributes[:title] || user.title + user.title = attributes.fetch(:title) { user.title } end - [ - :email_digests, - :email_always, - :email_direct, - :email_private_messages, - :external_links_in_new_tab, - :enable_quoting, - :dynamic_favicon, - :watch_new_topics - ].each do |attribute| + CATEGORY_IDS.each do |attribute, level| + if ids = attributes[attribute] + CategoryUser.batch_set(user, level, ids) + end + end + + USER_ATTR.each do |attribute| if attributes[attribute].present? user.send("#{attribute.to_s}=", attributes[attribute] == 'true') end