merb_form_fields 0.0.2

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
+ Copyright (c) 2009 Jacques Crocker
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,102 @@
1
+ merb_form_fields
2
+ ================
3
+
4
+ A plugin for the Merb framework that provides a Form Builder that wraps each of your fields.
5
+
6
+ It provides validation on the wrapper (adds a class), and will insert the error message automatically.
7
+
8
+ = form_with_fields_for(@user) do
9
+ = text_field :first_name
10
+
11
+ is equivalent to:
12
+ <form ...>
13
+ <div class="field text_field">
14
+ <input type="text" name="user_first_name" ... />
15
+ </div>
16
+ </form>
17
+
18
+ If there an error on first name it will render:
19
+ <form ...>
20
+ <div class="field text_field error">
21
+ <input type="text" name="user_first_name" ... />
22
+ <em class='error'>First Name is required.</em>
23
+ </div>
24
+ </form>
25
+
26
+ Customize Fields with the :field option (takes a hash)
27
+ = text_field :first_name, :field => {:class => "my_field_class", :id => "field_id"}
28
+
29
+ Customize the error message with the :error option (overrides the default model error)
30
+ = text_field :first_name, :error => "Bzzzzt... wrong!"
31
+
32
+
33
+ All render options are easily configurable via Merb::Plugins.config[:merb_form_fields]
34
+
35
+ Field Tag
36
+ ---------
37
+ Merb::Plugins.config[:merb_form_fields][:field_tag]
38
+ defaults to :div
39
+
40
+ this defines which tag is used for the field wrapper.
41
+ e.g. <div class="field ...">...</div>
42
+ if you set it to :li, it would render as <li class="field ...">...</li>
43
+
44
+
45
+ Field Class
46
+ ------------
47
+ Merb::Plugins.config[:merb_form_fields][:field_tag]
48
+ defaults to "field"
49
+
50
+ this defines the class used by default for the field wrapper.
51
+ e.g. <div class="field ...">...</div>
52
+ if you set it to "wrapper", it would render as <div class="wrapper ...">...</div>
53
+
54
+
55
+ Field Error Class
56
+ -----------------
57
+ Merb::Plugins.config[:merb_form_fields][:field_error_class]
58
+ defaults to "error"
59
+
60
+ this defines the class used on the field wrapper when the field is invalid.
61
+ e.g. <div class="field error ...">...</div>
62
+ if you set it to "invalid", it would render as <div class="field invalid ...">...</div>
63
+
64
+
65
+ Error Message Tag
66
+ ------------------
67
+ Merb::Plugins.config[:merb_form_fields][:error_message_tag]
68
+ defaults to :em
69
+
70
+ this is the tag that is displayed inside the field.
71
+ e.g. <em class='error'>First Name is required.</em>
72
+ If you set it to :span, it would display as <span class='error'>...</span>
73
+
74
+
75
+ Error Message Class
76
+ -------------------
77
+ Merb::Plugins.config[:merb_form_fields][:error_message_class]
78
+ defaults to "error"
79
+
80
+ this is the class that is used for the error messages displayed inside a field.
81
+ e.g. <em class='error'>First Name is required.</em>
82
+ If you set it to "invalid", it would display as <em class='invalid'>...</em>
83
+
84
+
85
+ Add Field Type Class Option
86
+ ----------------------------
87
+ Merb::Plugins.config[:merb_form_fields][:add_field_type_class?]
88
+ defaults to true
89
+
90
+ if this option is enabled, it will add the field_type as a class to the field wrapper
91
+ e.g. <div class="field text_field ...">....</div>
92
+ if you set it to false, it would render without the field_type <div class="field ...">....</div>
93
+
94
+
95
+ Skipped Field Types
96
+ ------------------
97
+ Merb::Plugins.config[:merb_form_fields][:skipped_field_types]
98
+ defaults to [:hidden_field, :check_box, :radio_button]
99
+
100
+ Field types defined in this list will not be wrapped with a field wrapper
101
+ You can force the field wrapper to show by passing :field => {:force => true} on your field
102
+ You can prevent the field wrapper from showing by passing :field => {:skip => true} on your field
data/Rakefile ADDED
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+
4
+ require 'merb-core'
5
+ require 'merb-core/tasks/merb'
6
+
7
+ GEM_NAME = "merb_form_fields"
8
+ GEM_VERSION = "0.0.2"
9
+ AUTHOR = "Jacques Crocker"
10
+ EMAIL = "merbjedi@gmail.com"
11
+ HOMEPAGE = "http://github.com/merbjedi/merb_form_fields"
12
+ SUMMARY = "Merb plugin that provides a Form Builder that will wrap your bound fields with a field div, integrated with the model validation"
13
+
14
+ spec = Gem::Specification.new do |s|
15
+ s.rubyforge_project = 'merb'
16
+ s.name = GEM_NAME
17
+ s.version = GEM_VERSION
18
+ s.platform = Gem::Platform::RUBY
19
+ s.has_rdoc = true
20
+ s.extra_rdoc_files = ["README", "LICENSE"]
21
+ s.summary = SUMMARY
22
+ s.description = s.summary
23
+ s.author = AUTHOR
24
+ s.email = EMAIL
25
+ s.homepage = HOMEPAGE
26
+ s.add_dependency('merb-helpers', '>= 1.0.8')
27
+ s.require_path = 'lib'
28
+ s.files = %w(LICENSE README Rakefile) + Dir.glob("{lib,spec}/**/*")
29
+
30
+ end
31
+
32
+ Rake::GemPackageTask.new(spec) do |pkg|
33
+ pkg.gem_spec = spec
34
+ end
35
+
36
+ desc "install the plugin as a gem"
37
+ task :install do
38
+ Merb::RakeHelper.install(GEM_NAME, :version => GEM_VERSION)
39
+ end
40
+
41
+ desc "Uninstall the gem"
42
+ task :uninstall do
43
+ Merb::RakeHelper.uninstall(GEM_NAME, :version => GEM_VERSION)
44
+ end
45
+
46
+ desc "Create a gemspec file"
47
+ task :gemspec do
48
+ File.open("#{GEM_NAME}.gemspec", "w") do |file|
49
+ file.puts spec.to_ruby
50
+ end
51
+ end
@@ -0,0 +1,153 @@
1
+ module MerbFormFields
2
+ class FormFieldBuilder < Merb::Helpers::Form::Builder::ResourcefulForm
3
+
4
+ # --------------------------------------------------------
5
+ # override the following methods to customize FieldBuilder
6
+ # --------------------------------------------------------
7
+
8
+ # This tag wraps inner error messages
9
+ def error_message_tag
10
+ Merb::Plugins.config[:merb_form_fields][:error_message_tag] || :em
11
+ end
12
+
13
+ # This tag wraps the field
14
+ def field_tag
15
+ Merb::Plugins.config[:merb_form_fields][:field_tag] || :div
16
+ end
17
+
18
+ # This is the default field class
19
+ def field_class
20
+ Merb::Plugins.config[:merb_form_fields][:field_class] || "field"
21
+ end
22
+
23
+ # whether or not to add the field_type class
24
+ def add_field_type_class?
25
+ if Merb::Plugins.config[:merb_form_fields][:add_field_type_class?].nil?
26
+ true
27
+ else
28
+ Merb::Plugins.config[:merb_form_fields][:add_field_type_class?]
29
+ end
30
+ end
31
+
32
+ # These field types are skipped (not wrapped in a field div)
33
+ def skipped_field_types
34
+ Merb::Plugins.config[:merb_form_fields][:skipped_field_types] || [:hidden_field, :check_box, :radio_button]
35
+ end
36
+
37
+ # ---------------------------------------------
38
+ # wrapper method implementation
39
+ # ---------------------------------------------
40
+ def field_wrapper(field_type, attrs = {})
41
+ field_options = attrs.delete(:field) || {}
42
+ error_override = attrs.delete(:error)
43
+
44
+ # build inner field html (using the passed in block)
45
+ inner_html = yield if block_given?
46
+
47
+ # skip certain field types
48
+ # :fields => {:force => true} will force the field to show
49
+ # :fields => {:skip => true} will prevent the field from showing
50
+ if (field_options[:force] != true and skipped_field_types.include?(field_type)) or field_options[:skip] == true
51
+ return inner_html
52
+ end
53
+
54
+ # build class string
55
+ css_class = field_options[:class] || "#{field_class} #{field_type if add_field_type_class?}"
56
+ if error_class = field_error_class(@obj, attrs[:method])
57
+ css_class << " #{error_class}"
58
+ end
59
+
60
+ # build field
61
+ tag field_tag,
62
+ "#{inner_html}#{field_error_message(@obj, attrs[:method], error_override)}",
63
+ field_options.merge(:class => css_class)
64
+ end
65
+
66
+ # returns the class added to the field element if there was an error
67
+ def field_error_class(obj, method)
68
+ if obj && method && !obj.errors.on(method.to_sym).blank?
69
+ Merb::Plugins.config[:merb_form_fields][:field_error_class] || "error"
70
+ else
71
+ ""
72
+ end
73
+ end
74
+
75
+ # displays the inner error message element
76
+ def field_error_message(obj, method, error_override = nil)
77
+ if obj && method
78
+ error = obj.errors.on(method.to_sym)
79
+ unless error.blank?
80
+ return "\n<#{error_message_tag} class='#{Merb::Plugins.config[:merb_form_fields][:error_message_class] || "error"}'>#{error_override || error}</#{error_message_tag}>"
81
+ end
82
+ end
83
+ end
84
+
85
+ # we'll make our own errors, thank you very much
86
+ def error_messages_for(obj, error_class, build_li, header, before)
87
+ obj ||= @obj
88
+ return "" unless obj.respond_to?(:errors)
89
+
90
+ sequel = !obj.errors.respond_to?(:each)
91
+ errors = sequel ? obj.errors.full_messages : obj.errors
92
+
93
+ return "" if errors.empty?
94
+
95
+ header_message = header % [errors.size, errors.size == 1 ? "" : "s"]
96
+ markup = %Q{<div class='#{error_class}'>#{header_message}<ul>}
97
+ errors.each {|err| markup << (build_li % (sequel ? err : err.join(" ")))}
98
+ markup << %Q{</ul></div>}
99
+ end
100
+
101
+ # ------------------------------------------------------------------------
102
+ # methods below are hacks to support merb field wrapping
103
+ # there needs to be a hook added to merb-core that allows this more easily
104
+ # ------------------------------------------------------------------------
105
+ %w(text password hidden file).each do |kind|
106
+ self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
107
+ def bound_#{kind}_field(method, attrs = {})
108
+ name = control_name(method)
109
+ field_wrapper(:#{kind}_field, attrs.merge(:method => method)) do
110
+ super
111
+ end
112
+ end
113
+ RUBY
114
+ end
115
+
116
+ def bound_check_box(method, attrs = {})
117
+ name = control_name(method)
118
+ field_wrapper(:check_box, attrs.merge(:method => method)) do
119
+ super
120
+ end
121
+ end
122
+
123
+ def bound_radio_button(method, attrs = {})
124
+ name = control_name(method)
125
+ field_wrapper(:radio_button, attrs.merge(:method => method)) do
126
+ super
127
+ end
128
+ end
129
+
130
+ def bound_radio_group(method, arr)
131
+ val = control_value(method)
132
+
133
+ field_wrapper(:radio_group, attrs.merge(:method => method, :arr => arr)) do
134
+ super
135
+ end
136
+ end
137
+
138
+ def bound_select(method, attrs = {})
139
+ name = control_name(method)
140
+ field_wrapper(:select, attrs.merge(:method => method)) do
141
+ super
142
+ end
143
+ end
144
+
145
+ def bound_text_area(method, attrs = {})
146
+ name = "#{@name}[#{method}]"
147
+ field_wrapper(:text_area, attrs.merge(:method => method)) do
148
+ super
149
+ end
150
+ end
151
+
152
+ end
153
+ end
@@ -0,0 +1,9 @@
1
+ module MerbFormFields
2
+ module ViewHelpers
3
+ def form_with_fields_for(name, attrs = {}, &blk)
4
+ with_form_context(name, MerbFormFields::FormFieldBuilder) do
5
+ current_form_context.form(attrs, &blk)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,23 @@
1
+ # make sure we're running inside Merb
2
+ if defined?(Merb::Plugins)
3
+ # Merb gives you a Merb::Plugins.config hash...feel free to put your stuff in your piece of it
4
+ Merb::Plugins.config[:merb_form_fields] = {
5
+ :field_tag => :div,
6
+ :field_class => "field",
7
+ :field_error_class => "error",
8
+ :add_field_type_class? => true,
9
+
10
+ :error_message_tag => :em,
11
+ :error_message_class => "error",
12
+
13
+ :skipped_field_types => [:hidden_field, :check_box, :radio_button]
14
+ }
15
+
16
+ Merb::BootLoader.before_app_loads do
17
+ Merb::Controller.send(:include, MerbFormFields::ViewHelpers)
18
+ end
19
+ end
20
+
21
+ # load classes
22
+ require 'merb_form_fields/form_field_builder'
23
+ require 'merb_form_fields/view_helpers'
@@ -0,0 +1,7 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "merb_form_fields" do
4
+ it "should do nothing" do
5
+ true.should == true
6
+ end
7
+ end
@@ -0,0 +1 @@
1
+ $:.push File.join(File.dirname(__FILE__), '..', 'lib')
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: merb_form_fields
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Jacques Crocker
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-22 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: merb-helpers
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.0.8
24
+ version:
25
+ description: Merb plugin that provides a Form Builder that will wrap your bound fields with a field div, integrated with the model validation
26
+ email: merbjedi@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README
33
+ - LICENSE
34
+ files:
35
+ - LICENSE
36
+ - README
37
+ - Rakefile
38
+ - lib/merb_form_fields
39
+ - lib/merb_form_fields/form_field_builder.rb
40
+ - lib/merb_form_fields/view_helpers.rb
41
+ - lib/merb_form_fields.rb
42
+ - spec/merb_form_fields_spec.rb
43
+ - spec/spec_helper.rb
44
+ has_rdoc: true
45
+ homepage: http://github.com/merbjedi/merb_form_fields
46
+ post_install_message:
47
+ rdoc_options: []
48
+
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project: merb
66
+ rubygems_version: 1.3.1
67
+ signing_key:
68
+ specification_version: 2
69
+ summary: Merb plugin that provides a Form Builder that will wrap your bound fields with a field div, integrated with the model validation
70
+ test_files: []
71
+