formtastic 1.2.4 → 3.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,97 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe 'color input' do
5
+
6
+ include FormtasticSpecHelper
7
+
8
+ before do
9
+ @output_buffer = ''
10
+ mock_everything
11
+ end
12
+
13
+ if Formtastic::Util.rails3?
14
+ describe "attempting to use color input with Rails 3" do
15
+ it "raises an error" do
16
+ expect {
17
+ concat(semantic_form_for(@new_post) do |builder|
18
+ concat(builder.input(:color))
19
+ end)
20
+ }.to raise_error "The :color input requires the color_field form helper, which is only available in Rails 4+"
21
+ end
22
+ end
23
+ else
24
+ describe "when object is provided" do
25
+ before do
26
+ concat(semantic_form_for(@new_post) do |builder|
27
+ concat(builder.input(:color))
28
+ end)
29
+ end
30
+
31
+ it_should_have_input_wrapper_with_class(:color)
32
+ it_should_have_input_wrapper_with_class(:input)
33
+ it_should_have_input_wrapper_with_class(:stringish)
34
+ it_should_have_input_wrapper_with_id("post_color_input")
35
+ it_should_have_label_with_text(/Color/)
36
+ it_should_have_label_for("post_color")
37
+ it_should_have_input_with_id("post_color")
38
+ it_should_have_input_with_type(:color)
39
+ it_should_have_input_with_name("post[color]")
40
+
41
+ end
42
+
43
+ describe "when namespace is provided" do
44
+
45
+ before do
46
+ concat(semantic_form_for(@new_post, :namespace => 'context2') do |builder|
47
+ concat(builder.input(:color))
48
+ end)
49
+ end
50
+
51
+ it_should_have_input_wrapper_with_id("context2_post_color_input")
52
+ it_should_have_label_and_input_with_id("context2_post_color")
53
+
54
+ end
55
+
56
+ describe "when index is provided" do
57
+
58
+ before do
59
+ @output_buffer = ''
60
+ mock_everything
61
+
62
+ concat(semantic_form_for(@new_post) do |builder|
63
+ concat(builder.fields_for(:author, :index => 3) do |author|
64
+ concat(author.input(:name, :as => :color))
65
+ end)
66
+ end)
67
+ end
68
+
69
+ it 'should index the id of the wrapper' do
70
+ output_buffer.should have_tag("li#post_author_attributes_3_name_input")
71
+ end
72
+
73
+ it 'should index the id of the select tag' do
74
+ output_buffer.should have_tag("input#post_author_attributes_3_name")
75
+ end
76
+
77
+ it 'should index the name of the select tag' do
78
+ output_buffer.should have_tag("input[@name='post[author_attributes][3][name]']")
79
+ end
80
+
81
+ end
82
+
83
+
84
+ describe "when required" do
85
+ it "should add the required attribute to the input's html options" do
86
+ with_config :use_required_attribute, true do
87
+ concat(semantic_form_for(@new_post) do |builder|
88
+ concat(builder.input(:title, :as => :color, :required => true))
89
+ end)
90
+ output_buffer.should have_tag("input[@required]")
91
+ end
92
+ end
93
+ end
94
+ end
95
+
96
+ end
97
+
@@ -0,0 +1,133 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe 'country input' do
5
+
6
+ include FormtasticSpecHelper
7
+
8
+ before do
9
+ @output_buffer = ''
10
+ mock_everything
11
+ end
12
+
13
+ describe "when country_select is not available as a helper from a plugin" do
14
+
15
+ it "should raise an error, sugesting the author installs a plugin" do
16
+ lambda {
17
+ semantic_form_for(@new_post) do |builder|
18
+ concat(builder.input(:country, :as => :country))
19
+ end
20
+ }.should raise_error(Formtastic::Inputs::CountryInput::CountrySelectPluginMissing)
21
+ end
22
+
23
+ end
24
+
25
+ describe "when country_select is available as a helper (from a plugin)" do
26
+
27
+ before do
28
+ concat(semantic_form_for(@new_post) do |builder|
29
+ builder.stub(:country_select).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
30
+ concat(builder.input(:country, :as => :country))
31
+ end)
32
+ end
33
+
34
+ it_should_have_input_wrapper_with_class("country")
35
+ it_should_have_input_wrapper_with_class(:input)
36
+ it_should_have_input_wrapper_with_id("post_country_input")
37
+
38
+ # TODO -- needs stubbing inside the builder block, tricky!
39
+ #it_should_apply_error_logic_for_input_type(:country)
40
+
41
+ it 'should generate a label for the input' do
42
+ output_buffer.should have_tag('form li label')
43
+ output_buffer.should have_tag('form li label[@for="post_country"]')
44
+ output_buffer.should have_tag('form li label', /Country/)
45
+ end
46
+
47
+ it "should generate a select" do
48
+ output_buffer.should have_tag("form li select")
49
+ end
50
+
51
+ end
52
+
53
+ describe ":priority_countries option" do
54
+
55
+ it "should be passed down to the country_select helper when provided" do
56
+ priority_countries = ["Foo", "Bah"]
57
+ semantic_form_for(@new_post) do |builder|
58
+ builder.stub(:country_select).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
59
+ builder.should_receive(:country_select).with(:country, priority_countries, {}, {:id => "post_country", :required => false, :autofocus => false, :readonly => false}).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
60
+
61
+ concat(builder.input(:country, :as => :country, :priority_countries => priority_countries))
62
+ end
63
+ end
64
+
65
+ it "should default to the @@priority_countries config when absent" do
66
+ priority_countries = Formtastic::FormBuilder.priority_countries
67
+ priority_countries.should_not be_empty
68
+ priority_countries.should_not be_nil
69
+
70
+ semantic_form_for(@new_post) do |builder|
71
+ builder.stub(:country_select).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
72
+ builder.should_receive(:country_select).with(:country, priority_countries, {}, {:id => "post_country", :required => false, :autofocus => false, :readonly => false}).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
73
+
74
+ concat(builder.input(:country, :as => :country))
75
+ end
76
+ end
77
+
78
+ end
79
+
80
+ describe "when namespace is provided" do
81
+
82
+ before do
83
+ @output_buffer = ''
84
+ mock_everything
85
+
86
+ concat(semantic_form_for(@new_post, :namespace => 'context2') do |builder|
87
+ builder.stub(:country_select).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
88
+ builder.should_receive(:country_select).with(:country, [], {}, {:id => "context2_post_country", :required => false, :autofocus => false, :readonly => false}).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
89
+ concat(builder.input(:country, :priority_countries => []))
90
+ end)
91
+ end
92
+
93
+ it_should_have_input_wrapper_with_id("context2_post_country_input")
94
+ it_should_have_label_for("context2_post_country")
95
+
96
+ end
97
+
98
+ describe "matching" do
99
+
100
+ describe "when the attribute is 'country'" do
101
+
102
+ before do
103
+ concat(semantic_form_for(@new_post) do |builder|
104
+ builder.stub(:country_select).and_return(Formtastic::Util.html_safe("<select><option>...</option></select>"))
105
+ concat(builder.input(:country))
106
+ end)
107
+ end
108
+
109
+ it "should render a country input" do
110
+ output_buffer.should have_tag "form li.country"
111
+ end
112
+ end
113
+
114
+ describe "whent the attribute is 'country_something'" do
115
+
116
+ before do
117
+ concat(semantic_form_for(@new_post) do |builder|
118
+ concat(builder.input(:country_subdivision))
119
+ concat(builder.input(:country_code))
120
+ end)
121
+ end
122
+
123
+ it "should render a country input" do
124
+ output_buffer.should_not have_tag "form li.country"
125
+ output_buffer.should have_tag "form li.string", :count => 2
126
+ end
127
+
128
+ end
129
+
130
+ end
131
+
132
+ end
133
+
@@ -0,0 +1,55 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ # TODO extract out
5
+ module TestInputs
6
+
7
+ def input_args
8
+ @template = self
9
+ @object = ::Post.new
10
+ @object_name = 'post'
11
+ @method = :title
12
+ @options = {}
13
+ @proc = Proc.new {}
14
+ if Rails::VERSION::MAJOR == 4
15
+ @builder = Formtastic::FormBuilder.new(@object_name, @object, @template, @options)
16
+ else
17
+ @builder = Formtastic::FormBuilder.new(@object_name, @object, @template, @options, @proc)
18
+ end
19
+ [@builder, @template, @object, @object_name, @method, @options]
20
+ end
21
+
22
+ class ::UnimplementedInput
23
+ include Formtastic::Inputs::Base
24
+ end
25
+
26
+ class ::ImplementedInput < UnimplementedInput
27
+ def to_html
28
+ "some HTML output"
29
+ end
30
+ end
31
+
32
+ end
33
+
34
+ describe 'AnyCustomInput' do
35
+
36
+ include TestInputs
37
+
38
+ describe "#to_html" do
39
+
40
+ describe 'without an implementation' do
41
+ it "should raise a NotImplementedError exception" do
42
+ expect { ::UnimplementedInput.new(*input_args).to_html }.to raise_error(NotImplementedError)
43
+ end
44
+ end
45
+
46
+ describe 'with an implementation' do
47
+ it "should raise a NotImplementedError exception" do
48
+ expect { ::ImplementedInput.new(*input_args).to_html }.to_not raise_error
49
+ end
50
+ end
51
+
52
+ end
53
+
54
+ end
55
+
@@ -0,0 +1,61 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe "datalist inputs" do
5
+ include FormtasticSpecHelper
6
+
7
+ before do
8
+ @output_buffer = ''
9
+ mock_everything
10
+ end
11
+
12
+ describe "renders correctly" do
13
+ lists_without_values =[
14
+ %w(a b c),
15
+ ["a", "b", "c"],
16
+ ("a".."c")
17
+ ]
18
+ lists_with_values = [
19
+ {a: 1, b: 2, c:3},
20
+ {"a" => 1, "b" => 2, "c" =>3},
21
+ [["a",1], ["b",2], ["c", 3]]
22
+ ]
23
+
24
+ def self.common_tests(list)
25
+ it_should_have_label_with_text(/Document/)
26
+ it_should_have_label_for("post_document")
27
+ it_should_have_input_wrapper_with_class(:datalist)
28
+ it_should_have_input_with(id: "post_document", type: :text, list:"post_document_datalist")
29
+ it_should_have_tag_with(:datalist, id: "post_document_datalist" )
30
+ it_should_have_many_tags(:option, list.count)
31
+ end
32
+
33
+ context "Rendering list of simple items" do
34
+ lists_without_values.each do |list|
35
+ describe "renders #{list.to_s} correctly" do
36
+ before do
37
+ concat(semantic_form_for(@new_post) do |builder|
38
+ concat(builder.input(:document, as: :datalist, collection: list))
39
+ end)
40
+ end
41
+ common_tests list
42
+ it_should_have_tag_with :option, value: list.first
43
+ end
44
+ end
45
+ end
46
+
47
+ context "Rendering list of complex items, key-value pairs and such" do
48
+ lists_with_values.each do |list|
49
+ describe "renders #{list.to_s} correctly" do
50
+ before do
51
+ concat(semantic_form_for(@new_post) do |builder|
52
+ concat(builder.input(:document, as: :datalist, collection: list))
53
+ end)
54
+ end
55
+ common_tests list
56
+ it_should_have_tag_with :option, value: list.first.last
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,449 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe 'date_picker input' do
5
+
6
+ include FormtasticSpecHelper
7
+
8
+ before do
9
+ @output_buffer = ''
10
+ mock_everything
11
+ end
12
+
13
+ context "with an object" do
14
+ before do
15
+ concat(semantic_form_for(@new_post) do |builder|
16
+ concat(builder.input(:publish_at, :as => :date_picker))
17
+ end)
18
+ end
19
+
20
+ it_should_have_input_wrapper_with_class(:date_picker)
21
+ it_should_have_input_wrapper_with_class(:input)
22
+ it_should_have_input_wrapper_with_class(:stringish)
23
+ it_should_have_input_wrapper_with_id("post_publish_at_input")
24
+ it_should_have_label_with_text(/Publish at/)
25
+ it_should_have_label_for("post_publish_at")
26
+ it_should_have_input_with_id("post_publish_at")
27
+ it_should_have_input_with_type(:date)
28
+ it_should_have_input_with_name("post[publish_at]")
29
+ it_should_apply_custom_input_attributes_when_input_html_provided(:date_picker)
30
+ it_should_apply_custom_for_to_label_when_input_html_id_provided(:date_picker)
31
+ # TODO why does this blow-up it_should_apply_error_logic_for_input_type(:date_picker)
32
+
33
+ end
34
+
35
+ describe "size attribute" do
36
+
37
+ it "defaults to 10 chars (YYYY-YY-YY)" do
38
+ concat(
39
+ semantic_form_for(@new_post) do |f|
40
+ concat(f.input(:publish_at, :as => :date_picker))
41
+ end
42
+ )
43
+ output_buffer.should have_tag "input[size='10']"
44
+ end
45
+
46
+ it "can be set from :input_html options" do
47
+ concat(
48
+ semantic_form_for(@new_post) do |f|
49
+ concat(f.input(:publish_at, :as => :date_picker, :input_html => { :size => "11" }))
50
+ end
51
+ )
52
+ output_buffer.should have_tag "input[size='11']"
53
+ end
54
+
55
+ it "can be set from options (ignoring input_html)" do
56
+ concat(
57
+ semantic_form_for(@new_post) do |f|
58
+ concat(f.input(:publish_at, :as => :date_picker, :size => '12', :input_html => { :size => "11" }))
59
+ end
60
+ )
61
+ output_buffer.should have_tag "input[size='12']"
62
+ end
63
+
64
+ end
65
+
66
+ describe "maxlength attribute" do
67
+
68
+ it "defaults to 10 chars (YYYY-YY-YY)" do
69
+ concat(
70
+ semantic_form_for(@new_post) do |f|
71
+ concat(f.input(:publish_at, :as => :date_picker))
72
+ end
73
+ )
74
+ output_buffer.should have_tag "input[maxlength='10']"
75
+ end
76
+
77
+ it "can be set from :input_html options" do
78
+ concat(
79
+ semantic_form_for(@new_post) do |f|
80
+ concat(f.input(:publish_at, :as => :date_picker, :input_html => { :maxlength => "11" }))
81
+ end
82
+ )
83
+ output_buffer.should have_tag "input[maxlength='11']"
84
+ end
85
+
86
+ it "can be set from options (ignoring input_html)" do
87
+ concat(
88
+ semantic_form_for(@new_post) do |f|
89
+ concat(f.input(:publish_at, :as => :date_picker, :maxlength => 12, :input_html => { :maxlength => "11" }))
90
+ end
91
+ )
92
+ output_buffer.should have_tag "input[maxlength='12']"
93
+ end
94
+
95
+ end
96
+
97
+ describe "value attribute" do
98
+
99
+ context "when method returns nil" do
100
+
101
+ it "has no value" do
102
+ concat(
103
+ semantic_form_for(@new_post) do |f|
104
+ concat(f.input(:publish_at, :as => :date_picker ))
105
+ end
106
+ )
107
+ output_buffer.should_not have_tag "li input[value]"
108
+ end
109
+
110
+ it "can be set from :input_html options" do
111
+ concat(
112
+ semantic_form_for(@new_post) do |f|
113
+ concat(f.input(:publish_at, :as => :date_picker, :input_html => { :value => "1111-11-11" }))
114
+ end
115
+ )
116
+ output_buffer.should have_tag "input[value='1111-11-11']"
117
+ end
118
+
119
+ end
120
+
121
+ context "when method returns a Date" do
122
+
123
+ before do
124
+ @date = Date.new(2000, 11, 11)
125
+ @new_post.stub(:publish_at).and_return(@date)
126
+ end
127
+
128
+ it "renders the date as YYYY-MM-DD" do
129
+ concat(
130
+ semantic_form_for(@new_post) do |f|
131
+ concat(f.input(:publish_at, :as => :date_picker ))
132
+ end
133
+ )
134
+ output_buffer.should have_tag "input[value='#{@date.to_s}']"
135
+ end
136
+
137
+ it "can be set from :input_html options" do
138
+ concat(
139
+ semantic_form_for(@new_post) do |f|
140
+ concat(f.input(:publish_at, :as => :date_picker, :input_html => { :value => "1111-11-11" }))
141
+ end
142
+ )
143
+ output_buffer.should have_tag "input[value='1111-11-11']"
144
+ end
145
+
146
+ end
147
+
148
+ context "when method returns a Time" do
149
+
150
+ before do
151
+ @time = Time.utc(2000,11,11,11,11,11)
152
+ @new_post.stub(:publish_at).and_return(@time)
153
+ end
154
+
155
+ it "renders the time as a YYYY-MM-DD" do
156
+ concat(
157
+ semantic_form_for(@new_post) do |f|
158
+ concat(f.input(:publish_at, :as => :date_picker ))
159
+ end
160
+ )
161
+ output_buffer.should have_tag "input[value='2000-11-11']"
162
+ end
163
+
164
+ it "can be set from :input_html options" do
165
+ concat(
166
+ semantic_form_for(@new_post) do |f|
167
+ concat(f.input(:publish_at, :as => :date_picker, :input_html => { :value => "1111-11-11" }))
168
+ end
169
+ )
170
+ output_buffer.should have_tag "input[value='1111-11-11']"
171
+ end
172
+
173
+ end
174
+
175
+ context "when method returns an empty String" do
176
+
177
+ before do
178
+ @new_post.stub(:publish_at).and_return("")
179
+ end
180
+
181
+ it "will be empty" do
182
+ concat(
183
+ semantic_form_for(@new_post) do |f|
184
+ concat(f.input(:publish_at, :as => :date_picker ))
185
+ end
186
+ )
187
+ output_buffer.should have_tag "input[value='']"
188
+ end
189
+
190
+ it "can be set from :input_html options" do
191
+ concat(
192
+ semantic_form_for(@new_post) do |f|
193
+ concat(f.input(:publish_at, :as => :date_picker, :input_html => { :value => "1111-11-11" }))
194
+ end
195
+ )
196
+ output_buffer.should have_tag "input[value='1111-11-11']"
197
+ end
198
+
199
+ end
200
+
201
+ context "when method returns a String" do
202
+
203
+ before do
204
+ @new_post.stub(:publish_at).and_return("yeah")
205
+ end
206
+
207
+ it "will be the string" do
208
+ concat(
209
+ semantic_form_for(@new_post) do |f|
210
+ concat(f.input(:publish_at, :as => :date_picker ))
211
+ end
212
+ )
213
+ output_buffer.should have_tag "input[value='yeah']"
214
+ end
215
+
216
+ it "can be set from :input_html options" do
217
+ concat(
218
+ semantic_form_for(@new_post) do |f|
219
+ concat(f.input(:publish_at, :as => :date_picker, :input_html => { :value => "1111-11-11" }))
220
+ end
221
+ )
222
+ output_buffer.should have_tag "input[value='1111-11-11']"
223
+ end
224
+
225
+ end
226
+
227
+ end
228
+
229
+ describe "min attribute" do
230
+
231
+ it "will be omitted by default" do
232
+ concat(
233
+ semantic_form_for(@new_post) do |f|
234
+ concat(f.input(:publish_at, :as => :date_picker))
235
+ end
236
+ )
237
+ output_buffer.should_not have_tag "input[min]"
238
+ end
239
+
240
+ it "can be set from :input_html options" do
241
+ concat(
242
+ semantic_form_for(@new_post) do |f|
243
+ concat(f.input(:publish_at, :as => :date_picker, :input_html => { :min => "1970-01-01" }))
244
+ end
245
+ )
246
+ output_buffer.should have_tag "input[min='1970-01-01']"
247
+ end
248
+
249
+ end
250
+
251
+ describe "max attribute" do
252
+
253
+ it "will be omitted by default" do
254
+ concat(
255
+ semantic_form_for(@new_post) do |f|
256
+ concat(f.input(:publish_at, :as => :date_picker))
257
+ end
258
+ )
259
+ output_buffer.should_not have_tag "input[max]"
260
+ end
261
+
262
+ it "can be set from :input_html options" do
263
+ concat(
264
+ semantic_form_for(@new_post) do |f|
265
+ concat(f.input(:publish_at, :as => :date_picker, :input_html => { :max => "1970-01-01" }))
266
+ end
267
+ )
268
+ output_buffer.should have_tag "input[max='1970-01-01']"
269
+ end
270
+
271
+ end
272
+
273
+ describe "step attribute" do
274
+
275
+ it "defaults to 1" do
276
+ concat(
277
+ semantic_form_for(@new_post) do |f|
278
+ concat(f.input(:publish_at, :as => :date_picker))
279
+ end
280
+ )
281
+ output_buffer.should have_tag "input[step='1']"
282
+ end
283
+
284
+ it "can be set from :input_html options" do
285
+ concat(
286
+ semantic_form_for(@new_post) do |f|
287
+ concat(f.input(:publish_at, :as => :date_picker, :input_html => { :step => "5" }))
288
+ end
289
+ )
290
+ output_buffer.should have_tag "input[step='5']"
291
+ end
292
+
293
+ describe "macros" do
294
+
295
+ before do
296
+ concat(
297
+ semantic_form_for(@new_post) do |f|
298
+ concat(f.input(:publish_at, :as => :date_picker, :input_html => { :step => step }))
299
+ end
300
+ )
301
+ end
302
+
303
+ context ":day" do
304
+ let(:step) { :day }
305
+ it "uses 1" do
306
+ output_buffer.should have_tag "input[step='1']"
307
+ end
308
+ end
309
+
310
+ context ":seven_days" do
311
+ let(:step) { :seven_days }
312
+ it "uses 7" do
313
+ output_buffer.should have_tag "input[step='7']"
314
+ end
315
+ end
316
+
317
+ context ":week" do
318
+ let(:step) { :week }
319
+ it "uses 7" do
320
+ output_buffer.should have_tag "input[step='7']"
321
+ end
322
+ end
323
+
324
+ context ":fortnight" do
325
+ let(:step) { :fortnight }
326
+ it "uses 14" do
327
+ output_buffer.should have_tag "input[step='14']"
328
+ end
329
+ end
330
+
331
+ context ":two_weeks" do
332
+ let(:step) { :two_weeks }
333
+ it "uses 14" do
334
+ output_buffer.should have_tag "input[step='14']"
335
+ end
336
+ end
337
+
338
+ context ":four_weeks" do
339
+ let(:step) { :four_weeks }
340
+ it "uses 28" do
341
+ output_buffer.should have_tag "input[step='28']"
342
+ end
343
+ end
344
+
345
+ context ":thirty_days" do
346
+ let(:step) { :thirty_days }
347
+ it "uses 30" do
348
+ output_buffer.should have_tag "input[step='30']"
349
+ end
350
+ end
351
+
352
+ end
353
+
354
+ end
355
+
356
+ describe "placeholder attribute" do
357
+
358
+ it "will be omitted" do
359
+ concat(
360
+ semantic_form_for(@new_post) do |f|
361
+ concat(f.input(:publish_at, :as => :date_picker))
362
+ end
363
+ )
364
+ output_buffer.should_not have_tag "input[placeholder]"
365
+ end
366
+
367
+ it "can be set from :input_html options" do
368
+ concat(
369
+ semantic_form_for(@new_post) do |f|
370
+ concat(f.input(:publish_at, :as => :date_picker, :input_html => { :placeholder => "1970-01-01" }))
371
+ end
372
+ )
373
+ output_buffer.should have_tag "input[placeholder='1970-01-01']"
374
+ end
375
+
376
+ context "with i18n set" do
377
+ before do
378
+ ::I18n.backend.store_translations :en, :formtastic => { :placeholders => { :publish_at => 'YYYY-MM-DD' }}
379
+ end
380
+
381
+ it "can be set with i18n" do
382
+ with_config :i18n_lookups_by_default, true do
383
+ concat(semantic_form_for(@new_post) do |builder|
384
+ concat(builder.input(:publish_at, :as => :date_picker))
385
+ end)
386
+ output_buffer.should have_tag('input[@placeholder="YYYY-MM-DD"]')
387
+ end
388
+ end
389
+
390
+ it "can be set with input_html, trumping i18n" do
391
+ with_config :i18n_lookups_by_default, true do
392
+ concat(semantic_form_for(@new_post) do |builder|
393
+ concat(builder.input(:publish_at, :as => :date_picker, :input_html => { :placeholder => "Something" }))
394
+ end)
395
+ output_buffer.should have_tag('input[@placeholder="Something"]')
396
+ end
397
+ end
398
+ end
399
+
400
+ end
401
+
402
+ describe "when namespace is provided" do
403
+ before do
404
+ concat(semantic_form_for(@new_post, :namespace => "context2") do |builder|
405
+ concat(builder.input(:publish_at, :as => :date_picker))
406
+ end)
407
+ end
408
+
409
+ it_should_have_input_wrapper_with_id("context2_post_publish_at_input")
410
+ it_should_have_label_and_input_with_id("context2_post_publish_at")
411
+ end
412
+
413
+ describe "when index is provided" do
414
+ before do
415
+ @output_buffer = ''
416
+ mock_everything
417
+
418
+ concat(semantic_form_for(@new_post) do |builder|
419
+ concat(builder.fields_for(:author, :index => 3) do |author|
420
+ concat(author.input(:created_at, :as => :date_picker))
421
+ end)
422
+ end)
423
+ end
424
+
425
+ it 'should index the id of the wrapper' do
426
+ output_buffer.should have_tag("li#post_author_attributes_3_created_at_input")
427
+ end
428
+
429
+ it 'should index the id of the select tag' do
430
+ output_buffer.should have_tag("input#post_author_attributes_3_created_at")
431
+ end
432
+
433
+ it 'should index the name of the select tag' do
434
+ output_buffer.should have_tag("input[@name='post[author_attributes][3][created_at]']")
435
+ end
436
+ end
437
+
438
+ describe "when required" do
439
+ it "should add the required attribute to the input's html options" do
440
+ with_config :use_required_attribute, true do
441
+ concat(semantic_form_for(@new_post) do |builder|
442
+ concat(builder.input(:publish_at, :as => :date_picker, :required => true))
443
+ end)
444
+ output_buffer.should have_tag("input[@required]")
445
+ end
446
+ end
447
+ end
448
+
449
+ end