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