From 6b8cd5d3907ec65192245ffa51dad0e08eee465f Mon Sep 17 00:00:00 2001 From: cmrd Senya Date: Sun, 28 Apr 2019 20:26:26 +0300 Subject: [PATCH] API: accept name parameter instead of first name and last name in user patch --- app/controllers/api/v1/users_controller.rb | 7 ++++++- app/presenters/profile_presenter.rb | 2 +- spec/integration/api/users_controller_spec.rb | 12 +++++------- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index cdf0db426..576cc1fc6 100644 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -90,8 +90,13 @@ module Api def profile_update_params raise RuntimeError if params.has_key?(:id) - updates = params.permit(:bio, :birthday, :gender, :location, :first_name, :last_name, + updates = params.permit(:bio, :birthday, :gender, :location, :name, :searchable, :show_profile_info, :nsfw, :tags).to_h || {} + if updates.has_key?(:name) + updates[:first_name] = updates[:name] + updates[:last_name] = nil + updates.delete(:name) + end if updates.has_key?(:show_profile_info) updates[:public_details] = updates[:show_profile_info] updates.delete(:show_profile_info) diff --git a/app/presenters/profile_presenter.rb b/app/presenters/profile_presenter.rb index 41d652c20..739089abc 100644 --- a/app/presenters/profile_presenter.rb +++ b/app/presenters/profile_presenter.rb @@ -55,7 +55,7 @@ class ProfilePresenter < BasePresenter def base_api_json { - name: [first_name, last_name].join(" ").presence, + name: [first_name, last_name].compact.join(" ").presence, diaspora_id: diaspora_handle, avatar: AvatarPresenter.new(@presentable).base_hash, tags: tags.pluck(:name) diff --git a/spec/integration/api/users_controller_spec.rb b/spec/integration/api/users_controller_spec.rb index 67681f8f5..226900c13 100644 --- a/spec/integration/api/users_controller_spec.rb +++ b/spec/integration/api/users_controller_spec.rb @@ -200,8 +200,7 @@ describe Api::V1::UsersController do new_birthday = Date.current + 100 new_gender = "ask1" new_location = "new location" - new_first_name = "new first" - new_last_name = "new last" + new_name = "New Name" new_searchable = !auth.user.profile[:searchable] new_show_profile_info = !auth.user.profile[:public_details] new_nsfw = !auth.user.profile[:nsfw] @@ -213,8 +212,7 @@ describe Api::V1::UsersController do location: new_location, gender: new_gender, birthday: new_birthday, - first_name: new_first_name, - last_name: new_last_name, + name: new_name, searchable: new_searchable, show_profile_info: new_show_profile_info, nsfw: new_nsfw, @@ -229,7 +227,7 @@ describe Api::V1::UsersController do expect(user["birthday"]).to eq(new_birthday.iso8601) expect(user["location"]).to eq(new_location) expect(user["gender"]).to eq(new_gender) - expect(user["name"]).to eq("#{new_first_name} #{new_last_name}") + expect(user["name"]).to eq(new_name) expect(user["searchable"]).to eq(new_searchable) expect(user["show_profile_info"]).to eq(new_show_profile_info) expect(user["nsfw"]).to eq(new_nsfw) @@ -239,8 +237,8 @@ describe Api::V1::UsersController do expect(auth.user.profile[:bio]).to eq(new_bio) expect(birthday_format(auth.user.profile[:birthday])).to eq(birthday_format(new_birthday)) expect(auth.user.profile[:gender]).to eq(new_gender) - expect(auth.user.profile[:first_name]).to eq(new_first_name) - expect(auth.user.profile[:last_name]).to eq(new_last_name) + expect(auth.user.profile[:first_name]).to eq(new_name) + expect(auth.user.profile[:last_name]).to eq(nil) expect(auth.user.profile[:searchable]).to eq(new_searchable) expect(auth.user.profile[:public_details]).to eq(new_show_profile_info) expect(auth.user.profile[:nsfw]).to eq(new_nsfw)