formtastic 1.2.4 → 3.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (189) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +46 -0
  5. data/.yardopts +1 -0
  6. data/Appraisals +43 -0
  7. data/CHANGELOG +54 -0
  8. data/DEPRECATIONS +52 -0
  9. data/Gemfile +3 -0
  10. data/README.md +629 -0
  11. data/RELEASE_PROCESS +6 -0
  12. data/Rakefile +35 -0
  13. data/app/assets/stylesheets/formtastic.css +289 -0
  14. data/app/assets/stylesheets/formtastic_ie6.css +33 -0
  15. data/app/assets/stylesheets/formtastic_ie7.css +23 -0
  16. data/formtastic.gemspec +42 -0
  17. data/gemfiles/rails_3.2.gemfile +9 -0
  18. data/gemfiles/rails_4.0.4.gemfile +8 -0
  19. data/gemfiles/rails_4.1.gemfile +8 -0
  20. data/gemfiles/rails_4.2.gemfile +8 -0
  21. data/gemfiles/rails_4.gemfile +8 -0
  22. data/gemfiles/rails_5.0.gemfile +8 -0
  23. data/gemfiles/rails_edge.gemfile +15 -0
  24. data/lib/formtastic.rb +40 -1945
  25. data/lib/formtastic/action_class_finder.rb +18 -0
  26. data/lib/formtastic/actions.rb +11 -0
  27. data/lib/formtastic/actions/base.rb +156 -0
  28. data/lib/formtastic/actions/button_action.rb +67 -0
  29. data/lib/formtastic/actions/buttonish.rb +17 -0
  30. data/lib/formtastic/actions/input_action.rb +70 -0
  31. data/lib/formtastic/actions/link_action.rb +88 -0
  32. data/lib/formtastic/deprecation.rb +42 -0
  33. data/lib/formtastic/engine.rb +11 -0
  34. data/lib/formtastic/form_builder.rb +124 -0
  35. data/lib/formtastic/helpers.rb +16 -0
  36. data/lib/formtastic/helpers/action_helper.rb +162 -0
  37. data/lib/formtastic/helpers/actions_helper.rb +168 -0
  38. data/lib/formtastic/helpers/enum.rb +13 -0
  39. data/lib/formtastic/helpers/errors_helper.rb +81 -0
  40. data/lib/formtastic/helpers/fieldset_wrapper.rb +80 -0
  41. data/lib/formtastic/helpers/file_column_detection.rb +16 -0
  42. data/lib/formtastic/helpers/form_helper.rb +203 -0
  43. data/lib/formtastic/helpers/input_helper.rb +407 -0
  44. data/lib/formtastic/helpers/inputs_helper.rb +411 -0
  45. data/lib/formtastic/helpers/reflection.rb +37 -0
  46. data/lib/formtastic/html_attributes.rb +32 -0
  47. data/lib/formtastic/i18n.rb +4 -2
  48. data/lib/formtastic/input_class_finder.rb +18 -0
  49. data/lib/formtastic/inputs.rb +39 -0
  50. data/lib/formtastic/inputs/base.rb +76 -0
  51. data/lib/formtastic/inputs/base/associations.rb +31 -0
  52. data/lib/formtastic/inputs/base/choices.rb +108 -0
  53. data/lib/formtastic/inputs/base/collections.rb +159 -0
  54. data/lib/formtastic/inputs/base/database.rb +22 -0
  55. data/lib/formtastic/inputs/base/datetime_pickerish.rb +85 -0
  56. data/lib/formtastic/inputs/base/errors.rb +58 -0
  57. data/lib/formtastic/inputs/base/fileish.rb +23 -0
  58. data/lib/formtastic/inputs/base/hints.rb +31 -0
  59. data/lib/formtastic/inputs/base/html.rb +53 -0
  60. data/lib/formtastic/inputs/base/labelling.rb +52 -0
  61. data/lib/formtastic/inputs/base/naming.rb +42 -0
  62. data/lib/formtastic/inputs/base/numeric.rb +50 -0
  63. data/lib/formtastic/inputs/base/options.rb +17 -0
  64. data/lib/formtastic/inputs/base/placeholder.rb +17 -0
  65. data/lib/formtastic/inputs/base/stringish.rb +38 -0
  66. data/lib/formtastic/inputs/base/timeish.rb +241 -0
  67. data/lib/formtastic/inputs/base/validations.rb +215 -0
  68. data/lib/formtastic/inputs/base/wrapping.rb +50 -0
  69. data/lib/formtastic/inputs/boolean_input.rb +118 -0
  70. data/lib/formtastic/inputs/check_boxes_input.rb +197 -0
  71. data/lib/formtastic/inputs/color_input.rb +42 -0
  72. data/lib/formtastic/inputs/country_input.rb +86 -0
  73. data/lib/formtastic/inputs/datalist_input.rb +41 -0
  74. data/lib/formtastic/inputs/date_picker_input.rb +93 -0
  75. data/lib/formtastic/inputs/date_select_input.rb +34 -0
  76. data/lib/formtastic/inputs/datetime_picker_input.rb +103 -0
  77. data/lib/formtastic/inputs/datetime_select_input.rb +12 -0
  78. data/lib/formtastic/inputs/email_input.rb +41 -0
  79. data/lib/formtastic/inputs/file_input.rb +42 -0
  80. data/lib/formtastic/inputs/hidden_input.rb +62 -0
  81. data/lib/formtastic/inputs/number_input.rb +88 -0
  82. data/lib/formtastic/inputs/password_input.rb +41 -0
  83. data/lib/formtastic/inputs/phone_input.rb +42 -0
  84. data/lib/formtastic/inputs/radio_input.rb +163 -0
  85. data/lib/formtastic/inputs/range_input.rb +95 -0
  86. data/lib/formtastic/inputs/search_input.rb +41 -0
  87. data/lib/formtastic/inputs/select_input.rb +235 -0
  88. data/lib/formtastic/inputs/string_input.rb +36 -0
  89. data/lib/formtastic/inputs/text_input.rb +48 -0
  90. data/lib/formtastic/inputs/time_picker_input.rb +99 -0
  91. data/lib/formtastic/inputs/time_select_input.rb +38 -0
  92. data/lib/formtastic/inputs/time_zone_input.rb +58 -0
  93. data/lib/formtastic/inputs/url_input.rb +41 -0
  94. data/lib/formtastic/localized_string.rb +17 -0
  95. data/lib/formtastic/localizer.rb +152 -0
  96. data/lib/formtastic/namespaced_class_finder.rb +99 -0
  97. data/lib/formtastic/util.rb +35 -16
  98. data/lib/formtastic/version.rb +3 -0
  99. data/lib/generators/formtastic/form/form_generator.rb +64 -37
  100. data/lib/generators/formtastic/input/input_generator.rb +46 -0
  101. data/lib/generators/formtastic/install/install_generator.rb +13 -5
  102. data/lib/generators/templates/_form.html.erb +10 -4
  103. data/lib/generators/templates/_form.html.haml +8 -4
  104. data/lib/generators/templates/_form.html.slim +8 -0
  105. data/lib/generators/templates/formtastic.rb +77 -44
  106. data/lib/generators/templates/input.rb +19 -0
  107. data/lib/locale/en.yml +3 -0
  108. data/sample/basic_inputs.html +224 -0
  109. data/sample/config.ru +69 -0
  110. data/sample/index.html +14 -0
  111. data/spec/action_class_finder_spec.rb +12 -0
  112. data/spec/actions/button_action_spec.rb +63 -0
  113. data/spec/actions/generic_action_spec.rb +521 -0
  114. data/spec/actions/input_action_spec.rb +59 -0
  115. data/spec/actions/link_action_spec.rb +92 -0
  116. data/spec/builder/custom_builder_spec.rb +116 -0
  117. data/spec/builder/error_proc_spec.rb +27 -0
  118. data/spec/builder/semantic_fields_for_spec.rb +142 -0
  119. data/spec/fast_spec_helper.rb +12 -0
  120. data/spec/generators/formtastic/form/form_generator_spec.rb +131 -0
  121. data/spec/generators/formtastic/input/input_generator_spec.rb +124 -0
  122. data/spec/generators/formtastic/install/install_generator_spec.rb +47 -0
  123. data/spec/helpers/action_helper_spec.rb +19 -0
  124. data/spec/helpers/actions_helper_spec.rb +143 -0
  125. data/spec/helpers/form_helper_spec.rb +218 -0
  126. data/spec/helpers/input_helper_spec.rb +6 -0
  127. data/spec/helpers/inputs_helper_spec.rb +655 -0
  128. data/spec/helpers/namespaced_action_helper_spec.rb +43 -0
  129. data/spec/helpers/namespaced_input_helper_spec.rb +36 -0
  130. data/spec/helpers/reflection_helper_spec.rb +32 -0
  131. data/spec/helpers/semantic_errors_helper_spec.rb +112 -0
  132. data/spec/i18n_spec.rb +210 -0
  133. data/spec/input_class_finder_spec.rb +10 -0
  134. data/spec/inputs/base/collections_spec.rb +76 -0
  135. data/spec/inputs/base/validations_spec.rb +342 -0
  136. data/spec/inputs/boolean_input_spec.rb +254 -0
  137. data/spec/inputs/check_boxes_input_spec.rb +546 -0
  138. data/spec/inputs/color_input_spec.rb +97 -0
  139. data/spec/inputs/country_input_spec.rb +133 -0
  140. data/spec/inputs/custom_input_spec.rb +55 -0
  141. data/spec/inputs/datalist_input_spec.rb +61 -0
  142. data/spec/inputs/date_picker_input_spec.rb +449 -0
  143. data/spec/inputs/date_select_input_spec.rb +235 -0
  144. data/spec/inputs/datetime_picker_input_spec.rb +490 -0
  145. data/spec/inputs/datetime_select_input_spec.rb +193 -0
  146. data/spec/inputs/email_input_spec.rb +85 -0
  147. data/spec/inputs/file_input_spec.rb +89 -0
  148. data/spec/inputs/hidden_input_spec.rb +135 -0
  149. data/spec/inputs/include_blank_spec.rb +78 -0
  150. data/spec/inputs/label_spec.rb +149 -0
  151. data/spec/inputs/number_input_spec.rb +815 -0
  152. data/spec/inputs/password_input_spec.rb +99 -0
  153. data/spec/inputs/phone_input_spec.rb +85 -0
  154. data/spec/inputs/placeholder_spec.rb +71 -0
  155. data/spec/inputs/radio_input_spec.rb +328 -0
  156. data/spec/inputs/range_input_spec.rb +505 -0
  157. data/spec/inputs/readonly_spec.rb +50 -0
  158. data/spec/inputs/search_input_spec.rb +84 -0
  159. data/spec/inputs/select_input_spec.rb +615 -0
  160. data/spec/inputs/string_input_spec.rb +260 -0
  161. data/spec/inputs/text_input_spec.rb +187 -0
  162. data/spec/inputs/time_picker_input_spec.rb +455 -0
  163. data/spec/inputs/time_select_input_spec.rb +248 -0
  164. data/spec/inputs/time_zone_input_spec.rb +143 -0
  165. data/spec/inputs/url_input_spec.rb +85 -0
  166. data/spec/inputs/with_options_spec.rb +43 -0
  167. data/spec/localizer_spec.rb +130 -0
  168. data/spec/namespaced_class_finder_spec.rb +79 -0
  169. data/spec/spec.opts +2 -0
  170. data/spec/spec_helper.rb +525 -0
  171. data/spec/support/custom_macros.rb +564 -0
  172. data/spec/support/deprecation.rb +6 -0
  173. data/spec/support/shared_examples.rb +1313 -0
  174. data/spec/support/specialized_class_finder_shared_example.rb +27 -0
  175. data/spec/support/test_environment.rb +31 -0
  176. data/spec/util_spec.rb +66 -0
  177. metadata +434 -161
  178. data/README.textile +0 -682
  179. data/generators/form/USAGE +0 -16
  180. data/generators/form/form_generator.rb +0 -111
  181. data/generators/formtastic/formtastic_generator.rb +0 -26
  182. data/init.rb +0 -5
  183. data/lib/formtastic/layout_helper.rb +0 -12
  184. data/lib/formtastic/railtie.rb +0 -14
  185. data/lib/generators/templates/formtastic.css +0 -145
  186. data/lib/generators/templates/formtastic_changes.css +0 -14
  187. data/lib/generators/templates/rails2/_form.html.erb +0 -5
  188. data/lib/generators/templates/rails2/_form.html.haml +0 -4
  189. data/rails/init.rb +0 -2
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'with action class finder' do
4
+ include_context 'form builder'
5
+
6
+ before {
7
+ allow(Formtastic::FormBuilder)
8
+ .to receive(:action_class_finder).and_return(Formtastic::ActionClassFinder)
9
+ }
10
+
11
+ it_behaves_like 'Action Helper'
12
+
13
+ describe 'instantiating an action class' do
14
+ it "should delegate to ActionClassFinder" do
15
+ concat(semantic_form_for(@new_post) do |builder|
16
+ Formtastic::ActionClassFinder.any_instance.should_receive(:find).
17
+ with(:button).and_call_original
18
+
19
+ builder.action(:submit, :as => :button)
20
+ end)
21
+ end
22
+
23
+ describe 'when instantiated multiple times with the same action type' do
24
+ it "should be cached" do
25
+ concat(semantic_form_for(@new_post) do |builder|
26
+ Formtastic::ActionClassFinder.should_receive(:new).once.and_call_original
27
+ builder.action(:submit, :as => :button)
28
+ builder.action(:submit, :as => :button)
29
+ end)
30
+ end
31
+ end
32
+
33
+ context 'of unknown action' do
34
+ it "should try to load class named as the action" do
35
+ expect {
36
+ semantic_form_for(@new_post) do |builder|
37
+ builder.action(:destroy)
38
+ end
39
+ }.to raise_error(Formtastic::UnknownActionError, 'Unable to find action class DestroyAction')
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,36 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe 'with input class finder' do
5
+ include_context 'form builder'
6
+
7
+ before {
8
+ allow(Formtastic::FormBuilder)
9
+ .to receive(:input_class_finder).and_return(Formtastic::InputClassFinder)
10
+ }
11
+ it_behaves_like 'Input Helper' # from spec/support/shared_examples.rb
12
+
13
+
14
+ describe 'instantiating an input class' do
15
+ describe 'when instantiated multiple times with the same input type' do
16
+
17
+ it "should be cached (not calling the internal methods)" do
18
+ # TODO this is really tied to the underlying implementation
19
+ concat(semantic_form_for(@new_post) do |builder|
20
+ Formtastic::InputClassFinder.should_receive(:new).once.and_call_original
21
+ builder.input(:title, :as => :string)
22
+ builder.input(:title, :as => :string)
23
+ end)
24
+ end
25
+ end
26
+
27
+ it "should delegate to InputClassFinder" do
28
+ concat(semantic_form_for(@new_post) do |builder|
29
+ Formtastic::InputClassFinder.any_instance.should_receive(:find).
30
+ with(:string).and_call_original
31
+
32
+ builder.input(:title, :as => :string)
33
+ end)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe 'Formtastic::Helpers::Reflection' do
5
+
6
+ include FormtasticSpecHelper
7
+
8
+ before do
9
+ @output_buffer = ''
10
+ mock_everything
11
+ end
12
+
13
+ class ReflectionTester
14
+ include Formtastic::Helpers::Reflection
15
+ def initialize(model_object)
16
+ @object = model_object
17
+ end
18
+ end
19
+
20
+ context 'with an ActiveRecord object' do
21
+ it "should return association details on an ActiveRecord association" do
22
+ @reflection_tester = ReflectionTester.new(@new_post)
23
+ @reflection_tester.reflection_for(:sub_posts).should_not be_nil
24
+ end
25
+ it "should return association details on a MongoMapper association" do
26
+ @reflection_tester = ReflectionTester.new(@new_mm_post)
27
+ @reflection_tester.reflection_for(:sub_posts).should_not be_nil
28
+ end
29
+ end
30
+
31
+
32
+ end
@@ -0,0 +1,112 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe 'Formtastic::FormBuilder#semantic_errors' do
5
+
6
+ include FormtasticSpecHelper
7
+
8
+ before do
9
+ @output_buffer = ''
10
+ mock_everything
11
+ @title_errors = ['must not be blank', 'must be awesome']
12
+ @base_errors = ['base error message', 'nasty error']
13
+ @base_error = 'one base error'
14
+ @errors = double('errors')
15
+ @new_post.stub(:errors).and_return(@errors)
16
+ end
17
+
18
+ describe 'when there is only one error on base' do
19
+ before do
20
+ @errors.stub(:[]).with(errors_matcher(:base)).and_return(@base_error)
21
+ end
22
+
23
+ it 'should render an unordered list' do
24
+ semantic_form_for(@new_post) do |builder|
25
+ builder.semantic_errors.should have_tag('ul.errors li', @base_error)
26
+ end
27
+ end
28
+ end
29
+
30
+ describe 'when there is more than one error on base' do
31
+ before do
32
+ @errors.stub(:[]).with(errors_matcher(:base)).and_return(@base_errors)
33
+ end
34
+
35
+ it 'should render an unordered list' do
36
+ semantic_form_for(@new_post) do |builder|
37
+ builder.semantic_errors.should have_tag('ul.errors')
38
+ @base_errors.each do |error|
39
+ builder.semantic_errors.should have_tag('ul.errors li', error)
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ describe 'when there are errors on title' do
46
+ before do
47
+ @errors.stub(:[]).with(errors_matcher(:title)).and_return(@title_errors)
48
+ @errors.stub(:[]).with(errors_matcher(:base)).and_return([])
49
+ end
50
+
51
+ it 'should render an unordered list' do
52
+ semantic_form_for(@new_post) do |builder|
53
+ title_name = builder.send(:localized_string, :title, :title, :label) || builder.send(:humanized_attribute_name, :title)
54
+ builder.semantic_errors(:title).should have_tag('ul.errors li', title_name << " " << @title_errors.to_sentence)
55
+ end
56
+ end
57
+ end
58
+
59
+ describe 'when there are errors on title and base' do
60
+ before do
61
+ @errors.stub(:[]).with(errors_matcher(:title)).and_return(@title_errors)
62
+ @errors.stub(:[]).with(errors_matcher(:base)).and_return(@base_error)
63
+ end
64
+
65
+ it 'should render an unordered list' do
66
+ semantic_form_for(@new_post) do |builder|
67
+ title_name = builder.send(:localized_string, :title, :title, :label) || builder.send(:humanized_attribute_name, :title)
68
+ builder.semantic_errors(:title).should have_tag('ul.errors li', title_name << " " << @title_errors.to_sentence)
69
+ builder.semantic_errors(:title).should have_tag('ul.errors li', @base_error)
70
+ end
71
+ end
72
+ end
73
+
74
+ describe 'when there are no errors' do
75
+ before do
76
+ @errors.stub(:[]).with(errors_matcher(:title)).and_return(nil)
77
+ @errors.stub(:[]).with(errors_matcher(:base)).and_return(nil)
78
+ end
79
+
80
+ it 'should return nil' do
81
+ semantic_form_for(@new_post) do |builder|
82
+ builder.semantic_errors(:title).should be_nil
83
+ end
84
+ end
85
+ end
86
+
87
+ describe 'when there is one error on base and options with class is passed' do
88
+ before do
89
+ @errors.stub(:[]).with(errors_matcher(:base)).and_return(@base_error)
90
+ end
91
+
92
+ it 'should render an unordered list with given class' do
93
+ semantic_form_for(@new_post) do |builder|
94
+ builder.semantic_errors(:class => "awesome").should have_tag('ul.awesome li', @base_error)
95
+ end
96
+ end
97
+ end
98
+
99
+ describe 'when :base is passed in as an argument' do
100
+ before do
101
+ @errors.stub(:[]).with(errors_matcher(:base)).and_return(@base_error)
102
+ end
103
+
104
+ it 'should ignore :base and only render base errors once' do
105
+ semantic_form_for(@new_post) do |builder|
106
+ builder.semantic_errors(:base).should have_tag('ul li', :count => 1)
107
+ builder.semantic_errors(:base).should_not have_tag('ul li', "Base #{@base_error}")
108
+ end
109
+ end
110
+ end
111
+
112
+ end
data/spec/i18n_spec.rb ADDED
@@ -0,0 +1,210 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe 'Formtastic::I18n' do
5
+
6
+ FORMTASTIC_KEYS = [:required, :yes, :no, :create, :update].freeze
7
+
8
+ it "should be defined" do
9
+ lambda { Formtastic::I18n }.should_not raise_error
10
+ end
11
+
12
+ describe "default translations" do
13
+ it "should be defined" do
14
+ lambda { Formtastic::I18n::DEFAULT_VALUES }.should_not raise_error
15
+ Formtastic::I18n::DEFAULT_VALUES.is_a?(::Hash).should == true
16
+ end
17
+
18
+ it "should exists for the core I18n lookup keys" do
19
+ (Formtastic::I18n::DEFAULT_VALUES.keys & FORMTASTIC_KEYS).size.should == FORMTASTIC_KEYS.size
20
+ end
21
+ end
22
+
23
+ describe "when I18n locales are available" do
24
+
25
+ before do
26
+ @formtastic_strings = {
27
+ :yes => 'Default Yes',
28
+ :no => 'Default No',
29
+ :create => 'Default Create %{model}',
30
+ :update => 'Default Update %{model}',
31
+ :custom_scope => {
32
+ :duck => 'Duck',
33
+ :duck_pond => '%{ducks} ducks in a pond'
34
+ }
35
+ }
36
+ ::I18n.backend.store_translations :en, :formtastic => @formtastic_strings
37
+ end
38
+
39
+ after do
40
+ ::I18n.backend.reload!
41
+ end
42
+
43
+ it "should translate core strings correctly" do
44
+ ::I18n.backend.store_translations :en, {:formtastic => {:required => 'Default Required'}}
45
+ Formtastic::I18n.t(:required).should == "Default Required"
46
+ Formtastic::I18n.t(:yes).should == "Default Yes"
47
+ Formtastic::I18n.t(:no).should == "Default No"
48
+ Formtastic::I18n.t(:create, :model => 'Post').should == "Default Create Post"
49
+ Formtastic::I18n.t(:update, :model => 'Post').should == "Default Update Post"
50
+ end
51
+
52
+ it "should all belong to scope 'formtastic'" do
53
+ Formtastic::I18n.t(:duck, :scope => [:custom_scope]).should == 'Duck'
54
+ end
55
+
56
+ it "should override default I18n lookup args if these are specified" do
57
+ Formtastic::I18n.t(:duck_pond, :scope => [:custom_scope], :ducks => 15).should == '15 ducks in a pond'
58
+ end
59
+
60
+ it "should be possible to override default values" do
61
+ Formtastic::I18n.t(:required, :default => 'Nothing found!').should == 'Nothing found!'
62
+ end
63
+
64
+ end
65
+
66
+ describe "when no I18n locales are available" do
67
+
68
+ before do
69
+ ::I18n.backend.reload!
70
+ end
71
+
72
+ it "should use default strings" do
73
+ (Formtastic::I18n::DEFAULT_VALUES.keys).each do |key|
74
+ Formtastic::I18n.t(key, :model => '%{model}').should == Formtastic::I18n::DEFAULT_VALUES[key]
75
+ end
76
+ end
77
+
78
+ end
79
+
80
+ describe "I18n string lookups" do
81
+
82
+ include FormtasticSpecHelper
83
+
84
+ before do
85
+ @output_buffer = ''
86
+ mock_everything
87
+
88
+ ::I18n.backend.store_translations :en, {:formtastic => {
89
+ :labels => {
90
+ :author => { :name => "Top author name transation" },
91
+ :post => {:title => "Hello post!", :author => {:name => "Hello author name!"}},
92
+ :project => {:title => "Hello project!"},
93
+ }
94
+ }, :helpers => {
95
+ :label => {
96
+ :post => {:body => "Elaborate..." },
97
+ :author => { :login => "Hello login" }
98
+ }
99
+ }}
100
+
101
+ @new_post.stub(:title)
102
+ @new_post.stub(:column_for_attribute).with(:title).and_return(double('column', :type => :string, :limit => 255))
103
+ end
104
+
105
+ after do
106
+ ::I18n.backend.reload!
107
+ end
108
+
109
+ it "lookup scopes should be defined" do
110
+ with_config :i18n_lookups_by_default, true do
111
+ lambda { Formtastic::I18n::SCOPES }.should_not raise_error
112
+ end
113
+ end
114
+
115
+ it "should be able to translate with namespaced object" do
116
+ with_config :i18n_lookups_by_default, true do
117
+ concat(semantic_form_for(@new_post) do |builder|
118
+ concat(builder.input(:title))
119
+ end)
120
+ output_buffer.should have_tag("form label", /Hello post!/)
121
+ end
122
+ end
123
+
124
+ it "should be able to translate without form-object" do
125
+ with_config :i18n_lookups_by_default, true do
126
+ concat(semantic_form_for(:project, :url => 'http://test.host') do |builder|
127
+ concat(builder.input(:title))
128
+ end)
129
+ output_buffer.should have_tag("form label", /Hello project!/)
130
+ end
131
+ end
132
+
133
+ it "should be able to translate when method name is same as model" do
134
+ with_config :i18n_lookups_by_default, true do
135
+ concat(semantic_form_for(:project, :url => 'http://test.host') do |builder|
136
+ concat(builder.input(:author))
137
+ end)
138
+ output_buffer.should have_tag("form label", /Author/)
139
+ end
140
+ end
141
+
142
+ it 'should be able to translate nested objects with nested translations' do
143
+ with_config :i18n_lookups_by_default, true do
144
+ concat(semantic_form_for(@new_post) do |builder|
145
+ concat(builder.semantic_fields_for(:author) do |f|
146
+ concat(f.input(:name))
147
+ end)
148
+ end)
149
+ output_buffer.should have_tag("form label", /Hello author name!/)
150
+ end
151
+ end
152
+
153
+ it 'should be able to translate nested objects with top level translations' do
154
+ with_config :i18n_lookups_by_default, true do
155
+ concat(semantic_form_for(@new_post) do |builder|
156
+ builder.semantic_fields_for(:author) do |f|
157
+ concat(f.input(:name))
158
+ end
159
+ end)
160
+ output_buffer.should have_tag("form label", /Hello author name!/)
161
+ end
162
+ end
163
+
164
+ it 'should be able to translate nested objects with nested object translations' do
165
+ with_config :i18n_lookups_by_default, true do
166
+ concat(semantic_form_for(@new_post) do |builder|
167
+ builder.semantic_fields_for(:project) do |f|
168
+ concat(f.input(:title))
169
+ end
170
+ end)
171
+ output_buffer.should have_tag("form label", /Hello project!/)
172
+ end
173
+ end
174
+
175
+ it 'should be able to translate nested forms with top level translations' do
176
+ with_config :i18n_lookups_by_default, true do
177
+ concat(semantic_form_for(:post) do |builder|
178
+ builder.semantic_fields_for(:author) do |f|
179
+ concat(f.input(:name))
180
+ end
181
+ end)
182
+ output_buffer.should have_tag("form label", /Hello author name!/)
183
+ end
184
+ end
185
+
186
+ it 'should be able to translate helper label as Rails does' do
187
+ with_config :i18n_lookups_by_default, true do
188
+ concat(semantic_form_for(@new_post) do |builder|
189
+ concat(builder.input(:body))
190
+ end)
191
+ output_buffer.should have_tag("form label", /Elaborate/)
192
+ end
193
+ end
194
+
195
+ it 'should be able to translate nested helper label as Rails does' do
196
+ with_config :i18n_lookups_by_default, true do
197
+ concat(semantic_form_for(@new_post) do |builder|
198
+ concat(builder.inputs(:for => :author) do |f|
199
+ concat(f.input(:login))
200
+ end)
201
+ end)
202
+ output_buffer.should have_tag("form label", /Hello login/)
203
+ end
204
+ end
205
+
206
+ # TODO: Add spec for namespaced models?
207
+
208
+ end
209
+
210
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'formtastic/input_class_finder'
4
+
5
+ describe Formtastic::InputClassFinder do
6
+ it_behaves_like 'Specialized Class Finder' do
7
+ let(:default) { Formtastic::Inputs }
8
+ let(:namespaces_setting) { :input_namespaces }
9
+ end
10
+ end