Skip to content

Commit

Permalink
✨ create operation to update user profile
Browse files Browse the repository at this point in the history
  • Loading branch information
karinevieira committed May 1, 2024
1 parent 7a7228f commit dd6a662
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
16 changes: 16 additions & 0 deletions app/operations/user_profiles/update.rb
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
2 changes: 1 addition & 1 deletion spec/factories/users_factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
password { "password" }

transient do
sequence(:username) { |n| "janedoe#{n}" }
sequence(:username) { |n| "test#{n}" }
end

profile_attributes do
Expand Down
35 changes: 35 additions & 0 deletions spec/operations/user_profiles/update_spec.rb
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

0 comments on commit dd6a662

Please sign in to comment.