Skip to content

Commit fe6e8d2

Browse files
author
Marco van Kampen
committed
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]
1 parent 122ca32 commit fe6e8d2

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/superform/rails.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ def title
7474
end
7575
end
7676

77-
def initialize(model, action: nil, method: nil, **attributes)
77+
def initialize(model, action: nil, method: nil, namespace: nil, **attributes)
7878
@model = model
7979
@action = action
8080
@method = method
8181
@attributes = attributes
82-
@namespace = Namespace.root(key, object: model, field_class: self.class::Field)
82+
@namespace = Namespace.root(namespace || key, object: model, field_class: self.class::Field)
8383
end
8484

8585
def around_template(&)

spec/superform/rails/form_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
RSpec.describe Superform::Rails::Form do
2+
let(:model) { double('MockModel', model_name: double(param_key: 'mock_model')) }
3+
4+
it 'creates a root namespace based on the active model' do
5+
expect(Superform::Namespace).to receive(:root).with('mock_model', any_args)
6+
7+
described_class.new(model)
8+
end
9+
10+
context 'override root namespace with an argument' do
11+
it 'creates a root namespace based on the active model' do
12+
expect(Superform::Namespace).to receive(:root).with('user', any_args)
13+
14+
described_class.new(model, namespace: 'user')
15+
end
16+
end
17+
end

0 commit comments

Comments
 (0)