Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added new :intro option, which is like a hint, but comes before the l…

…abel.
  • Loading branch information...
commit b2e34afe6d2a5dc303ae65958aac13ed2c2357d9 1 parent 02db6f9
@justinfrench justinfrench authored
View
5 README.textile
@@ -393,7 +393,7 @@ Basic localization (labels only, with ActiveRecord):
h3. Enhanced Localization (Formtastic I18n API)
-Formtastic supports localized *labels*, *hints*, *legends*, *actions* using the I18n API for more advanced usage. Your forms can now be DRYer and more flexible than ever, and still fully localized. This is how:
+Formtastic supports localized *labels*, *hints*, *intros*, *legends*, *actions* using the I18n API for more advanced usage. Your forms can now be DRYer and more flexible than ever, and still fully localized. This is how:
*1. Enable I18n lookups by default (@config/initializers/formtastic.rb@):*
@@ -418,6 +418,9 @@ Formtastic supports localized *labels*, *hints*, *legends*, *actions* using the
post:
title: "Choose a good title for your post."
body: "Write something inspiring here."
+ intros:
+ user:
+ login: "Choose a login name for your account"
placeholders:
post:
title: "Title your post"
View
9 app/assets/stylesheets/formtastic.css
@@ -168,6 +168,15 @@ This stylesheet forms part of the Formtastic Rails Plugin
}
+/* INLINE INTRO
+--------------------------------------------------------------------------------------------------*/
+.formtastic .input .intro {
+ color:#000;
+ font-weight:bold;
+ margin:0 0 0.5em 25%;
+}
+
+
/* INLINE ERRORS
--------------------------------------------------------------------------------------------------*/
.formtastic .inline-errors {
View
1  lib/formtastic/form_builder.rb
@@ -30,6 +30,7 @@ def self.configure(name, value = nil)
configure :default_inline_error_class, 'inline-errors'
configure :default_error_list_class, 'errors'
configure :default_hint_class, 'inline-hints'
+ configure :default_intro_class, 'intro'
configure :use_required_attribute, false
configure :perform_browser_validations, false
View
11 lib/formtastic/helpers/input_helper.rb
@@ -125,6 +125,9 @@ module InputHelper
# @option options :hint [String, Symbol, false]
# Override hint text
#
+ # @option options :intro [String, Symbol, false]
+ # Override intro text
+ #
# @option options :required [Boolean]
# Override to mark the input as required (or not) — adds a required/optional class to the wrapper, and a HTML5 required attribute to the `<input>`
#
@@ -170,7 +173,7 @@ module InputHelper
# @todo Can we kill `:hint_class` & `:error_class`? What's the use case for input-by-input? Shift to config or burn!
# @todo Can we kill `:group_by` & `:group_label`? Should be done with :collection => grouped_options_for_select(...)
# @todo Can we kill `:find_options`? Should be done with MyModel.some_scope.where(...).order(...).whatever_scope
- # @todo Can we kill `:label`, `:hint` & `:prompt`? All strings could be shifted to i18n!
+ # @todo Can we kill `:label`, `:hint` & `:prompt` & `:intro`? All strings could be shifted to i18n!
#
# @example Accept all default options
# <%= f.input :title %>
@@ -190,6 +193,12 @@ module InputHelper
# @example Disabling the hint with false, even if an i18n translation exists
# <%= f.input :title, :hint => false %>
#
+ # @example Changing or adding an intro with a String
+ # <%= f.input :title, :intro => "A title is important because..." %>
+ #
+ # @example Disabling the intro with false, even if an i18n translation exists
+ # <%= f.input :title, :intro => false %>
+ #
# @example Marking a field as required or not (even if validations do not enforce it)
# <%= f.input :title, :required => true %>
# <%= f.input :title, :required => false %>
View
2  lib/formtastic/inputs/base.rb
@@ -40,6 +40,7 @@ def removed_option!(old_option_name)
autoload :Fileish
autoload :GroupedCollections
autoload :Hints
+ autoload :Intros
autoload :Html
autoload :Labelling
autoload :Naming
@@ -56,6 +57,7 @@ def removed_option!(old_option_name)
include Database
include Errors
include Hints
+ include Intros
include Naming
include Validations
include Fileish
View
4 lib/formtastic/inputs/base/hints.rb
@@ -12,7 +12,7 @@ def hint_html
)
end
end
-
+
def hint?
!hint_text.blank? && !hint_text.kind_of?(Hash)
end
@@ -24,7 +24,7 @@ def hint_text
def hint_text_from_options
options[:hint]
end
-
+
end
end
end
View
31 lib/formtastic/inputs/base/intros.rb
@@ -0,0 +1,31 @@
+module Formtastic
+ module Inputs
+ module Base
+ module Intros
+
+ def intro_html
+ if intro?
+ template.content_tag(
+ :p,
+ Formtastic::Util.html_safe(intro_text),
+ :class => builder.default_intro_class
+ )
+ end
+ end
+
+ def intro?
+ !intro_text.blank? && !intro_text.kind_of?(Hash)
+ end
+
+ def intro_text
+ localized_string(method, options[:intro], :intro)
+ end
+
+ def intro_text_from_options
+ options[:intro]
+ end
+
+ end
+ end
+ end
+end
View
2  lib/formtastic/inputs/base/wrapping.rb
@@ -8,7 +8,7 @@ module Wrapping
# errors before the body of the input).
def input_wrapping(&block)
template.content_tag(:li,
- [template.capture(&block), error_html, hint_html].join("\n").html_safe,
+ [intro_html, template.capture(&block), error_html, hint_html].join("\n").html_safe,
wrapper_html_options
)
end
View
4 lib/formtastic/inputs/hidden_input.rb
@@ -57,6 +57,10 @@ def hint_html
""
end
+ def intro_html
+ ""
+ end
+
def hint?
false
end
View
1  lib/formtastic/inputs/string_input.rb
@@ -30,7 +30,6 @@ class StringInput
include Base
include Base::Stringish
include Base::Placeholder
-
end
end
end
View
1  sample/basic_inputs.html
@@ -34,6 +34,7 @@
</li>
<li class="input url stringish optional" id="gem_url_input">
+ <p class="intro">Enter a URL</p>
<label class="label" for="gem_url">URL</label>
<input id="gem_url" name="gem[url]" type="url">
<p class="inline-hints">Example: https://github.com/justinfrench/formtastic</p>
View
104 spec/helpers/input_helper_spec.rb
@@ -790,6 +790,110 @@ def length_should_be_required(options)
end
+ describe ':intro option' do
+
+ describe 'when a string' do
+ let(:intro) { "some introductory text for the input" }
+
+ it 'should be passed down to the paragraph tag' do
+ concat(semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:title, :intro => intro))
+ end)
+ output_buffer.should have_tag("form li p.intro", intro)
+ end
+ end
+
+ describe 'when true' do
+
+ before do
+ @localized_intro_text = "This is the localized intro."
+ @default_localized_intro_text = "This is the default localized intro."
+ end
+
+ after do
+ ::I18n.backend.reload!
+ end
+
+ describe 'and generic localization is provided' do
+
+ it 'should render an intro paragraph containing the localized intro' do
+ with_config :i18n_lookups_by_default, false do
+ ::I18n.backend.store_translations :en,
+ :formtastic => {
+ :intros => {
+ :title => @default_localized_intro_text,
+ }
+ }
+ concat(semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:title, :intro => true))
+ end)
+ output_buffer.should have_tag('form li p.intro', @default_localized_intro_text)
+ end
+ end
+
+ end
+
+ describe "and specific localization is provided" do
+ it 'should render an into paragraph containing a specific intro' do
+ with_config :i18n_lookups_by_default, false do
+ ::I18n.backend.store_translations :en,
+ :formtastic => {
+ :intros => {
+ :title => @default_localized_intro_text,
+ :post => {
+ :title => @localized_intro_text
+ }
+ }
+ }
+ concat(semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:title, :intro => true))
+ end)
+ output_buffer.should have_tag('form li p.intro', @localized_intro_text)
+ end
+ end
+ end
+
+ describe "and no translations exist" do
+ it 'should not render an intro paragraph' do
+ concat(semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:title, :intro => true))
+ end)
+ output_buffer.should_not have_tag('form li p.intro')
+ end
+ end
+
+ end
+
+ describe "when false" do
+ it "should not render an intro paragraph, even if a translation exists" do
+ ::I18n.backend.store_translations :en,
+ :formtastic => {
+ :intros => {
+ :title => @default_localized_intro_text,
+ :post => {
+ :title => @localized_intro_text
+ }
+ }
+ }
+ with_config :i18n_lookups_by_default, false do
+ concat(semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:title, :intro => false))
+ end)
+ output_buffer.should_not have_tag('form li p.intro')
+ end
+ end
+ end
+
+ describe "when not provided and translation exists and lookups should be performed" do
+ # TODO
+ end
+
+ describe "when not provided and translation exists and lookups should not be performed" do
+ # TODO
+ end
+
+ end
+
describe ':wrapper_html option' do
describe 'when provided' do
View
8 spec/inputs/hidden_input_spec.rb
@@ -71,6 +71,14 @@
output_buffer.should_not have_tag("form li p.inline-hints")
output_buffer.should_not have_tag("form li ul.hints")
end
+
+ it "should not render inline intros" do
+ concat(semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:secret, :as => :hidden, :intro => "all your base are belong to use"))
+ end)
+
+ output_buffer.should_not have_tag("form li p.intro")
+ end
describe "when namespace is provided" do
Please sign in to comment.
Something went wrong with that request. Please try again.