Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added IconishSegmentsInput support: use :as => :iconish_segments to c…

…reate input groups

Input groups provide an easy way to give more context to your inputs.
  • Loading branch information...
commit 7b37cb06677a0617d79a8ac87cf49bab0242e106 1 parent 74fff65
@trimentor authored
View
1  lib/formtastic/helpers/input_helper.rb
@@ -105,6 +105,7 @@ module InputHelper
# * `:time_zone` (see {Inputs::TimeZoneInput})
# * `:time_select` (see {Inputs::TimeSelectInput})
# * `:url` (see {Inputs::UrlInput})
+ # * `:iconish_segments` (see {Inputs::IconishSegmentsInput})
#
# Calling `:as => :string` (for example) will call `#to_html` on a new instance of
# `Formtastic::Inputs::StringInput`. Before this, Formtastic will try to instantiate a top-level
View
1  lib/formtastic/inputs.rb
@@ -32,6 +32,7 @@ module Inputs
autoload :TimeZoneInput
autoload :Timeish
autoload :UrlInput
+ autoload :IconishSegmentsInput
end
end
View
72 lib/formtastic/inputs/iconish_segments_input.rb
@@ -0,0 +1,72 @@
+module Formtastic
+ module Inputs
+ class IconishSegmentsInput
+ include Base
+ include Base::Stringish
+
+ def to_html
+ input_wrapping do
+ label_html <<
+ iconish_segments_controls
+ end
+ end
+
+ def iconish_segments_controls
+ iconish_segments_wrapping do
+ builder.text_field(method, input_html_options)
+ end
+ end
+
+ def iconish_segments_wrapping(&block)
+ template.content_tag(:div,
+ iconish_segments(&block),
+ iconish_segments_wrapping_html_options
+ )
+ end
+
+ def iconish_segments(&block)
+ [add_on_prepend, template.capture(&block), add_on_append].compact.join("\n").html_safe
+ end
+
+ def iconish_segments_wrapping_html_options
+ {:class => iconish_segments_wrapping_classes}
+ end
+
+ def iconish_segments_wrapping_classes
+ opt = options
+ classes = ['iconish-segments-controls']
+ classes << 'input-prepend' if opt[:input_prepend]
+ classes << 'input-append' if opt[:input_append]
+ classes.join(' ')
+ end
+
+ def add_on_prepend
+ add_on_from_options(:input_prepend)
+ end
+
+ def add_on_append
+ add_on_from_options(:input_append)
+ end
+
+ def add_on_from_options(key)
+ if opt = options[key]
+ content = if opt.is_a?(Proc)
+ opt.call
+ else
+ opt.to_s
+ end
+ add_on(content)
+ end
+ end
+
+ def add_on(content)
+ if content =~ /^<button.*>/
+ content
+ else
+ template.content_tag(:span, content, :class => 'add-on')
+ end
+ end
+ end
+
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.