Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

…abel.
  • Loading branch information...
commit b2e34afe6d2a5dc303ae65958aac13ed2c2357d9 1 parent 02db6f9
Justin French authored February 22, 2012
5  README.textile
Source Rendered
@@ -393,7 +393,7 @@ Basic localization (labels only, with ActiveRecord):
393 393
 
394 394
 h3. Enhanced Localization (Formtastic I18n API)
395 395
 
396  
-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:
  396
+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:
397 397
 
398 398
 *1. Enable I18n lookups by default (@config/initializers/formtastic.rb@):*
399 399
 
@@ -418,6 +418,9 @@ Formtastic supports localized *labels*, *hints*, *legends*, *actions* using the
418 418
         post:
419 419
           title: "Choose a good title for your post."
420 420
           body: "Write something inspiring here."
  421
+      intros:
  422
+        user:
  423
+          login: "Choose a login name for your account"
421 424
       placeholders:
422 425
         post:
423 426
           title: "Title your post"
9  app/assets/stylesheets/formtastic.css
@@ -168,6 +168,15 @@ This stylesheet forms part of the Formtastic Rails Plugin
168 168
 }
169 169
 
170 170
 
  171
+/* INLINE INTRO
  172
+--------------------------------------------------------------------------------------------------*/
  173
+.formtastic .input .intro {
  174
+  color:#000;
  175
+  font-weight:bold;
  176
+  margin:0 0 0.5em 25%;
  177
+}
  178
+
  179
+
171 180
 /* INLINE ERRORS
172 181
 --------------------------------------------------------------------------------------------------*/
173 182
 .formtastic .inline-errors {
1  lib/formtastic/form_builder.rb
@@ -30,6 +30,7 @@ def self.configure(name, value = nil)
30 30
     configure :default_inline_error_class, 'inline-errors'
31 31
     configure :default_error_list_class, 'errors'
32 32
     configure :default_hint_class, 'inline-hints'
  33
+    configure :default_intro_class, 'intro'
33 34
     configure :use_required_attribute, false
34 35
     configure :perform_browser_validations, false
35 36
 
11  lib/formtastic/helpers/input_helper.rb
@@ -125,6 +125,9 @@ module InputHelper
125 125
       # @option options :hint [String, Symbol, false]
126 126
       #   Override hint text
127 127
       #
  128
+      # @option options :intro [String, Symbol, false]
  129
+      #   Override intro text
  130
+      #
128 131
       # @option options :required [Boolean]
129 132
       #   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>`
130 133
       #
@@ -170,7 +173,7 @@ module InputHelper
170 173
       # @todo Can we kill `:hint_class` & `:error_class`? What's the use case for input-by-input? Shift to config or burn!
171 174
       # @todo Can we kill `:group_by` & `:group_label`? Should be done with :collection => grouped_options_for_select(...)
172 175
       # @todo Can we kill `:find_options`? Should be done with MyModel.some_scope.where(...).order(...).whatever_scope
173  
-      # @todo Can we kill `:label`, `:hint` & `:prompt`? All strings could be shifted to i18n!
  176
+      # @todo Can we kill `:label`, `:hint` & `:prompt` & `:intro`? All strings could be shifted to i18n!
174 177
       #
175 178
       # @example Accept all default options
176 179
       #   <%= f.input :title %>
@@ -190,6 +193,12 @@ module InputHelper
190 193
       # @example Disabling the hint with false, even if an i18n translation exists
191 194
       #   <%= f.input :title, :hint => false  %>
192 195
       #
  196
+      # @example Changing or adding an intro with a String
  197
+      #   <%= f.input :title, :intro => "A title is important because..." %>
  198
+      #
  199
+      # @example Disabling the intro with false, even if an i18n translation exists
  200
+      #   <%= f.input :title, :intro => false  %>
  201
+      #
193 202
       # @example Marking a field as required or not (even if validations do not enforce it)
194 203
       #   <%= f.input :title, :required => true  %>
195 204
       #   <%= f.input :title, :required => false  %>
2  lib/formtastic/inputs/base.rb
@@ -40,6 +40,7 @@ def removed_option!(old_option_name)
40 40
       autoload :Fileish
41 41
       autoload :GroupedCollections
42 42
       autoload :Hints
  43
+      autoload :Intros
43 44
       autoload :Html
44 45
       autoload :Labelling
45 46
       autoload :Naming
@@ -56,6 +57,7 @@ def removed_option!(old_option_name)
56 57
       include Database
57 58
       include Errors
58 59
       include Hints
  60
+      include Intros
59 61
       include Naming
60 62
       include Validations
61 63
       include Fileish
4  lib/formtastic/inputs/base/hints.rb
@@ -12,7 +12,7 @@ def hint_html
12 12
             )
13 13
           end
14 14
         end
15  
-        
  15
+
16 16
         def hint?
17 17
           !hint_text.blank? && !hint_text.kind_of?(Hash)
18 18
         end
@@ -24,7 +24,7 @@ def hint_text
24 24
         def hint_text_from_options
25 25
           options[:hint]
26 26
         end
27  
-
  27
+        
28 28
       end
29 29
     end
30 30
   end
31  lib/formtastic/inputs/base/intros.rb
... ...
@@ -0,0 +1,31 @@
  1
+module Formtastic
  2
+  module Inputs
  3
+    module Base
  4
+      module Intros
  5
+        
  6
+        def intro_html
  7
+          if intro?
  8
+            template.content_tag(
  9
+              :p, 
  10
+              Formtastic::Util.html_safe(intro_text), 
  11
+              :class => builder.default_intro_class
  12
+            )
  13
+          end
  14
+        end
  15
+
  16
+        def intro?
  17
+          !intro_text.blank? && !intro_text.kind_of?(Hash)
  18
+        end
  19
+        
  20
+        def intro_text
  21
+          localized_string(method, options[:intro], :intro)
  22
+        end
  23
+        
  24
+        def intro_text_from_options
  25
+          options[:intro]
  26
+        end
  27
+
  28
+      end
  29
+    end
  30
+  end
  31
+end
2  lib/formtastic/inputs/base/wrapping.rb
@@ -8,7 +8,7 @@ module Wrapping
8 8
         # errors before the body of the input).
9 9
         def input_wrapping(&block)
10 10
           template.content_tag(:li, 
11  
-            [template.capture(&block), error_html, hint_html].join("\n").html_safe, 
  11
+            [intro_html, template.capture(&block), error_html, hint_html].join("\n").html_safe, 
12 12
             wrapper_html_options
13 13
           )
14 14
         end
4  lib/formtastic/inputs/hidden_input.rb
@@ -57,6 +57,10 @@ def hint_html
57 57
         ""
58 58
       end
59 59
       
  60
+      def intro_html
  61
+        ""
  62
+      end
  63
+      
60 64
       def hint?
61 65
         false
62 66
       end
1  lib/formtastic/inputs/string_input.rb
@@ -30,7 +30,6 @@ class StringInput
30 30
       include Base
31 31
       include Base::Stringish
32 32
       include Base::Placeholder
33  
-      
34 33
     end
35 34
   end
36 35
 end
1  sample/basic_inputs.html
@@ -34,6 +34,7 @@
34 34
         </li>
35 35
 
36 36
         <li class="input url stringish optional" id="gem_url_input">
  37
+          <p class="intro">Enter a URL</p>
37 38
           <label class="label" for="gem_url">URL</label>
38 39
           <input id="gem_url" name="gem[url]" type="url">
39 40
           <p class="inline-hints">Example: https://github.com/justinfrench/formtastic</p>
104  spec/helpers/input_helper_spec.rb
@@ -790,6 +790,110 @@ def length_should_be_required(options)
790 790
 
791 791
     end
792 792
 
  793
+    describe ':intro option' do
  794
+
  795
+      describe 'when a string' do
  796
+        let(:intro) { "some introductory text for the input" }
  797
+        
  798
+        it 'should be passed down to the paragraph tag' do
  799
+          concat(semantic_form_for(@new_post) do |builder|
  800
+            concat(builder.input(:title, :intro => intro))
  801
+          end)
  802
+          output_buffer.should have_tag("form li p.intro", intro)
  803
+        end
  804
+      end
  805
+      
  806
+      describe 'when true' do
  807
+        
  808
+        before do
  809
+          @localized_intro_text = "This is the localized intro."
  810
+          @default_localized_intro_text = "This is the default localized intro."
  811
+        end
  812
+        
  813
+        after do
  814
+          ::I18n.backend.reload!
  815
+        end
  816
+        
  817
+        describe 'and generic localization is provided' do
  818
+          
  819
+          it 'should render an intro paragraph containing the localized intro' do
  820
+            with_config :i18n_lookups_by_default, false do
  821
+              ::I18n.backend.store_translations :en,
  822
+              :formtastic => {
  823
+                  :intros => {
  824
+                    :title => @default_localized_intro_text,
  825
+                   }
  826
+                }
  827
+              concat(semantic_form_for(@new_post) do |builder|
  828
+                concat(builder.input(:title, :intro => true))
  829
+              end)
  830
+              output_buffer.should have_tag('form li p.intro', @default_localized_intro_text)
  831
+            end
  832
+          end
  833
+          
  834
+        end
  835
+        
  836
+        describe "and specific localization is provided" do
  837
+					it 'should render an into paragraph containing a specific intro' do
  838
+            with_config :i18n_lookups_by_default, false do
  839
+              ::I18n.backend.store_translations :en,
  840
+              :formtastic => {
  841
+                  :intros => {
  842
+                    :title => @default_localized_intro_text,
  843
+                    :post => {
  844
+                      :title => @localized_intro_text
  845
+                     }
  846
+                  }
  847
+                }
  848
+              concat(semantic_form_for(@new_post) do |builder|
  849
+                concat(builder.input(:title, :intro => true))
  850
+              end)
  851
+              output_buffer.should have_tag('form li p.intro', @localized_intro_text)
  852
+            end
  853
+          end
  854
+        end
  855
+        
  856
+        describe "and no translations exist" do
  857
+          it 'should not render an intro paragraph' do
  858
+            concat(semantic_form_for(@new_post) do |builder|
  859
+              concat(builder.input(:title, :intro => true))
  860
+            end)
  861
+            output_buffer.should_not have_tag('form li p.intro')
  862
+          end
  863
+        end
  864
+        
  865
+      end
  866
+      
  867
+      describe "when false" do
  868
+        it "should not render an intro paragraph, even if a translation exists" do
  869
+          ::I18n.backend.store_translations :en,
  870
+          :formtastic => {
  871
+              :intros => {
  872
+                :title => @default_localized_intro_text,
  873
+                :post => {
  874
+                  :title => @localized_intro_text
  875
+                 }
  876
+              }
  877
+            }
  878
+          with_config :i18n_lookups_by_default, false do
  879
+            concat(semantic_form_for(@new_post) do |builder|
  880
+              concat(builder.input(:title, :intro => false))
  881
+            end)
  882
+            output_buffer.should_not have_tag('form li p.intro')
  883
+          end
  884
+        end
  885
+      end
  886
+      
  887
+      describe "when not provided and translation exists and lookups should be performed" do
  888
+        # TODO
  889
+      end
  890
+      
  891
+      describe "when not provided and translation exists and lookups should not be performed" do
  892
+        # TODO
  893
+      end
  894
+      
  895
+    end
  896
+
793 897
     describe ':wrapper_html option' do
794 898
 
795 899
       describe 'when provided' do
8  spec/inputs/hidden_input_spec.rb
@@ -71,6 +71,14 @@
71 71
     output_buffer.should_not have_tag("form li p.inline-hints")
72 72
     output_buffer.should_not have_tag("form li ul.hints")
73 73
   end
  74
+  
  75
+  it "should not render inline intros" do
  76
+    concat(semantic_form_for(@new_post) do |builder|
  77
+      concat(builder.input(:secret, :as => :hidden, :intro => "all your base are belong to use"))
  78
+    end)
  79
+
  80
+    output_buffer.should_not have_tag("form li p.intro")
  81
+  end
74 82
 
75 83
   describe "when namespace is provided" do
76 84
 

0 notes on commit b2e34af

Please sign in to comment.
Something went wrong with that request. Please try again.