discourse/documentation/assets/blobs.svg

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

7 lines
1.6 KiB
XML
Raw Normal View History

DEV: Chat service object initial implementation (#19814) This is a combined work of Martin Brennan, Loïc Guitaut, and Joffrey Jaffeux. --- This commit implements a base service object when working in chat. The documentation is available at https://discourse.github.io/discourse/chat/backend/Chat/Service.html Generating documentation has been made as part of this commit with a bigger goal in mind of generally making it easier to dive into the chat project. Working with services generally involves 3 parts: - The service object itself, which is a series of steps where few of them are specialized (model, transaction, policy) ```ruby class UpdateAge include Chat::Service::Base model :user, :fetch_user policy :can_see_user contract step :update_age class Contract attribute :age, :integer end def fetch_user(user_id:, **) User.find_by(id: user_id) end def can_see_user(guardian:, **) guardian.can_see_user(user) end def update_age(age:, **) user.update!(age: age) end end ``` - The `with_service` controller helper, handling success and failure of the service within a service and making easy to return proper response to it from the controller ```ruby def update with_service(UpdateAge) do on_success { render_serialized(result.user, BasicUserSerializer, root: "user") } end end ``` - Rspec matchers and steps inspector, improving the dev experience while creating specs for a service ```ruby RSpec.describe(UpdateAge) do subject(:result) do described_class.call(guardian: guardian, user_id: user.id, age: age) end fab!(:user) { Fabricate(:user) } fab!(:current_user) { Fabricate(:admin) } let(:guardian) { Guardian.new(current_user) } let(:age) { 1 } it { expect(user.reload.age).to eq(age) } end ``` Note in case of unexpected failure in your spec, the output will give all the relevant information: ``` 1) UpdateAge when no channel_id is given is expected to fail to find a model named 'user' Failure/Error: it { is_expected.to fail_to_find_a_model(:user) } Expected model 'foo' (key: 'result.model.user') was not found in the result object. [1/4] [model] 'user' ❌ [2/4] [policy] 'can_see_user' [3/4] [contract] 'default' [4/4] [step] 'update_age' /Users/joffreyjaffeux/Code/pr-discourse/plugins/chat/app/services/update_age.rb:32:in `fetch_user': missing keyword: :user_id (ArgumentError) from /Users/joffreyjaffeux/Code/pr-discourse/plugins/chat/app/services/base.rb:202:in `instance_exec' from /Users/joffreyjaffeux/Code/pr-discourse/plugins/chat/app/services/base.rb:202:in `call' from /Users/joffreyjaffeux/Code/pr-discourse/plugins/chat/app/services/base.rb:219:in `call' from /Users/joffreyjaffeux/Code/pr-discourse/plugins/chat/app/services/base.rb:417:in `block in run!' from /Users/joffreyjaffeux/Code/pr-discourse/plugins/chat/app/services/base.rb:417:in `each' from /Users/joffreyjaffeux/Code/pr-discourse/plugins/chat/app/services/base.rb:417:in `run!' from /Users/joffreyjaffeux/Code/pr-discourse/plugins/chat/app/services/base.rb:411:in `run' from <internal:kernel>:90:in `tap' from /Users/joffreyjaffeux/Code/pr-discourse/plugins/chat/app/services/base.rb:302:in `call' from /Users/joffreyjaffeux/Code/pr-discourse/plugins/chat/spec/services/update_age_spec.rb:15:in `block (3 levels) in <main>' ```
2023-02-13 07:09:57 -05:00
<svg width="1867" height="475" viewBox="0 0 1867 475" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M1865.6 220.428C1856.87 332.048 1771.2 390.61 1665.99 404.379C1563.03 417.858 1471.85 319.945 1480.58 208.325C1489.3 96.7051 1591.56 4.34115 1697.59 0.168844C1828.53 -4.98699 1874.33 108.807 1865.6 220.428Z" fill="#FBF5AF"/>
<path d="M1464.63 416.562C1464.23 416.562 1463.83 416.594 1463.42 416.659C1458.84 417.393 1454.7 421.929 1455.71 428.253C1456.72 434.575 1462.62 438.145 1467.25 437.407C1469.45 437.053 1471.1 435.907 1472.15 433.993C1473.3 431.918 1473.65 429.03 1473.14 425.867C1472.64 422.761 1471.34 420.134 1469.46 418.463C1468.05 417.209 1466.4 416.562 1464.63 416.562ZM1465.91 444.071C1457.94 444.071 1450.59 437.774 1449.23 429.29C1447.75 419.993 1453.52 411.598 1462.38 410.179C1466.57 409.514 1470.62 410.714 1473.82 413.561C1476.83 416.233 1478.89 420.237 1479.62 424.827C1480.37 429.495 1479.77 433.758 1477.9 437.154C1475.88 440.829 1472.47 443.217 1468.29 443.883C1467.49 444.011 1466.7 444.071 1465.91 444.071" fill="black"/>
<path fill-rule="evenodd" clip-rule="evenodd" d="M344 323.966C344 353.644 324.906 366.211 293.64 366.211C262.374 366.211 223 321.874 223 292.194C223 262.514 272.175 254 303.441 254C334.707 254 344 294.286 344 323.966Z" fill="#0CA64E"/>
<path fill-rule="evenodd" clip-rule="evenodd" d="M396.325 444.694C396.653 458.473 386.913 467.649 370.18 473.648C356.257 478.638 342.267 465.392 336.512 453.65C330.052 440.465 344.903 425.749 369.376 421.378C383.936 418.778 395.973 429.909 396.325 444.694Z" fill="#E84A51"/>
</svg>