From 2695a399020a0b94a12413bfcd2b79ac4abf718f Mon Sep 17 00:00:00 2001 From: Marco van Kampen Date: Wed, 9 Apr 2025 12:14:16 +0200 Subject: [PATCH] Allow Rails::Form root namespace to be given a custom key This adds flexibility in how your (customized) form can be reused with different models. [#52] --- lib/superform/rails.rb | 4 ++-- spec/superform/rails/form_spec.rb | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 spec/superform/rails/form_spec.rb diff --git a/lib/superform/rails.rb b/lib/superform/rails.rb index 77507cc..ba0d390 100644 --- a/lib/superform/rails.rb +++ b/lib/superform/rails.rb @@ -74,12 +74,12 @@ def title end end - def initialize(model, action: nil, method: nil, **attributes) + def initialize(model, action: nil, method: nil, namespace: nil, **attributes) @model = model @action = action @method = method @attributes = attributes - @namespace = Namespace.root(key, object: model, field_class: self.class::Field) + @namespace = Namespace.root(namespace || key, object: model, field_class: self.class::Field) end def around_template(&) diff --git a/spec/superform/rails/form_spec.rb b/spec/superform/rails/form_spec.rb new file mode 100644 index 0000000..2bb267c --- /dev/null +++ b/spec/superform/rails/form_spec.rb @@ -0,0 +1,17 @@ +RSpec.describe Superform::Rails::Form do + let(:model) { double('MockModel', model_name: double(param_key: 'mock_model')) } + + it 'creates a root namespace based on the active model' do + expect(Superform::Namespace).to receive(:root).with('mock_model', any_args) + + described_class.new(model) + end + + context 'override root namespace with an argument' do + it 'creates a root namespace based on the namespace argument' do + expect(Superform::Namespace).to receive(:root).with('user', any_args) + + described_class.new(model, namespace: 'user') + end + end +end