Skip to content

Commit

Permalink
Added UberForms to the Uberkit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Bleigh committed Jul 14, 2008
1 parent 7894d8a commit c219287
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 4 deletions.
72 changes: 72 additions & 0 deletions README
Expand Up @@ -16,6 +16,78 @@ To install it as a plugin (Rails 2.1 or later):

script/plugin install git://github.com/mbleigh/uberkit.git

UberForms
---------

UberForms provide a simple context for building forms in a DRYer
manner by abstracting the markup into a simple, CSS-styleable
format. It is available as a form builder as Uberkit::Forms::Builder,
but is likely more useful when used in one of the helper forms:
uberform_for or remote_uberform_for.

=== Basic Example

<% uberform_for :user do |f| %>
<%= f.text_field :login %>
<%= f.password_field :password %>
<%= f.submit "Submit"%>
<% end %>

Becomes...

<form method="post" class="uberform" action="/users">
<div class="field_row">
<label for="user_login">Login:</label>
<input type="text" size="30" name="user[login]" id="user_login" class="text_field"/>
<br/>
</div>
<div class="field_row">
<label for="user_password">Password:</label>
<input type="password" size="30" name="user[password]" id="user_password" class="password_field"/>
<br/>
</div>
<button type="submit">Submit</button>
</form>

=== Labels, Help, and Descriptions

You can pass options into a given field to set a custom label,
some help text, or a description of the field.

<%= f.text_field :login, :label => "Username",
:help => "Only a-z and underscores.",
:description => "The name you will use to log in." %>

Becomes...

<div class="field_row">
<label for="user_login">Username:</label>
<input type="text" size="30" name="user[login]" label="Username" id="user_login" help="Only a-z and underscores." description="The name you will use to log in." class="text_field"/>
<span class="help">Only a-z and underscores.</span>
<span class="description">The name you will use to log in.</span>
<br/>
</div>

=== Custom Fields

Maybe the built-in form helpers won't do it for you. In that case, you
can use the custom helper to insert arbitrary HTML that still plays
nice with the rest of UberForms:

<% f.custom :label => "State", :for => "user_state" do |f| %>
<%= state_select :user, :state %>
<% end %>

Becomes...

<div class="field_row">
<label for="user_state">State:</label>
<div class="pseudo_field">
<select id="user_state">...</select>
</div>
<br/>
</div>

UberMenu
--------

Expand Down
3 changes: 2 additions & 1 deletion lib/uberkit.rb
@@ -1,2 +1,3 @@
require 'uberkit/displayer'
require 'uberkit/menu'
require 'uberkit/menu'
require 'uberkit/form'
4 changes: 4 additions & 0 deletions lib/uberkit/form.rb
@@ -0,0 +1,4 @@
require File.dirname(__FILE__) + "/forms/builder"
require File.dirname(__FILE__) + "/forms/helper"

ActionView::Base.send(:include, Uberkit::Forms::Helper)
53 changes: 53 additions & 0 deletions lib/uberkit/forms/builder.rb
@@ -0,0 +1,53 @@
class Uberkit::Forms::Builder < ActionView::Helpers::FormBuilder
include ActionView::Helpers::CaptureHelper
include ActionView::Helpers::TextHelper
include ActionView::Helpers::UrlHelper
include ActionView::Helpers::TagHelper

helpers = field_helpers + %w(date_select datetime_select time_select select html_area state_select country_select) - %w(hidden_field label fields_for)

helpers.each do |name|
define_method(name) do |field, *args|
options = args.extract_options!
class_names = array_from_classes(options[:class])
class_names << name
options[:class] = class_names.join(" ")
args << options
generic_field(options[:label],field,super(field,*args),{:description => options.delete(:description), :help => options.delete(:help), :required => options.delete(:required)})
end
end

def generic_field(label_text,field,content,options = {})
required = options.delete(:required)
content_tag(:div, :class => "field_row#{' required' if required}#{' labelless' if label_text == ""}") do
ret = label(field, (label_text || field.to_s.titleize).to_s + ":") unless label_text == ""
ret << content
ret << content_tag(:span, options.delete(:help), :class => "help") if options[:help]
ret << content_tag(:span, options.delete(:description), :class => "description") if options[:description]
ret << "<br/>"
ret
end
end

def submit(text)
content_tag(:button, text, :type => "submit")
end

def custom(options = {}, &block)
concat("<div class='field_row#{' labelless' unless options[:label]}'>#{"<label#{" for='#{options[:for]}'" if options[:for]}>#{options[:label] + ":" if options[:label]}</label>" if options[:label]}<div class='pseudo_field'>",block.binding)
yield
concat("</div> <br/></div>",block.binding)
end

def array_from_classes(html_classes)
html_classes ? html_classes.split(" ") : []
end

def fieldset(legend=nil,&block)
concat("<fieldset>#{"<legend>#{legend}</legend>" if legend}",block.binding)
yield
concat("</fieldset>",block.binding)
end

def is_haml?; false end
end
22 changes: 22 additions & 0 deletions lib/uberkit/forms/helper.rb
@@ -0,0 +1,22 @@
module Uberkit::Forms::Helper
def parse_options(*args)
options = args.extract_options!
options.merge!(:builder => Uberkit::Forms::Builder)
options[:html] ||= {}
class_names = options[:html][:class] ? options[:html][:class].split(" ") : []
class_names << "uberform"
class_names << options.delete(:kind).to_s
options[:html][:class] = class_names.join(" ")
args << options
end

def uberform_for(name_or_object_or_array, *args, &proc)
args = parse_options(*args)
form_for(name_or_object_or_array, *args, &proc)
end

def remote_uberform_for(name_or_object_or_array, *args, &proc)
args = parse_options(*args)
remote_form_for(name_or_object_or_array, *args, &proc)
end
end
6 changes: 3 additions & 3 deletions uberkit.gemspec
@@ -1,11 +1,11 @@
Gem::Specification.new do |s|
s.name = "uberkit"
s.version = "0.0.1"
s.date = "2008-07-07"
s.version = "0.0.2"
s.date = "2008-07-14"
s.summary = "A Rails plugin for a common set of UI tools and helpers for building interfaces."
s.email = "michael@intridea.com"
s.homepage = "http://www.actsascommunity.com/projects/uberkit"
s.description = "UberKit is a set of tools for common UI problems in Rails including menus."
s.description = "UberKit is a set of tools for common UI problems in Rails including menus and forms."
s.has_rdoc = false
s.authors = ["Michael Bleigh"]
s.files = [ "MIT-LICENSE",
Expand Down

0 comments on commit c219287

Please sign in to comment.