Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion app/controllers/people_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def show
def new
set_user
@person = @user ? PersonFromUserService.new(user: @user).call : Person.new
@person.user = @user if @user
authorize! @person
set_form_variables
end
Expand All @@ -90,6 +91,9 @@ def create
authorize! @person
@person.user ||= User.find_by(id: params[:user_id]) if params[:user_id].present?
@person.user ||= User.find_by(id: params.dig(:person, :user_attributes, :id)) if params.dig(:person, :user_attributes, :id).present?
if @person.user && person_params[:user_attributes].present?
@person.user.assign_attributes(person_params[:user_attributes].except(:id))
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needed if on create you select a different timezone


unless params[:skip_duplicate_check].present?
duplicates = find_duplicate_people(
Expand Down Expand Up @@ -369,7 +373,8 @@ def person_params
:city2,
:state2,
:zip2,
:notes
:notes,
:time_zone
],
affiliations_attributes: [
:id,
Expand Down
16 changes: 16 additions & 0 deletions app/views/people/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,22 @@
},
hint: "Maximum 250 characters" %></div>

<!-- Preferences: timezone for event times etc. -->
<% if f.object.user %>
<%= f.fields_for :user do |user_form| %>
<div class="space-y-6 p-3">
<%= user_form.input :time_zone,
as: :select,
collection: us_time_zone_fundamentals,
selected: user_form.object.time_zone.presence || "Pacific Time (US & Canada)",
include_blank: false,
hint: "Event times and other dates will be shown in this timezone.",
input_html: { class: "w-full" },
wrapper_html: { class: "w-full max-w-md" } %>
</div>
<% end %>
<% end %>

<div class="profile-preferences-section">
<div class="font-medium text-gray-700 mb-2 block">
Social media links:
Expand Down
38 changes: 28 additions & 10 deletions app/views/users/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,34 @@
</div>

<!-- Preferences: timezone for event times etc. -->
<div class="space-y-6 p-3">
<%= f.input :time_zone,
as: :select,
collection: us_time_zone_fundamentals,
selected: f.object.time_zone.presence || "Pacific Time (US & Canada)",
include_blank: false,
hint: "Event times and other dates will be shown in this timezone.",
input_html: { class: "w-full" },
wrapper_html: { class: "w-full max-w-md" } %>
</div>
<% if @person %>
<div class="space-y-6 p-3">
<div class="w-full max-w-md">
<label class="block text-md font-medium text-gray-700 mb-1">
Time zone
</label>
<div class="flex gap-x-2 items-center">
<div class="text-gray-900 font-medium mt-2">
<%= f.object.time_zone || "Pacific Time (US & Canada)" %>
</div>
</div>
<p class="text-sm text-gray-500 mt-3">
Edit on <%= link_to "person profile", edit_person_path(@person), class: "underline" %>
</p>
</div>
</div>
<% else %>
<div class="space-y-6 p-3">
<%= f.input :time_zone,
as: :select,
collection: us_time_zone_fundamentals,
selected: f.object.time_zone.presence || "Pacific Time (US & Canada)",
include_blank: false,
hint: "Event times and other dates will be shown in this timezone.",
input_html: { class: "w-full" },
wrapper_html: { class: "w-full max-w-md" } %>
</div>
<% end %>

<% if f.object.persisted? %>
<!-- Comments -->
Expand Down
31 changes: 31 additions & 0 deletions spec/requests/people_create_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require "rails_helper"

RSpec.describe "POST /people", type: :request do
let(:admin) { create(:user, :admin) }

before { sign_in admin }

describe "user_attributes are applied on create" do
it "updates the user's time_zone from the person form" do
user = create(:user, time_zone: "Hawaii")

post people_path, params: {
skip_duplicate_check: "1",
person: {
first_name: user.first_name,
last_name: user.last_name,
email: user.email,
created_by_id: admin.id,
updated_by_id: admin.id,
user_attributes: {
id: user.id,
time_zone: "Eastern Time (US & Canada)"
}
}
}

expect(response).to redirect_to(Person.last)
expect(user.reload.time_zone).to eq("Eastern Time (US & Canada)")
end
end
end