speedo-formstrap 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. data/.document +5 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +13 -0
  4. data/Gemfile.lock +117 -0
  5. data/LICENSE.txt +20 -0
  6. data/README.md +192 -0
  7. data/Rakefile +49 -0
  8. data/VERSION +1 -0
  9. data/formtastic-bootstrap.gemspec +56 -0
  10. data/lib/action_view/helpers/text_field_date_helper.rb +166 -0
  11. data/lib/formtastic-bootstrap.rb +7 -0
  12. data/lib/formtastic-bootstrap/actions.rb +4 -0
  13. data/lib/formtastic-bootstrap/actions/base.rb +18 -0
  14. data/lib/formtastic-bootstrap/actions/button_action.rb +7 -0
  15. data/lib/formtastic-bootstrap/actions/input_action.rb +7 -0
  16. data/lib/formtastic-bootstrap/actions/link_action.rb +7 -0
  17. data/lib/formtastic-bootstrap/engine.rb +4 -0
  18. data/lib/formtastic-bootstrap/form_builder.rb +40 -0
  19. data/lib/formtastic-bootstrap/helpers.rb +21 -0
  20. data/lib/formtastic-bootstrap/helpers/action_helper.rb +12 -0
  21. data/lib/formtastic-bootstrap/helpers/actions_helper.rb +23 -0
  22. data/lib/formtastic-bootstrap/helpers/buttons_helper.rb +56 -0
  23. data/lib/formtastic-bootstrap/helpers/fieldset_wrapper.rb +42 -0
  24. data/lib/formtastic-bootstrap/helpers/input_helper.rb +12 -0
  25. data/lib/formtastic-bootstrap/helpers/inputs_helper.rb +36 -0
  26. data/lib/formtastic-bootstrap/inputs.rb +29 -0
  27. data/lib/formtastic-bootstrap/inputs/base.rb +23 -0
  28. data/lib/formtastic-bootstrap/inputs/base/choices.rb +38 -0
  29. data/lib/formtastic-bootstrap/inputs/base/errors.rb +48 -0
  30. data/lib/formtastic-bootstrap/inputs/base/hints.rb +27 -0
  31. data/lib/formtastic-bootstrap/inputs/base/html.rb +21 -0
  32. data/lib/formtastic-bootstrap/inputs/base/labelling.rb +28 -0
  33. data/lib/formtastic-bootstrap/inputs/base/numeric.rb +18 -0
  34. data/lib/formtastic-bootstrap/inputs/base/stringish.rb +18 -0
  35. data/lib/formtastic-bootstrap/inputs/base/timeish.rb +28 -0
  36. data/lib/formtastic-bootstrap/inputs/base/wrapping.rb +74 -0
  37. data/lib/formtastic-bootstrap/inputs/boolean_input.rb +29 -0
  38. data/lib/formtastic-bootstrap/inputs/check_boxes_input.rb +36 -0
  39. data/lib/formtastic-bootstrap/inputs/date_input.rb +16 -0
  40. data/lib/formtastic-bootstrap/inputs/datetime_input.rb +19 -0
  41. data/lib/formtastic-bootstrap/inputs/email_input.rb +15 -0
  42. data/lib/formtastic-bootstrap/inputs/file_input.rb +14 -0
  43. data/lib/formtastic-bootstrap/inputs/hidden_input.rb +14 -0
  44. data/lib/formtastic-bootstrap/inputs/number_input.rb +15 -0
  45. data/lib/formtastic-bootstrap/inputs/password_input.rb +15 -0
  46. data/lib/formtastic-bootstrap/inputs/phone_input.rb +15 -0
  47. data/lib/formtastic-bootstrap/inputs/radio_input.rb +33 -0
  48. data/lib/formtastic-bootstrap/inputs/range_input.rb +15 -0
  49. data/lib/formtastic-bootstrap/inputs/search_input.rb +15 -0
  50. data/lib/formtastic-bootstrap/inputs/select_input.rb +14 -0
  51. data/lib/formtastic-bootstrap/inputs/string_input.rb +15 -0
  52. data/lib/formtastic-bootstrap/inputs/text_input.rb +14 -0
  53. data/lib/formtastic-bootstrap/inputs/time_input.rb +16 -0
  54. data/lib/formtastic-bootstrap/inputs/time_zone_input.rb +14 -0
  55. data/lib/formtastic-bootstrap/inputs/url_input.rb +14 -0
  56. data/spec/actions/button_action_spec.rb +68 -0
  57. data/spec/actions/generic_action_spec.rb +486 -0
  58. data/spec/actions/input_action_spec.rb +64 -0
  59. data/spec/actions/link_action_spec.rb +93 -0
  60. data/spec/builder/semantic_fields_for_spec.rb +130 -0
  61. data/spec/helpers/action_helper_spec.rb +365 -0
  62. data/spec/helpers/actions_helper_spec.rb +143 -0
  63. data/spec/helpers/buttons_helper_spec.rb +159 -0
  64. data/spec/helpers/input_helper_spec.rb +956 -0
  65. data/spec/helpers/inputs_helper_spec.rb +591 -0
  66. data/spec/inputs/boolean_input_spec.rb +221 -0
  67. data/spec/inputs/check_boxes_input_spec.rb +467 -0
  68. data/spec/inputs/date_input_spec.rb +213 -0
  69. data/spec/inputs/datetime_input_spec.rb +101 -0
  70. data/spec/inputs/email_input_spec.rb +87 -0
  71. data/spec/inputs/file_input_spec.rb +91 -0
  72. data/spec/inputs/hidden_input_spec.rb +150 -0
  73. data/spec/inputs/number_input_spec.rb +817 -0
  74. data/spec/inputs/password_input_spec.rb +101 -0
  75. data/spec/inputs/phone_input_spec.rb +87 -0
  76. data/spec/inputs/radio_input_spec.rb +268 -0
  77. data/spec/inputs/range_input_spec.rb +507 -0
  78. data/spec/inputs/search_input_spec.rb +86 -0
  79. data/spec/inputs/select_input_spec.rb +613 -0
  80. data/spec/inputs/string_input_spec.rb +227 -0
  81. data/spec/inputs/text_input_spec.rb +190 -0
  82. data/spec/inputs/time_input_spec.rb +206 -0
  83. data/spec/inputs/time_zone_input_spec.rb +120 -0
  84. data/spec/inputs/url_input_spec.rb +87 -0
  85. data/spec/spec_helper.rb +24 -0
  86. data/spec/support/custom_macros.rb +575 -0
  87. data/spec/support/depracation.rb +6 -0
  88. data/spec/support/formtastic_spec_helper.rb +398 -0
  89. metadata +216 -0
@@ -0,0 +1,166 @@
1
+ require 'action_view/helpers/form_tag_helper'
2
+
3
+ module ActionView
4
+ module Helpers
5
+
6
+ module TextFieldDateHelper
7
+
8
+ # These two handle an object with a method that returns a Date or Time object.
9
+ def date_text_field(object_name, method, options = {}, html_options = {})
10
+ InstanceTag.new(object_name, method, self, options.delete(:object)).to_date_text_field_tag(options, html_options)
11
+ end
12
+
13
+ def time_text_field(object_name, method, options = {}, html_options = {})
14
+ InstanceTag.new(object_name, method, self, options.delete(:object)).to_time_text_field_tag(options, html_options)
15
+ end
16
+
17
+ # These two handle Date and Time objects.
18
+ def text_field_date(date = Date.current, options = {}, html_options = {})
19
+ DateTimeSelector.new(date, options, html_options).text_field_date
20
+ end
21
+
22
+ def text_field_time(time = Time.current, options = {}, html_options = {})
23
+ DateTimeSelector.new(time, options, html_options).text_field_time
24
+ end
25
+ end
26
+
27
+ class DateTimeSelector
28
+
29
+ include ActionView::Helpers::FormTagHelper
30
+
31
+ def text_field_date
32
+ order = date_order.dup
33
+
34
+ @options[:discard_hour] = true
35
+ @options[:discard_minute] = true
36
+ @options[:discard_second] = true
37
+
38
+ @options[:discard_year] ||= true unless order.include?(:year)
39
+ @options[:discard_month] ||= true unless order.include?(:month)
40
+ @options[:discard_day] ||= true if @options[:discard_month] || !order.include?(:day)
41
+
42
+ # Save this so we can restore it.
43
+ original_datetime_separator_separator = @options[:datetime_separator]
44
+ original_date_separator = @options[:date_separator]
45
+
46
+ @options[:datetime_separator] = ""
47
+ @options[:date_separator] = " "
48
+
49
+ # If the day is hidden and the month is visible, the day should be set to the 1st so all month choices are
50
+ # valid (otherwise it could be 31 and February wouldn't be a valid date)
51
+ if @datetime && @options[:discard_day] && !@options[:discard_month]
52
+ @datetime = @datetime.change(:day => 1)
53
+ end
54
+
55
+ [:day, :month, :year].each { |o| order.unshift(o) unless order.include?(o) }
56
+
57
+ build_text_field_from_types(order).tap do
58
+ # Restore.
59
+ @options[:datetime_separator] = original_datetime_separator_separator
60
+ @options[:date_separator] = original_date_separator
61
+ end
62
+ end
63
+
64
+ def text_field_time
65
+ order = []
66
+
67
+ @options[:discard_month] = true
68
+ @options[:discard_year] = true
69
+ @options[:discard_day] = true
70
+ @options[:discard_second] ||= true unless @options[:include_seconds]
71
+
72
+ # Save this so we can restore it.
73
+ original_datetime_separator = @options[:datetime_separator]
74
+ original_time_separator = @options[:time_separator]
75
+ original_date_separator = @options[:date_separator]
76
+
77
+ @options[:datetime_separator] = ''
78
+ @options[:time_separator] = ':'
79
+ @options[:date_separator] = " "
80
+
81
+ order += [:year, :month, :day] unless @options[:ignore_date]
82
+
83
+ order += [:hour, :minute]
84
+ order << :second if @options[:include_seconds]
85
+
86
+ build_text_field_from_types(order).tap do
87
+ # Restore.
88
+ @options[:datetime_separator] = original_datetime_separator
89
+ @options[:date_separator] = original_date_separator
90
+ @options[:time_separator] = original_time_separator
91
+ end
92
+ end
93
+
94
+ def text_field_second
95
+ unless @options[:use_hidden] || @options[:discard_second]
96
+ build_text(:second, sec)
97
+ end
98
+ end
99
+
100
+ def text_field_minute
101
+ unless @options[:use_hidden] || @options[:discard_minute]
102
+ build_text(:minute, min)
103
+ end
104
+ end
105
+
106
+ def text_field_hour
107
+ unless @options[:use_hidden] || @options[:discard_hour]
108
+ build_text(:hour, hour, :ampm => @options[:ampm])
109
+ end
110
+ end
111
+
112
+ def text_field_day
113
+ unless @options[:use_hidden] || @options[:discard_day]
114
+ build_text(:day, day, :leading_zeros => false)
115
+ end
116
+ end
117
+
118
+ def text_field_month
119
+ unless @options[:use_hidden] || @options[:discard_month]
120
+ build_text(:month, month.nil? ? "" : month_name(month), :leading_zeros => false)
121
+ end
122
+ end
123
+
124
+ def text_field_year
125
+ unless @options[:use_hidden] || @options[:discard_year]
126
+ build_text(:year, year, :leading_zeros => false)
127
+ end
128
+ end
129
+
130
+ def build_text_field_from_types(order)
131
+ input = ''
132
+ order.reverse.each do |type|
133
+ separator = separator(type) unless type == order.first # don't add on last field
134
+ input.insert(0, separator.to_s + send("text_field_#{type}").to_s)
135
+ end
136
+ text_field_tag("", input.html_safe, @html_options).html_safe
137
+ end
138
+
139
+ def build_text(selected, value, options = {})
140
+ unless value.nil?
141
+ options.reverse_merge!({:leading_zeros => true, :ampm => false})
142
+ leading_zeros = options.delete(:leading_zeros)
143
+ value = leading_zeros ? sprintf("%02d", value) : value
144
+ text = options[:ampm] ? AMPM_TRANSLATION[value] : value
145
+ else
146
+ ""
147
+ end
148
+ end
149
+
150
+ end
151
+
152
+ class InstanceTag #:nodoc:
153
+ def to_date_text_field_tag(options = {}, html_options = {})
154
+ datetime_selector(options, html_options).text_field_date.html_safe
155
+ end
156
+
157
+ def to_time_text_field_tag(options = {}, html_options = {})
158
+ datetime_selector(options, html_options).text_field_time.html_safe
159
+ end
160
+ end
161
+
162
+ autoload :TextFieldDateHelper
163
+ include TextFieldDateHelper
164
+
165
+ end
166
+ end
@@ -0,0 +1,7 @@
1
+ require "formtastic"
2
+ require "formtastic-bootstrap/engine" if defined?(::Rails) # For tests
3
+ require "formtastic-bootstrap/helpers"
4
+ require "formtastic-bootstrap/inputs"
5
+ require "formtastic-bootstrap/actions"
6
+ require "formtastic-bootstrap/form_builder"
7
+ require "action_view/helpers/text_field_date_helper"
@@ -0,0 +1,4 @@
1
+ require "formtastic-bootstrap/actions/base"
2
+ require "formtastic-bootstrap/actions/input_action"
3
+ require "formtastic-bootstrap/actions/link_action"
4
+ require "formtastic-bootstrap/actions/button_action"
@@ -0,0 +1,18 @@
1
+ module FormtasticBootstrap
2
+ module Actions
3
+ module Base
4
+
5
+ def wrapper(&block)
6
+ template.capture(&block)
7
+ end
8
+
9
+ def button_html
10
+ new_class = [super[:class], wrapper_html_options[:class], "btn"]
11
+ new_class << "btn-primary" if method == :submit
12
+ new_class = new_class.compact.join(" ")
13
+ wrapper_html_options.merge(super).merge(:class => new_class)
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,7 @@
1
+ module FormtasticBootstrap
2
+ module Actions
3
+ class ButtonAction < Formtastic::Actions::ButtonAction
4
+ include Base
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module FormtasticBootstrap
2
+ module Actions
3
+ class InputAction < Formtastic::Actions::InputAction
4
+ include Base
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module FormtasticBootstrap
2
+ module Actions
3
+ class LinkAction < Formtastic::Actions::LinkAction
4
+ include Base
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,4 @@
1
+ module FormtasticBootstrap
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,40 @@
1
+ module FormtasticBootstrap
2
+
3
+ class FormBuilder < Formtastic::FormBuilder
4
+
5
+ configure :default_inline_error_class, 'help-inline'
6
+ configure :default_block_error_class, 'help-block'
7
+ configure :default_inline_hint_class, 'help-inline'
8
+ configure :default_block_hint_class, 'help-block'
9
+
10
+ def self.default_error_class
11
+ # self.default_inline_error_class
12
+ raise
13
+ end
14
+
15
+ def self.default_error_class=(error_class)
16
+ # self.default_inline_error_class = error_class
17
+ # self.default_block_error_class = error_class
18
+ raise
19
+ end
20
+
21
+ def self.default_hint_class
22
+ # self.default_inline_hint_class
23
+ raise
24
+ end
25
+
26
+ def self.default_hint_class=(hint_class)
27
+ # self.default_inline_hint_class = hint_class
28
+ # self.default_block_hint_class = hint_class
29
+ raise
30
+ end
31
+
32
+ include FormtasticBootstrap::Helpers::InputHelper
33
+ include FormtasticBootstrap::Helpers::InputsHelper
34
+ include FormtasticBootstrap::Helpers::ButtonsHelper
35
+ include FormtasticBootstrap::Helpers::ActionHelper
36
+ include FormtasticBootstrap::Helpers::ActionsHelper
37
+
38
+ end
39
+
40
+ end
@@ -0,0 +1,21 @@
1
+ require "formtastic-bootstrap/helpers/buttons_helper"
2
+ require "formtastic-bootstrap/helpers/fieldset_wrapper"
3
+ require "formtastic-bootstrap/helpers/action_helper"
4
+ require "formtastic-bootstrap/helpers/actions_helper"
5
+ require "formtastic-bootstrap/helpers/input_helper"
6
+ require "formtastic-bootstrap/helpers/inputs_helper"
7
+
8
+ module FormtasticBootstrap
9
+ module Helpers
10
+ # autoload :ErrorsHelper, 'formtastic/helpers/errors_helper'
11
+ # autoload :FieldsetWrapper, 'formtastic/helpers/fieldset_wrapper'
12
+ # autoload :FileColumnDetection, 'formtastic/helpers/file_column_detection'
13
+ # autoload :FormHelper, 'formtastic/helpers/form_helper'
14
+ # autoload :InputHelper, 'formtastic/helpers/input_helper'
15
+ # autoload :InputsHelper, 'formtastic/helpers/inputs_helper'
16
+ # autoload :LabelHelper, 'formtastic/helpers/label_helper'
17
+ # autoload :SemanticFormHelper, 'formtastic/helpers/semantic_form_helper'
18
+ # autoload :Reflection, 'formtastic/helpers/reflection'
19
+ end
20
+ end
21
+
@@ -0,0 +1,12 @@
1
+ module FormtasticBootstrap
2
+ module Helpers
3
+ module ActionHelper
4
+ include Formtastic::Helpers::ActionHelper
5
+
6
+ def standard_action_class_name(as)
7
+ "FormtasticBootstrap::Actions::#{as.to_s.camelize}Action"
8
+ end
9
+
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,23 @@
1
+ module FormtasticBootstrap
2
+ module Helpers
3
+ module ActionsHelper
4
+
5
+ include Formtastic::Helpers::ActionsHelper
6
+ include FormtasticBootstrap::Helpers::FieldsetWrapper
7
+
8
+ def actions(*args, &block)
9
+ html_options = args.extract_options!
10
+ html_options[:class] ||= "form-actions"
11
+
12
+ if block_given?
13
+ field_set_and_list_wrapping(html_options, &block)
14
+ else
15
+ args = default_actions if args.empty?
16
+ contents = args.map { |action_name| action(action_name) }
17
+ field_set_and_list_wrapping(html_options, contents)
18
+ end
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,56 @@
1
+ module FormtasticBootstrap
2
+ module Helpers
3
+ module ButtonsHelper
4
+
5
+ include Formtastic::Helpers::ButtonsHelper
6
+
7
+ def buttons(*args, &block)
8
+ ::ActiveSupport::Deprecation.warn("f.buttons is deprecated in favour of f.actions and will be removed from Formtastic after 2.1. Please see ActionsHelper and InputAction or ButtonAction for more information")
9
+
10
+ html_options = args.extract_options!
11
+ html_options[:class] ||= "form-actions"
12
+
13
+ if html_options.has_key?(:name)
14
+ ActiveSupport::Deprecation.warn('The :name option is not supported')
15
+ end
16
+
17
+ if block_given?
18
+ template.content_tag(:div, html_options) do
19
+ yield
20
+ end
21
+ else
22
+ args = [:commit] if args.empty?
23
+ contents = args.map { |button_name| send(:"#{button_name}_button") }
24
+ template.content_tag(:div, html_options.except(:builder, :parent, :name)) do
25
+ Formtastic::Util.html_safe(contents.join)
26
+ end
27
+ end
28
+
29
+ end
30
+
31
+ def commit_button(*args)
32
+ ::ActiveSupport::Deprecation.warn("f.commit_button is deprecated in favour of f.action(:submit) and will be removed from Formtastic after 2.1. Please see ActionsHelper and InputAction or ButtonAction for more information")
33
+
34
+ options = args.extract_options!
35
+ text = options.delete(:label) || args.shift
36
+
37
+ text = (localized_string(commit_button_i18n_key, text, :action, :model => commit_button_object_name) ||
38
+ Formtastic::I18n.t(commit_button_i18n_key, :model => commit_button_object_name)) unless text.is_a?(::String)
39
+
40
+ button_html = options.delete(:button_html) || {}
41
+ button_html[:id] ||= "#{@object_name}_submit"
42
+ button_html.merge!(:class => [button_html[:class], "btn commit", commit_button_i18n_key].compact.join(' '))
43
+
44
+ # TODO We don't have a wrapper. Add deprecation message.
45
+ # wrapper_html = options.delete(:wrapper_html) || {}
46
+ # wrapper_html[:class] = (commit_button_wrapper_html_class << wrapper_html[:class]).flatten.compact.join(' ')
47
+
48
+ accesskey = (options.delete(:accesskey) || default_commit_button_accesskey) unless button_html.has_key?(:accesskey)
49
+ button_html = button_html.merge(:accesskey => accesskey) if accesskey
50
+
51
+ Formtastic::Util.html_safe(submit(text, button_html))
52
+ end
53
+
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,42 @@
1
+ module FormtasticBootstrap
2
+ module Helpers
3
+ module FieldsetWrapper
4
+
5
+ include Formtastic::Helpers::FieldsetWrapper
6
+
7
+ protected
8
+
9
+ def field_set_and_list_wrapping(*args, &block) #:nodoc:
10
+ contents = args.last.is_a?(::Hash) ? '' : args.pop.flatten
11
+ html_options = args.extract_options!
12
+
13
+ if block_given?
14
+ contents = if template.respond_to?(:is_haml?) && template.is_haml?
15
+ template.capture_haml(&block)
16
+ else
17
+ template.capture(&block)
18
+ end
19
+ end
20
+
21
+ # Ruby 1.9: String#to_s behavior changed, need to make an explicit join.
22
+ contents = contents.join if contents.respond_to?(:join)
23
+
24
+ legend = field_set_legend(html_options)
25
+ fieldset = template.content_tag(:fieldset,
26
+ Formtastic::Util.html_safe(legend) << Formtastic::Util.html_safe(contents),
27
+ html_options.except(:builder, :parent, :name)
28
+ )
29
+
30
+ fieldset
31
+ end
32
+
33
+ def field_set_legend(html_options)
34
+ legend = (html_options[:name] || '').to_s
35
+ legend %= parent_child_index(html_options[:parent]) if html_options[:parent]
36
+ legend = template.content_tag(:legend, Formtastic::Util.html_safe(legend)) unless legend.blank?
37
+ legend
38
+ end
39
+
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,12 @@
1
+ module FormtasticBootstrap
2
+ module Helpers
3
+ module InputHelper
4
+ include Formtastic::Helpers::InputHelper
5
+
6
+ def standard_input_class_name(as)
7
+ "FormtasticBootstrap::Inputs::#{as.to_s.camelize}Input"
8
+ end
9
+
10
+ end
11
+ end
12
+ end