david-former 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 0
3
+ :patch: 1
4
+ :major: 0
data/lib/former.rb ADDED
@@ -0,0 +1,41 @@
1
+ gem 'actionpack'; require 'action_view'
2
+
3
+ class StandardFormBuilder < ActionView::Helpers::FormBuilder
4
+ %w(text_field text_area file_field).each do |field_type|
5
+ self.class_eval <<-RUBY
6
+ def #{field_type}(name, options = {})
7
+ str = ""
8
+
9
+ if options[:label]
10
+ str << label(name, options.delete(:label))
11
+ elsif !options.has_key?(:label)
12
+ str << label(name)
13
+ else
14
+ options.delete(:label)
15
+ end
16
+
17
+ str << super(name, options)
18
+ end
19
+ RUBY
20
+ end
21
+
22
+ def radio_button_group(name, values, *args)
23
+ options = Hash === args.last ? args.pop : {}
24
+
25
+ result = values.inject("") do |html, value|
26
+ label = label("#{name}_#{value}", value)
27
+ rb = radio_button(name, value)
28
+ unless options[:label_position] == :after
29
+ content = "#{label} #{rb}"
30
+ else
31
+ content = "#{rb} #{label}"
32
+ end
33
+
34
+ html << "<li>#{content}</li>"
35
+ end
36
+
37
+ "<ul>#{result}</ul>"
38
+ end
39
+ end
40
+
41
+ ActionView::Base.default_form_builder = StandardFormBuilder
@@ -0,0 +1,56 @@
1
+ require File.join(File.dirname(__FILE__), %w(spec_helper))
2
+
3
+ describe StandardFormBuilder do
4
+ before do
5
+ @template = mock("template")
6
+ @object = mock("model")
7
+ @fb = StandardFormBuilder.new(:model, @object, @template, {}, nil)
8
+ end
9
+
10
+ %w(text_field text_area file_field).each do |field_type|
11
+ describe "#{field_type}" do
12
+ before do
13
+ @template.should_receive(field_type.to_sym).with(:model, :name, :object => @object).and_return("#{field_type}")
14
+ end
15
+
16
+ it "should display a default label" do
17
+ @template.should_receive(:label).with(:model, :name, nil, :object => @object).and_return("label ")
18
+
19
+ @fb.send(field_type.to_sym, :name).should == "label #{field_type}"
20
+ end
21
+
22
+ it "should display the given label" do
23
+ @template.should_receive(:label).with(:model, :name, "myLabel", :object => @object).and_return("label ")
24
+
25
+ @fb.send(field_type.to_sym, :name, :label => "myLabel").should == "label #{field_type}"
26
+ end
27
+
28
+ it "should display no label" do
29
+ @template.should_not_receive(:label)
30
+
31
+ @fb.send(field_type.to_sym, :name, :label => nil).should == "#{field_type}"
32
+ end
33
+ end
34
+ end
35
+
36
+ describe "#radio_button_group" do
37
+ it "should display all the radio buttons, labels first" do
38
+ %w(one two).each do |value|
39
+ @template.should_receive(:label).with(:model, "whoa_#{value}", value, :object => @object).and_return("label whoa_#{value}")
40
+ @template.should_receive(:radio_button).with(:model, "whoa", value, :object => @object).and_return("rb #{value}")
41
+ end
42
+
43
+ @fb.radio_button_group("whoa", %w(one two)).should == "<ul><li>label whoa_one rb one</li><li>label whoa_two rb two</li></ul>"
44
+ end
45
+
46
+ it "should display all the radio buttons, labels after" do
47
+ %w(one two).each do |value|
48
+ @template.should_receive(:radio_button).with(:model, "whoa", value, :object => @object).and_return("rb #{value}")
49
+ @template.should_receive(:label).with(:model, "whoa_#{value}", value, :object => @object).and_return("label whoa_#{value}")
50
+ end
51
+
52
+ @fb.radio_button_group("whoa", %w(one two), :label_position => :after).
53
+ should == "<ul><li>rb one label whoa_one</li><li>rb two label whoa_two</li></ul>"
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec'
2
+
3
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), %w(.. lib)))
4
+
5
+ require 'former'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: david-former
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - David Leal
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-17 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Rails form builder
17
+ email: dgleal@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - VERSION.yml
26
+ - lib/former.rb
27
+ - spec/former_spec.rb
28
+ - spec/spec_helper.rb
29
+ has_rdoc: true
30
+ homepage: http://github.com/david/former
31
+ post_install_message:
32
+ rdoc_options:
33
+ - --inline-source
34
+ - --charset=UTF-8
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ version:
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ requirements: []
50
+
51
+ rubyforge_project:
52
+ rubygems_version: 1.2.0
53
+ signing_key:
54
+ specification_version: 2
55
+ summary: TODO
56
+ test_files: []
57
+