Skip to content

Commit 166f794

Browse files
committed
Option to disable automatic decoration in forms
Ref: activeadmin#2057
1 parent f9d92ae commit 166f794

File tree

3 files changed

+51
-30
lines changed

3 files changed

+51
-30
lines changed

docs/11-decorators.md

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -53,32 +53,14 @@ Then the following is possible
5353

5454
Note that the resource proveded to form_for also gets decorated.
5555

56-
In most cases this will work as expected. However, it can have some unexpected
57-
results. Here's an example gotcha:
58-
59-
```ruby
60-
class UserDecorator < Draper::Base
61-
decorates :user
62-
63-
def self.status_options_for_select
64-
User.status_options.map { |s| [s.humanize, s] }
65-
end
66-
67-
def status
68-
model.status.titleize
69-
end
70-
end
71-
72-
ActiveAdmin.register User do
73-
form do
74-
f.inputs do
75-
f.input :status, collection: UserDecorator.status_options_for_select
76-
end
77-
end
78-
end
79-
```
56+
In most cases this will work as expected. However, it is possible to disable
57+
automatic decoration in the form with the `decorate` option:
58+
59+
ActiveAdmin.register Post do
60+
decorate_with PostDecorator
8061

81-
In this example, `f.object.status` now returns "Submitted", which does not match
82-
the actual status option: "submitted". Because of this, the `<select>` will not
83-
have the correct status selected.
62+
form decorate: false do
63+
# ...
64+
end
65+
end
8466

lib/active_admin/view_helpers/form_helper.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
module ActiveAdmin
22
module ViewHelpers
33
module FormHelper
4-
4+
55
def active_admin_form_for(resource, options = {}, &block)
66
options = Marshal.load( Marshal.dump(options) )
77
options[:builder] ||= ActiveAdmin::FormBuilder
8+
9+
if ! options.fetch(:decorate, true)
10+
resource = resource.model
11+
end
12+
813
semantic_form_for resource, options, &block
914
end
1015

spec/unit/view_helpers/form_helper_spec.rb

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,45 @@
11
require 'spec_helper'
22

33
describe ActiveAdmin::ViewHelpers::FormHelper do
4+
5+
describe '.active_admin_form_for' do
6+
let(:view) { action_view }
7+
let(:resource) { stub('resource') }
8+
let(:default_options) { { builder: ActiveAdmin::FormBuilder } }
9+
10+
it 'calls semantic_form_for with the ActiveAdmin form builder' do
11+
view.should_receive(:semantic_form_for).with(resource, builder: ActiveAdmin::FormBuilder)
12+
view.active_admin_form_for(resource)
13+
end
14+
15+
it 'allows the form builder to be customized' do
16+
# We can't use a stub here because options gets marshalled, and a new
17+
# instance built. Any constant will work.
18+
custom_builder = Object
19+
view.should_receive(:semantic_form_for).with(resource, builder: custom_builder)
20+
view.active_admin_form_for(resource, builder: custom_builder)
21+
end
22+
23+
context 'with a decorated resource' do
24+
let(:decorated) { stub('decorated_resource', model: resource) }
25+
26+
it 'can disable automatic decoration' do
27+
view.should_receive(:semantic_form_for).with(resource, default_options.merge(decorate: false))
28+
view.active_admin_form_for(decorated, decorate: false)
29+
end
30+
31+
it 'can enable automatic decoration' do
32+
view.should_receive(:semantic_form_for).with(decorated, default_options.merge(decorate: true))
33+
view.active_admin_form_for(decorated, decorate: true)
34+
end
35+
end
36+
end
37+
438
describe ".hidden_field_tags_for" do
539
let(:view) { action_view }
640

741
it "should render hidden field tags for params" do
8-
view.hidden_field_tags_for(:scope => "All", :filter => "None").should ==
42+
view.hidden_field_tags_for(:scope => "All", :filter => "None").should ==
943
%{<input id="hidden_active_admin_scope" name="scope" type="hidden" value="All" />\n<input id="hidden_active_admin_filter" name="filter" type="hidden" value="None" />}
1044
end
1145

@@ -14,7 +48,7 @@
1448
end
1549

1650
it "should filter out the field passed via the option :except" do
17-
view.hidden_field_tags_for({:scope => "All", :filter => "None"}, :except => :filter).should ==
51+
view.hidden_field_tags_for({:scope => "All", :filter => "None"}, :except => :filter).should ==
1852
%{<input id="hidden_active_admin_scope" name="scope" type="hidden" value="All" />}
1953
end
2054
end

0 commit comments

Comments
 (0)