|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# What the Ractor blocks below reach through constants (a Ractor block must be |
| 4 | +# capture-free). A Struct is the model: an ActiveModel class keeps its |
| 5 | +# validators in class state of its own. |
| 6 | +module RactorCanary |
| 7 | + User = Struct.new(:email, :password, :admin, :bio, keyword_init: true) |
| 8 | + |
| 9 | + SignupForm = Class.new(Forms::Base) do |
| 10 | + form_options :spaced, url: "/signup", data: { controller: "signup" }, field_variants: [:primary] |
| 11 | + end |
| 12 | + |
| 13 | + JoinForm = Class.new(SignupForm) { form_options url: "/join" } |
| 14 | + |
| 15 | + if defined?(Phlex::Reactive) |
| 16 | + LiveForm = Class.new(Forms::Base) do |
| 17 | + live model: User, scope: :user, debounce: 150 |
| 18 | + live_permit :email, :bio |
| 19 | + live_deny :bio |
| 20 | + live_tags :tags, suggestions: %w[Ruby Rails] |
| 21 | + end |
| 22 | + end |
| 23 | +end |
| 24 | + |
| 25 | +# Tier 3 of conformance-kit, on Ruby 4.0 (skipped with the reason below it): |
| 26 | +# with the configuration finalized, field inference and the class-level form |
| 27 | +# settings run inside a non-main Ractor and agree with the main Ractor. |
| 28 | +RSpec.describe "phlex-forms inside a non-main Ractor" do |
| 29 | + before { PhlexForms.finalize! } |
| 30 | + |
| 31 | + context "when resolving a field's options" do |
| 32 | + let(:subject_call) do |
| 33 | + lambda do |name| |
| 34 | + user = RactorCanary::User.new(email: "ada@example.com", admin: true) |
| 35 | + field = Forms::Field.new(name:, model: user, scope: "user", errors: nil, form: nil) |
| 36 | + inferred = PhlexForms::Inference.resolve(model: user, name:) |
| 37 | + [field.field_name, field.field_id, field.field_value, field.field_label, field.required?, |
| 38 | + field.input.class, inferred.as, inferred.attributes] |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + it_behaves_like "runs inside a non-main Ractor", inputs: [:email] |
| 43 | + it_behaves_like "runs inside a non-main Ractor", inputs: [:admin] |
| 44 | + end |
| 45 | + |
| 46 | + context "when resolving a theme and an icon" do |
| 47 | + let(:subject_call) do |
| 48 | + lambda do |
| 49 | + [PhlexForms::Theme.resolve(nil)[:input], PhlexForms::Theme.resolve(:plain)[:select], |
| 50 | + PhlexForms.config.render_icon("chevron-down")] |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + it_behaves_like "runs inside a non-main Ractor" |
| 55 | + end |
| 56 | + |
| 57 | + context "when reading form_options" do |
| 58 | + let(:subject_call) do |
| 59 | + lambda do |
| 60 | + [RactorCanary::SignupForm.form_modifiers, RactorCanary::SignupForm.form_defaults, |
| 61 | + RactorCanary::JoinForm.form_defaults] |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + it_behaves_like "runs inside a non-main Ractor" |
| 66 | + end |
| 67 | + |
| 68 | + if defined?(Phlex::Reactive) |
| 69 | + context "when reading a live form's settings" do |
| 70 | + let(:subject_call) do |
| 71 | + lambda do |
| 72 | + form = RactorCanary::LiveForm |
| 73 | + [form.live_model_class, form.live_scope, form.live_debounce, form.live_tags_declaration, |
| 74 | + form.live_permitted_attributes(RactorCanary::User.new)] |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + it_behaves_like "runs inside a non-main Ractor" |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + context "when rendering a field", |
| 83 | + pending: "Phlex reads its non-shareable Phlex::ATTRIBUTE_CACHE constant on every element " \ |
| 84 | + "with attributes; a per-Ractor cache is pending upstream (yippee-fun/phlex#1055)" do |
| 85 | + let(:subject_call) do |
| 86 | + lambda do |
| 87 | + user = RactorCanary::User.new(email: "ada@example.com") |
| 88 | + form = Forms::Form.new(model: user, scope: "user", url: "/users", theme: :plain) |
| 89 | + form.field_object(:email).input.call |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + it_behaves_like "runs inside a non-main Ractor" |
| 94 | + end |
| 95 | +end |
0 commit comments