neorails-form_fu 0.1.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/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ ViewFu - Copyright (c) 2008 NeoRails.com
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,86 @@
1
+ FormFu
2
+ ======
3
+ FormFu is a Rails plugin that enables you to easily build nice, tableless forms
4
+
5
+
6
+ Maintainer Info
7
+ ======
8
+
9
+ Tyler Crocker
10
+ NeoRails.com
11
+
12
+ Looking for *the one* awesome rails developer to add to your project? I may be available to help out. Contact me at neorails@gmail.com.
13
+
14
+
15
+ Installation
16
+ ======
17
+
18
+ script/plugin install git://github.com/neorails/view_fu.git
19
+
20
+ or
21
+
22
+ # Add this to your environment.rb
23
+ config.gem 'neorails-form_fu', :version => '>= 0.1',
24
+ :lib => 'form_fu',
25
+ :source => 'http://gems.github.com'
26
+
27
+
28
+ Usage
29
+ ======
30
+
31
+ Create a Basic Form for a Model
32
+ <% build_form_for @widget do |f| %>
33
+ <%= f.text_field :name %>
34
+ <%= f.text_area :description %>
35
+ <%= f.submit %>
36
+ <% end %>
37
+
38
+ --------
39
+
40
+ Create Fields for a Model
41
+ <% build_fields_for @address do |a| %>
42
+ <%= a.text_field :street %>
43
+ <%= a.text_field :city %>
44
+ <%= a.select :state, State.to_select %>
45
+ <% end %>
46
+
47
+ --------
48
+
49
+ Set up a label
50
+ <%= f.text_field :name, :label => "Enter Your Name" %>
51
+
52
+ --------
53
+
54
+ Change the separator
55
+ <%= f.text_field :name, :separator => "" %>
56
+
57
+ --------
58
+
59
+ Take a Block (adds it to the field div)
60
+ <% f.text_field :name do %>
61
+ <div class="hint">
62
+ please enter your name...
63
+ </div>
64
+ <% end %>
65
+
66
+ --------
67
+
68
+ Add options to the field
69
+ <%= f.text_field :name, :field => {:class => "left"} %>
70
+
71
+ --------
72
+
73
+ Add an Error Messages Summary
74
+ <%= f.error_messages %>
75
+
76
+ --------
77
+
78
+
79
+ Try my other plugins
80
+ ======
81
+
82
+ If you like FormFu, you'll *love* ViewFu. It will help you deal with Stylesheets, Javascripts, Page titles, and adds tons of useful view helper methods.
83
+ https://github.com/neorails/view_fu
84
+
85
+
86
+ Copyright (c) 2008 NeoRails.com, released under the MIT license
data/form_fu.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'form_fu'
3
+ s.version = '0.1.1'
4
+ s.date = '2008-07-06'
5
+
6
+ s.summary = "Build Nice DRY Rails Forms"
7
+ s.description = "FormFu is a Rails plugin that enables you to easily build nice, tableless forms"
8
+
9
+ s.authors = ['Tyler Crocker']
10
+ s.email = 'neorails@gmail.com'
11
+ s.homepage = 'http://github.com/neorails/form_fu'
12
+
13
+ s.has_rdoc = true
14
+ s.rdoc_options = ["--main", "README"]
15
+ s.extra_rdoc_files = ["README"]
16
+
17
+ s.add_dependency 'rails', ['>= 2.1']
18
+
19
+ s.files = ["LICENSE",
20
+ "README",
21
+ "form_fu.gemspec",
22
+ "init.rb",
23
+ "lib/form_fu/form_builder.rb",
24
+ "lib/form_fu/helpers.rb",
25
+ "lib/form_fu.rb",
26
+ "rails/init.rb"]
27
+
28
+ s.test_files = ["test/form_fu_test.rb"]
29
+
30
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/rails/init"
@@ -0,0 +1,113 @@
1
+ module FormFu
2
+
3
+ # A form builder that produces tableless, lined-up forms.
4
+ class FormBuilder < ActionView::Helpers::FormBuilder
5
+ # automatically wrap all the standard formbuilder helpers
6
+ (field_helpers - %w(label radio_button hidden_field text_area)).each do |selector|
7
+ src = <<-END_SRC
8
+ def #{selector}(field, options = {}, &block)
9
+ format_with_label(field, options.merge(:field_type => "#{selector}"), super(field, purge_custom_tags(options)), &block)
10
+ end
11
+ END_SRC
12
+ class_eval src, __FILE__, __LINE__
13
+ end
14
+
15
+ def text_area(field, options = {}, &block)
16
+ format_with_label(field, options.merge(:field_type => "text_area", :preserve => true), super(field, purge_custom_tags(options)), &block)
17
+ end
18
+
19
+ # wrap the date_select helper
20
+ def date_select(field, options={}, &block)
21
+ format_with_label(field, options.merge(:field_type => "date"), super(field, purge_custom_tags(options)), &block)
22
+ end
23
+
24
+ # create a radio group helper that works very similarly to the select helper
25
+ def radio_group(field, choices, options={}, &block)
26
+
27
+ # handle special cases
28
+ if choices == :boolean
29
+ choices = [["True", "true"], ["False", "false"]]
30
+ elsif choices == :yes_no
31
+ choices = [["Yes", "yes"], ["No", "no"]]
32
+ elsif choices.class != Array
33
+ choices = []
34
+ end
35
+
36
+ # build radio choices html
37
+ choices_html = ""
38
+ choices.each do |key, value|
39
+ radio_html = radio_button(field, value)+key
40
+
41
+ # wrap radio html in a label (for easier selection)
42
+ choices_html << content_tag(:label, radio_html, :class => "radio-option")
43
+ end
44
+
45
+ # wrap the radio-group with a label
46
+ format_with_label(field, options.merge(:field_type => "radio-group"), choices_html, &block)
47
+ end
48
+
49
+ # wrap the select helper
50
+ def select(field, choices, options={}, &block)
51
+ html_options = options.delete(:html) || {}
52
+ format_with_label(field, options.merge(:field_type => "select"), super(field, choices, options, html_options), &block)
53
+ end
54
+
55
+ # create a submit helper
56
+ def submit(value = "Submit", options = {})
57
+ @template.submit_tag(value, options)
58
+ end
59
+
60
+ # create a image_submit helper
61
+ def image_submit(img_path, options = {})
62
+ @template.image_submit_tag(img_path, options)
63
+ end
64
+
65
+ # create an error messages helper
66
+ def error_messages(object_name = nil)
67
+ @template.error_messages_for object_name || @object_name
68
+ end
69
+
70
+ private
71
+
72
+ # format a helper by generating the haml to wrap it in a field_tag and include a label
73
+ def format_with_label(field, options, tag_output, &block)
74
+ # see if we have an error on the field
75
+ errors_on = object.send(:errors).send(:on, field)
76
+ has_error = true unless errors_on.blank?
77
+
78
+ # set field options
79
+ options[:field] ||= {}
80
+ options[:field].merge!(:has_error => has_error, :field_type => options[:field_type])
81
+
82
+ if options[:preserve]
83
+ tag_output = @template.preserve(tag_output)
84
+ end
85
+
86
+ output_html = @template.field_tag(options[:field], [
87
+ @template.label(@object_name, field, options[:label], :separator => options[:separator] || default_separator),
88
+ tag_output,
89
+ @template.validation_tag(@object, field),
90
+ block_given? ? @template.capture(&block) : nil
91
+ ].compact.join("\n"))
92
+
93
+ Rails.logger.debug "OUTPUTING LABEL FORMAT:\n\n#{output_html}\n\n"
94
+
95
+ if block_given?
96
+ # concat to page if block was given
97
+ return concat(output_html, block.binding)
98
+ else
99
+ # otherwise return html directly
100
+ return output_html
101
+ end
102
+ end
103
+
104
+ def default_separator
105
+ ": "
106
+ end
107
+
108
+ # Removes tags such as :html, :label so they dont get rendered on the final html
109
+ def purge_custom_tags(options)
110
+ options.reject{ |key,value| [:label, :html, :field, :field_type, :separator].include?(key.to_sym) }
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,90 @@
1
+ module FormFu
2
+ module Helpers
3
+ # Create a form_for block using FormFuBuilder
4
+ def formfu_for(record_or_name_or_array, *args, &proc)
5
+ options = args.extract_options!
6
+ args << options.merge(:builder => FormFu::FormBuilder)
7
+ form_for(record_or_name_or_array, *args, &proc)
8
+ end
9
+
10
+ # also work with the more semantic name (build_form_for)
11
+ alias :build_form_for :formfu_for
12
+
13
+ # Create a fields_for block using FormFuBuilder
14
+ def formfu_fields_for(object_or_object_name, *args, &block)
15
+ raise ArgumentError, "Missing block" unless block_given?
16
+ options = args.extract_options!
17
+
18
+ if object_or_object_name.class == Symbol
19
+ # if object_or_object_name is a symbol, use the object from args
20
+ object_name = object_or_object_name
21
+ object = args.first
22
+ else
23
+ # otherwise retrieve the object_name from ActiveRecord helper
24
+ object = object_or_object_name
25
+ object_name = ActionController::RecordIdentifier.singular_class_name(object)
26
+ end
27
+
28
+ yield FormFu::FormBuilder.new(object_name, object, self, options, block)
29
+ end
30
+
31
+ # also work with the more semantic name (build_fields_for)
32
+ alias :build_fields_for :formfu_fields_for
33
+
34
+ # wrap content with a fieldset tag with a legend
35
+ def fieldset_tag(legend_name, options = {}, &block)
36
+ if block_given?
37
+ concat(content_tag(:fieldset, options) do
38
+ (legend_name ? content_tag(:legend, legend_name) : "")+
39
+ capture(&block)
40
+ end, block.binding)
41
+ else
42
+ return content_tag(:fieldset, options) do
43
+ content_tag :legend, legend_name
44
+ end
45
+ end
46
+ end
47
+
48
+ #generate a field div with label
49
+ def field_tag(options_or_label = {}, content = nil, &block)
50
+ label, options = "", {}
51
+ if options_or_label.class == String
52
+ label = options_or_label
53
+ options[:class] = "field"
54
+ else
55
+ options = options_or_label
56
+ # grab the error indicator out of the options
57
+ has_error = options.delete(:has_error) || false
58
+
59
+ # grab the field_type out of the options
60
+ field_type = options.delete(:field_type) || nil
61
+
62
+ # append field as css class for div options
63
+ options[:class] = "field #{field_type} #{options[:class]} #{'withErrors' if has_error}"
64
+ end
65
+
66
+ if block_given?
67
+ concat(content_tag(:div, options) do
68
+ (label.blank? ? "" : content_tag(:label, label.strip)) +
69
+ capture(&block)
70
+ end, block.binding)
71
+ else
72
+ content_tag(:div, options) do
73
+ (label.blank? ? "" : content_tag(:label, label.strip))+content.to_s
74
+ end
75
+ end
76
+ end
77
+
78
+ # show validation error is applicable
79
+ def validation_tag(model, attribute, options = {})
80
+ return if model.blank? or model.errors.blank?
81
+ unless model.errors[attribute].blank?
82
+ # generate error markup
83
+ content_tag :span, :class => "error-message" do
84
+ [model.errors[attribute]].flatten.join(options[:separator] || ", ").to_s
85
+ end
86
+ end
87
+ end
88
+
89
+ end
90
+ end
data/lib/form_fu.rb ADDED
@@ -0,0 +1,21 @@
1
+ require "form_fu/form_builder"
2
+ require "form_fu/helpers"
3
+ ActionView::Base.send :include, FormFu::Helpers
4
+
5
+ # Override the error proc to use a span instead of a div
6
+ module ActionView
7
+ class Base #:nodoc:
8
+ @@field_error_proc = Proc.new{ |html_tag, instance| "#{html_tag}" }
9
+ cattr_accessor :field_error_proc
10
+ end
11
+ end
12
+
13
+ # Add a to_select method to our ActiveRecord models
14
+ module ActiveRecord
15
+ class Base
16
+ #find all items matching _condition_, and return an array of [value,key], useful for using with select
17
+ def self.to_select(method='to_s', conditions=nil)
18
+ find(:all, :conditions => conditions).collect{|record| [record.send(method), record.id]}
19
+ end
20
+ end
21
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'form_fu'
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'form_fu'
4
+
5
+ class FormFuTest < Test::Unit::TestCase
6
+
7
+ include ActionView::Helpers::TagHelper
8
+ include FormFu::Helpers
9
+
10
+ def test_truth
11
+ assert true
12
+ end
13
+
14
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: neorails-form_fu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Tyler Crocker
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-07-06 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rails
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "2.1"
23
+ version:
24
+ description: FormFu is a Rails plugin that enables you to easily build nice, tableless forms
25
+ email: neorails@gmail.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - README
32
+ files:
33
+ - LICENSE
34
+ - README
35
+ - form_fu.gemspec
36
+ - init.rb
37
+ - lib/form_fu/form_builder.rb
38
+ - lib/form_fu/helpers.rb
39
+ - lib/form_fu.rb
40
+ - rails/init.rb
41
+ has_rdoc: true
42
+ homepage: http://github.com/neorails/form_fu
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --main
46
+ - README
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.2.0
65
+ signing_key:
66
+ specification_version: 2
67
+ summary: Build Nice DRY Rails Forms
68
+ test_files:
69
+ - test/form_fu_test.rb