diff --git a/README b/README index 405acdf..bd8efc1 100644 --- a/README +++ b/README @@ -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... + +
+
+ + +
+
+
+ + +
+
+ +
+ +=== 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... + +
+ + + Only a-z and underscores. + The name you will use to log in. +
+
+ +=== 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... + +
+ +
+ +
+
+
+ UberMenu -------- diff --git a/lib/uberkit.rb b/lib/uberkit.rb index 29ceab7..bea5000 100644 --- a/lib/uberkit.rb +++ b/lib/uberkit.rb @@ -1,2 +1,3 @@ require 'uberkit/displayer' -require 'uberkit/menu' \ No newline at end of file +require 'uberkit/menu' +require 'uberkit/form' \ No newline at end of file diff --git a/lib/uberkit/form.rb b/lib/uberkit/form.rb new file mode 100644 index 0000000..55fdac4 --- /dev/null +++ b/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) \ No newline at end of file diff --git a/lib/uberkit/forms/builder.rb b/lib/uberkit/forms/builder.rb new file mode 100644 index 0000000..2d2d897 --- /dev/null +++ b/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 << "
" + ret + end + end + + def submit(text) + content_tag(:button, text, :type => "submit") + end + + def custom(options = {}, &block) + concat("
#{"#{options[:label] + ":" if options[:label]}" if options[:label]}
",block.binding) + yield + concat("

",block.binding) + end + + def array_from_classes(html_classes) + html_classes ? html_classes.split(" ") : [] + end + + def fieldset(legend=nil,&block) + concat("
#{"#{legend}" if legend}",block.binding) + yield + concat("
",block.binding) + end + + def is_haml?; false end +end \ No newline at end of file diff --git a/lib/uberkit/forms/helper.rb b/lib/uberkit/forms/helper.rb new file mode 100644 index 0000000..ee50500 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/uberkit.gemspec b/uberkit.gemspec index 1ac7719..56b1543 100644 --- a/uberkit.gemspec +++ b/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",