From 3fdace4e49b0eb20976d47e0a6542d841a3b8d11 Mon Sep 17 00:00:00 2001 From: Joe Ferris Date: Sat, 11 Dec 2010 13:59:52 -0500 Subject: [PATCH] Buttons --- features/semiformal.feature | 6 ++++-- lib/semiformal/form.rb | 10 +++++++++- lib/semiformal/renderer.rb | 10 ++++++++++ spec/semiformal/form_spec.rb | 4 ++++ spec/semiformal/renderer_spec.rb | 14 +++++++++++++- 5 files changed, 40 insertions(+), 4 deletions(-) diff --git a/features/semiformal.feature b/features/semiformal.feature index f587b6a..e12dd72 100644 --- a/features/semiformal.feature +++ b/features/semiformal.feature @@ -33,7 +33,9 @@ Feature: generate an application and run rake <%= form.inputs do -%> <%= form.input :title %> <% end -%> - + <%= form.buttons do -%> + <%= form.commit_button %> + <% end -%> <% end -%> """ When I route the "posts" resource @@ -41,6 +43,6 @@ Feature: generate an application and run rake And I start the application And I visit /posts/new And I fill in "Title" with "example" - And I press "Create" + And I press "Create Post" Then I should see "example" diff --git a/lib/semiformal/form.rb b/lib/semiformal/form.rb index e040ddc..d8cfdcb 100644 --- a/lib/semiformal/form.rb +++ b/lib/semiformal/form.rb @@ -32,7 +32,11 @@ def html_id end def param_name - target.class.model_name.singular + name.singular + end + + def commit_button_value + "Create #{name.human}" end private @@ -44,6 +48,10 @@ def html_class def html_action controller.url_for(url) end + + def name + target.class.model_name + end end end diff --git a/lib/semiformal/renderer.rb b/lib/semiformal/renderer.rb index 8279e4c..8cc4a57 100644 --- a/lib/semiformal/renderer.rb +++ b/lib/semiformal/renderer.rb @@ -21,6 +21,12 @@ def inputs(&block) content_tag(:fieldset, fields, :class => 'inputs') end + def buttons(&block) + contents = capture(&block) + buttons = content_tag(:ol, contents) + content_tag(:fieldset, buttons, :class => 'buttons') + end + def input(name) attribute = form.attribute(name) html_id = attribute.html_id @@ -31,6 +37,10 @@ def input(name) content_tag(:li, label + input) end + def commit_button + tag(:input, :type => 'submit', :name => 'commit', :value => form.commit_button_value) + end + private def capture(*args, &block) diff --git a/spec/semiformal/form_spec.rb b/spec/semiformal/form_spec.rb index 5166823..6e7d96a 100644 --- a/spec/semiformal/form_spec.rb +++ b/spec/semiformal/form_spec.rb @@ -23,6 +23,10 @@ subject.param_name.should == 'model' end + it "has a commit button value" do + subject.commit_button_value.should == "Create Model" + end + context "default attributes" do subject { form.default_attributes } diff --git a/spec/semiformal/renderer_spec.rb b/spec/semiformal/renderer_spec.rb index 5a6d03e..9b0ac60 100644 --- a/spec/semiformal/renderer_spec.rb +++ b/spec/semiformal/renderer_spec.rb @@ -27,7 +27,7 @@ def capture(*args, &block) rendered.should have_css("span:contains('inner')") end - it "generates a fieldset" do + it "generates a list of inputs" do rendered = subject.inputs { "
  • inner
  • ".html_safe } rendered.should have_css("fieldset.inputs ol li:contains('inner')") @@ -39,5 +39,17 @@ def capture(*args, &block) rendered.should have_css("li input[type=text][name='model[title]'][id=model_title]") rendered.should have_css("li label[for=model_title]:contains('Title')") end + + it "generates a list of buttons" do + rendered = subject.buttons { "
  • inner
  • ".html_safe } + + rendered.should have_css("fieldset.buttons ol li:contains('inner')") + end + + it "generates a commit button" do + rendered = subject.commit_button + + rendered.should have_css("input[type=submit][name=commit][value='Create Model']") + end end