Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add support for f.actions / f.action in forms. ( issue #1085 )

- add new form helpers
- add css styling (thanks @Daxter )
- add new specs / rename a few existing specs

Fixes the deprecation warnings in mentioned in issue #1085 except for when running specs.
  • Loading branch information...
commit afbdf6ba9cbc66984b58b93a90619067bdb1af0d 1 parent 1ce193d
Alli authored
4 app/assets/stylesheets/active_admin/_forms.css.scss
@@ -165,12 +165,12 @@ form {
165 165
166 166 }
167 167
168   - .buttons {
  168 + .buttons, .actions {
169 169 margin-top: 15px;
170 170 input[type=submit] { margin-right: 10px; }
171 171 }
172 172
173   - fieldset.buttons li {
  173 + fieldset.buttons li, fieldset.actions li {
174 174 float:left;
175 175 padding: 0;
176 176
4 app/views/active_admin/devise/passwords/new.html.erb
@@ -5,8 +5,8 @@
5 5 f.inputs do
6 6 f.input :email
7 7 end
8   - f.buttons do
9   - f.commit_button "Reset My Password"
  8 + f.actions do
  9 + f.action :submit, :label => "Reset My Password", :button_html => { :value => "Reset My Password" }
10 10 end
11 11 end %>
12 12
4 app/views/active_admin/devise/sessions/new.html.erb
@@ -8,8 +8,8 @@
8 8 f.input :password
9 9 f.input :remember_me, :as => :boolean, :if => false #devise_mapping.rememberable? }
10 10 end
11   - f.buttons do
12   - f.commit_button "Login"
  11 + f.actions do
  12 + f.action :submit, :label => "Login", :button_html => { :value => "Login" }
13 13 end
14 14 end
15 15 %>
4 lib/active_admin/comments/views/active_admin_comments.rb
@@ -70,8 +70,8 @@ def build_comment_form
70 70 form.input :resource_id, :value => @record.id, :as => :hidden
71 71 form.input :body, :input_html => {:size => "80x8"}, :label => false
72 72 end
73   - form.buttons do
74   - form.commit_button I18n.t('active_admin.comments.add')
  73 + form.actions do
  74 + form.action :submit, :label => I18n.t('active_admin.comments.add'), :button_html => { :value => I18n.t('active_admin.comments.add') }
75 75 end
76 76 end
77 77 end
17 lib/active_admin/form_builder.rb
@@ -48,6 +48,23 @@ def commit_button_with_cancel_link
48 48 content << cancel_link
49 49 end
50 50
  51 + def actions(*args, &block)
  52 + content = with_new_form_buffer do
  53 + block_given? ? super : super { commit_action_with_cancel_link }
  54 + end
  55 + form_buffers.last << content.html_safe
  56 + end
  57 +
  58 + def action(*args)
  59 + content = with_new_form_buffer { super }
  60 + form_buffers.last << content.html_safe
  61 + end
  62 +
  63 + def commit_action_with_cancel_link
  64 + content = action(:submit)
  65 + content << cancel_link
  66 + end
  67 +
51 68 def has_many(association, options = {}, &block)
52 69 options = { :for => association }.merge(options)
53 70 options[:class] ||= ""
2  lib/active_admin/views/pages/form.rb
@@ -41,7 +41,7 @@ def default_form_path
41 41 def default_form_config
42 42 ActiveAdmin::PagePresenter.new do |f|
43 43 f.inputs
44   - f.buttons
  44 + f.actions
45 45 end
46 46 end
47 47 end
78 spec/unit/form_builder_spec.rb
@@ -36,7 +36,7 @@ def build_form(options = {}, &block)
36 36 active_admin_form_for Post.new, options, &block
37 37 end
38 38
39   - context "in general" do
  39 + context "in general with buttons" do
40 40 let :body do
41 41 build_form do |f|
42 42 f.inputs do
@@ -50,6 +50,38 @@ def build_form(options = {}, &block)
50 50 end
51 51 end
52 52
  53 + it "should generate a text input" do
  54 + body.should have_tag("input", :attributes => { :type => "text",
  55 + :name => "post[title]" })
  56 + end
  57 + it "should generate a textarea" do
  58 + body.should have_tag("textarea", :attributes => { :name => "post[body]" })
  59 + end
  60 + it "should only generate the form once" do
  61 + body.scan(/Title/).size.should == 1
  62 + end
  63 + it "should generate buttons" do
  64 + body.should have_tag("input", :attributes => { :type => "submit",
  65 + :value => "Submit Me" })
  66 + body.should have_tag("input", :attributes => { :type => "submit",
  67 + :value => "Another Button" })
  68 + end
  69 + end
  70 +
  71 + context "in general with actions" do
  72 + let :body do
  73 + build_form do |f|
  74 + f.inputs do
  75 + f.input :title
  76 + f.input :body
  77 + end
  78 + f.actions do
  79 + f.action :submit, :button_html => { :value => "Submit Me" }
  80 + f.action :submit, :button_html => { :value => "Another Button" }
  81 + end
  82 + end
  83 + end
  84 +
53 85 it "should generate a text input" do
54 86 body.should have_tag("input", :attributes => { :type => "text",
55 87 :name => "post[title]" })
@@ -79,7 +111,7 @@ def build_form(options = {}, &block)
79 111 end
80 112 end
81 113
82   - describe "passing in options" do
  114 + describe "passing in options with buttons" do
83 115 let :body do
84 116 build_form :html => { :multipart => true } do |f|
85 117 f.inputs :title
@@ -91,6 +123,19 @@ def build_form(options = {}, &block)
91 123 end
92 124 end
93 125
  126 + describe "passing in options with actions" do
  127 + let :body do
  128 + build_form :html => { :multipart => true } do |f|
  129 + f.inputs :title
  130 + f.actions
  131 + end
  132 + end
  133 + it "should pass the options on to the form" do
  134 + body.should have_tag("form", :attributes => { :enctype => "multipart/form-data" })
  135 + end
  136 + end
  137 +
  138 +
94 139 context "with buttons" do
95 140 it "should generate the form once" do
96 141 body = build_form do |f|
@@ -121,6 +166,35 @@ def build_form(options = {}, &block)
121 166
122 167 end
123 168
  169 + context "with actons" do
  170 + it "should generate the form once" do
  171 + body = build_form do |f|
  172 + f.inputs do
  173 + f.input :title
  174 + end
  175 + f.actions
  176 + end
  177 + body.scan(/id=\"post_title\"/).size.should == 1
  178 + end
  179 + it "should generate one button and a cancel link" do
  180 + body = build_form do |f|
  181 + f.actions
  182 + end
  183 + body.scan(/type=\"submit\"/).size.should == 1
  184 + body.scan(/class=\"cancel\"/).size.should == 1
  185 + end
  186 + it "should generate multiple actions" do
  187 + body = build_form do |f|
  188 + f.actions do
  189 + f.action :submit, :label => "Create & Continue"
  190 + f.action :submit, :label => "Create & Edit"
  191 + end
  192 + end
  193 + body.scan(/type=\"submit\"/).size.should == 2
  194 + body.scan(/class=\"cancel\"/).size.should == 0
  195 + end
  196 + end
  197 +
124 198 context "without passing a block to inputs" do
125 199 let :body do
126 200 build_form do |f|

0 comments on commit afbdf6b

Sean Linsley

Shouldn't you also rename line 160 to be /* Buttons & Actions */?

Alli

Maybe. They still look like form buttons - despite the code calling them actions. Not sure Actions will make as much sense once buttons are gone.

Please sign in to comment.
Something went wrong with that request. Please try again.