Skip to content
Browse files

Preparing to render rails-ified <form>

  • Loading branch information...
1 parent f79dc1d commit 2bb62c623dc49025b4762da03d53c9317ab70311 @drnic committed Oct 14, 2009
Showing with 59 additions and 17 deletions.
  1. +15 −5 lib/install_theme/parsers/rails_form.rb
  2. +44 −12 spec/parsers_rails_form_spec.rb
View
20 lib/install_theme/parsers/rails_form.rb
@@ -20,6 +20,14 @@ def valid?
model
end
+ def render
+ if valid?
+ "TODO"
+ else
+ @form_node.to_html
+ end
+ end
+
protected
def parse_form_info
if @form_node.attributes['id'] =~ /new_(.*)$/
@@ -34,10 +42,8 @@ def parse_form_info
def parse_input(input_node)
input = OpenStruct.new(:field => nil, :helper => 'text_field', :value => nil, :render => "", :node => input_node)
- if input_node.attributes['name']
- if input_node.attributes['name'] =~ /\[(.*?)\]$/
- input.field = $1
- end
+ if input_node.attributes['name'] && input_node.attributes['name'] =~ /\[(.*?)\]$/
+ input.field = $1
end
input.helper = 'text_area' if input_node.name == 'textarea'
input.value = input_node.attributes['value']
@@ -46,7 +52,7 @@ def parse_input(input_node)
end
def render_input(input)
- if valid?
+ if input.field
options_list = []
attributes = input.node.attributes.clone
attributes.delete('id')
@@ -61,4 +67,8 @@ def render_input(input)
input.node.to_html # render normal HTML if form isn't rails form
end
end
+
+ def render_header
+ "<% form_for @#{model} do |f| %>"
+ end
end
View
56 spec/parsers_rails_form_spec.rb
@@ -35,18 +35,50 @@
it { @form.model.should == 'message' }
it { @form.inputs.size.should == 3 }
it { @form.should be_valid }
- it { @form.inputs[0].field.should == "to" }
- it { @form.inputs[1].field.should == "subject" }
- it { @form.inputs[2].field.should == "body" }
- it { @form.inputs[0].helper.should == "text_field" }
- it { @form.inputs[1].helper.should == "text_field" }
- it { @form.inputs[2].helper.should == "text_area" }
- it { @form.inputs[0].value.should == "" }
- it { @form.inputs[1].value.should == nil }
- it { @form.inputs[2].value.should == nil }
- it { @form.inputs[0].render.should == "<%= f.text_field :to, :autocomplete => 'off', :class => 'autocompleter ac_input', :size => '30', :value => '' %>" }
- it { @form.inputs[1].render.should == "<%= f.text_field :subject, :size => '30' %>" }
- it { @form.inputs[2].render.should == "<%= f.text_area :body, :cols => '40', :rows => '20' %>" }
+ describe "input attributes" do
+ it { @form.inputs[0].field.should == "to" }
+ it { @form.inputs[1].field.should == "subject" }
+ it { @form.inputs[2].field.should == "body" }
+ it { @form.inputs[0].helper.should == "text_field" }
+ it { @form.inputs[1].helper.should == "text_field" }
+ it { @form.inputs[2].helper.should == "text_area" }
+ it { @form.inputs[0].value.should == "" }
+ it { @form.inputs[1].value.should == nil }
+ it { @form.inputs[2].value.should == nil }
+ end
+ describe "rendering" do
+ it { @form.send(:render_header).should == "<% form_for @message do |f| %>"}
+ it { @form.inputs[0].render.should == "<%= f.text_field :to, :autocomplete => 'off', :class => 'autocompleter ac_input', :size => '30', :value => '' %>" }
+ it { @form.inputs[1].render.should == "<%= f.text_field :subject, :size => '30' %>" }
+ it { @form.inputs[2].render.should == "<%= f.text_area :body, :cols => '40', :rows => '20' %>" }
+ end
+ it do
+ expected = <<-HTML
+ <h1>Compose message</h1>
+ <% form_for @message do |f| %>
+ <div class="buttons buttons-top">
+ <input type="submit" value="Send" name="commit" id="message_submit" class="send"/>
+ <a class="cancel" href="#">Cancel</a>
+ </div>
+ <div class="field">
+ <label for="message_to">To:</label>
+ <%= f.text_field :to, :autocomplete => 'off', :class => 'autocompleter ac_input', :size => '30', :value => '' %>
+ </div>
+ <div class="field">
+ <label for="message_subject">Subject:</label>
+ <%= f.text_field :subject, :size => '30' %>
+ </div>
+ <%= f.text_area :body, :cols => '40', :rows => '20' %>
+ <div class="submits">
+ <div class="buttons buttons-bottom">
+ <input type="submit" value="Send" name="commit" id="message_submit" class="send"/>
+ <a class="cancel" href="#">Cancel</a>
+ </div>
+ </div>
+ </form>
+ HTML
+ @form.render.should == expected
+ end
end
end
end

0 comments on commit 2bb62c6

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