table_form_builder 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,18 @@
1
+ # TableFormBuilder
2
+
3
+ Sometimes it's not the end of the world to use an HTML table to build form views. Take for example http://www.twitter.com/signup.
4
+
5
+ Using table_form_for is just like form_for with the addition of the title and actions methods. Field labels are generated automatically. Validation errors on fields are displayed inline. Validation errors on models are displayed before any fields.
6
+
7
+ <% table_form_for @widget do |f| %>
8
+ <%= f.title "Widget Properties" %>
9
+ <%= f.text_field :name %>
10
+ <%= f.text_field :description %>
11
+ <%= f.check_box :enabled %>
12
+ <%= f.actions do %>
13
+ <%= f.submit %>
14
+ <%= link_to "Back", widgets_path %>
15
+ <% end %>
16
+ <% end %>
17
+
18
+ Now wave your CSS wand over the generated HTML and you have yourself a table-based form.
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the action_context plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the action_context plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'ActionContext'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
24
+
25
+ require 'jeweler'
26
+ Jeweler::Tasks.new do |gem|
27
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
28
+ gem.name = "table_form_builder"
29
+ gem.summary = "Table-based form builder for Rails 3"
30
+ gem.email = "mdaubert@gmail.com"
31
+ gem.homepage = "http://github.com/MDaubs/table_form_builder"
32
+ gem.authors = ["Matthew Daubert"]
33
+ end
34
+ Jeweler::GemcutterTasks.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'table_form_builder'
data/install.rb ADDED
File without changes
@@ -0,0 +1,3 @@
1
+ require 'table_form_builder/application_helper'
2
+ require 'table_form_builder/builder'
3
+ ActionView::Base.send :include, TableFormBuilder::ApplicationHelper
@@ -0,0 +1,21 @@
1
+ require 'active_support/concern'
2
+
3
+ module TableFormBuilder
4
+ module ApplicationHelper
5
+ extend ActiveSupport::Concern
6
+
7
+ module InstanceMethods
8
+
9
+ # Wraps a standard form_for inside of a table element and sets the builder to TableFormBuilder
10
+ def table_form_for(record_or_name_or_array, *args, &proc)
11
+ options = args.extract_options!
12
+ content_tag(:table, :class => "form") do
13
+ form_for(record_or_name_or_array, *(args << options.merge(:builder => TableFormBuilder::Builder)), &proc)
14
+ end
15
+ end
16
+
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -0,0 +1,54 @@
1
+ module TableFormBuilder
2
+ class Builder < ActionView::Helpers::FormBuilder
3
+
4
+ %w{text_field password_field}.each do |method_name|
5
+ define_method(method_name) do |field, *args|
6
+ options = args.last.is_a?(Hash) ? args.pop : {}
7
+ label = label(field, options[:label])
8
+ extra = options[:extra].nil? ? "" : @template.content_tag(:span, :class => "extra") { options[:extra] }
9
+ errors = generate_errors(field)
10
+ @template.content_tag(:tr) do
11
+ @template.content_tag(:th, label) + @template.content_tag(:td, super(field, *args) + extra + errors)
12
+ end
13
+ end
14
+ end
15
+
16
+ def check_box(field, *args)
17
+ options = args.last.is_a?(Hash) ? args.pop : {}
18
+ label = label(field, options[:label])
19
+ extra = options[:extra].nil? ? "" : @template.content_tag(:span, :class => "extra") { options[:extra] }
20
+ @template.content_tag(:tr) do
21
+ @template.content_tag(:th) + @template.content_tag(:td, super(field, *args) + label + extra)
22
+ end
23
+ end
24
+
25
+ def group(text, &block)
26
+ @template.content_tag(:tr) do
27
+ @template.content_tag(:th, text) + @template.content_tag(:td, @template.capture(&block))
28
+ end
29
+ end
30
+
31
+ def title(text)
32
+ @template.content_tag(:tr) do
33
+ @template.content_tag(:th) + @template.content_tag(:td) do
34
+ @template.content_tag(:h3, text)
35
+ end
36
+ end
37
+ end
38
+
39
+ def actions(&block)
40
+ @template.content_tag(:tr) do
41
+ @template.content_tag(:th) + @template.content_tag(:td, @template.capture(&block))
42
+ end
43
+ end
44
+
45
+ def generate_errors(field)
46
+ @object.errors[field].collect{|e| @template.content_tag(:div, :class => "error"){generate_error_message(field, e)}}.join.html_safe
47
+ end
48
+
49
+ def generate_error_message(field, error)
50
+ "#{field.to_s.humanize} #{error}"
51
+ end
52
+
53
+ end
54
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'table_form_builder'
@@ -0,0 +1,45 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{table_form_builder}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Matthew Daubert"]
12
+ s.date = %q{2011-02-06}
13
+ s.email = %q{mdaubert@gmail.com}
14
+ s.extra_rdoc_files = [
15
+ "README.md"
16
+ ]
17
+ s.files = [
18
+ "README.md",
19
+ "Rakefile",
20
+ "VERSION",
21
+ "init.rb",
22
+ "install.rb",
23
+ "lib/table_form_builder.rb",
24
+ "lib/table_form_builder/application_helper.rb",
25
+ "lib/table_form_builder/builder.rb",
26
+ "rails/init.rb",
27
+ "table_form_builder.gemspec",
28
+ "uninstall.rb"
29
+ ]
30
+ s.homepage = %q{http://github.com/MDaubs/table_form_builder}
31
+ s.require_paths = ["lib"]
32
+ s.rubygems_version = %q{1.3.7}
33
+ s.summary = %q{Table-based form builder for Rails 3}
34
+
35
+ if s.respond_to? :specification_version then
36
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
37
+ s.specification_version = 3
38
+
39
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
40
+ else
41
+ end
42
+ else
43
+ end
44
+ end
45
+
data/uninstall.rb ADDED
File without changes
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: table_form_builder
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Matthew Daubert
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-02-06 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description:
22
+ email: mdaubert@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README.md
29
+ files:
30
+ - README.md
31
+ - Rakefile
32
+ - VERSION
33
+ - init.rb
34
+ - install.rb
35
+ - lib/table_form_builder.rb
36
+ - lib/table_form_builder/application_helper.rb
37
+ - lib/table_form_builder/builder.rb
38
+ - rails/init.rb
39
+ - table_form_builder.gemspec
40
+ - uninstall.rb
41
+ has_rdoc: true
42
+ homepage: http://github.com/MDaubs/table_form_builder
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.3.7
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Table-based form builder for Rails 3
73
+ test_files: []
74
+