Permalink
Browse files

fixed problems with renderer for file choosing

  • Loading branch information...
1 parent d0852df commit acb9882494d0bda5981685f40e80491f90d9e4de @nesquena committed Apr 26, 2008
View
@@ -29,6 +29,18 @@ svn:special svn:externals svn:needs-lock
.git
dir
+.gitignore
+file
+872
+
+
+
+2008-04-25T22:41:24.000000Z
+bc1b7e087216f297bb479d0a365b780b
+2008-04-26T03:53:01.746806Z
+872
+nate
+
MIT-LICENSE
file
@@ -43,14 +55,14 @@ nate
README
file
+872
-
-2008-04-14T04:28:57.000000Z
-c7b90e76696dbea2cacf887aac4c1ddf
-2008-04-14T04:36:21.422973Z
-795
+2008-04-26T02:34:04.000000Z
+c57c0cceedf965b54b4fb236e206f599
+2008-04-26T03:53:01.746806Z
+872
nate
Rakefile
@@ -67,14 +79,14 @@ nate
init.rb
file
+872
-
-2008-04-14T04:27:50.000000Z
-b1cb018155b176780c6105f4a47ba407
-2008-04-14T04:36:21.422973Z
-795
+2008-04-26T02:17:18.000000Z
+377b66cc8ad900a17123fc40f0e64339
+2008-04-26T03:53:01.746806Z
+872
nate
has-props
@@ -13,39 +13,47 @@ Overview
========
This class is a standardized form builder which works to create semantically correct forms,
- each field with the appropriate label and wrapped within a definition list item in order to
- represent a list of items using proper xhtml markup.
+each field with the appropriate label and wrapped within a definition list item in order to
+represent a list of items using proper xhtml markup.
The tags allowed are as follows:
* fieldset(name:string, &block) - wraps the rest of the form items in a definition list
- * text_field_item (attribute:symbol, options_hash:hash)
- * password_field_item (attribute:symbol, options_hash:hash)
- * file_field_item (attribute:symbol, options_hash:hash)
- * text_area_item (attribute:symbol, options_hash:hash)
- * check_box_item (attribute:symbol, options_hash:hash)
- * radio_button_group (attribute:symbol, options_hash:hash)
- * submit_button (label:string) - create a submit button in a definition item
+ * text_field (attribute:symbol, options_hash:hash)
+ * password_field (attribute:symbol, options_hash:hash)
+ * file_field (attribute:symbol, options_hash:hash)
+ * text_area (attribute:symbol, options_hash:hash)
+ * check_box (attribute:symbol, options_hash:hash)
+ * radio_buttons (attribute:symbol, options_hash:hash)
+ * submit_button (label:string) - create a submit button in a definition item
+
+These methods can be used within the context of a form builder or within any form as simple helper tags:
+
+ * semantic_form_for [ model-backed forms ]
+ * semantic_remote_form_for [ remote model-backed forms ]
+ * semantic_fields_for [ model-backed fields ]
+ * semantic_ajaxy_form_for [ ajax model-backed forms ]
+ * semantic_fieldset_tag [ non-model-backed forms ]
Example
=======
This form builder is rather easy to use as the example illustrates:
- - standard_form_for :user, :url => users_path do |f|
+ - semantic_form_for :user, :url => users_path do |f|
- f.fieldset 'Register' do
- = f.text_field_item :login, :label => 'Login'
- = f.text_field_item :email, :label => 'Email'
- = f.password_field_item :password, :label => 'Password'
- = f.password_field_item :password_confirmation, :label => 'Password Confirmation'
- = f.submit_button 'Sign up'
+ = f.text_field :login, :label => 'Login'
+ = f.text_field :email, :label => 'Email'
+ = f.password_field :password, :label => 'Password'
+ = f.password_field :password_confirmation, :label => 'Password Confirmation'
+ = f.submit_button 'Sign up'
which generates the following semantically valid markup:
<form method="post" action="/users">
<fieldset>
<legend>Register</legend>
- <dl class = "standard-form">
+ <dl class = "semantic-form">
<dt><label for="user_login">Login:</label></dt>
<dd><input type="text" size="30" name="user[login]" id="user_login"/></dd>
@@ -64,5 +72,28 @@ This form builder is rather easy to use as the example illustrates:
</fieldset>
</form>
+Other Examples
+==============
+Non-Model Backed Form
+
+ - form_tag some_url do
+ - semantic_fieldset_tag "Name" do |f|
+ = f.text_field_tag :username, :label => "Username"
+ = f.password_field_tag :password, :label => "Password"
+ = f.check_box_tag :is_admin, :label => "Administrator?"
+ = f.select_tag :category, @option_values
+ = f.submit_tag "Submit"
+
+Remote Model Backed Form
+
+ - semantic_remote_form_for :user, :url => register_url do |u|
+ - u.fieldset do
+ = u.text_field "login"
+ = u.password_field "password"
+ = u.text_field "email"
+ = u.text_field "mobile_number", :label => "Mobile No"
+ = u.password_field "invite_code", :label => 'Invite'
+ = u.submit_button
+
Copyright (c) 2008 Nathan Esquenazi, released under the MIT license
@@ -1,3 +1,4 @@
+require 'semantic_fields_renderer'
require 'semantic_form_builder'
require 'semantic_form_helpers'
ActionView::Base.send :include, SemanticFormBuilder::Helpers
View
@@ -26,29 +26,42 @@ svn:special svn:externals svn:needs-lock
75e973b7-daca-42f5-98ca-6c0bed327aa9
-semantic_form_builder.rb
+semantic_fields_renderer.rb
file
+872
+2008-04-26T03:52:26.000000Z
+9280e8273d1405f1c07fe854723404cd
+2008-04-26T03:53:01.746806Z
+872
+nate
+has-props
+
+semantic_form_builder.rb
+file
+872
-2008-04-14T04:27:50.000000Z
-9a3c475f406067192ea5a5e033ac6070
-2008-04-14T04:36:21.422973Z
-795
+
+
+2008-04-26T02:30:04.000000Z
+cb0895d6f387ea432868142e3f79ee53
+2008-04-26T03:53:01.746806Z
+872
nate
has-props
semantic_form_helpers.rb
file
+872
-
-2008-04-14T04:27:50.000000Z
-3a949a79d57c3ec03c3b9ccffad57d5e
-2008-04-14T04:36:21.422973Z
-795
+2008-04-26T02:31:27.000000Z
+5590e29b0c050abe74ab8c4b996ba832
+2008-04-26T03:53:01.746806Z
+872
nate
has-props
@@ -7,30 +7,30 @@
The tags allowed are as follows:
* fieldset(name:string, &block) - wraps the rest of the form items in a definition list
- * text_field_item (attribute:symbol, options_hash:hash)
- * password_field_item (attribute:symbol, options_hash:hash)
- * file_field_item (attribute:symbol, options_hash:hash)
- * text_area_item (attribute:symbol, options_hash:hash)
- * check_box_item (attribute:symbol, options_hash:hash)
- * radio_button_group (attribute:symbol, options_hash:hash)
- * submit_button (label:string) - create a submit button in a definition item
+ * text_field (attribute:symbol, options_hash:hash)
+ * password_field (attribute:symbol, options_hash:hash)
+ * file_field (attribute:symbol, options_hash:hash)
+ * text_area (attribute:symbol, options_hash:hash)
+ * check_box (attribute:symbol, options_hash:hash)
+ * radio_buttons (attribute:symbol, options_hash:hash)
+ * submit_button (label:string) - create a submit button in a definition item
This form builder is rather easy to use as the example illustrates:
- - standard_form_for :user, :url => users_path do |f|
+ - semantic_form_for :user, :url => users_path do |f|
- f.fieldset 'Register' do
- = f.text_field_item :login, :label => 'Login'
- = f.text_field_item :email, :label => 'Email'
- = f.password_field_item :password, :label => 'Password'
- = f.password_field_item :password_confirmation, :label => 'Password Confirmation'
- = f.submit_button 'Sign up'
+ = f.text_field :login, :label => 'Login'
+ = f.text_field :email, :label => 'Email'
+ = f.password_field :password, :label => 'Password'
+ = f.password_field :password_confirmation, :label => 'Password Confirmation'
+ = f.submit_button 'Sign up'
which generates the following semantically valid markup:
<form method="post" action="/users">
<fieldset>
<legend>Register</legend>
- <dl class = "standard-form">
+ <dl class = "semantic-form">
<dt><label for="user_login">Login:</label></dt>
<dd><input type="text" size="30" name="user[login]" id="user_login"/></dd>
@@ -52,7 +52,7 @@
=end
module SemanticFormBuilder
- class StandardBuilder < ActionView::Helpers::FormBuilder
+ class SemanticBuilder < ActionView::Helpers::FormBuilder
# Creates a fieldset around the content block as well as wrapping
# the fields within a definition item and providing an optional legend
@@ -71,7 +71,8 @@ module SemanticFormBuilder
# end
#
def fieldset(name=nil, &block)
- @template.standard_fieldset_tag(name, &block)
+ @renderer = FieldsRenderer.new(@template) # stores the renderer for later use
+ @template.semantic_fieldset_tag(name, &block)
end
# submit element tag for form within a definition item
@@ -85,7 +86,7 @@ module SemanticFormBuilder
# f.submit_button "Label"
#
def submit_button(label, options={})
- @template.submit_standard_tag(label, options)
+ @renderer.submit_tag(label, options)
end
# options => :choices [Array]
@@ -100,9 +101,9 @@ module SemanticFormBuilder
# </dd>
#
# ex:
- # f.radio_button_group :gender, :choices => [ :male, :female ]
+ # f.radio_buttons :gender, :choices => [ :male, :female ]
#
- def radio_button_group(attribute, options)
+ def radio_buttons(attribute, options)
caption = (options.delete(:label) || attribute.to_s.titleize).gsub(' ', '&nbsp;') + ":"
html = @template.content_tag(:dt, caption)
html << @template.content_tag(:dd) do
@@ -120,22 +121,29 @@ module SemanticFormBuilder
# as well as add an automatic label when this is added to the view
#
# attribute => the field of the record that will be binded to this
- # options => any of the calendar_date_select options which can be found here:
+ # options => { :label => "some text" } && any of the calendar_date_select options which can be found here:
#
# http://code.google.com/p/calendardateselect/
# http://code.google.com/p/calendardateselect/wiki/CalendarDateSelectParameters
#
- def calendar_date_item(attribute, options={})
+ # ex:
+ # f.date_select :start_time, :label => "Start", :time => true
+ #
+ def date_select(attribute, options={})
caption = (options.delete(:label) || attribute.to_s.humanize).gsub(' ', '&nbsp;')
options.reverse_merge!(:valid_date_check => "date >= (new Date()).stripTime()")
html = @template.content_tag("dt", @template.content_tag("label" , "#{caption}:", :for => "#{object_name}_#{attribute}" ))
html << @template.content_tag("dd", calendar_date_select(attribute, options))
end
- # options => :day (String), :month (String), :year (String), :start_year (Integer), :include_blank (Boolean)
# constructs a date selector specifically for birthdays. The day, month, year fields are the attributes
# for the user mapping to those individual values
#
+ # options => :day (String), :month (String), :year (String), :start_year (Integer), :include_blank (Boolean)
+ #
+ # ex:
+ # f.simple_date_select "Birthday", :month => 'birth_month', :day => 'birth_day', :year => 'birth_year'
+ #
# <dt>Birthday:</dd>
# <dd>
# <select name="user[birth_month]" id="user_birth_month">
@@ -157,9 +165,7 @@ module SemanticFormBuilder
# </dd>
#
#
- # ex: f.date_selector_item "Birthday", :month => 'birth_month', :day => 'birth_day', :year => 'birth_year', :include_blank => true
- #
- def date_selector_item(caption, options)
+ def simple_date_select(caption, options)
month_value, day_value, year_value = object.send(options[:month]), object.send(options[:day]), object.send(options[:year])
end_year = Time.now.year - 10
html = @template.content_tag("dt", "#{caption}:")
@@ -182,16 +188,17 @@ module SemanticFormBuilder
# <dt><label for="someid">Name</label></dt>
# <dd><input id="someid" ... /></dd>
#
- # ex: f.text_field_element :login, :label => "Login"
+ # ex: self.create_field_element 'text_field' # => overwrites text_field to use definition pair
#
def self.create_field_element(input_type_name)
- method_name = input_type_name + "_item" #i.e text_field_item
- template_method = input_type_name + "_standard_tag" #i.e text_field_standard_tag
- define_method(method_name) do |attribute, *args| # defines a method called 'text_field_item'
+ method_name = input_type_name #i.e method will be defined as 'text_field'
+ template_method = "#{input_type_name}_tag" #i.e text_field_tag
+ define_method(method_name) do |attribute, *args| # defines a method called 'text_field'
+ raise "Semantic form fields must be contained within a fieldset!" unless @renderer
options = args.length > 0 ? args[0] : {} # grab the options hash
object_value = @object ? @object.send(attribute) : nil # grab the object's value
options.reverse_merge!(:label => attribute.to_s.titleize, :id => "#{object_name(:id)}_#{attribute}", :value => object_value)
- @template.send(template_method, "#{object_name}[#{attribute}]", options)
+ @renderer.send(template_method, "#{object_name}[#{attribute}]", options)
end
end
@@ -203,9 +210,10 @@ module SemanticFormBuilder
end
# Correctly parses the empty array brackets that can be passed into a form and inserts the id
- # ex.
- # standard_fields_for "social_site[]", social_site_fields do |s|
- # s.text_field_item "login"
+ #
+ # ex:
+ # semantic_fields_for "social_site[]", social_site_fields do |s|
+ # s.semantic_text_field_tag "login"
#
# "social_site[]" means that the object name should be "social_site[#{@object.id}]"
#
Oops, something went wrong.

0 comments on commit acb9882

Please sign in to comment.