Skip to content

Commit

Permalink
Buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
jferris committed Dec 11, 2010
1 parent 6c310e4 commit 3fdace4
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
6 changes: 4 additions & 2 deletions features/semiformal.feature
Expand Up @@ -33,14 +33,16 @@ Feature: generate an application and run rake
<%= form.inputs do -%>
<%= form.input :title %>
<% end -%>
<input type="submit" value="Create" />
<%= form.buttons do -%>
<%= form.commit_button %>
<% end -%>
<% end -%>
"""
When I route the "posts" resource
When I successfully run "rake db:migrate db:test:prepare"
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"

10 changes: 9 additions & 1 deletion lib/semiformal/form.rb
Expand Up @@ -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
Expand All @@ -44,6 +48,10 @@ def html_class
def html_action
controller.url_for(url)
end

def name
target.class.model_name
end
end
end

10 changes: 10 additions & 0 deletions lib/semiformal/renderer.rb
Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions spec/semiformal/form_spec.rb
Expand Up @@ -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 }

Expand Down
14 changes: 13 additions & 1 deletion spec/semiformal/renderer_spec.rb
Expand Up @@ -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 { "<li>inner</li>".html_safe }

rendered.should have_css("fieldset.inputs ol li:contains('inner')")
Expand All @@ -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 { "<li>inner</li>".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

0 comments on commit 3fdace4

Please sign in to comment.