API: accept name parameter instead of first name and last name in user patch

This commit is contained in:
cmrd Senya 2019-04-28 20:26:26 +03:00 committed by Jonne Haß
parent 9e18b19d6a
commit 6b8cd5d390
3 changed files with 12 additions and 9 deletions

View file

@ -90,8 +90,13 @@ module Api
def profile_update_params def profile_update_params
raise RuntimeError if params.has_key?(:id) 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 || {} :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) if updates.has_key?(:show_profile_info)
updates[:public_details] = updates[:show_profile_info] updates[:public_details] = updates[:show_profile_info]
updates.delete(:show_profile_info) updates.delete(:show_profile_info)

View file

@ -55,7 +55,7 @@ class ProfilePresenter < BasePresenter
def base_api_json def base_api_json
{ {
name: [first_name, last_name].join(" ").presence, name: [first_name, last_name].compact.join(" ").presence,
diaspora_id: diaspora_handle, diaspora_id: diaspora_handle,
avatar: AvatarPresenter.new(@presentable).base_hash, avatar: AvatarPresenter.new(@presentable).base_hash,
tags: tags.pluck(:name) tags: tags.pluck(:name)

View file

@ -200,8 +200,7 @@ describe Api::V1::UsersController do
new_birthday = Date.current + 100 new_birthday = Date.current + 100
new_gender = "ask1" new_gender = "ask1"
new_location = "new location" new_location = "new location"
new_first_name = "new first" new_name = "New Name"
new_last_name = "new last"
new_searchable = !auth.user.profile[:searchable] new_searchable = !auth.user.profile[:searchable]
new_show_profile_info = !auth.user.profile[:public_details] new_show_profile_info = !auth.user.profile[:public_details]
new_nsfw = !auth.user.profile[:nsfw] new_nsfw = !auth.user.profile[:nsfw]
@ -213,8 +212,7 @@ describe Api::V1::UsersController do
location: new_location, location: new_location,
gender: new_gender, gender: new_gender,
birthday: new_birthday, birthday: new_birthday,
first_name: new_first_name, name: new_name,
last_name: new_last_name,
searchable: new_searchable, searchable: new_searchable,
show_profile_info: new_show_profile_info, show_profile_info: new_show_profile_info,
nsfw: new_nsfw, nsfw: new_nsfw,
@ -229,7 +227,7 @@ describe Api::V1::UsersController do
expect(user["birthday"]).to eq(new_birthday.iso8601) expect(user["birthday"]).to eq(new_birthday.iso8601)
expect(user["location"]).to eq(new_location) expect(user["location"]).to eq(new_location)
expect(user["gender"]).to eq(new_gender) 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["searchable"]).to eq(new_searchable)
expect(user["show_profile_info"]).to eq(new_show_profile_info) expect(user["show_profile_info"]).to eq(new_show_profile_info)
expect(user["nsfw"]).to eq(new_nsfw) 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(auth.user.profile[:bio]).to eq(new_bio)
expect(birthday_format(auth.user.profile[:birthday])).to eq(birthday_format(new_birthday)) 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[:gender]).to eq(new_gender)
expect(auth.user.profile[:first_name]).to eq(new_first_name) expect(auth.user.profile[:first_name]).to eq(new_name)
expect(auth.user.profile[:last_name]).to eq(new_last_name) expect(auth.user.profile[:last_name]).to eq(nil)
expect(auth.user.profile[:searchable]).to eq(new_searchable) 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[:public_details]).to eq(new_show_profile_info)
expect(auth.user.profile[:nsfw]).to eq(new_nsfw) expect(auth.user.profile[:nsfw]).to eq(new_nsfw)