-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ create operation to update user profile
- Loading branch information
1 parent
7a7228f
commit dd6a662
Showing
3 changed files
with
52 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
module UserProfiles | ||
class Update < Actor | ||
input :id, type: String | ||
input :attributes, type: Hash | ||
|
||
output :profile, type: UserProfile | ||
|
||
def call | ||
self.profile = UserProfile.find(id) | ||
|
||
fail!(error: :invalid_record) unless profile.update(attributes) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe UserProfiles::Update, type: :operation do | ||
context "with valid attributes" do | ||
it "updates user profile" do | ||
profile = create(:user_profile, display_name: "Old display name") | ||
|
||
result = described_class.result(id: profile.id, attributes: { display_name: "New display name" }) | ||
|
||
expect(result).to be_success | ||
expect(profile.reload.display_name).to eq "New display name" | ||
end | ||
end | ||
|
||
context "with invalid attributes" do | ||
it "returns invalid user profile" do | ||
profile = create(:user_profile, display_name: "Old display name") | ||
|
||
result = described_class.result(id: profile.id, attributes: { display_name: "" }) | ||
|
||
expect(result).to be_failure | ||
expect(result.profile).to be_invalid | ||
end | ||
end | ||
|
||
context "when user profile with given id doesn't exist" do | ||
it "raises ActiveRecord::RecordNotFound error" do | ||
expect do | ||
described_class.result(id: "not-found-id", attributes: { display_name: "New display name" }) | ||
end.to raise_error(ActiveRecord::RecordNotFound) | ||
end | ||
end | ||
end |